Home | History | Annotate | Line # | Download | only in ic
smc83c170.c revision 1.71.2.1
      1  1.71.2.1   keiichi /*	$NetBSD: smc83c170.c,v 1.71.2.1 2008/03/24 07:15:17 keiichi Exp $	*/
      2       1.1   thorpej 
      3       1.1   thorpej /*-
      4      1.10   thorpej  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
      5       1.1   thorpej  * All rights reserved.
      6       1.1   thorpej  *
      7       1.1   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9       1.1   thorpej  * NASA Ames Research Center.
     10       1.1   thorpej  *
     11       1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     12       1.1   thorpej  * modification, are permitted provided that the following conditions
     13       1.1   thorpej  * are met:
     14       1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     15       1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     16       1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18       1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     19       1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     20       1.1   thorpej  *    must display the following acknowledgement:
     21       1.1   thorpej  *	This product includes software developed by the NetBSD
     22       1.1   thorpej  *	Foundation, Inc. and its contributors.
     23       1.1   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24       1.1   thorpej  *    contributors may be used to endorse or promote products derived
     25       1.1   thorpej  *    from this software without specific prior written permission.
     26       1.1   thorpej  *
     27       1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28       1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29       1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30       1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31       1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32       1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33       1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34       1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35       1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36       1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37       1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38       1.1   thorpej  */
     39       1.1   thorpej 
     40       1.1   thorpej /*
     41       1.1   thorpej  * Device driver for the Standard Microsystems Corp. 83C170
     42       1.1   thorpej  * Ethernet PCI Integrated Controller (EPIC/100).
     43       1.1   thorpej  */
     44      1.49     lukem 
     45      1.49     lukem #include <sys/cdefs.h>
     46  1.71.2.1   keiichi __KERNEL_RCSID(0, "$NetBSD: smc83c170.c,v 1.71.2.1 2008/03/24 07:15:17 keiichi Exp $");
     47       1.1   thorpej 
     48       1.1   thorpej #include "bpfilter.h"
     49       1.1   thorpej 
     50       1.1   thorpej #include <sys/param.h>
     51      1.59     perry #include <sys/systm.h>
     52      1.29   thorpej #include <sys/callout.h>
     53      1.59     perry #include <sys/mbuf.h>
     54       1.1   thorpej #include <sys/malloc.h>
     55       1.1   thorpej #include <sys/kernel.h>
     56       1.1   thorpej #include <sys/socket.h>
     57       1.1   thorpej #include <sys/ioctl.h>
     58       1.1   thorpej #include <sys/errno.h>
     59       1.1   thorpej #include <sys/device.h>
     60      1.38   thorpej 
     61      1.38   thorpej #include <uvm/uvm_extern.h>
     62      1.38   thorpej 
     63       1.1   thorpej #include <net/if.h>
     64       1.1   thorpej #include <net/if_dl.h>
     65       1.1   thorpej #include <net/if_media.h>
     66       1.1   thorpej #include <net/if_ether.h>
     67       1.1   thorpej 
     68      1.59     perry #if NBPFILTER > 0
     69       1.1   thorpej #include <net/bpf.h>
     70      1.59     perry #endif
     71       1.1   thorpej 
     72      1.67        ad #include <sys/bus.h>
     73      1.67        ad #include <sys/intr.h>
     74       1.1   thorpej 
     75       1.8   thorpej #include <dev/mii/miivar.h>
     76      1.43  drochner #include <dev/mii/lxtphyreg.h>
     77       1.8   thorpej 
     78       1.1   thorpej #include <dev/ic/smc83c170reg.h>
     79       1.1   thorpej #include <dev/ic/smc83c170var.h>
     80       1.1   thorpej 
     81      1.58     perry void	epic_start(struct ifnet *);
     82      1.58     perry void	epic_watchdog(struct ifnet *);
     83      1.64  christos int	epic_ioctl(struct ifnet *, u_long, void *);
     84      1.58     perry int	epic_init(struct ifnet *);
     85      1.58     perry void	epic_stop(struct ifnet *, int);
     86      1.58     perry 
     87      1.58     perry void	epic_shutdown(void *);
     88      1.58     perry 
     89      1.58     perry void	epic_reset(struct epic_softc *);
     90      1.58     perry void	epic_rxdrain(struct epic_softc *);
     91      1.58     perry int	epic_add_rxbuf(struct epic_softc *, int);
     92      1.63   tsutsui void	epic_read_eeprom(struct epic_softc *, int, int, uint16_t *);
     93      1.58     perry void	epic_set_mchash(struct epic_softc *);
     94      1.58     perry void	epic_fixup_clock_source(struct epic_softc *);
     95      1.58     perry int	epic_mii_read(struct device *, int, int);
     96      1.58     perry void	epic_mii_write(struct device *, int, int, int);
     97      1.63   tsutsui int	epic_mii_wait(struct epic_softc *, uint32_t);
     98      1.58     perry void	epic_tick(void *);
     99      1.58     perry 
    100      1.58     perry void	epic_statchg(struct device *);
    101      1.58     perry int	epic_mediachange(struct ifnet *);
    102       1.1   thorpej 
    103       1.1   thorpej #define	INTMASK	(INTSTAT_FATAL_INT | INTSTAT_TXU | \
    104      1.21   thorpej 	    INTSTAT_TXC | INTSTAT_RXE | INTSTAT_RQE | INTSTAT_RCC)
    105       1.1   thorpej 
    106      1.19   thorpej int	epic_copy_small = 0;
    107      1.19   thorpej 
    108      1.52    bouyer #define	ETHER_PAD_LEN (ETHER_MIN_LEN - ETHER_CRC_LEN)
    109      1.52    bouyer 
    110       1.1   thorpej /*
    111       1.1   thorpej  * Attach an EPIC interface to the system.
    112       1.1   thorpej  */
    113       1.1   thorpej void
    114       1.1   thorpej epic_attach(sc)
    115       1.1   thorpej 	struct epic_softc *sc;
    116       1.1   thorpej {
    117       1.1   thorpej 	bus_space_tag_t st = sc->sc_st;
    118       1.1   thorpej 	bus_space_handle_t sh = sc->sc_sh;
    119       1.1   thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    120      1.51   thorpej 	int rseg, error, miiflags;
    121      1.51   thorpej 	u_int i;
    122       1.1   thorpej 	bus_dma_segment_t seg;
    123      1.63   tsutsui 	uint8_t enaddr[ETHER_ADDR_LEN], devname[12 + 1];
    124      1.63   tsutsui 	uint16_t myea[ETHER_ADDR_LEN / 2], mydevname[6];
    125      1.52    bouyer 	char *nullbuf;
    126       1.1   thorpej 
    127      1.65        ad 	callout_init(&sc->sc_mii_callout, 0);
    128      1.29   thorpej 
    129       1.1   thorpej 	/*
    130       1.1   thorpej 	 * Allocate the control data structures, and create and load the
    131       1.1   thorpej 	 * DMA map for it.
    132       1.1   thorpej 	 */
    133       1.1   thorpej 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    134      1.52    bouyer 	    sizeof(struct epic_control_data) + ETHER_PAD_LEN, PAGE_SIZE, 0,
    135      1.52    bouyer 	    &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    136      1.53   thorpej 		aprint_error(
    137      1.53   thorpej 		    "%s: unable to allocate control data, error = %d\n",
    138       1.1   thorpej 		    sc->sc_dev.dv_xname, error);
    139      1.14   thorpej 		goto fail_0;
    140       1.1   thorpej 	}
    141       1.1   thorpej 
    142       1.1   thorpej 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    143      1.52    bouyer 	    sizeof(struct epic_control_data) + ETHER_PAD_LEN,
    144      1.64  christos 	    (void **)&sc->sc_control_data,
    145       1.1   thorpej 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    146      1.53   thorpej 		aprint_error("%s: unable to map control data, error = %d\n",
    147       1.1   thorpej 		    sc->sc_dev.dv_xname, error);
    148      1.14   thorpej 		goto fail_1;
    149       1.1   thorpej 	}
    150      1.52    bouyer 	nullbuf =
    151      1.52    bouyer 	    (char *)sc->sc_control_data + sizeof(struct epic_control_data);
    152      1.52    bouyer 	memset(nullbuf, 0, ETHER_PAD_LEN);
    153       1.1   thorpej 
    154       1.1   thorpej 	if ((error = bus_dmamap_create(sc->sc_dmat,
    155       1.1   thorpej 	    sizeof(struct epic_control_data), 1,
    156       1.1   thorpej 	    sizeof(struct epic_control_data), 0, BUS_DMA_NOWAIT,
    157       1.1   thorpej 	    &sc->sc_cddmamap)) != 0) {
    158      1.53   thorpej 		aprint_error("%s: unable to create control data DMA map, "
    159       1.1   thorpej 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    160      1.14   thorpej 		goto fail_2;
    161       1.1   thorpej 	}
    162       1.1   thorpej 
    163       1.1   thorpej 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
    164       1.1   thorpej 	    sc->sc_control_data, sizeof(struct epic_control_data), NULL,
    165       1.1   thorpej 	    BUS_DMA_NOWAIT)) != 0) {
    166      1.53   thorpej 		aprint_error(
    167      1.53   thorpej 		    "%s: unable to load control data DMA map, error = %d\n",
    168       1.1   thorpej 		    sc->sc_dev.dv_xname, error);
    169      1.14   thorpej 		goto fail_3;
    170       1.1   thorpej 	}
    171       1.1   thorpej 
    172       1.1   thorpej 	/*
    173       1.1   thorpej 	 * Create the transmit buffer DMA maps.
    174       1.1   thorpej 	 */
    175       1.1   thorpej 	for (i = 0; i < EPIC_NTXDESC; i++) {
    176       1.1   thorpej 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    177       1.1   thorpej 		    EPIC_NFRAGS, MCLBYTES, 0, BUS_DMA_NOWAIT,
    178      1.10   thorpej 		    &EPIC_DSTX(sc, i)->ds_dmamap)) != 0) {
    179      1.53   thorpej 			aprint_error("%s: unable to create tx DMA map %d, "
    180       1.1   thorpej 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    181      1.14   thorpej 			goto fail_4;
    182       1.1   thorpej 		}
    183       1.1   thorpej 	}
    184       1.1   thorpej 
    185       1.1   thorpej 	/*
    186      1.42   tsutsui 	 * Create the receive buffer DMA maps.
    187       1.1   thorpej 	 */
    188       1.1   thorpej 	for (i = 0; i < EPIC_NRXDESC; i++) {
    189       1.1   thorpej 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    190       1.1   thorpej 		    MCLBYTES, 0, BUS_DMA_NOWAIT,
    191      1.10   thorpej 		    &EPIC_DSRX(sc, i)->ds_dmamap)) != 0) {
    192      1.53   thorpej 			aprint_error("%s: unable to create rx DMA map %d, "
    193       1.1   thorpej 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    194      1.14   thorpej 			goto fail_5;
    195       1.1   thorpej 		}
    196      1.19   thorpej 		EPIC_DSRX(sc, i)->ds_mbuf = NULL;
    197       1.1   thorpej 	}
    198       1.1   thorpej 
    199      1.52    bouyer 	/*
    200      1.52    bouyer 	 * create and map the pad buffer
    201      1.52    bouyer 	 */
    202      1.52    bouyer 	if ((error = bus_dmamap_create(sc->sc_dmat, ETHER_PAD_LEN, 1,
    203      1.52    bouyer 	    ETHER_PAD_LEN, 0, BUS_DMA_NOWAIT,&sc->sc_nulldmamap)) != 0) {
    204      1.52    bouyer 		printf("%s: unable to create pad buffer DMA map, "
    205      1.52    bouyer 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    206      1.52    bouyer 		goto fail_5;
    207      1.52    bouyer 	}
    208      1.52    bouyer 
    209      1.52    bouyer 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_nulldmamap,
    210      1.52    bouyer 	    nullbuf, ETHER_PAD_LEN, NULL, BUS_DMA_NOWAIT)) != 0) {
    211      1.52    bouyer 		printf("%s: unable to load pad buffer DMA map, "
    212      1.52    bouyer 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    213      1.52    bouyer 		goto fail_6;
    214      1.52    bouyer 	}
    215      1.52    bouyer 	bus_dmamap_sync(sc->sc_dmat, sc->sc_nulldmamap, 0, ETHER_PAD_LEN,
    216      1.52    bouyer 	    BUS_DMASYNC_PREWRITE);
    217       1.1   thorpej 
    218       1.1   thorpej 	/*
    219       1.1   thorpej 	 * Bring the chip out of low-power mode and reset it to a known state.
    220       1.1   thorpej 	 */
    221       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_GENCTL, 0);
    222       1.1   thorpej 	epic_reset(sc);
    223       1.1   thorpej 
    224       1.1   thorpej 	/*
    225       1.1   thorpej 	 * Read the Ethernet address from the EEPROM.
    226       1.1   thorpej 	 */
    227      1.62   tsutsui 	epic_read_eeprom(sc, 0, __arraycount(myea), myea);
    228      1.62   tsutsui 	for (i = 0; i < __arraycount(myea); i++) {
    229      1.32   tsutsui 		enaddr[i * 2]     = myea[i] & 0xff;
    230      1.32   tsutsui 		enaddr[i * 2 + 1] = myea[i] >> 8;
    231      1.32   tsutsui 	}
    232       1.1   thorpej 
    233       1.1   thorpej 	/*
    234       1.1   thorpej 	 * ...and the device name.
    235       1.1   thorpej 	 */
    236      1.62   tsutsui 	epic_read_eeprom(sc, 0x2c, __arraycount(mydevname), mydevname);
    237      1.62   tsutsui 	for (i = 0; i < __arraycount(mydevname); i++) {
    238      1.32   tsutsui 		devname[i * 2]     = mydevname[i] & 0xff;
    239      1.32   tsutsui 		devname[i * 2 + 1] = mydevname[i] >> 8;
    240      1.32   tsutsui 	}
    241      1.32   tsutsui 
    242       1.1   thorpej 	devname[sizeof(mydevname)] = '\0';
    243      1.61   tsutsui 	for (i = sizeof(mydevname) ; i > 0; i--) {
    244      1.61   tsutsui 		if (devname[i - 1] == ' ')
    245      1.61   tsutsui 			devname[i - 1] = '\0';
    246       1.1   thorpej 		else
    247       1.1   thorpej 			break;
    248       1.1   thorpej 	}
    249       1.1   thorpej 
    250      1.53   thorpej 	aprint_normal("%s: %s, Ethernet address %s\n", sc->sc_dev.dv_xname,
    251       1.1   thorpej 	    devname, ether_sprintf(enaddr));
    252       1.1   thorpej 
    253      1.43  drochner 	miiflags = 0;
    254      1.43  drochner 	if (sc->sc_hwflags & EPIC_HAS_MII_FIBER)
    255      1.43  drochner 		miiflags |= MIIF_HAVEFIBER;
    256      1.43  drochner 
    257       1.8   thorpej 	/*
    258       1.8   thorpej 	 * Initialize our media structures and probe the MII.
    259       1.8   thorpej 	 */
    260       1.8   thorpej 	sc->sc_mii.mii_ifp = ifp;
    261       1.8   thorpej 	sc->sc_mii.mii_readreg = epic_mii_read;
    262       1.8   thorpej 	sc->sc_mii.mii_writereg = epic_mii_write;
    263       1.8   thorpej 	sc->sc_mii.mii_statchg = epic_statchg;
    264      1.71    dyoung 
    265      1.71    dyoung 	sc->sc_ethercom.ec_mii = &sc->sc_mii;
    266      1.50      fair 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, epic_mediachange,
    267      1.71    dyoung 	    ether_mediastatus);
    268      1.24   thorpej 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    269      1.43  drochner 	    MII_OFFSET_ANY, miiflags);
    270      1.69    dyoung 	if (LIST_EMPTY(&sc->sc_mii.mii_phys)) {
    271       1.8   thorpej 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    272       1.8   thorpej 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
    273       1.8   thorpej 	} else
    274       1.8   thorpej 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    275       1.8   thorpej 
    276      1.43  drochner 	if (sc->sc_hwflags & EPIC_HAS_BNC) {
    277      1.43  drochner 		/* use the next free media instance */
    278      1.43  drochner 		sc->sc_serinst = sc->sc_mii.mii_instance++;
    279      1.43  drochner 		ifmedia_add(&sc->sc_mii.mii_media,
    280      1.43  drochner 			    IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0,
    281      1.43  drochner 					 sc->sc_serinst),
    282      1.43  drochner 			    0, NULL);
    283      1.53   thorpej 		aprint_normal("%s: 10base2/BNC\n", sc->sc_dev.dv_xname);
    284      1.43  drochner 	} else
    285      1.43  drochner 		sc->sc_serinst = -1;
    286      1.43  drochner 
    287       1.1   thorpej 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    288       1.1   thorpej 	ifp->if_softc = sc;
    289       1.1   thorpej 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    290       1.1   thorpej 	ifp->if_ioctl = epic_ioctl;
    291       1.1   thorpej 	ifp->if_start = epic_start;
    292       1.1   thorpej 	ifp->if_watchdog = epic_watchdog;
    293      1.34   thorpej 	ifp->if_init = epic_init;
    294      1.34   thorpej 	ifp->if_stop = epic_stop;
    295      1.40   thorpej 	IFQ_SET_READY(&ifp->if_snd);
    296      1.36    bouyer 
    297      1.36    bouyer 	/*
    298      1.36    bouyer 	 * We can support 802.1Q VLAN-sized frames.
    299      1.36    bouyer 	 */
    300      1.36    bouyer 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    301       1.1   thorpej 
    302       1.1   thorpej 	/*
    303       1.1   thorpej 	 * Attach the interface.
    304       1.1   thorpej 	 */
    305       1.1   thorpej 	if_attach(ifp);
    306       1.1   thorpej 	ether_ifattach(ifp, enaddr);
    307       1.1   thorpej 
    308       1.1   thorpej 	/*
    309       1.1   thorpej 	 * Make sure the interface is shutdown during reboot.
    310       1.1   thorpej 	 */
    311       1.1   thorpej 	sc->sc_sdhook = shutdownhook_establish(epic_shutdown, sc);
    312       1.1   thorpej 	if (sc->sc_sdhook == NULL)
    313      1.53   thorpej 		aprint_error("%s: WARNING: unable to establish shutdown hook\n",
    314       1.1   thorpej 		    sc->sc_dev.dv_xname);
    315       1.1   thorpej 	return;
    316       1.1   thorpej 
    317       1.1   thorpej 	/*
    318       1.1   thorpej 	 * Free any resources we've allocated during the failed attach
    319       1.1   thorpej 	 * attempt.  Do this in reverse order and fall through.
    320       1.1   thorpej 	 */
    321      1.52    bouyer  fail_6:
    322      1.52    bouyer 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_nulldmamap);
    323      1.14   thorpej  fail_5:
    324      1.14   thorpej 	for (i = 0; i < EPIC_NRXDESC; i++) {
    325      1.14   thorpej 		if (EPIC_DSRX(sc, i)->ds_dmamap != NULL)
    326       1.1   thorpej 			bus_dmamap_destroy(sc->sc_dmat,
    327      1.10   thorpej 			    EPIC_DSRX(sc, i)->ds_dmamap);
    328      1.14   thorpej 	}
    329      1.14   thorpej  fail_4:
    330      1.14   thorpej 	for (i = 0; i < EPIC_NTXDESC; i++) {
    331      1.14   thorpej 		if (EPIC_DSTX(sc, i)->ds_dmamap != NULL)
    332       1.1   thorpej 			bus_dmamap_destroy(sc->sc_dmat,
    333      1.10   thorpej 			    EPIC_DSTX(sc, i)->ds_dmamap);
    334       1.1   thorpej 	}
    335      1.14   thorpej 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    336      1.14   thorpej  fail_3:
    337      1.14   thorpej 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    338      1.14   thorpej  fail_2:
    339      1.64  christos 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
    340      1.14   thorpej 	    sizeof(struct epic_control_data));
    341      1.14   thorpej  fail_1:
    342      1.14   thorpej 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    343      1.14   thorpej  fail_0:
    344      1.14   thorpej 	return;
    345       1.1   thorpej }
    346       1.1   thorpej 
    347       1.1   thorpej /*
    348       1.1   thorpej  * Shutdown hook.  Make sure the interface is stopped at reboot.
    349       1.1   thorpej  */
    350       1.1   thorpej void
    351       1.1   thorpej epic_shutdown(arg)
    352       1.1   thorpej 	void *arg;
    353       1.1   thorpej {
    354       1.1   thorpej 	struct epic_softc *sc = arg;
    355       1.1   thorpej 
    356      1.34   thorpej 	epic_stop(&sc->sc_ethercom.ec_if, 1);
    357       1.1   thorpej }
    358       1.1   thorpej 
    359       1.1   thorpej /*
    360       1.1   thorpej  * Start packet transmission on the interface.
    361       1.1   thorpej  * [ifnet interface function]
    362       1.1   thorpej  */
    363       1.1   thorpej void
    364       1.1   thorpej epic_start(ifp)
    365       1.1   thorpej 	struct ifnet *ifp;
    366       1.1   thorpej {
    367       1.1   thorpej 	struct epic_softc *sc = ifp->if_softc;
    368      1.10   thorpej 	struct mbuf *m0, *m;
    369       1.1   thorpej 	struct epic_txdesc *txd;
    370       1.1   thorpej 	struct epic_descsoft *ds;
    371       1.1   thorpej 	struct epic_fraglist *fr;
    372       1.1   thorpej 	bus_dmamap_t dmamap;
    373      1.10   thorpej 	int error, firsttx, nexttx, opending, seg;
    374      1.55   tsutsui 	u_int len;
    375       1.1   thorpej 
    376      1.10   thorpej 	/*
    377      1.10   thorpej 	 * Remember the previous txpending and the first transmit
    378      1.10   thorpej 	 * descriptor we use.
    379      1.10   thorpej 	 */
    380      1.10   thorpej 	opending = sc->sc_txpending;
    381      1.10   thorpej 	firsttx = EPIC_NEXTTX(sc->sc_txlast);
    382       1.1   thorpej 
    383       1.1   thorpej 	/*
    384       1.1   thorpej 	 * Loop through the send queue, setting up transmit descriptors
    385       1.1   thorpej 	 * until we drain the queue, or use up all available transmit
    386       1.1   thorpej 	 * descriptors.
    387       1.1   thorpej 	 */
    388      1.10   thorpej 	while (sc->sc_txpending < EPIC_NTXDESC) {
    389       1.1   thorpej 		/*
    390       1.1   thorpej 		 * Grab a packet off the queue.
    391       1.1   thorpej 		 */
    392      1.40   thorpej 		IFQ_POLL(&ifp->if_snd, m0);
    393      1.10   thorpej 		if (m0 == NULL)
    394      1.10   thorpej 			break;
    395      1.41   thorpej 		m = NULL;
    396       1.1   thorpej 
    397       1.1   thorpej 		/*
    398       1.1   thorpej 		 * Get the last and next available transmit descriptor.
    399       1.1   thorpej 		 */
    400       1.1   thorpej 		nexttx = EPIC_NEXTTX(sc->sc_txlast);
    401      1.10   thorpej 		txd = EPIC_CDTX(sc, nexttx);
    402      1.10   thorpej 		fr = EPIC_CDFL(sc, nexttx);
    403      1.10   thorpej 		ds = EPIC_DSTX(sc, nexttx);
    404       1.1   thorpej 		dmamap = ds->ds_dmamap;
    405       1.1   thorpej 
    406       1.1   thorpej 		/*
    407      1.10   thorpej 		 * Load the DMA map.  If this fails, the packet either
    408      1.10   thorpej 		 * didn't fit in the alloted number of frags, or we were
    409      1.10   thorpej 		 * short on resources.  In this case, we'll copy and try
    410      1.10   thorpej 		 * again.
    411       1.1   thorpej 		 */
    412      1.52    bouyer 		if ((error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    413      1.52    bouyer 		    BUS_DMA_WRITE|BUS_DMA_NOWAIT)) != 0 ||
    414      1.52    bouyer 		    (m0->m_pkthdr.len < ETHER_PAD_LEN &&
    415      1.52    bouyer 		    dmamap-> dm_nsegs == EPIC_NFRAGS)) {
    416      1.52    bouyer 			if (error == 0)
    417      1.52    bouyer 				bus_dmamap_unload(sc->sc_dmat, dmamap);
    418      1.59     perry 
    419      1.10   thorpej 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    420      1.10   thorpej 			if (m == NULL) {
    421      1.10   thorpej 				printf("%s: unable to allocate Tx mbuf\n",
    422      1.10   thorpej 				    sc->sc_dev.dv_xname);
    423      1.10   thorpej 				break;
    424       1.1   thorpej 			}
    425       1.1   thorpej 			if (m0->m_pkthdr.len > MHLEN) {
    426      1.10   thorpej 				MCLGET(m, M_DONTWAIT);
    427      1.10   thorpej 				if ((m->m_flags & M_EXT) == 0) {
    428      1.10   thorpej 					printf("%s: unable to allocate Tx "
    429      1.10   thorpej 					    "cluster\n", sc->sc_dev.dv_xname);
    430      1.10   thorpej 					m_freem(m);
    431      1.10   thorpej 					break;
    432       1.1   thorpej 				}
    433       1.1   thorpej 			}
    434      1.64  christos 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
    435      1.10   thorpej 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
    436      1.10   thorpej 			error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
    437      1.47   thorpej 			    m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
    438      1.10   thorpej 			if (error) {
    439      1.10   thorpej 				printf("%s: unable to load Tx buffer, "
    440      1.10   thorpej 				    "error = %d\n", sc->sc_dev.dv_xname, error);
    441      1.10   thorpej 				break;
    442      1.10   thorpej 			}
    443       1.1   thorpej 		}
    444      1.40   thorpej 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    445      1.41   thorpej 		if (m != NULL) {
    446      1.41   thorpej 			m_freem(m0);
    447      1.41   thorpej 			m0 = m;
    448      1.41   thorpej 		}
    449       1.1   thorpej 
    450      1.10   thorpej 		/* Initialize the fraglist. */
    451       1.1   thorpej 		for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
    452       1.1   thorpej 			fr->ef_frags[seg].ef_addr =
    453       1.1   thorpej 			    dmamap->dm_segs[seg].ds_addr;
    454       1.1   thorpej 			fr->ef_frags[seg].ef_length =
    455       1.1   thorpej 			    dmamap->dm_segs[seg].ds_len;
    456       1.1   thorpej 		}
    457      1.55   tsutsui 		len = m0->m_pkthdr.len;
    458      1.55   tsutsui 		if (len < ETHER_PAD_LEN) {
    459      1.52    bouyer 			fr->ef_frags[seg].ef_addr = sc->sc_nulldma;
    460      1.55   tsutsui 			fr->ef_frags[seg].ef_length = ETHER_PAD_LEN - len;
    461      1.55   tsutsui 			len = ETHER_PAD_LEN;
    462      1.52    bouyer 			seg++;
    463      1.52    bouyer 		}
    464      1.52    bouyer 		fr->ef_nfrags = seg;
    465       1.1   thorpej 
    466      1.10   thorpej 		EPIC_CDFLSYNC(sc, nexttx, BUS_DMASYNC_PREWRITE);
    467      1.10   thorpej 
    468      1.10   thorpej 		/* Sync the DMA map. */
    469       1.1   thorpej 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
    470       1.1   thorpej 		    BUS_DMASYNC_PREWRITE);
    471       1.1   thorpej 
    472       1.1   thorpej 		/*
    473       1.1   thorpej 		 * Store a pointer to the packet so we can free it later.
    474       1.1   thorpej 		 */
    475       1.1   thorpej 		ds->ds_mbuf = m0;
    476       1.1   thorpej 
    477       1.1   thorpej 		/*
    478      1.52    bouyer 		 * Fill in the transmit descriptor.
    479       1.1   thorpej 		 */
    480      1.10   thorpej 		txd->et_control = ET_TXCTL_LASTDESC | ET_TXCTL_FRAGLIST;
    481       1.1   thorpej 
    482       1.1   thorpej 		/*
    483      1.10   thorpej 		 * If this is the first descriptor we're enqueueing,
    484      1.10   thorpej 		 * don't give it to the EPIC yet.  That could cause
    485      1.10   thorpej 		 * a race condition.  We'll do it below.
    486       1.1   thorpej 		 */
    487      1.10   thorpej 		if (nexttx == firsttx)
    488      1.55   tsutsui 			txd->et_txstatus = TXSTAT_TXLENGTH(len);
    489      1.10   thorpej 		else
    490      1.55   tsutsui 			txd->et_txstatus =
    491      1.55   tsutsui 			    TXSTAT_TXLENGTH(len) | ET_TXSTAT_OWNER;
    492      1.10   thorpej 
    493      1.10   thorpej 		EPIC_CDTXSYNC(sc, nexttx,
    494      1.10   thorpej 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    495       1.1   thorpej 
    496      1.10   thorpej 		/* Advance the tx pointer. */
    497       1.1   thorpej 		sc->sc_txpending++;
    498      1.10   thorpej 		sc->sc_txlast = nexttx;
    499       1.1   thorpej 
    500       1.1   thorpej #if NBPFILTER > 0
    501       1.1   thorpej 		/*
    502       1.1   thorpej 		 * Pass the packet to any BPF listeners.
    503       1.1   thorpej 		 */
    504       1.1   thorpej 		if (ifp->if_bpf)
    505       1.1   thorpej 			bpf_mtap(ifp->if_bpf, m0);
    506       1.1   thorpej #endif
    507       1.1   thorpej 	}
    508       1.1   thorpej 
    509      1.10   thorpej 	if (sc->sc_txpending == EPIC_NTXDESC) {
    510      1.10   thorpej 		/* No more slots left; notify upper layer. */
    511      1.10   thorpej 		ifp->if_flags |= IFF_OACTIVE;
    512      1.10   thorpej 	}
    513      1.10   thorpej 
    514      1.10   thorpej 	if (sc->sc_txpending != opending) {
    515      1.10   thorpej 		/*
    516      1.10   thorpej 		 * We enqueued packets.  If the transmitter was idle,
    517      1.10   thorpej 		 * reset the txdirty pointer.
    518      1.10   thorpej 		 */
    519      1.10   thorpej 		if (opending == 0)
    520      1.10   thorpej 			sc->sc_txdirty = firsttx;
    521      1.10   thorpej 
    522      1.10   thorpej 		/*
    523      1.10   thorpej 		 * Cause a transmit interrupt to happen on the
    524      1.10   thorpej 		 * last packet we enqueued.
    525      1.10   thorpej 		 */
    526      1.10   thorpej 		EPIC_CDTX(sc, sc->sc_txlast)->et_control |= ET_TXCTL_IAF;
    527      1.10   thorpej 		EPIC_CDTXSYNC(sc, sc->sc_txlast,
    528      1.10   thorpej 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    529      1.10   thorpej 
    530      1.10   thorpej 		/*
    531      1.10   thorpej 		 * The entire packet chain is set up.  Give the
    532      1.10   thorpej 		 * first descriptor to the EPIC now.
    533      1.10   thorpej 		 */
    534      1.55   tsutsui 		EPIC_CDTX(sc, firsttx)->et_txstatus |= ET_TXSTAT_OWNER;
    535      1.10   thorpej 		EPIC_CDTXSYNC(sc, firsttx,
    536      1.10   thorpej 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    537      1.10   thorpej 
    538      1.10   thorpej 		/* Start the transmitter. */
    539       1.1   thorpej 		bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_COMMAND,
    540       1.1   thorpej 		    COMMAND_TXQUEUED);
    541       1.1   thorpej 
    542      1.10   thorpej 		/* Set a watchdog timer in case the chip flakes out. */
    543       1.1   thorpej 		ifp->if_timer = 5;
    544       1.1   thorpej 	}
    545       1.1   thorpej }
    546       1.1   thorpej 
    547       1.1   thorpej /*
    548       1.1   thorpej  * Watchdog timer handler.
    549       1.1   thorpej  * [ifnet interface function]
    550       1.1   thorpej  */
    551       1.1   thorpej void
    552       1.1   thorpej epic_watchdog(ifp)
    553       1.1   thorpej 	struct ifnet *ifp;
    554       1.1   thorpej {
    555       1.1   thorpej 	struct epic_softc *sc = ifp->if_softc;
    556       1.1   thorpej 
    557       1.1   thorpej 	printf("%s: device timeout\n", sc->sc_dev.dv_xname);
    558       1.1   thorpej 	ifp->if_oerrors++;
    559       1.1   thorpej 
    560      1.34   thorpej 	(void) epic_init(ifp);
    561       1.1   thorpej }
    562       1.1   thorpej 
    563       1.1   thorpej /*
    564       1.1   thorpej  * Handle control requests from the operator.
    565       1.1   thorpej  * [ifnet interface function]
    566       1.1   thorpej  */
    567       1.1   thorpej int
    568       1.1   thorpej epic_ioctl(ifp, cmd, data)
    569       1.1   thorpej 	struct ifnet *ifp;
    570       1.1   thorpej 	u_long cmd;
    571      1.64  christos 	void *data;
    572       1.1   thorpej {
    573       1.1   thorpej 	struct epic_softc *sc = ifp->if_softc;
    574      1.34   thorpej 	int s, error;
    575       1.1   thorpej 
    576       1.7   mycroft 	s = splnet();
    577       1.1   thorpej 
    578      1.71    dyoung 	error = ether_ioctl(ifp, cmd, data);
    579      1.71    dyoung 	if (error == ENETRESET) {
    580      1.71    dyoung 		/*
    581      1.71    dyoung 		 * Multicast list has changed; set the hardware filter
    582      1.71    dyoung 		 * accordingly.  Update our idea of the current media;
    583      1.71    dyoung 		 * epic_set_mchash() needs to know what it is.
    584      1.71    dyoung 		 */
    585      1.71    dyoung 		if (ifp->if_flags & IFF_RUNNING) {
    586      1.71    dyoung 			mii_pollstat(&sc->sc_mii);
    587      1.71    dyoung 			epic_set_mchash(sc);
    588       1.1   thorpej 		}
    589      1.71    dyoung 		error = 0;
    590       1.1   thorpej 	}
    591       1.1   thorpej 
    592       1.1   thorpej 	splx(s);
    593       1.1   thorpej 	return (error);
    594       1.1   thorpej }
    595       1.1   thorpej 
    596       1.1   thorpej /*
    597       1.1   thorpej  * Interrupt handler.
    598       1.1   thorpej  */
    599       1.1   thorpej int
    600       1.1   thorpej epic_intr(arg)
    601       1.1   thorpej 	void *arg;
    602       1.1   thorpej {
    603       1.1   thorpej 	struct epic_softc *sc = arg;
    604       1.1   thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    605       1.1   thorpej 	struct epic_rxdesc *rxd;
    606       1.1   thorpej 	struct epic_txdesc *txd;
    607       1.1   thorpej 	struct epic_descsoft *ds;
    608       1.1   thorpej 	struct mbuf *m;
    609      1.63   tsutsui 	uint32_t intstat, rxstatus, txstatus;
    610      1.51   thorpej 	int i, claimed = 0;
    611      1.51   thorpej 	u_int len;
    612       1.1   thorpej 
    613       1.1   thorpej  top:
    614       1.1   thorpej 	/*
    615       1.1   thorpej 	 * Get the interrupt status from the EPIC.
    616       1.1   thorpej 	 */
    617       1.1   thorpej 	intstat = bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_INTSTAT);
    618       1.1   thorpej 	if ((intstat & INTSTAT_INT_ACTV) == 0)
    619       1.1   thorpej 		return (claimed);
    620       1.1   thorpej 
    621       1.1   thorpej 	claimed = 1;
    622       1.1   thorpej 
    623       1.1   thorpej 	/*
    624       1.1   thorpej 	 * Acknowledge the interrupt.
    625       1.1   thorpej 	 */
    626       1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_INTSTAT,
    627       1.1   thorpej 	    intstat & INTMASK);
    628       1.1   thorpej 
    629       1.1   thorpej 	/*
    630       1.1   thorpej 	 * Check for receive interrupts.
    631       1.1   thorpej 	 */
    632      1.21   thorpej 	if (intstat & (INTSTAT_RCC | INTSTAT_RXE | INTSTAT_RQE)) {
    633       1.1   thorpej 		for (i = sc->sc_rxptr;; i = EPIC_NEXTRX(i)) {
    634      1.10   thorpej 			rxd = EPIC_CDRX(sc, i);
    635      1.10   thorpej 			ds = EPIC_DSRX(sc, i);
    636      1.10   thorpej 
    637      1.10   thorpej 			EPIC_CDRXSYNC(sc, i,
    638      1.10   thorpej 			    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    639       1.1   thorpej 
    640      1.55   tsutsui 			rxstatus = rxd->er_rxstatus;
    641      1.55   tsutsui 			if (rxstatus & ER_RXSTAT_OWNER) {
    642       1.1   thorpej 				/*
    643       1.1   thorpej 				 * We have processed all of the
    644       1.1   thorpej 				 * receive buffers.
    645       1.1   thorpej 				 */
    646       1.1   thorpej 				break;
    647       1.1   thorpej 			}
    648       1.1   thorpej 
    649       1.1   thorpej 			/*
    650      1.10   thorpej 			 * Make sure the packet arrived intact.  If an error
    651      1.10   thorpej 			 * occurred, update stats and reset the descriptor.
    652      1.10   thorpej 			 * The buffer will be reused the next time the
    653      1.10   thorpej 			 * descriptor comes up in the ring.
    654       1.1   thorpej 			 */
    655      1.55   tsutsui 			if ((rxstatus & ER_RXSTAT_PKTINTACT) == 0) {
    656      1.55   tsutsui 				if (rxstatus & ER_RXSTAT_CRCERROR)
    657       1.1   thorpej 					printf("%s: CRC error\n",
    658       1.1   thorpej 					    sc->sc_dev.dv_xname);
    659      1.55   tsutsui 				if (rxstatus & ER_RXSTAT_ALIGNERROR)
    660       1.1   thorpej 					printf("%s: alignment error\n",
    661       1.1   thorpej 					    sc->sc_dev.dv_xname);
    662       1.1   thorpej 				ifp->if_ierrors++;
    663      1.10   thorpej 				EPIC_INIT_RXDESC(sc, i);
    664      1.10   thorpej 				continue;
    665       1.1   thorpej 			}
    666       1.1   thorpej 
    667      1.10   thorpej 			bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
    668      1.10   thorpej 			    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
    669      1.10   thorpej 
    670      1.21   thorpej 			/*
    671      1.57   thorpej 			 * The EPIC includes the CRC with every packet;
    672      1.57   thorpej 			 * trim it.
    673      1.21   thorpej 			 */
    674      1.57   thorpej 			len = RXSTAT_RXLENGTH(rxstatus) - ETHER_CRC_LEN;
    675      1.21   thorpej 
    676      1.19   thorpej 			if (len < sizeof(struct ether_header)) {
    677      1.19   thorpej 				/*
    678      1.19   thorpej 				 * Runt packet; drop it now.
    679      1.19   thorpej 				 */
    680      1.10   thorpej 				ifp->if_ierrors++;
    681      1.10   thorpej 				EPIC_INIT_RXDESC(sc, i);
    682      1.10   thorpej 				bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
    683      1.10   thorpej 				    ds->ds_dmamap->dm_mapsize,
    684      1.10   thorpej 				    BUS_DMASYNC_PREREAD);
    685      1.10   thorpej 				continue;
    686      1.10   thorpej 			}
    687      1.10   thorpej 
    688      1.19   thorpej 			/*
    689      1.19   thorpej 			 * If the packet is small enough to fit in a
    690      1.19   thorpej 			 * single header mbuf, allocate one and copy
    691      1.19   thorpej 			 * the data into it.  This greatly reduces
    692      1.19   thorpej 			 * memory consumption when we receive lots
    693      1.19   thorpej 			 * of small packets.
    694      1.19   thorpej 			 *
    695      1.19   thorpej 			 * Otherwise, we add a new buffer to the receive
    696      1.19   thorpej 			 * chain.  If this fails, we drop the packet and
    697      1.19   thorpej 			 * recycle the old buffer.
    698      1.19   thorpej 			 */
    699      1.19   thorpej 			if (epic_copy_small != 0 && len <= MHLEN) {
    700      1.19   thorpej 				MGETHDR(m, M_DONTWAIT, MT_DATA);
    701      1.19   thorpej 				if (m == NULL)
    702      1.19   thorpej 					goto dropit;
    703      1.64  christos 				memcpy(mtod(m, void *),
    704      1.64  christos 				    mtod(ds->ds_mbuf, void *), len);
    705      1.19   thorpej 				EPIC_INIT_RXDESC(sc, i);
    706      1.19   thorpej 				bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
    707      1.19   thorpej 				    ds->ds_dmamap->dm_mapsize,
    708      1.19   thorpej 				    BUS_DMASYNC_PREREAD);
    709      1.19   thorpej 			} else {
    710      1.19   thorpej 				m = ds->ds_mbuf;
    711      1.19   thorpej 				if (epic_add_rxbuf(sc, i) != 0) {
    712      1.19   thorpej  dropit:
    713      1.19   thorpej 					ifp->if_ierrors++;
    714      1.19   thorpej 					EPIC_INIT_RXDESC(sc, i);
    715      1.19   thorpej 					bus_dmamap_sync(sc->sc_dmat,
    716      1.19   thorpej 					    ds->ds_dmamap, 0,
    717      1.19   thorpej 					    ds->ds_dmamap->dm_mapsize,
    718      1.19   thorpej 					    BUS_DMASYNC_PREREAD);
    719      1.19   thorpej 					continue;
    720      1.19   thorpej 				}
    721      1.10   thorpej 			}
    722      1.10   thorpej 
    723      1.10   thorpej 			m->m_pkthdr.rcvif = ifp;
    724      1.10   thorpej 			m->m_pkthdr.len = m->m_len = len;
    725       1.1   thorpej 
    726      1.10   thorpej #if NBPFILTER > 0
    727      1.10   thorpej 			/*
    728      1.10   thorpej 			 * Pass this up to any BPF listeners, but only
    729      1.68   tsutsui 			 * pass it up the stack if it's for us.
    730      1.10   thorpej 			 */
    731      1.33   thorpej 			if (ifp->if_bpf)
    732      1.10   thorpej 				bpf_mtap(ifp->if_bpf, m);
    733      1.33   thorpej #endif
    734      1.33   thorpej 
    735      1.16   thorpej 			/* Pass it on. */
    736      1.16   thorpej 			(*ifp->if_input)(ifp, m);
    737      1.17   thorpej 			ifp->if_ipackets++;
    738       1.1   thorpej 		}
    739      1.10   thorpej 
    740      1.42   tsutsui 		/* Update the receive pointer. */
    741       1.1   thorpej 		sc->sc_rxptr = i;
    742       1.1   thorpej 
    743       1.1   thorpej 		/*
    744       1.1   thorpej 		 * Check for receive queue underflow.
    745       1.1   thorpej 		 */
    746       1.1   thorpej 		if (intstat & INTSTAT_RQE) {
    747       1.1   thorpej 			printf("%s: receiver queue empty\n",
    748       1.1   thorpej 			    sc->sc_dev.dv_xname);
    749       1.1   thorpej 			/*
    750       1.1   thorpej 			 * Ring is already built; just restart the
    751       1.1   thorpej 			 * receiver.
    752       1.1   thorpej 			 */
    753       1.1   thorpej 			bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_PRCDAR,
    754      1.10   thorpej 			    EPIC_CDRXADDR(sc, sc->sc_rxptr));
    755       1.1   thorpej 			bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_COMMAND,
    756       1.1   thorpej 			    COMMAND_RXQUEUED | COMMAND_START_RX);
    757       1.1   thorpej 		}
    758       1.1   thorpej 	}
    759       1.1   thorpej 
    760       1.1   thorpej 	/*
    761       1.1   thorpej 	 * Check for transmission complete interrupts.
    762       1.1   thorpej 	 */
    763       1.1   thorpej 	if (intstat & (INTSTAT_TXC | INTSTAT_TXU)) {
    764      1.10   thorpej 		ifp->if_flags &= ~IFF_OACTIVE;
    765      1.10   thorpej 		for (i = sc->sc_txdirty; sc->sc_txpending != 0;
    766      1.10   thorpej 		     i = EPIC_NEXTTX(i), sc->sc_txpending--) {
    767      1.10   thorpej 			txd = EPIC_CDTX(sc, i);
    768      1.10   thorpej 			ds = EPIC_DSTX(sc, i);
    769       1.1   thorpej 
    770      1.10   thorpej 			EPIC_CDTXSYNC(sc, i,
    771      1.10   thorpej 			    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    772      1.10   thorpej 
    773      1.55   tsutsui 			txstatus = txd->et_txstatus;
    774      1.55   tsutsui 			if (txstatus & ET_TXSTAT_OWNER)
    775       1.1   thorpej 				break;
    776       1.1   thorpej 
    777      1.10   thorpej 			EPIC_CDFLSYNC(sc, i, BUS_DMASYNC_POSTWRITE);
    778      1.10   thorpej 
    779      1.10   thorpej 			bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap,
    780      1.10   thorpej 			    0, ds->ds_dmamap->dm_mapsize,
    781      1.10   thorpej 			    BUS_DMASYNC_POSTWRITE);
    782      1.10   thorpej 			bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
    783      1.10   thorpej 			m_freem(ds->ds_mbuf);
    784      1.10   thorpej 			ds->ds_mbuf = NULL;
    785       1.1   thorpej 
    786       1.1   thorpej 			/*
    787       1.1   thorpej 			 * Check for errors and collisions.
    788       1.1   thorpej 			 */
    789      1.55   tsutsui 			if ((txstatus & ET_TXSTAT_PACKETTX) == 0)
    790       1.1   thorpej 				ifp->if_oerrors++;
    791      1.10   thorpej 			else
    792      1.10   thorpej 				ifp->if_opackets++;
    793       1.1   thorpej 			ifp->if_collisions +=
    794      1.55   tsutsui 			    TXSTAT_COLLISIONS(txstatus);
    795      1.55   tsutsui 			if (txstatus & ET_TXSTAT_CARSENSELOST)
    796       1.1   thorpej 				printf("%s: lost carrier\n",
    797       1.1   thorpej 				    sc->sc_dev.dv_xname);
    798       1.1   thorpej 		}
    799      1.59     perry 
    800      1.10   thorpej 		/* Update the dirty transmit buffer pointer. */
    801       1.1   thorpej 		sc->sc_txdirty = i;
    802       1.1   thorpej 
    803       1.1   thorpej 		/*
    804       1.1   thorpej 		 * Cancel the watchdog timer if there are no pending
    805       1.1   thorpej 		 * transmissions.
    806       1.1   thorpej 		 */
    807       1.1   thorpej 		if (sc->sc_txpending == 0)
    808       1.1   thorpej 			ifp->if_timer = 0;
    809       1.1   thorpej 
    810       1.1   thorpej 		/*
    811       1.1   thorpej 		 * Kick the transmitter after a DMA underrun.
    812       1.1   thorpej 		 */
    813       1.1   thorpej 		if (intstat & INTSTAT_TXU) {
    814       1.1   thorpej 			printf("%s: transmit underrun\n", sc->sc_dev.dv_xname);
    815       1.1   thorpej 			bus_space_write_4(sc->sc_st, sc->sc_sh,
    816       1.1   thorpej 			    EPIC_COMMAND, COMMAND_TXUGO);
    817       1.1   thorpej 			if (sc->sc_txpending)
    818       1.1   thorpej 				bus_space_write_4(sc->sc_st, sc->sc_sh,
    819       1.1   thorpej 				    EPIC_COMMAND, COMMAND_TXQUEUED);
    820       1.1   thorpej 		}
    821       1.1   thorpej 
    822       1.1   thorpej 		/*
    823       1.1   thorpej 		 * Try to get more packets going.
    824       1.1   thorpej 		 */
    825       1.1   thorpej 		epic_start(ifp);
    826       1.1   thorpej 	}
    827       1.1   thorpej 
    828       1.1   thorpej 	/*
    829       1.1   thorpej 	 * Check for fatal interrupts.
    830       1.1   thorpej 	 */
    831       1.1   thorpej 	if (intstat & INTSTAT_FATAL_INT) {
    832      1.21   thorpej 		if (intstat & INTSTAT_PTA)
    833      1.21   thorpej 			printf("%s: PCI target abort error\n",
    834      1.21   thorpej 			    sc->sc_dev.dv_xname);
    835      1.21   thorpej 		else if (intstat & INTSTAT_PMA)
    836      1.21   thorpej 			printf("%s: PCI master abort error\n",
    837      1.21   thorpej 			    sc->sc_dev.dv_xname);
    838      1.21   thorpej 		else if (intstat & INTSTAT_APE)
    839      1.21   thorpej 			printf("%s: PCI address parity error\n",
    840      1.21   thorpej 			    sc->sc_dev.dv_xname);
    841      1.21   thorpej 		else if (intstat & INTSTAT_DPE)
    842      1.21   thorpej 			printf("%s: PCI data parity error\n",
    843      1.21   thorpej 			    sc->sc_dev.dv_xname);
    844      1.21   thorpej 		else
    845      1.21   thorpej 			printf("%s: unknown fatal error\n",
    846      1.21   thorpej 			    sc->sc_dev.dv_xname);
    847      1.34   thorpej 		(void) epic_init(ifp);
    848       1.1   thorpej 	}
    849       1.1   thorpej 
    850       1.1   thorpej 	/*
    851       1.1   thorpej 	 * Check for more interrupts.
    852       1.1   thorpej 	 */
    853       1.1   thorpej 	goto top;
    854       1.1   thorpej }
    855       1.1   thorpej 
    856       1.1   thorpej /*
    857       1.8   thorpej  * One second timer, used to tick the MII.
    858       1.8   thorpej  */
    859       1.8   thorpej void
    860       1.8   thorpej epic_tick(arg)
    861       1.8   thorpej 	void *arg;
    862       1.8   thorpej {
    863       1.8   thorpej 	struct epic_softc *sc = arg;
    864       1.8   thorpej 	int s;
    865       1.8   thorpej 
    866      1.12   thorpej 	s = splnet();
    867       1.8   thorpej 	mii_tick(&sc->sc_mii);
    868       1.8   thorpej 	splx(s);
    869       1.8   thorpej 
    870      1.29   thorpej 	callout_reset(&sc->sc_mii_callout, hz, epic_tick, sc);
    871       1.8   thorpej }
    872       1.8   thorpej 
    873       1.8   thorpej /*
    874       1.6   thorpej  * Fixup the clock source on the EPIC.
    875       1.6   thorpej  */
    876       1.6   thorpej void
    877       1.6   thorpej epic_fixup_clock_source(sc)
    878       1.6   thorpej 	struct epic_softc *sc;
    879       1.6   thorpej {
    880       1.6   thorpej 	int i;
    881       1.6   thorpej 
    882       1.6   thorpej 	/*
    883       1.6   thorpej 	 * According to SMC Application Note 7-15, the EPIC's clock
    884       1.6   thorpej 	 * source is incorrect following a reset.  This manifests itself
    885       1.6   thorpej 	 * as failure to recognize when host software has written to
    886       1.6   thorpej 	 * a register on the EPIC.  The appnote recommends issuing at
    887       1.6   thorpej 	 * least 16 consecutive writes to the CLOCK TEST bit to correctly
    888       1.6   thorpej 	 * configure the clock source.
    889       1.6   thorpej 	 */
    890       1.6   thorpej 	for (i = 0; i < 16; i++)
    891       1.6   thorpej 		bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_TEST,
    892       1.6   thorpej 		    TEST_CLOCKTEST);
    893       1.6   thorpej }
    894       1.6   thorpej 
    895       1.6   thorpej /*
    896       1.1   thorpej  * Perform a soft reset on the EPIC.
    897       1.1   thorpej  */
    898       1.1   thorpej void
    899       1.1   thorpej epic_reset(sc)
    900       1.1   thorpej 	struct epic_softc *sc;
    901       1.1   thorpej {
    902       1.1   thorpej 
    903       1.6   thorpej 	epic_fixup_clock_source(sc);
    904       1.6   thorpej 
    905       1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_GENCTL, 0);
    906       1.1   thorpej 	delay(100);
    907       1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_GENCTL, GENCTL_SOFTRESET);
    908       1.1   thorpej 	delay(100);
    909       1.6   thorpej 
    910       1.6   thorpej 	epic_fixup_clock_source(sc);
    911       1.1   thorpej }
    912       1.1   thorpej 
    913       1.1   thorpej /*
    914       1.7   mycroft  * Initialize the interface.  Must be called at splnet().
    915       1.1   thorpej  */
    916      1.19   thorpej int
    917      1.34   thorpej epic_init(ifp)
    918      1.34   thorpej 	struct ifnet *ifp;
    919       1.1   thorpej {
    920      1.34   thorpej 	struct epic_softc *sc = ifp->if_softc;
    921       1.1   thorpej 	bus_space_tag_t st = sc->sc_st;
    922       1.1   thorpej 	bus_space_handle_t sh = sc->sc_sh;
    923      1.66    dyoung 	const uint8_t *enaddr = CLLADDR(ifp->if_sadl);
    924       1.1   thorpej 	struct epic_txdesc *txd;
    925      1.19   thorpej 	struct epic_descsoft *ds;
    926      1.63   tsutsui 	uint32_t genctl, reg0;
    927      1.19   thorpej 	int i, error = 0;
    928       1.1   thorpej 
    929       1.1   thorpej 	/*
    930       1.1   thorpej 	 * Cancel any pending I/O.
    931       1.1   thorpej 	 */
    932      1.34   thorpej 	epic_stop(ifp, 0);
    933       1.1   thorpej 
    934       1.1   thorpej 	/*
    935       1.1   thorpej 	 * Reset the EPIC to a known state.
    936       1.1   thorpej 	 */
    937       1.1   thorpej 	epic_reset(sc);
    938       1.1   thorpej 
    939       1.1   thorpej 	/*
    940       1.1   thorpej 	 * Magical mystery initialization.
    941       1.1   thorpej 	 */
    942       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_TXTEST, 0);
    943       1.1   thorpej 
    944       1.1   thorpej 	/*
    945       1.1   thorpej 	 * Initialize the EPIC genctl register:
    946       1.1   thorpej 	 *
    947       1.1   thorpej 	 *	- 64 byte receive FIFO threshold
    948       1.1   thorpej 	 *	- automatic advance to next receive frame
    949       1.1   thorpej 	 */
    950       1.1   thorpej 	genctl = GENCTL_RX_FIFO_THRESH0 | GENCTL_ONECOPY;
    951      1.18   thorpej #if BYTE_ORDER == BIG_ENDIAN
    952      1.18   thorpej 	genctl |= GENCTL_BIG_ENDIAN;
    953      1.18   thorpej #endif
    954       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_GENCTL, genctl);
    955       1.1   thorpej 
    956       1.1   thorpej 	/*
    957       1.1   thorpej 	 * Reset the MII bus and PHY.
    958       1.1   thorpej 	 */
    959       1.1   thorpej 	reg0 = bus_space_read_4(st, sh, EPIC_NVCTL);
    960       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_NVCTL, reg0 | NVCTL_GPIO1 | NVCTL_GPOE1);
    961       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_MIICFG, MIICFG_ENASER);
    962       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_GENCTL, genctl | GENCTL_RESET_PHY);
    963       1.1   thorpej 	delay(100);
    964       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_GENCTL, genctl);
    965      1.44  drochner 	delay(1000);
    966       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_NVCTL, reg0);
    967       1.1   thorpej 
    968       1.1   thorpej 	/*
    969       1.1   thorpej 	 * Initialize Ethernet address.
    970       1.1   thorpej 	 */
    971       1.1   thorpej 	reg0 = enaddr[1] << 8 | enaddr[0];
    972       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_LAN0, reg0);
    973       1.1   thorpej 	reg0 = enaddr[3] << 8 | enaddr[2];
    974       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_LAN1, reg0);
    975       1.1   thorpej 	reg0 = enaddr[5] << 8 | enaddr[4];
    976       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_LAN2, reg0);
    977       1.1   thorpej 
    978       1.1   thorpej 	/*
    979       1.1   thorpej 	 * Initialize receive control.  Remember the external buffer
    980       1.1   thorpej 	 * size setting.
    981       1.1   thorpej 	 */
    982       1.1   thorpej 	reg0 = bus_space_read_4(st, sh, EPIC_RXCON) &
    983       1.1   thorpej 	    (RXCON_EXTBUFSIZESEL1 | RXCON_EXTBUFSIZESEL0);
    984       1.1   thorpej 	reg0 |= (RXCON_RXMULTICAST | RXCON_RXBROADCAST);
    985       1.1   thorpej 	if (ifp->if_flags & IFF_PROMISC)
    986       1.1   thorpej 		reg0 |= RXCON_PROMISCMODE;
    987       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_RXCON, reg0);
    988       1.1   thorpej 
    989      1.13   thorpej 	/* Set the current media. */
    990      1.71    dyoung 	if ((error = epic_mediachange(ifp)) != 0)
    991      1.71    dyoung 		goto out;
    992       1.1   thorpej 
    993      1.13   thorpej 	/* Set up the multicast hash table. */
    994      1.13   thorpej 	epic_set_mchash(sc);
    995      1.13   thorpej 
    996       1.1   thorpej 	/*
    997      1.10   thorpej 	 * Initialize the transmit descriptor ring.  txlast is initialized
    998      1.10   thorpej 	 * to the end of the list so that it will wrap around to the first
    999      1.10   thorpej 	 * descriptor when the first packet is transmitted.
   1000       1.1   thorpej 	 */
   1001       1.1   thorpej 	for (i = 0; i < EPIC_NTXDESC; i++) {
   1002      1.10   thorpej 		txd = EPIC_CDTX(sc, i);
   1003      1.10   thorpej 		memset(txd, 0, sizeof(struct epic_txdesc));
   1004      1.10   thorpej 		txd->et_bufaddr = EPIC_CDFLADDR(sc, i);
   1005      1.10   thorpej 		txd->et_nextdesc = EPIC_CDTXADDR(sc, EPIC_NEXTTX(i));
   1006      1.10   thorpej 		EPIC_CDTXSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1007       1.1   thorpej 	}
   1008      1.10   thorpej 	sc->sc_txpending = 0;
   1009      1.10   thorpej 	sc->sc_txdirty = 0;
   1010      1.10   thorpej 	sc->sc_txlast = EPIC_NTXDESC - 1;
   1011       1.1   thorpej 
   1012       1.1   thorpej 	/*
   1013      1.19   thorpej 	 * Initialize the receive descriptor ring.
   1014       1.1   thorpej 	 */
   1015      1.19   thorpej 	for (i = 0; i < EPIC_NRXDESC; i++) {
   1016      1.19   thorpej 		ds = EPIC_DSRX(sc, i);
   1017      1.19   thorpej 		if (ds->ds_mbuf == NULL) {
   1018      1.19   thorpej 			if ((error = epic_add_rxbuf(sc, i)) != 0) {
   1019      1.19   thorpej 				printf("%s: unable to allocate or map rx "
   1020      1.19   thorpej 				    "buffer %d error = %d\n",
   1021      1.19   thorpej 				    sc->sc_dev.dv_xname, i, error);
   1022      1.19   thorpej 				/*
   1023      1.19   thorpej 				 * XXX Should attempt to run with fewer receive
   1024      1.19   thorpej 				 * XXX buffers instead of just failing.
   1025      1.19   thorpej 				 */
   1026      1.19   thorpej 				epic_rxdrain(sc);
   1027      1.19   thorpej 				goto out;
   1028      1.19   thorpej 			}
   1029      1.48   thorpej 		} else
   1030      1.48   thorpej 			EPIC_INIT_RXDESC(sc, i);
   1031      1.19   thorpej 	}
   1032      1.10   thorpej 	sc->sc_rxptr = 0;
   1033       1.1   thorpej 
   1034       1.1   thorpej 	/*
   1035       1.1   thorpej 	 * Initialize the interrupt mask and enable interrupts.
   1036       1.1   thorpej 	 */
   1037       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_INTMASK, INTMASK);
   1038       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_GENCTL, genctl | GENCTL_INTENA);
   1039       1.1   thorpej 
   1040       1.1   thorpej 	/*
   1041       1.1   thorpej 	 * Give the transmit and receive rings to the EPIC.
   1042       1.1   thorpej 	 */
   1043       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_PTCDAR,
   1044      1.10   thorpej 	    EPIC_CDTXADDR(sc, EPIC_NEXTTX(sc->sc_txlast)));
   1045       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_PRCDAR,
   1046      1.10   thorpej 	    EPIC_CDRXADDR(sc, sc->sc_rxptr));
   1047       1.1   thorpej 
   1048       1.1   thorpej 	/*
   1049       1.1   thorpej 	 * Set the EPIC in motion.
   1050       1.1   thorpej 	 */
   1051       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_COMMAND,
   1052       1.1   thorpej 	    COMMAND_RXQUEUED | COMMAND_START_RX);
   1053       1.1   thorpej 
   1054       1.1   thorpej 	/*
   1055       1.1   thorpej 	 * ...all done!
   1056       1.1   thorpej 	 */
   1057       1.1   thorpej 	ifp->if_flags |= IFF_RUNNING;
   1058       1.1   thorpej 	ifp->if_flags &= ~IFF_OACTIVE;
   1059       1.8   thorpej 
   1060       1.8   thorpej 	/*
   1061       1.8   thorpej 	 * Start the one second clock.
   1062       1.8   thorpej 	 */
   1063      1.29   thorpej 	callout_reset(&sc->sc_mii_callout, hz, epic_tick, sc);
   1064       1.9   thorpej 
   1065       1.9   thorpej 	/*
   1066       1.9   thorpej 	 * Attempt to start output on the interface.
   1067       1.9   thorpej 	 */
   1068       1.9   thorpej 	epic_start(ifp);
   1069      1.19   thorpej 
   1070      1.19   thorpej  out:
   1071      1.19   thorpej 	if (error)
   1072      1.19   thorpej 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
   1073      1.19   thorpej 	return (error);
   1074      1.19   thorpej }
   1075      1.19   thorpej 
   1076      1.19   thorpej /*
   1077      1.19   thorpej  * Drain the receive queue.
   1078      1.19   thorpej  */
   1079      1.19   thorpej void
   1080      1.19   thorpej epic_rxdrain(sc)
   1081      1.19   thorpej 	struct epic_softc *sc;
   1082      1.19   thorpej {
   1083      1.19   thorpej 	struct epic_descsoft *ds;
   1084      1.19   thorpej 	int i;
   1085      1.19   thorpej 
   1086      1.19   thorpej 	for (i = 0; i < EPIC_NRXDESC; i++) {
   1087      1.19   thorpej 		ds = EPIC_DSRX(sc, i);
   1088      1.19   thorpej 		if (ds->ds_mbuf != NULL) {
   1089      1.19   thorpej 			bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
   1090      1.19   thorpej 			m_freem(ds->ds_mbuf);
   1091      1.19   thorpej 			ds->ds_mbuf = NULL;
   1092      1.19   thorpej 		}
   1093      1.19   thorpej 	}
   1094       1.1   thorpej }
   1095       1.1   thorpej 
   1096       1.1   thorpej /*
   1097       1.1   thorpej  * Stop transmission on the interface.
   1098       1.1   thorpej  */
   1099       1.1   thorpej void
   1100      1.34   thorpej epic_stop(ifp, disable)
   1101      1.34   thorpej 	struct ifnet *ifp;
   1102      1.34   thorpej 	int disable;
   1103       1.1   thorpej {
   1104      1.34   thorpej 	struct epic_softc *sc = ifp->if_softc;
   1105       1.1   thorpej 	bus_space_tag_t st = sc->sc_st;
   1106       1.1   thorpej 	bus_space_handle_t sh = sc->sc_sh;
   1107       1.1   thorpej 	struct epic_descsoft *ds;
   1108      1.63   tsutsui 	uint32_t reg;
   1109       1.1   thorpej 	int i;
   1110       1.6   thorpej 
   1111       1.8   thorpej 	/*
   1112       1.8   thorpej 	 * Stop the one second clock.
   1113       1.8   thorpej 	 */
   1114      1.29   thorpej 	callout_stop(&sc->sc_mii_callout);
   1115      1.23   thorpej 
   1116      1.23   thorpej 	/* Down the MII. */
   1117      1.23   thorpej 	mii_down(&sc->sc_mii);
   1118       1.8   thorpej 
   1119       1.6   thorpej 	/* Paranoia... */
   1120       1.6   thorpej 	epic_fixup_clock_source(sc);
   1121       1.1   thorpej 
   1122       1.1   thorpej 	/*
   1123       1.1   thorpej 	 * Disable interrupts.
   1124       1.1   thorpej 	 */
   1125       1.1   thorpej 	reg = bus_space_read_4(st, sh, EPIC_GENCTL);
   1126       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_GENCTL, reg & ~GENCTL_INTENA);
   1127       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_INTMASK, 0);
   1128       1.1   thorpej 
   1129       1.1   thorpej 	/*
   1130       1.1   thorpej 	 * Stop the DMA engine and take the receiver off-line.
   1131       1.1   thorpej 	 */
   1132       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_COMMAND, COMMAND_STOP_RDMA |
   1133       1.1   thorpej 	    COMMAND_STOP_TDMA | COMMAND_STOP_RX);
   1134       1.1   thorpej 
   1135       1.1   thorpej 	/*
   1136       1.1   thorpej 	 * Release any queued transmit buffers.
   1137       1.1   thorpej 	 */
   1138       1.1   thorpej 	for (i = 0; i < EPIC_NTXDESC; i++) {
   1139      1.10   thorpej 		ds = EPIC_DSTX(sc, i);
   1140       1.1   thorpej 		if (ds->ds_mbuf != NULL) {
   1141       1.1   thorpej 			bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
   1142       1.1   thorpej 			m_freem(ds->ds_mbuf);
   1143       1.1   thorpej 			ds->ds_mbuf = NULL;
   1144       1.1   thorpej 		}
   1145      1.19   thorpej 	}
   1146      1.19   thorpej 
   1147       1.1   thorpej 	/*
   1148       1.1   thorpej 	 * Mark the interface down and cancel the watchdog timer.
   1149       1.1   thorpej 	 */
   1150       1.1   thorpej 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1151       1.1   thorpej 	ifp->if_timer = 0;
   1152  1.71.2.1   keiichi 
   1153  1.71.2.1   keiichi 	if (disable)
   1154  1.71.2.1   keiichi 		epic_rxdrain(sc);
   1155       1.1   thorpej }
   1156       1.1   thorpej 
   1157       1.1   thorpej /*
   1158       1.1   thorpej  * Read the EPIC Serial EEPROM.
   1159       1.1   thorpej  */
   1160       1.1   thorpej void
   1161       1.1   thorpej epic_read_eeprom(sc, word, wordcnt, data)
   1162       1.1   thorpej 	struct epic_softc *sc;
   1163       1.1   thorpej 	int word, wordcnt;
   1164      1.63   tsutsui 	uint16_t *data;
   1165       1.1   thorpej {
   1166       1.1   thorpej 	bus_space_tag_t st = sc->sc_st;
   1167       1.1   thorpej 	bus_space_handle_t sh = sc->sc_sh;
   1168      1.63   tsutsui 	uint16_t reg;
   1169       1.1   thorpej 	int i, x;
   1170       1.1   thorpej 
   1171       1.1   thorpej #define	EEPROM_WAIT_READY(st, sh) \
   1172       1.1   thorpej 	while ((bus_space_read_4((st), (sh), EPIC_EECTL) & EECTL_EERDY) == 0) \
   1173       1.1   thorpej 		/* nothing */
   1174       1.1   thorpej 
   1175       1.1   thorpej 	/*
   1176       1.1   thorpej 	 * Enable the EEPROM.
   1177       1.1   thorpej 	 */
   1178       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_EECTL, EECTL_ENABLE);
   1179       1.1   thorpej 	EEPROM_WAIT_READY(st, sh);
   1180       1.1   thorpej 
   1181       1.1   thorpej 	for (i = 0; i < wordcnt; i++) {
   1182       1.1   thorpej 		/* Send CHIP SELECT for one clock tick. */
   1183       1.1   thorpej 		bus_space_write_4(st, sh, EPIC_EECTL, EECTL_ENABLE|EECTL_EECS);
   1184       1.1   thorpej 		EEPROM_WAIT_READY(st, sh);
   1185       1.1   thorpej 
   1186       1.1   thorpej 		/* Shift in the READ opcode. */
   1187       1.1   thorpej 		for (x = 3; x > 0; x--) {
   1188       1.1   thorpej 			reg = EECTL_ENABLE|EECTL_EECS;
   1189       1.1   thorpej 			if (EPIC_EEPROM_OPC_READ & (1 << (x - 1)))
   1190       1.1   thorpej 				reg |= EECTL_EEDI;
   1191       1.1   thorpej 			bus_space_write_4(st, sh, EPIC_EECTL, reg);
   1192       1.1   thorpej 			EEPROM_WAIT_READY(st, sh);
   1193       1.1   thorpej 			bus_space_write_4(st, sh, EPIC_EECTL, reg|EECTL_EESK);
   1194       1.1   thorpej 			EEPROM_WAIT_READY(st, sh);
   1195       1.1   thorpej 			bus_space_write_4(st, sh, EPIC_EECTL, reg);
   1196       1.1   thorpej 			EEPROM_WAIT_READY(st, sh);
   1197       1.1   thorpej 		}
   1198       1.1   thorpej 
   1199       1.1   thorpej 		/* Shift in address. */
   1200       1.1   thorpej 		for (x = 6; x > 0; x--) {
   1201       1.1   thorpej 			reg = EECTL_ENABLE|EECTL_EECS;
   1202       1.1   thorpej 			if ((word + i) & (1 << (x - 1)))
   1203      1.59     perry 				reg |= EECTL_EEDI;
   1204       1.1   thorpej 			bus_space_write_4(st, sh, EPIC_EECTL, reg);
   1205       1.1   thorpej 			EEPROM_WAIT_READY(st, sh);
   1206       1.1   thorpej 			bus_space_write_4(st, sh, EPIC_EECTL, reg|EECTL_EESK);
   1207       1.1   thorpej 			EEPROM_WAIT_READY(st, sh);
   1208       1.1   thorpej 			bus_space_write_4(st, sh, EPIC_EECTL, reg);
   1209       1.1   thorpej 			EEPROM_WAIT_READY(st, sh);
   1210       1.1   thorpej 		}
   1211       1.1   thorpej 
   1212       1.1   thorpej 		/* Shift out data. */
   1213       1.1   thorpej 		reg = EECTL_ENABLE|EECTL_EECS;
   1214       1.1   thorpej 		data[i] = 0;
   1215       1.1   thorpej 		for (x = 16; x > 0; x--) {
   1216       1.1   thorpej 			bus_space_write_4(st, sh, EPIC_EECTL, reg|EECTL_EESK);
   1217       1.1   thorpej 			EEPROM_WAIT_READY(st, sh);
   1218       1.1   thorpej 			if (bus_space_read_4(st, sh, EPIC_EECTL) & EECTL_EEDO)
   1219       1.1   thorpej 				data[i] |= (1 << (x - 1));
   1220       1.1   thorpej 			bus_space_write_4(st, sh, EPIC_EECTL, reg);
   1221       1.1   thorpej 			EEPROM_WAIT_READY(st, sh);
   1222       1.1   thorpej 		}
   1223       1.1   thorpej 
   1224       1.1   thorpej 		/* Clear CHIP SELECT. */
   1225       1.1   thorpej 		bus_space_write_4(st, sh, EPIC_EECTL, EECTL_ENABLE);
   1226       1.1   thorpej 		EEPROM_WAIT_READY(st, sh);
   1227       1.1   thorpej 	}
   1228       1.1   thorpej 
   1229       1.1   thorpej 	/*
   1230       1.1   thorpej 	 * Disable the EEPROM.
   1231       1.1   thorpej 	 */
   1232       1.1   thorpej 	bus_space_write_4(st, sh, EPIC_EECTL, 0);
   1233       1.1   thorpej 
   1234       1.1   thorpej #undef EEPROM_WAIT_READY
   1235       1.1   thorpej }
   1236       1.1   thorpej 
   1237       1.1   thorpej /*
   1238       1.1   thorpej  * Add a receive buffer to the indicated descriptor.
   1239       1.1   thorpej  */
   1240       1.1   thorpej int
   1241      1.59     perry epic_add_rxbuf(sc, idx)
   1242       1.1   thorpej 	struct epic_softc *sc;
   1243       1.1   thorpej 	int idx;
   1244       1.1   thorpej {
   1245      1.10   thorpej 	struct epic_descsoft *ds = EPIC_DSRX(sc, idx);
   1246      1.10   thorpej 	struct mbuf *m;
   1247      1.10   thorpej 	int error;
   1248       1.1   thorpej 
   1249      1.10   thorpej 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1250      1.10   thorpej 	if (m == NULL)
   1251      1.10   thorpej 		return (ENOBUFS);
   1252       1.1   thorpej 
   1253      1.10   thorpej 	MCLGET(m, M_DONTWAIT);
   1254      1.10   thorpej 	if ((m->m_flags & M_EXT) == 0) {
   1255      1.10   thorpej 		m_freem(m);
   1256      1.10   thorpej 		return (ENOBUFS);
   1257       1.1   thorpej 	}
   1258       1.1   thorpej 
   1259      1.10   thorpej 	if (ds->ds_mbuf != NULL)
   1260      1.10   thorpej 		bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
   1261      1.10   thorpej 
   1262       1.1   thorpej 	ds->ds_mbuf = m;
   1263       1.1   thorpej 
   1264      1.10   thorpej 	error = bus_dmamap_load(sc->sc_dmat, ds->ds_dmamap,
   1265      1.47   thorpej 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
   1266      1.47   thorpej 	    BUS_DMA_READ|BUS_DMA_NOWAIT);
   1267      1.10   thorpej 	if (error) {
   1268      1.10   thorpej 		printf("%s: can't load rx DMA map %d, error = %d\n",
   1269      1.10   thorpej 		    sc->sc_dev.dv_xname, idx, error);
   1270      1.10   thorpej 		panic("epic_add_rxbuf");	/* XXX */
   1271       1.1   thorpej 	}
   1272       1.1   thorpej 
   1273       1.1   thorpej 	bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
   1274       1.1   thorpej 	    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1275       1.1   thorpej 
   1276      1.10   thorpej 	EPIC_INIT_RXDESC(sc, idx);
   1277       1.1   thorpej 
   1278      1.10   thorpej 	return (0);
   1279       1.1   thorpej }
   1280       1.1   thorpej 
   1281       1.1   thorpej /*
   1282       1.1   thorpej  * Set the EPIC multicast hash table.
   1283      1.13   thorpej  *
   1284      1.13   thorpej  * NOTE: We rely on a recently-updated mii_media_active here!
   1285       1.1   thorpej  */
   1286       1.1   thorpej void
   1287       1.1   thorpej epic_set_mchash(sc)
   1288       1.1   thorpej 	struct epic_softc *sc;
   1289       1.1   thorpej {
   1290       1.1   thorpej 	struct ethercom *ec = &sc->sc_ethercom;
   1291       1.1   thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1292       1.1   thorpej 	struct ether_multi *enm;
   1293       1.1   thorpej 	struct ether_multistep step;
   1294      1.63   tsutsui 	uint32_t hash, mchash[4];
   1295       1.1   thorpej 
   1296       1.1   thorpej 	/*
   1297       1.1   thorpej 	 * Set up the multicast address filter by passing all multicast
   1298      1.31   thorpej 	 * addresses through a CRC generator, and then using the low-order
   1299       1.1   thorpej 	 * 6 bits as an index into the 64 bit multicast hash table (only
   1300       1.1   thorpej 	 * the lower 16 bits of each 32 bit multicast hash register are
   1301      1.31   thorpej 	 * valid).  The high order bits select the register, while the
   1302       1.1   thorpej 	 * rest of the bits select the bit within the register.
   1303       1.1   thorpej 	 */
   1304       1.1   thorpej 
   1305       1.1   thorpej 	if (ifp->if_flags & IFF_PROMISC)
   1306       1.1   thorpej 		goto allmulti;
   1307       1.1   thorpej 
   1308      1.13   thorpej 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T) {
   1309      1.13   thorpej 		/* XXX hardware bug in 10Mbps mode. */
   1310      1.13   thorpej 		goto allmulti;
   1311      1.13   thorpej 	}
   1312       1.1   thorpej 
   1313       1.1   thorpej 	mchash[0] = mchash[1] = mchash[2] = mchash[3] = 0;
   1314       1.1   thorpej 
   1315       1.1   thorpej 	ETHER_FIRST_MULTI(step, ec, enm);
   1316       1.1   thorpej 	while (enm != NULL) {
   1317      1.46   thorpej 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1318       1.1   thorpej 			/*
   1319       1.1   thorpej 			 * We must listen to a range of multicast addresses.
   1320       1.1   thorpej 			 * For now, just accept all multicasts, rather than
   1321       1.1   thorpej 			 * trying to set only those filter bits needed to match
   1322       1.1   thorpej 			 * the range.  (At this time, the only use of address
   1323       1.1   thorpej 			 * ranges is for IP multicast routing, for which the
   1324       1.1   thorpej 			 * range is big enough to require all bits set.)
   1325       1.1   thorpej 			 */
   1326       1.1   thorpej 			goto allmulti;
   1327       1.1   thorpej 		}
   1328       1.1   thorpej 
   1329      1.37   thorpej 		hash = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
   1330      1.37   thorpej 		hash >>= 26;
   1331       1.1   thorpej 
   1332       1.1   thorpej 		/* Set the corresponding bit in the hash table. */
   1333      1.31   thorpej 		mchash[hash >> 4] |= 1 << (hash & 0xf);
   1334       1.1   thorpej 
   1335       1.1   thorpej 		ETHER_NEXT_MULTI(step, enm);
   1336       1.1   thorpej 	}
   1337       1.1   thorpej 
   1338       1.1   thorpej 	ifp->if_flags &= ~IFF_ALLMULTI;
   1339       1.1   thorpej 	goto sethash;
   1340       1.1   thorpej 
   1341       1.1   thorpej  allmulti:
   1342       1.1   thorpej 	ifp->if_flags |= IFF_ALLMULTI;
   1343       1.1   thorpej 	mchash[0] = mchash[1] = mchash[2] = mchash[3] = 0xffff;
   1344       1.1   thorpej 
   1345       1.1   thorpej  sethash:
   1346       1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MC0, mchash[0]);
   1347       1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MC1, mchash[1]);
   1348       1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MC2, mchash[2]);
   1349       1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MC3, mchash[3]);
   1350       1.8   thorpej }
   1351       1.8   thorpej 
   1352       1.8   thorpej /*
   1353       1.8   thorpej  * Wait for the MII to become ready.
   1354       1.8   thorpej  */
   1355       1.8   thorpej int
   1356       1.8   thorpej epic_mii_wait(sc, rw)
   1357       1.8   thorpej 	struct epic_softc *sc;
   1358      1.63   tsutsui 	uint32_t rw;
   1359       1.8   thorpej {
   1360       1.8   thorpej 	int i;
   1361       1.8   thorpej 
   1362       1.8   thorpej 	for (i = 0; i < 50; i++) {
   1363       1.8   thorpej 		if ((bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_MMCTL) & rw)
   1364       1.8   thorpej 		    == 0)
   1365       1.8   thorpej 			break;
   1366       1.8   thorpej 		delay(2);
   1367       1.8   thorpej 	}
   1368       1.8   thorpej 	if (i == 50) {
   1369       1.8   thorpej 		printf("%s: MII timed out\n", sc->sc_dev.dv_xname);
   1370       1.8   thorpej 		return (1);
   1371       1.8   thorpej 	}
   1372       1.8   thorpej 
   1373       1.8   thorpej 	return (0);
   1374       1.8   thorpej }
   1375       1.8   thorpej 
   1376       1.8   thorpej /*
   1377       1.8   thorpej  * Read from the MII.
   1378       1.8   thorpej  */
   1379       1.8   thorpej int
   1380       1.8   thorpej epic_mii_read(self, phy, reg)
   1381       1.8   thorpej 	struct device *self;
   1382       1.8   thorpej 	int phy, reg;
   1383       1.8   thorpej {
   1384       1.8   thorpej 	struct epic_softc *sc = (struct epic_softc *)self;
   1385       1.8   thorpej 
   1386       1.8   thorpej 	if (epic_mii_wait(sc, MMCTL_WRITE))
   1387       1.8   thorpej 		return (0);
   1388       1.8   thorpej 
   1389       1.8   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MMCTL,
   1390       1.8   thorpej 	    MMCTL_ARG(phy, reg, MMCTL_READ));
   1391       1.8   thorpej 
   1392       1.8   thorpej 	if (epic_mii_wait(sc, MMCTL_READ))
   1393       1.8   thorpej 		return (0);
   1394       1.8   thorpej 
   1395       1.8   thorpej 	return (bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_MMDATA) &
   1396       1.8   thorpej 	    MMDATA_MASK);
   1397       1.8   thorpej }
   1398       1.8   thorpej 
   1399       1.8   thorpej /*
   1400       1.8   thorpej  * Write to the MII.
   1401       1.8   thorpej  */
   1402       1.8   thorpej void
   1403       1.8   thorpej epic_mii_write(self, phy, reg, val)
   1404       1.8   thorpej 	struct device *self;
   1405       1.8   thorpej 	int phy, reg, val;
   1406       1.8   thorpej {
   1407       1.8   thorpej 	struct epic_softc *sc = (struct epic_softc *)self;
   1408       1.8   thorpej 
   1409       1.8   thorpej 	if (epic_mii_wait(sc, MMCTL_WRITE))
   1410       1.8   thorpej 		return;
   1411       1.8   thorpej 
   1412       1.8   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MMDATA, val);
   1413       1.8   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MMCTL,
   1414       1.8   thorpej 	    MMCTL_ARG(phy, reg, MMCTL_WRITE));
   1415       1.8   thorpej }
   1416       1.8   thorpej 
   1417       1.8   thorpej /*
   1418       1.8   thorpej  * Callback from PHY when media changes.
   1419       1.8   thorpej  */
   1420       1.8   thorpej void
   1421       1.8   thorpej epic_statchg(self)
   1422       1.8   thorpej 	struct device *self;
   1423       1.8   thorpej {
   1424      1.11   thorpej 	struct epic_softc *sc = (struct epic_softc *)self;
   1425      1.63   tsutsui 	uint32_t txcon, miicfg;
   1426      1.11   thorpej 
   1427      1.11   thorpej 	/*
   1428      1.11   thorpej 	 * Update loopback bits in TXCON to reflect duplex mode.
   1429      1.11   thorpej 	 */
   1430      1.11   thorpej 	txcon = bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_TXCON);
   1431      1.11   thorpej 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   1432      1.11   thorpej 		txcon |= (TXCON_LOOPBACK_D1|TXCON_LOOPBACK_D2);
   1433      1.11   thorpej 	else
   1434      1.11   thorpej 		txcon &= ~(TXCON_LOOPBACK_D1|TXCON_LOOPBACK_D2);
   1435      1.11   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_TXCON, txcon);
   1436      1.13   thorpej 
   1437      1.43  drochner 	/* On some cards we need manualy set fullduplex led */
   1438      1.43  drochner 	if (sc->sc_hwflags & EPIC_DUPLEXLED_ON_694) {
   1439      1.43  drochner 		miicfg = bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_MIICFG);
   1440      1.59     perry 		if (IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX)
   1441      1.43  drochner 			miicfg |= MIICFG_ENABLE;
   1442      1.43  drochner 		else
   1443      1.43  drochner 			miicfg &= ~MIICFG_ENABLE;
   1444      1.43  drochner 		bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MIICFG, miicfg);
   1445      1.43  drochner 	}
   1446      1.43  drochner 
   1447      1.13   thorpej 	/*
   1448      1.13   thorpej 	 * There is a multicast filter bug in 10Mbps mode.  Kick the
   1449      1.13   thorpej 	 * multicast filter in case the speed changed.
   1450      1.13   thorpej 	 */
   1451      1.13   thorpej 	epic_set_mchash(sc);
   1452       1.8   thorpej }
   1453       1.8   thorpej 
   1454       1.8   thorpej /*
   1455       1.8   thorpej  * Callback from ifmedia to request new media setting.
   1456      1.70    dyoung  *
   1457      1.70    dyoung  * XXX Looks to me like some of this complexity should move into
   1458      1.70    dyoung  * XXX one or two custom PHY drivers. --dyoung
   1459       1.8   thorpej  */
   1460       1.8   thorpej int
   1461       1.8   thorpej epic_mediachange(ifp)
   1462       1.8   thorpej 	struct ifnet *ifp;
   1463       1.8   thorpej {
   1464      1.11   thorpej 	struct epic_softc *sc = ifp->if_softc;
   1465      1.43  drochner 	struct mii_data *mii = &sc->sc_mii;
   1466      1.43  drochner 	struct ifmedia *ifm = &mii->mii_media;
   1467      1.43  drochner 	int media = ifm->ifm_cur->ifm_media;
   1468      1.63   tsutsui 	uint32_t miicfg;
   1469      1.43  drochner 	struct mii_softc *miisc;
   1470      1.71    dyoung 	int cfg, rc;
   1471      1.43  drochner 
   1472      1.70    dyoung 	if ((ifp->if_flags & IFF_UP) == 0)
   1473      1.43  drochner 		return (0);
   1474      1.43  drochner 
   1475      1.43  drochner 	if (IFM_INST(media) != sc->sc_serinst) {
   1476      1.43  drochner 		/* If we're not selecting serial interface, select MII mode */
   1477      1.43  drochner #ifdef EPICMEDIADEBUG
   1478      1.43  drochner 		printf("%s: parallel mode\n", ifp->if_xname);
   1479      1.59     perry #endif
   1480      1.43  drochner 		miicfg = bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_MIICFG);
   1481      1.43  drochner 		miicfg &= ~MIICFG_SERMODEENA;
   1482      1.43  drochner 		bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MIICFG, miicfg);
   1483      1.43  drochner 	}
   1484      1.43  drochner 
   1485      1.71    dyoung 	if ((rc = mii_mediachg(mii)) == ENXIO)
   1486      1.71    dyoung 		rc = 0;
   1487      1.43  drochner 
   1488      1.43  drochner 	if (IFM_INST(media) == sc->sc_serinst) {
   1489      1.43  drochner 		/* select serial interface */
   1490      1.43  drochner #ifdef EPICMEDIADEBUG
   1491      1.43  drochner 		printf("%s: serial mode\n", ifp->if_xname);
   1492      1.43  drochner #endif
   1493      1.43  drochner 		miicfg = bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_MIICFG);
   1494      1.43  drochner 		miicfg |= (MIICFG_SERMODEENA | MIICFG_ENABLE);
   1495      1.43  drochner 		bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MIICFG, miicfg);
   1496      1.43  drochner 
   1497      1.43  drochner 		/* There is no driver to fill this */
   1498      1.43  drochner 		mii->mii_media_active = media;
   1499      1.43  drochner 		mii->mii_media_status = 0;
   1500      1.43  drochner 
   1501      1.43  drochner 		epic_statchg(&sc->sc_dev);
   1502      1.43  drochner 		return (0);
   1503      1.43  drochner 	}
   1504      1.43  drochner 
   1505      1.43  drochner 	/* Lookup selected PHY */
   1506      1.69    dyoung 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
   1507      1.43  drochner 		if (IFM_INST(media) == miisc->mii_inst)
   1508      1.43  drochner 			break;
   1509      1.43  drochner 	}
   1510      1.43  drochner 	if (!miisc) {
   1511      1.43  drochner 		printf("epic_mediachange: can't happen\n"); /* ??? panic */
   1512      1.43  drochner 		return (0);
   1513      1.43  drochner 	}
   1514      1.43  drochner #ifdef EPICMEDIADEBUG
   1515      1.43  drochner 	printf("%s: using phy %s\n", ifp->if_xname,
   1516      1.43  drochner 	       miisc->mii_dev.dv_xname);
   1517      1.43  drochner #endif
   1518      1.43  drochner 
   1519      1.43  drochner 	if (miisc->mii_flags & MIIF_HAVEFIBER) {
   1520      1.43  drochner 		/* XXX XXX assume it's a Level1 - should check */
   1521      1.43  drochner 
   1522      1.54       wiz 		/* We have to powerup fiber transceivers */
   1523      1.43  drochner 		cfg = PHY_READ(miisc, MII_LXTPHY_CONFIG);
   1524      1.43  drochner 		if (IFM_SUBTYPE(media) == IFM_100_FX) {
   1525      1.43  drochner #ifdef EPICMEDIADEBUG
   1526      1.43  drochner 			printf("%s: power up fiber\n", ifp->if_xname);
   1527      1.43  drochner #endif
   1528      1.43  drochner 			cfg |= (CONFIG_LEDC1 | CONFIG_LEDC0);
   1529      1.43  drochner 		} else {
   1530      1.43  drochner #ifdef EPICMEDIADEBUG
   1531      1.43  drochner 			printf("%s: power down fiber\n", ifp->if_xname);
   1532      1.43  drochner #endif
   1533      1.43  drochner 			cfg &= ~(CONFIG_LEDC1 | CONFIG_LEDC0);
   1534      1.43  drochner 		}
   1535      1.43  drochner 		PHY_WRITE(miisc, MII_LXTPHY_CONFIG, cfg);
   1536      1.43  drochner 	}
   1537       1.8   thorpej 
   1538      1.71    dyoung 	return rc;
   1539       1.1   thorpej }
   1540