Home | History | Annotate | Line # | Download | only in sbus
be.c revision 1.77
      1  1.77     joerg /*	$NetBSD: be.c,v 1.77 2010/04/05 07:21:47 joerg Exp $	*/
      2   1.1        pk 
      3   1.1        pk /*-
      4   1.1        pk  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5   1.1        pk  * All rights reserved.
      6   1.1        pk  *
      7   1.1        pk  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1        pk  * by Paul Kranenburg.
      9   1.1        pk  *
     10   1.1        pk  * Redistribution and use in source and binary forms, with or without
     11   1.1        pk  * modification, are permitted provided that the following conditions
     12   1.1        pk  * are met:
     13   1.1        pk  * 1. Redistributions of source code must retain the above copyright
     14   1.1        pk  *    notice, this list of conditions and the following disclaimer.
     15   1.1        pk  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1        pk  *    notice, this list of conditions and the following disclaimer in the
     17   1.1        pk  *    documentation and/or other materials provided with the distribution.
     18   1.1        pk  *
     19   1.1        pk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1        pk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1        pk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1        pk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1        pk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1        pk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1        pk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1        pk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1        pk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1        pk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1        pk  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1        pk  */
     31   1.1        pk 
     32   1.1        pk /*
     33   1.1        pk  * Copyright (c) 1998 Theo de Raadt and Jason L. Wright.
     34   1.1        pk  * All rights reserved.
     35   1.1        pk  *
     36   1.1        pk  * Redistribution and use in source and binary forms, with or without
     37   1.1        pk  * modification, are permitted provided that the following conditions
     38   1.1        pk  * are met:
     39   1.1        pk  * 1. Redistributions of source code must retain the above copyright
     40   1.1        pk  *    notice, this list of conditions and the following disclaimer.
     41   1.1        pk  * 2. Redistributions in binary form must reproduce the above copyright
     42   1.1        pk  *    notice, this list of conditions and the following disclaimer in the
     43   1.1        pk  *    documentation and/or other materials provided with the distribution.
     44   1.1        pk  * 3. The name of the authors may not be used to endorse or promote products
     45   1.1        pk  *    derived from this software without specific prior written permission.
     46   1.1        pk  *
     47   1.1        pk  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     48   1.1        pk  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49   1.1        pk  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50   1.1        pk  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     51   1.1        pk  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52   1.1        pk  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53   1.1        pk  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54   1.1        pk  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55   1.1        pk  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56   1.1        pk  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57   1.1        pk  */
     58  1.29     lukem 
     59  1.29     lukem #include <sys/cdefs.h>
     60  1.77     joerg __KERNEL_RCSID(0, "$NetBSD: be.c,v 1.77 2010/04/05 07:21:47 joerg Exp $");
     61   1.1        pk 
     62   1.1        pk #include "opt_ddb.h"
     63   1.1        pk #include "opt_inet.h"
     64   1.1        pk #include "rnd.h"
     65   1.1        pk 
     66   1.1        pk #include <sys/param.h>
     67   1.1        pk #include <sys/systm.h>
     68  1.17   thorpej #include <sys/callout.h>
     69   1.1        pk #include <sys/kernel.h>
     70   1.1        pk #include <sys/errno.h>
     71   1.1        pk #include <sys/ioctl.h>
     72   1.1        pk #include <sys/mbuf.h>
     73   1.1        pk #include <sys/socket.h>
     74   1.1        pk #include <sys/syslog.h>
     75   1.1        pk #include <sys/device.h>
     76   1.1        pk #include <sys/malloc.h>
     77   1.1        pk #if NRND > 0
     78   1.1        pk #include <sys/rnd.h>
     79   1.1        pk #endif
     80   1.1        pk 
     81   1.1        pk #include <net/if.h>
     82   1.1        pk #include <net/if_dl.h>
     83   1.1        pk #include <net/if_types.h>
     84   1.1        pk #include <net/netisr.h>
     85   1.1        pk #include <net/if_media.h>
     86   1.1        pk #include <net/if_ether.h>
     87   1.1        pk 
     88   1.1        pk #ifdef INET
     89   1.1        pk #include <netinet/in.h>
     90   1.1        pk #include <netinet/if_inarp.h>
     91   1.1        pk #include <netinet/in_systm.h>
     92   1.1        pk #include <netinet/in_var.h>
     93   1.1        pk #include <netinet/ip.h>
     94   1.1        pk #endif
     95   1.1        pk 
     96   1.3        pk 
     97   1.1        pk #include <net/bpf.h>
     98   1.1        pk #include <net/bpfdesc.h>
     99   1.1        pk 
    100  1.55        ad #include <sys/bus.h>
    101  1.55        ad #include <sys/intr.h>
    102   1.1        pk #include <machine/autoconf.h>
    103   1.1        pk 
    104   1.1        pk #include <dev/sbus/sbusvar.h>
    105   1.1        pk 
    106   1.1        pk #include <dev/mii/mii.h>
    107   1.1        pk #include <dev/mii/miivar.h>
    108   1.1        pk 
    109   1.1        pk #include <dev/sbus/qecreg.h>
    110   1.1        pk #include <dev/sbus/qecvar.h>
    111   1.1        pk #include <dev/sbus/bereg.h>
    112   1.1        pk 
    113   1.1        pk struct be_softc {
    114  1.69   tsutsui 	device_t	sc_dev;
    115  1.39       wiz 	bus_space_tag_t	sc_bustag;	/* bus & DMA tags */
    116   1.1        pk 	bus_dma_tag_t	sc_dmatag;
    117  1.18        pk 	bus_dmamap_t	sc_dmamap;
    118   1.1        pk 	struct	ethercom sc_ethercom;
    119   1.1        pk 	/*struct	ifmedia sc_ifmedia;	-* interface media */
    120   1.1        pk 	struct mii_data	sc_mii;		/* MII media control */
    121   1.1        pk #define sc_media	sc_mii.mii_media/* shorthand */
    122  1.11        pk 	int		sc_phys[2];	/* MII instance -> phy */
    123   1.1        pk 
    124  1.17   thorpej 	struct callout sc_tick_ch;
    125  1.17   thorpej 
    126  1.12        pk 	/*
    127  1.12        pk 	 * Some `mii_softc' items we need to emulate MII operation
    128  1.12        pk 	 * for our internal transceiver.
    129  1.12        pk 	 */
    130  1.12        pk 	int		sc_mii_inst;	/* instance of internal phy */
    131  1.12        pk 	int		sc_mii_active;	/* currently active medium */
    132  1.12        pk 	int		sc_mii_ticks;	/* tick counter */
    133  1.13        pk 	int		sc_mii_flags;	/* phy status flags */
    134  1.13        pk #define MIIF_HAVELINK	0x04000000
    135  1.13        pk 	int		sc_intphy_curspeed;	/* Established link speed */
    136  1.12        pk 
    137   1.1        pk 	struct	qec_softc *sc_qec;	/* QEC parent */
    138   1.1        pk 
    139   1.1        pk 	bus_space_handle_t	sc_qr;	/* QEC registers */
    140   1.1        pk 	bus_space_handle_t	sc_br;	/* BE registers */
    141   1.1        pk 	bus_space_handle_t	sc_cr;	/* channel registers */
    142   1.1        pk 	bus_space_handle_t	sc_tr;	/* transceiver registers */
    143   1.1        pk 
    144   1.1        pk 	u_int	sc_rev;
    145   1.1        pk 
    146   1.1        pk 	int	sc_channel;		/* channel number */
    147   1.1        pk 	int	sc_burst;
    148   1.1        pk 
    149   1.2        pk 	struct  qec_ring	sc_rb;	/* Packet Ring Buffer */
    150   1.1        pk 
    151   1.1        pk 	/* MAC address */
    152  1.73   tsutsui 	uint8_t sc_enaddr[ETHER_ADDR_LEN];
    153  1.43        pk #ifdef BEDEBUG
    154  1.43        pk 	int	sc_debug;
    155  1.43        pk #endif
    156   1.1        pk };
    157   1.1        pk 
    158  1.72   tsutsui static int	bematch(device_t, cfdata_t, void *);
    159  1.72   tsutsui static void	beattach(device_t, device_t, void *);
    160   1.1        pk 
    161  1.72   tsutsui static int	beinit(struct ifnet *);
    162  1.72   tsutsui static void	bestart(struct ifnet *);
    163  1.72   tsutsui static void	bestop(struct ifnet *, int);
    164  1.72   tsutsui static void	bewatchdog(struct ifnet *);
    165  1.72   tsutsui static int	beioctl(struct ifnet *, u_long, void *);
    166  1.72   tsutsui static void	bereset(struct be_softc *);
    167  1.72   tsutsui static void	behwreset(struct be_softc *);
    168  1.72   tsutsui 
    169  1.72   tsutsui static int	beintr(void *);
    170  1.72   tsutsui static int	berint(struct be_softc *);
    171  1.72   tsutsui static int	betint(struct be_softc *);
    172  1.73   tsutsui static int	beqint(struct be_softc *, uint32_t);
    173  1.73   tsutsui static int	beeint(struct be_softc *, uint32_t);
    174  1.45     perry 
    175  1.45     perry static void	be_read(struct be_softc *, int, int);
    176  1.45     perry static int	be_put(struct be_softc *, int, struct mbuf *);
    177  1.45     perry static struct mbuf *be_get(struct be_softc *, int, int);
    178   1.1        pk 
    179  1.72   tsutsui static void	be_pal_gate(struct be_softc *, int);
    180   1.1        pk 
    181   1.1        pk /* ifmedia callbacks */
    182  1.72   tsutsui static void	be_ifmedia_sts(struct ifnet *, struct ifmediareq *);
    183  1.72   tsutsui static int	be_ifmedia_upd(struct ifnet *);
    184   1.2        pk 
    185  1.72   tsutsui static void	be_mcreset(struct be_softc *);
    186   1.1        pk 
    187   1.1        pk /* MII methods & callbacks */
    188  1.65    cegger static int	be_mii_readreg(device_t, int, int);
    189  1.65    cegger static void	be_mii_writereg(device_t, int, int, int);
    190  1.65    cegger static void	be_mii_statchg(device_t);
    191   1.1        pk 
    192   1.1        pk /* MII helpers */
    193  1.45     perry static void	be_mii_sync(struct be_softc *);
    194  1.73   tsutsui static void	be_mii_sendbits(struct be_softc *, int, uint32_t, int);
    195  1.45     perry static int	be_mii_reset(struct be_softc *, int);
    196  1.45     perry static int	be_tcvr_read_bit(struct be_softc *, int);
    197  1.45     perry static void	be_tcvr_write_bit(struct be_softc *, int, int);
    198  1.45     perry 
    199  1.72   tsutsui static void	be_tick(void *);
    200  1.72   tsutsui #if 0
    201  1.72   tsutsui static void	be_intphy_auto(struct be_softc *);
    202  1.72   tsutsui #endif
    203  1.72   tsutsui static void	be_intphy_status(struct be_softc *);
    204  1.72   tsutsui static int	be_intphy_service(struct be_softc *, struct mii_data *, int);
    205   1.1        pk 
    206   1.1        pk 
    207  1.69   tsutsui CFATTACH_DECL_NEW(be, sizeof(struct be_softc),
    208  1.37   thorpej     bematch, beattach, NULL, NULL);
    209   1.1        pk 
    210   1.1        pk int
    211  1.65    cegger bematch(device_t parent, cfdata_t cf, void *aux)
    212   1.1        pk {
    213   1.1        pk 	struct sbus_attach_args *sa = aux;
    214   1.1        pk 
    215  1.73   tsutsui 	return strcmp(cf->cf_name, sa->sa_name) == 0;
    216   1.1        pk }
    217   1.1        pk 
    218   1.1        pk void
    219  1.65    cegger beattach(device_t parent, device_t self, void *aux)
    220   1.1        pk {
    221   1.1        pk 	struct sbus_attach_args *sa = aux;
    222  1.67   tsutsui 	struct qec_softc *qec = device_private(parent);
    223  1.67   tsutsui 	struct be_softc *sc = device_private(self);
    224   1.1        pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    225   1.1        pk 	struct mii_data *mii = &sc->sc_mii;
    226  1.11        pk 	struct mii_softc *child;
    227   1.1        pk 	int node = sa->sa_node;
    228  1.18        pk 	bus_dma_tag_t dmatag = sa->sa_dmatag;
    229   1.1        pk 	bus_dma_segment_t seg;
    230   1.1        pk 	bus_size_t size;
    231  1.18        pk 	int instance;
    232   1.1        pk 	int rseg, error;
    233  1.73   tsutsui 	uint32_t v;
    234   1.1        pk 
    235  1.69   tsutsui 	sc->sc_dev = self;
    236  1.69   tsutsui 
    237   1.1        pk 	if (sa->sa_nreg < 3) {
    238  1.70   tsutsui 		printf(": only %d register sets\n", sa->sa_nreg);
    239   1.1        pk 		return;
    240   1.1        pk 	}
    241   1.1        pk 
    242  1.30        pk 	if (bus_space_map(sa->sa_bustag,
    243  1.73   tsutsui 	    (bus_addr_t)BUS_ADDR(sa->sa_reg[0].oa_space, sa->sa_reg[0].oa_base),
    244  1.73   tsutsui 	    (bus_size_t)sa->sa_reg[0].oa_size,
    245  1.73   tsutsui 	    0, &sc->sc_cr) != 0) {
    246  1.70   tsutsui 		printf(": cannot map registers\n");
    247   1.1        pk 		return;
    248   1.1        pk 	}
    249   1.1        pk 
    250  1.30        pk 	if (bus_space_map(sa->sa_bustag,
    251  1.73   tsutsui 	    (bus_addr_t)BUS_ADDR(sa->sa_reg[1].oa_space, sa->sa_reg[1].oa_base),
    252  1.73   tsutsui 	    (bus_size_t)sa->sa_reg[1].oa_size,
    253  1.73   tsutsui 	    0, &sc->sc_br) != 0) {
    254  1.70   tsutsui 		printf(": cannot map registers\n");
    255   1.1        pk 		return;
    256   1.1        pk 	}
    257   1.1        pk 
    258  1.30        pk 	if (bus_space_map(sa->sa_bustag,
    259  1.73   tsutsui 	    (bus_addr_t)BUS_ADDR(sa->sa_reg[2].oa_space, sa->sa_reg[2].oa_base),
    260  1.73   tsutsui 	    (bus_size_t)sa->sa_reg[2].oa_size,
    261  1.73   tsutsui 	    0, &sc->sc_tr) != 0) {
    262  1.70   tsutsui 		printf(": cannot map registers\n");
    263   1.1        pk 		return;
    264   1.1        pk 	}
    265   1.1        pk 
    266  1.27       eeh 	sc->sc_bustag = sa->sa_bustag;
    267   1.1        pk 	sc->sc_qec = qec;
    268   1.1        pk 	sc->sc_qr = qec->sc_regs;
    269   1.1        pk 
    270  1.42        pk 	sc->sc_rev = prom_getpropint(node, "board-version", -1);
    271  1.70   tsutsui 	printf(": rev %x,", sc->sc_rev);
    272   1.1        pk 
    273  1.61  macallan 	callout_init(&sc->sc_tick_ch, 0);
    274  1.61  macallan 
    275  1.42        pk 	sc->sc_channel = prom_getpropint(node, "channel#", -1);
    276   1.1        pk 	if (sc->sc_channel == -1)
    277   1.1        pk 		sc->sc_channel = 0;
    278   1.1        pk 
    279  1.42        pk 	sc->sc_burst = prom_getpropint(node, "burst-sizes", -1);
    280   1.1        pk 	if (sc->sc_burst == -1)
    281   1.1        pk 		sc->sc_burst = qec->sc_burst;
    282   1.1        pk 
    283   1.1        pk 	/* Clamp at parent's burst sizes */
    284   1.1        pk 	sc->sc_burst &= qec->sc_burst;
    285   1.1        pk 
    286   1.9        pk 	/* Establish interrupt handler */
    287   1.9        pk 	if (sa->sa_nintr)
    288  1.21        pk 		(void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_NET,
    289  1.73   tsutsui 		    beintr, sc);
    290   1.1        pk 
    291  1.41        pk 	prom_getether(node, sc->sc_enaddr);
    292   1.1        pk 	printf(" address %s\n", ether_sprintf(sc->sc_enaddr));
    293   1.1        pk 
    294   1.1        pk 	/*
    295   1.1        pk 	 * Allocate descriptor ring and buffers.
    296   1.1        pk 	 */
    297   1.2        pk 
    298   1.2        pk 	/* for now, allocate as many bufs as there are ring descriptors */
    299   1.2        pk 	sc->sc_rb.rb_ntbuf = QEC_XD_RING_MAXSIZE;
    300   1.2        pk 	sc->sc_rb.rb_nrbuf = QEC_XD_RING_MAXSIZE;
    301   1.1        pk 
    302  1.73   tsutsui 	size =
    303  1.73   tsutsui 	    QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd) +
    304  1.73   tsutsui 	    QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd) +
    305  1.73   tsutsui 	    sc->sc_rb.rb_ntbuf * BE_PKT_BUF_SZ +
    306  1.73   tsutsui 	    sc->sc_rb.rb_nrbuf * BE_PKT_BUF_SZ;
    307  1.18        pk 
    308  1.19        pk 	/* Get a DMA handle */
    309  1.19        pk 	if ((error = bus_dmamap_create(dmatag, size, 1, size, 0,
    310  1.73   tsutsui 	    BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
    311  1.57    cegger 		aprint_error_dev(self, "DMA map create error %d\n", error);
    312  1.18        pk 		return;
    313  1.18        pk 	}
    314  1.18        pk 
    315  1.18        pk 	/* Allocate DMA buffer */
    316  1.20        pk 	if ((error = bus_dmamem_alloc(sa->sa_dmatag, size, 0, 0,
    317  1.73   tsutsui 	    &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    318  1.73   tsutsui 		aprint_error_dev(self, "DMA buffer alloc error %d\n", error);
    319   1.1        pk 		return;
    320   1.1        pk 	}
    321  1.18        pk 
    322  1.18        pk 	/* Map DMA memory in CPU addressable space */
    323   1.1        pk 	if ((error = bus_dmamem_map(sa->sa_dmatag, &seg, rseg, size,
    324  1.73   tsutsui 	    &sc->sc_rb.rb_membase, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    325  1.73   tsutsui 		aprint_error_dev(self, "DMA buffer map error %d\n", error);
    326   1.1        pk 		bus_dmamem_free(sa->sa_dmatag, &seg, rseg);
    327  1.25   thorpej 		return;
    328  1.25   thorpej 	}
    329  1.25   thorpej 
    330  1.25   thorpej 	/* Load the buffer */
    331  1.25   thorpej 	if ((error = bus_dmamap_load(dmatag, sc->sc_dmamap,
    332  1.73   tsutsui 	    sc->sc_rb.rb_membase, size, NULL, BUS_DMA_NOWAIT)) != 0) {
    333  1.73   tsutsui 		aprint_error_dev(self, "DMA buffer map load error %d\n", error);
    334  1.25   thorpej 		bus_dmamem_unmap(dmatag, sc->sc_rb.rb_membase, size);
    335  1.25   thorpej 		bus_dmamem_free(dmatag, &seg, rseg);
    336   1.1        pk 		return;
    337   1.1        pk 	}
    338  1.26        pk 	sc->sc_rb.rb_dmabase = sc->sc_dmamap->dm_segs[0].ds_addr;
    339   1.1        pk 
    340   1.1        pk 	/*
    341   1.1        pk 	 * Initialize our media structures and MII info.
    342   1.1        pk 	 */
    343   1.1        pk 	mii->mii_ifp = ifp;
    344   1.1        pk 	mii->mii_readreg = be_mii_readreg;
    345   1.1        pk 	mii->mii_writereg = be_mii_writereg;
    346  1.10        pk 	mii->mii_statchg = be_mii_statchg;
    347   1.1        pk 
    348   1.1        pk 	ifmedia_init(&mii->mii_media, 0, be_ifmedia_upd, be_ifmedia_sts);
    349   1.1        pk 
    350  1.11        pk 	/*
    351  1.11        pk 	 * Initialize transceiver and determine which PHY connection to use.
    352  1.11        pk 	 */
    353  1.11        pk 	be_mii_sync(sc);
    354  1.11        pk 	v = bus_space_read_4(sc->sc_bustag, sc->sc_tr, BE_TRI_MGMTPAL);
    355  1.11        pk 
    356  1.11        pk 	instance = 0;
    357  1.11        pk 
    358  1.11        pk 	if ((v & MGMT_PAL_EXT_MDIO) != 0) {
    359  1.10        pk 
    360  1.67   tsutsui 		mii_attach(self, mii, 0xffffffff, BE_PHY_EXTERNAL,
    361  1.15   thorpej 		    MII_OFFSET_ANY, 0);
    362   1.1        pk 
    363  1.11        pk 		child = LIST_FIRST(&mii->mii_phys);
    364  1.11        pk 		if (child == NULL) {
    365   1.1        pk 			/* No PHY attached */
    366  1.11        pk 			ifmedia_add(&sc->sc_media,
    367  1.73   tsutsui 			    IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, instance),
    368  1.73   tsutsui 			    0, NULL);
    369  1.11        pk 			ifmedia_set(&sc->sc_media,
    370  1.73   tsutsui 			    IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, instance));
    371   1.1        pk 		} else {
    372   1.1        pk 			/*
    373  1.11        pk 			 * Note: we support just one PHY on the external
    374  1.11        pk 			 * MII connector.
    375  1.11        pk 			 */
    376  1.11        pk #ifdef DIAGNOSTIC
    377  1.11        pk 			if (LIST_NEXT(child, mii_list) != NULL) {
    378  1.67   tsutsui 				aprint_error_dev(self,
    379  1.67   tsutsui 				    "spurious MII device %s attached\n",
    380  1.67   tsutsui 				    device_xname(child->mii_dev));
    381  1.11        pk 			}
    382  1.11        pk #endif
    383  1.11        pk 			if (child->mii_phy != BE_PHY_EXTERNAL ||
    384  1.11        pk 			    child->mii_inst > 0) {
    385  1.67   tsutsui 				aprint_error_dev(self,
    386  1.67   tsutsui 				    "cannot accommodate MII device %s"
    387  1.67   tsutsui 				    " at phy %d, instance %d\n",
    388  1.59   xtraeme 				       device_xname(child->mii_dev),
    389  1.11        pk 				       child->mii_phy, child->mii_inst);
    390  1.11        pk 			} else {
    391  1.11        pk 				sc->sc_phys[instance] = child->mii_phy;
    392  1.11        pk 			}
    393  1.11        pk 
    394  1.11        pk 			/*
    395   1.1        pk 			 * XXX - we can really do the following ONLY if the
    396   1.1        pk 			 * phy indeed has the auto negotiation capability!!
    397   1.1        pk 			 */
    398  1.11        pk 			ifmedia_set(&sc->sc_media,
    399  1.73   tsutsui 			    IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, instance));
    400  1.11        pk 
    401  1.11        pk 			/* Mark our current media setting */
    402  1.11        pk 			be_pal_gate(sc, BE_PHY_EXTERNAL);
    403  1.11        pk 			instance++;
    404   1.1        pk 		}
    405  1.11        pk 
    406  1.11        pk 	}
    407  1.11        pk 
    408  1.11        pk 	if ((v & MGMT_PAL_INT_MDIO) != 0) {
    409   1.1        pk 		/*
    410   1.1        pk 		 * The be internal phy looks vaguely like MII hardware,
    411   1.1        pk 		 * but not enough to be able to use the MII device
    412   1.1        pk 		 * layer. Hence, we have to take care of media selection
    413   1.1        pk 		 * ourselves.
    414   1.1        pk 		 */
    415   1.1        pk 
    416  1.12        pk 		sc->sc_mii_inst = instance;
    417  1.11        pk 		sc->sc_phys[instance] = BE_PHY_INTERNAL;
    418  1.11        pk 
    419   1.1        pk 		/* Use `ifm_data' to store BMCR bits */
    420   1.1        pk 		ifmedia_add(&sc->sc_media,
    421  1.73   tsutsui 		    IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, instance),
    422  1.73   tsutsui 		    0, NULL);
    423   1.1        pk 		ifmedia_add(&sc->sc_media,
    424  1.73   tsutsui 		    IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, instance),
    425  1.73   tsutsui 		    BMCR_S100, NULL);
    426   1.1        pk 		ifmedia_add(&sc->sc_media,
    427  1.73   tsutsui 		    IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, instance),
    428  1.73   tsutsui 		    0, NULL);
    429  1.11        pk 
    430  1.13        pk 		printf("on-board transceiver at %s: 10baseT, 100baseTX, auto\n",
    431  1.73   tsutsui 		    device_xname(self));
    432  1.13        pk 
    433  1.12        pk 		be_mii_reset(sc, BE_PHY_INTERNAL);
    434  1.11        pk 		/* Only set default medium here if there's no external PHY */
    435  1.11        pk 		if (instance == 0) {
    436  1.11        pk 			be_pal_gate(sc, BE_PHY_INTERNAL);
    437  1.11        pk 			ifmedia_set(&sc->sc_media,
    438  1.73   tsutsui 			    IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, instance));
    439  1.12        pk 		} else
    440  1.67   tsutsui 			be_mii_writereg(self,
    441  1.73   tsutsui 			    BE_PHY_INTERNAL, MII_BMCR, BMCR_ISO);
    442   1.1        pk 	}
    443   1.1        pk 
    444  1.67   tsutsui 	memcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
    445   1.1        pk 	ifp->if_softc = sc;
    446   1.1        pk 	ifp->if_start = bestart;
    447   1.1        pk 	ifp->if_ioctl = beioctl;
    448   1.1        pk 	ifp->if_watchdog = bewatchdog;
    449  1.68   tsutsui 	ifp->if_init = beinit;
    450  1.68   tsutsui 	ifp->if_stop = bestop;
    451   1.1        pk 	ifp->if_flags =
    452  1.73   tsutsui 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    453  1.23   thorpej 	IFQ_SET_READY(&ifp->if_snd);
    454   1.1        pk 
    455  1.40        pk 	/* claim 802.1q capability */
    456  1.40        pk 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    457  1.40        pk 
    458   1.1        pk 	/* Attach the interface. */
    459   1.1        pk 	if_attach(ifp);
    460   1.1        pk 	ether_ifattach(ifp, sc->sc_enaddr);
    461   1.1        pk }
    462   1.1        pk 
    463   1.1        pk 
    464   1.1        pk /*
    465   1.1        pk  * Routine to copy from mbuf chain to transmit buffer in
    466   1.1        pk  * network buffer memory.
    467   1.1        pk  */
    468  1.48     perry static inline int
    469  1.62       dsl be_put(struct be_softc *sc, int idx, struct mbuf *m)
    470   1.1        pk {
    471   1.1        pk 	struct mbuf *n;
    472   1.1        pk 	int len, tlen = 0, boff = 0;
    473  1.75   tsutsui 	uint8_t *bp;
    474   1.2        pk 
    475  1.71   tsutsui 	bp = sc->sc_rb.rb_txbuf + (idx % sc->sc_rb.rb_ntbuf) * BE_PKT_BUF_SZ;
    476   1.1        pk 
    477   1.1        pk 	for (; m; m = n) {
    478   1.1        pk 		len = m->m_len;
    479   1.1        pk 		if (len == 0) {
    480   1.1        pk 			MFREE(m, n);
    481   1.1        pk 			continue;
    482   1.1        pk 		}
    483  1.75   tsutsui 		memcpy(bp + boff, mtod(m, void *), len);
    484   1.1        pk 		boff += len;
    485   1.1        pk 		tlen += len;
    486   1.1        pk 		MFREE(m, n);
    487   1.1        pk 	}
    488  1.73   tsutsui 	return tlen;
    489   1.1        pk }
    490   1.1        pk 
    491   1.1        pk /*
    492   1.1        pk  * Pull data off an interface.
    493   1.1        pk  * Len is the length of data, with local net header stripped.
    494   1.1        pk  * We copy the data into mbufs.  When full cluster sized units are present,
    495   1.1        pk  * we copy into clusters.
    496   1.1        pk  */
    497  1.48     perry static inline struct mbuf *
    498  1.63       dsl be_get(struct be_softc *sc, int idx, int totlen)
    499   1.1        pk {
    500   1.1        pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    501   1.1        pk 	struct mbuf *m;
    502   1.1        pk 	struct mbuf *top, **mp;
    503   1.1        pk 	int len, pad, boff = 0;
    504  1.75   tsutsui 	uint8_t *bp;
    505   1.2        pk 
    506  1.71   tsutsui 	bp = sc->sc_rb.rb_rxbuf + (idx % sc->sc_rb.rb_nrbuf) * BE_PKT_BUF_SZ;
    507   1.1        pk 
    508   1.1        pk 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    509   1.1        pk 	if (m == NULL)
    510   1.1        pk 		return (NULL);
    511   1.1        pk 	m->m_pkthdr.rcvif = ifp;
    512   1.1        pk 	m->m_pkthdr.len = totlen;
    513   1.1        pk 
    514   1.1        pk 	pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
    515   1.1        pk 	m->m_data += pad;
    516   1.1        pk 	len = MHLEN - pad;
    517   1.1        pk 	top = NULL;
    518   1.1        pk 	mp = &top;
    519   1.1        pk 
    520   1.1        pk 	while (totlen > 0) {
    521   1.1        pk 		if (top) {
    522   1.1        pk 			MGET(m, M_DONTWAIT, MT_DATA);
    523   1.1        pk 			if (m == NULL) {
    524   1.1        pk 				m_freem(top);
    525   1.1        pk 				return (NULL);
    526   1.1        pk 			}
    527   1.1        pk 			len = MLEN;
    528   1.1        pk 		}
    529   1.1        pk 		if (top && totlen >= MINCLSIZE) {
    530   1.1        pk 			MCLGET(m, M_DONTWAIT);
    531   1.1        pk 			if (m->m_flags & M_EXT)
    532   1.1        pk 				len = MCLBYTES;
    533   1.1        pk 		}
    534   1.1        pk 		m->m_len = len = min(totlen, len);
    535  1.75   tsutsui 		memcpy(mtod(m, void *), bp + boff, len);
    536   1.1        pk 		boff += len;
    537   1.1        pk 		totlen -= len;
    538   1.1        pk 		*mp = m;
    539   1.1        pk 		mp = &m->m_next;
    540   1.1        pk 	}
    541   1.1        pk 
    542  1.73   tsutsui 	return top;
    543   1.1        pk }
    544   1.1        pk 
    545   1.1        pk /*
    546   1.1        pk  * Pass a packet to the higher levels.
    547   1.1        pk  */
    548  1.48     perry static inline void
    549  1.63       dsl be_read(struct be_softc *sc, int idx, int len)
    550   1.1        pk {
    551   1.1        pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    552   1.1        pk 	struct mbuf *m;
    553   1.1        pk 
    554  1.43        pk 	if (len <= sizeof(struct ether_header) ||
    555  1.46    bouyer 	    len > ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN) {
    556  1.43        pk #ifdef BEDEBUG
    557  1.43        pk 		if (sc->sc_debug)
    558  1.43        pk 			printf("%s: invalid packet size %d; dropping\n",
    559  1.73   tsutsui 			    ifp->if_xname, len);
    560  1.43        pk #endif
    561   1.1        pk 		ifp->if_ierrors++;
    562   1.1        pk 		return;
    563   1.1        pk 	}
    564   1.1        pk 
    565   1.1        pk 	/*
    566   1.1        pk 	 * Pull packet off interface.
    567   1.1        pk 	 */
    568   1.1        pk 	m = be_get(sc, idx, len);
    569   1.1        pk 	if (m == NULL) {
    570   1.1        pk 		ifp->if_ierrors++;
    571   1.1        pk 		return;
    572   1.1        pk 	}
    573   1.1        pk 	ifp->if_ipackets++;
    574   1.1        pk 
    575   1.1        pk 	/*
    576   1.1        pk 	 * Check if there's a BPF listener on this interface.
    577   1.1        pk 	 * If so, hand off the raw packet to BPF.
    578   1.1        pk 	 */
    579  1.77     joerg 	bpf_mtap(ifp, m);
    580   1.6   thorpej 	/* Pass the packet up. */
    581   1.6   thorpej 	(*ifp->if_input)(ifp, m);
    582   1.1        pk }
    583   1.1        pk 
    584   1.1        pk /*
    585   1.1        pk  * Start output on interface.
    586   1.1        pk  * We make two assumptions here:
    587   1.1        pk  *  1) that the current priority is set to splnet _before_ this code
    588   1.1        pk  *     is called *and* is returned to the appropriate priority after
    589   1.1        pk  *     return
    590   1.1        pk  *  2) that the IFF_OACTIVE flag is checked before this code is called
    591   1.1        pk  *     (i.e. that the output part of the interface is idle)
    592   1.1        pk  */
    593   1.1        pk void
    594  1.62       dsl bestart(struct ifnet *ifp)
    595   1.1        pk {
    596  1.67   tsutsui 	struct be_softc *sc = ifp->if_softc;
    597   1.2        pk 	struct qec_xd *txd = sc->sc_rb.rb_txd;
    598   1.1        pk 	struct mbuf *m;
    599   1.1        pk 	unsigned int bix, len;
    600   1.2        pk 	unsigned int ntbuf = sc->sc_rb.rb_ntbuf;
    601   1.1        pk 
    602   1.1        pk 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    603   1.1        pk 		return;
    604   1.1        pk 
    605   1.2        pk 	bix = sc->sc_rb.rb_tdhead;
    606   1.1        pk 
    607   1.1        pk 	for (;;) {
    608  1.23   thorpej 		IFQ_DEQUEUE(&ifp->if_snd, m);
    609   1.1        pk 		if (m == 0)
    610   1.1        pk 			break;
    611   1.1        pk 
    612   1.1        pk 		/*
    613   1.1        pk 		 * If BPF is listening on this interface, let it see the
    614   1.1        pk 		 * packet before we commit it to the wire.
    615   1.1        pk 		 */
    616  1.77     joerg 		bpf_mtap(ifp, m);
    617   1.1        pk 
    618   1.1        pk 		/*
    619   1.1        pk 		 * Copy the mbuf chain into the transmit buffer.
    620   1.1        pk 		 */
    621   1.1        pk 		len = be_put(sc, bix, m);
    622   1.1        pk 
    623   1.1        pk 		/*
    624   1.1        pk 		 * Initialize transmit registers and start transmission
    625   1.1        pk 		 */
    626   1.1        pk 		txd[bix].xd_flags = QEC_XD_OWN | QEC_XD_SOP | QEC_XD_EOP |
    627  1.73   tsutsui 		    (len & QEC_XD_LENGTH);
    628  1.73   tsutsui 		bus_space_write_4(sc->sc_bustag, sc->sc_cr,
    629  1.73   tsutsui 		    BE_CRI_CTRL, BE_CR_CTRL_TWAKEUP);
    630   1.1        pk 
    631   1.1        pk 		if (++bix == QEC_XD_RING_MAXSIZE)
    632   1.1        pk 			bix = 0;
    633   1.1        pk 
    634   1.2        pk 		if (++sc->sc_rb.rb_td_nbusy == ntbuf) {
    635   1.1        pk 			ifp->if_flags |= IFF_OACTIVE;
    636   1.1        pk 			break;
    637   1.1        pk 		}
    638   1.1        pk 	}
    639   1.1        pk 
    640   1.2        pk 	sc->sc_rb.rb_tdhead = bix;
    641   1.1        pk }
    642   1.1        pk 
    643   1.1        pk void
    644  1.68   tsutsui bestop(struct ifnet *ifp, int disable)
    645   1.1        pk {
    646  1.68   tsutsui 	struct be_softc *sc = ifp->if_softc;
    647   1.1        pk 
    648  1.17   thorpej 	callout_stop(&sc->sc_tick_ch);
    649   1.8   thorpej 
    650  1.12        pk 	/* Down the MII. */
    651  1.12        pk 	mii_down(&sc->sc_mii);
    652  1.12        pk 	(void)be_intphy_service(sc, &sc->sc_mii, MII_DOWN);
    653   1.1        pk 
    654  1.68   tsutsui 	behwreset(sc);
    655  1.68   tsutsui }
    656  1.68   tsutsui 
    657  1.68   tsutsui void
    658  1.68   tsutsui behwreset(struct be_softc *sc)
    659  1.68   tsutsui {
    660  1.68   tsutsui 	int n;
    661  1.68   tsutsui 	bus_space_tag_t t = sc->sc_bustag;
    662  1.68   tsutsui 	bus_space_handle_t br = sc->sc_br;
    663  1.68   tsutsui 
    664   1.1        pk 	/* Stop the transmitter */
    665   1.1        pk 	bus_space_write_4(t, br, BE_BRI_TXCFG, 0);
    666   1.1        pk 	for (n = 32; n > 0; n--) {
    667   1.1        pk 		if (bus_space_read_4(t, br, BE_BRI_TXCFG) == 0)
    668   1.1        pk 			break;
    669   1.1        pk 		DELAY(20);
    670   1.1        pk 	}
    671   1.1        pk 
    672   1.1        pk 	/* Stop the receiver */
    673   1.1        pk 	bus_space_write_4(t, br, BE_BRI_RXCFG, 0);
    674   1.1        pk 	for (n = 32; n > 0; n--) {
    675   1.1        pk 		if (bus_space_read_4(t, br, BE_BRI_RXCFG) == 0)
    676   1.1        pk 			break;
    677   1.1        pk 		DELAY(20);
    678   1.1        pk 	}
    679   1.1        pk }
    680   1.1        pk 
    681   1.1        pk /*
    682   1.1        pk  * Reset interface.
    683   1.1        pk  */
    684   1.1        pk void
    685  1.62       dsl bereset(struct be_softc *sc)
    686   1.1        pk {
    687  1.68   tsutsui 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    688   1.1        pk 	int s;
    689   1.1        pk 
    690   1.1        pk 	s = splnet();
    691  1.68   tsutsui 	behwreset(sc);
    692  1.13        pk 	if ((sc->sc_ethercom.ec_if.if_flags & IFF_UP) != 0)
    693  1.68   tsutsui 		beinit(ifp);
    694   1.1        pk 	splx(s);
    695   1.1        pk }
    696   1.1        pk 
    697   1.1        pk void
    698  1.62       dsl bewatchdog(struct ifnet *ifp)
    699   1.1        pk {
    700   1.1        pk 	struct be_softc *sc = ifp->if_softc;
    701   1.1        pk 
    702  1.69   tsutsui 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
    703   1.1        pk 	++sc->sc_ethercom.ec_if.if_oerrors;
    704   1.1        pk 
    705   1.1        pk 	bereset(sc);
    706   1.1        pk }
    707   1.1        pk 
    708   1.1        pk int
    709  1.67   tsutsui beintr(void *arg)
    710   1.1        pk {
    711  1.67   tsutsui 	struct be_softc *sc = arg;
    712   1.1        pk 	bus_space_tag_t t = sc->sc_bustag;
    713  1.73   tsutsui 	uint32_t whyq, whyb, whyc;
    714   1.1        pk 	int r = 0;
    715   1.1        pk 
    716   1.1        pk 	/* Read QEC status, channel status and BE status */
    717   1.1        pk 	whyq = bus_space_read_4(t, sc->sc_qr, QEC_QRI_STAT);
    718   1.1        pk 	whyc = bus_space_read_4(t, sc->sc_cr, BE_CRI_STAT);
    719   1.1        pk 	whyb = bus_space_read_4(t, sc->sc_br, BE_BRI_STAT);
    720   1.1        pk 
    721   1.1        pk 	if (whyq & QEC_STAT_BM)
    722   1.1        pk 		r |= beeint(sc, whyb);
    723   1.1        pk 
    724   1.1        pk 	if (whyq & QEC_STAT_ER)
    725   1.1        pk 		r |= beqint(sc, whyc);
    726   1.1        pk 
    727   1.1        pk 	if (whyq & QEC_STAT_TX && whyc & BE_CR_STAT_TXIRQ)
    728   1.1        pk 		r |= betint(sc);
    729   1.1        pk 
    730   1.1        pk 	if (whyq & QEC_STAT_RX && whyc & BE_CR_STAT_RXIRQ)
    731   1.1        pk 		r |= berint(sc);
    732   1.1        pk 
    733  1.73   tsutsui 	return r;
    734   1.1        pk }
    735   1.1        pk 
    736   1.1        pk /*
    737   1.1        pk  * QEC Interrupt.
    738   1.1        pk  */
    739   1.1        pk int
    740  1.73   tsutsui beqint(struct be_softc *sc, uint32_t why)
    741   1.1        pk {
    742  1.69   tsutsui 	device_t self = sc->sc_dev;
    743   1.1        pk 	int r = 0, rst = 0;
    744   1.1        pk 
    745   1.1        pk 	if (why & BE_CR_STAT_TXIRQ)
    746   1.1        pk 		r |= 1;
    747   1.1        pk 	if (why & BE_CR_STAT_RXIRQ)
    748   1.1        pk 		r |= 1;
    749   1.1        pk 
    750   1.1        pk 	if (why & BE_CR_STAT_BERROR) {
    751   1.1        pk 		r |= 1;
    752   1.1        pk 		rst = 1;
    753  1.67   tsutsui 		aprint_error_dev(self, "bigmac error\n");
    754   1.1        pk 	}
    755   1.1        pk 
    756   1.1        pk 	if (why & BE_CR_STAT_TXDERR) {
    757   1.1        pk 		r |= 1;
    758   1.1        pk 		rst = 1;
    759  1.67   tsutsui 		aprint_error_dev(self, "bogus tx descriptor\n");
    760   1.1        pk 	}
    761   1.1        pk 
    762   1.1        pk 	if (why & (BE_CR_STAT_TXLERR | BE_CR_STAT_TXPERR | BE_CR_STAT_TXSERR)) {
    763   1.1        pk 		r |= 1;
    764   1.1        pk 		rst = 1;
    765  1.67   tsutsui 		aprint_error_dev(self, "tx DMA error ( ");
    766   1.1        pk 		if (why & BE_CR_STAT_TXLERR)
    767   1.1        pk 			printf("Late ");
    768   1.1        pk 		if (why & BE_CR_STAT_TXPERR)
    769   1.1        pk 			printf("Parity ");
    770   1.1        pk 		if (why & BE_CR_STAT_TXSERR)
    771   1.1        pk 			printf("Generic ");
    772   1.1        pk 		printf(")\n");
    773   1.1        pk 	}
    774   1.1        pk 
    775   1.1        pk 	if (why & BE_CR_STAT_RXDROP) {
    776   1.1        pk 		r |= 1;
    777   1.1        pk 		rst = 1;
    778  1.67   tsutsui 		aprint_error_dev(self, "out of rx descriptors\n");
    779   1.1        pk 	}
    780   1.1        pk 
    781   1.1        pk 	if (why & BE_CR_STAT_RXSMALL) {
    782   1.1        pk 		r |= 1;
    783   1.1        pk 		rst = 1;
    784  1.67   tsutsui 		aprint_error_dev(self, "rx descriptor too small\n");
    785   1.1        pk 	}
    786   1.1        pk 
    787   1.1        pk 	if (why & (BE_CR_STAT_RXLERR | BE_CR_STAT_RXPERR | BE_CR_STAT_RXSERR)) {
    788   1.1        pk 		r |= 1;
    789   1.1        pk 		rst = 1;
    790  1.67   tsutsui 		aprint_error_dev(self, "rx DMA error ( ");
    791   1.1        pk 		if (why & BE_CR_STAT_RXLERR)
    792   1.1        pk 			printf("Late ");
    793   1.1        pk 		if (why & BE_CR_STAT_RXPERR)
    794   1.1        pk 			printf("Parity ");
    795   1.1        pk 		if (why & BE_CR_STAT_RXSERR)
    796   1.1        pk 			printf("Generic ");
    797   1.1        pk 		printf(")\n");
    798   1.1        pk 	}
    799   1.1        pk 
    800   1.1        pk 	if (!r) {
    801   1.1        pk 		rst = 1;
    802  1.67   tsutsui 		aprint_error_dev(self, "unexpected error interrupt %08x\n",
    803  1.73   tsutsui 		    why);
    804   1.1        pk 	}
    805   1.1        pk 
    806   1.1        pk 	if (rst) {
    807  1.67   tsutsui 		printf("%s: resetting\n", device_xname(self));
    808   1.1        pk 		bereset(sc);
    809   1.1        pk 	}
    810   1.1        pk 
    811  1.73   tsutsui 	return r;
    812   1.1        pk }
    813   1.1        pk 
    814   1.1        pk /*
    815   1.1        pk  * Error interrupt.
    816   1.1        pk  */
    817   1.1        pk int
    818  1.73   tsutsui beeint(struct be_softc *sc, uint32_t why)
    819   1.1        pk {
    820  1.69   tsutsui 	device_t self = sc->sc_dev;
    821   1.1        pk 	int r = 0, rst = 0;
    822   1.1        pk 
    823   1.1        pk 	if (why & BE_BR_STAT_RFIFOVF) {
    824   1.1        pk 		r |= 1;
    825   1.1        pk 		rst = 1;
    826  1.67   tsutsui 		aprint_error_dev(self, "receive fifo overrun\n");
    827   1.1        pk 	}
    828   1.1        pk 	if (why & BE_BR_STAT_TFIFO_UND) {
    829   1.1        pk 		r |= 1;
    830   1.1        pk 		rst = 1;
    831  1.67   tsutsui 		aprint_error_dev(self, "transmit fifo underrun\n");
    832   1.1        pk 	}
    833   1.1        pk 	if (why & BE_BR_STAT_MAXPKTERR) {
    834   1.1        pk 		r |= 1;
    835   1.1        pk 		rst = 1;
    836  1.67   tsutsui 		aprint_error_dev(self, "max packet size error\n");
    837   1.1        pk 	}
    838   1.1        pk 
    839   1.1        pk 	if (!r) {
    840   1.1        pk 		rst = 1;
    841  1.67   tsutsui 		aprint_error_dev(self, "unexpected error interrupt %08x\n",
    842  1.73   tsutsui 		    why);
    843   1.1        pk 	}
    844   1.1        pk 
    845   1.1        pk 	if (rst) {
    846  1.67   tsutsui 		printf("%s: resetting\n", device_xname(self));
    847   1.1        pk 		bereset(sc);
    848   1.1        pk 	}
    849   1.1        pk 
    850  1.73   tsutsui 	return r;
    851   1.1        pk }
    852   1.1        pk 
    853   1.1        pk /*
    854   1.1        pk  * Transmit interrupt.
    855   1.1        pk  */
    856   1.1        pk int
    857  1.62       dsl betint(struct be_softc *sc)
    858   1.1        pk {
    859   1.1        pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    860   1.1        pk 	bus_space_tag_t t = sc->sc_bustag;
    861   1.1        pk 	bus_space_handle_t br = sc->sc_br;
    862   1.1        pk 	unsigned int bix, txflags;
    863   1.1        pk 
    864   1.1        pk 	/*
    865   1.1        pk 	 * Unload collision counters
    866   1.1        pk 	 */
    867   1.1        pk 	ifp->if_collisions +=
    868  1.73   tsutsui 	    bus_space_read_4(t, br, BE_BRI_NCCNT) +
    869  1.73   tsutsui 	    bus_space_read_4(t, br, BE_BRI_FCCNT) +
    870  1.73   tsutsui 	    bus_space_read_4(t, br, BE_BRI_EXCNT) +
    871  1.73   tsutsui 	    bus_space_read_4(t, br, BE_BRI_LTCNT);
    872   1.1        pk 
    873   1.1        pk 	/*
    874   1.1        pk 	 * the clear the hardware counters
    875   1.1        pk 	 */
    876   1.1        pk 	bus_space_write_4(t, br, BE_BRI_NCCNT, 0);
    877   1.1        pk 	bus_space_write_4(t, br, BE_BRI_FCCNT, 0);
    878   1.1        pk 	bus_space_write_4(t, br, BE_BRI_EXCNT, 0);
    879   1.1        pk 	bus_space_write_4(t, br, BE_BRI_LTCNT, 0);
    880   1.1        pk 
    881   1.2        pk 	bix = sc->sc_rb.rb_tdtail;
    882   1.1        pk 
    883   1.1        pk 	for (;;) {
    884   1.2        pk 		if (sc->sc_rb.rb_td_nbusy <= 0)
    885   1.1        pk 			break;
    886   1.1        pk 
    887   1.2        pk 		txflags = sc->sc_rb.rb_txd[bix].xd_flags;
    888   1.1        pk 
    889   1.1        pk 		if (txflags & QEC_XD_OWN)
    890   1.1        pk 			break;
    891   1.1        pk 
    892   1.1        pk 		ifp->if_flags &= ~IFF_OACTIVE;
    893   1.1        pk 		ifp->if_opackets++;
    894   1.1        pk 
    895   1.1        pk 		if (++bix == QEC_XD_RING_MAXSIZE)
    896   1.1        pk 			bix = 0;
    897   1.1        pk 
    898   1.2        pk 		--sc->sc_rb.rb_td_nbusy;
    899   1.1        pk 	}
    900   1.1        pk 
    901   1.2        pk 	sc->sc_rb.rb_tdtail = bix;
    902   1.1        pk 
    903   1.1        pk 	bestart(ifp);
    904   1.1        pk 
    905   1.2        pk 	if (sc->sc_rb.rb_td_nbusy == 0)
    906   1.1        pk 		ifp->if_timer = 0;
    907   1.1        pk 
    908  1.73   tsutsui 	return 1;
    909   1.1        pk }
    910   1.1        pk 
    911   1.1        pk /*
    912   1.1        pk  * Receive interrupt.
    913   1.1        pk  */
    914   1.1        pk int
    915  1.62       dsl berint(struct be_softc *sc)
    916   1.1        pk {
    917   1.2        pk 	struct qec_xd *xd = sc->sc_rb.rb_rxd;
    918   1.1        pk 	unsigned int bix, len;
    919   1.2        pk 	unsigned int nrbuf = sc->sc_rb.rb_nrbuf;
    920   1.1        pk 
    921   1.2        pk 	bix = sc->sc_rb.rb_rdtail;
    922   1.1        pk 
    923   1.1        pk 	/*
    924   1.1        pk 	 * Process all buffers with valid data.
    925   1.1        pk 	 */
    926   1.1        pk 	for (;;) {
    927   1.1        pk 		len = xd[bix].xd_flags;
    928   1.1        pk 		if (len & QEC_XD_OWN)
    929   1.1        pk 			break;
    930   1.1        pk 
    931   1.1        pk 		len &= QEC_XD_LENGTH;
    932   1.1        pk 		be_read(sc, bix, len);
    933   1.1        pk 
    934   1.1        pk 		/* ... */
    935   1.1        pk 		xd[(bix+nrbuf) % QEC_XD_RING_MAXSIZE].xd_flags =
    936  1.73   tsutsui 		    QEC_XD_OWN | (BE_PKT_BUF_SZ & QEC_XD_LENGTH);
    937   1.1        pk 
    938   1.1        pk 		if (++bix == QEC_XD_RING_MAXSIZE)
    939   1.1        pk 			bix = 0;
    940   1.1        pk 	}
    941   1.1        pk 
    942   1.2        pk 	sc->sc_rb.rb_rdtail = bix;
    943   1.1        pk 
    944  1.73   tsutsui 	return 1;
    945   1.1        pk }
    946   1.1        pk 
    947   1.1        pk int
    948  1.60    dyoung beioctl(struct ifnet *ifp, u_long cmd, void *data)
    949   1.1        pk {
    950   1.1        pk 	struct be_softc *sc = ifp->if_softc;
    951  1.67   tsutsui 	struct ifaddr *ifa = data;
    952  1.67   tsutsui 	struct ifreq *ifr = data;
    953   1.1        pk 	int s, error = 0;
    954   1.1        pk 
    955   1.1        pk 	s = splnet();
    956   1.1        pk 
    957   1.1        pk 	switch (cmd) {
    958  1.60    dyoung 	case SIOCINITIFADDR:
    959   1.1        pk 		ifp->if_flags |= IFF_UP;
    960  1.68   tsutsui 		beinit(ifp);
    961   1.1        pk 		switch (ifa->ifa_addr->sa_family) {
    962   1.1        pk #ifdef INET
    963   1.1        pk 		case AF_INET:
    964   1.1        pk 			arp_ifinit(ifp, ifa);
    965   1.1        pk 			break;
    966   1.1        pk #endif /* INET */
    967   1.1        pk 		default:
    968   1.1        pk 			break;
    969   1.1        pk 		}
    970   1.1        pk 		break;
    971   1.1        pk 
    972   1.1        pk 	case SIOCSIFFLAGS:
    973  1.60    dyoung 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    974  1.60    dyoung 			break;
    975  1.60    dyoung 		/* XXX re-use ether_ioctl() */
    976  1.60    dyoung 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
    977  1.60    dyoung 		case IFF_RUNNING:
    978   1.1        pk 			/*
    979   1.1        pk 			 * If interface is marked down and it is running, then
    980   1.1        pk 			 * stop it.
    981   1.1        pk 			 */
    982  1.68   tsutsui 			bestop(ifp, 0);
    983   1.1        pk 			ifp->if_flags &= ~IFF_RUNNING;
    984  1.60    dyoung 			break;
    985  1.60    dyoung 		case IFF_UP:
    986   1.1        pk 			/*
    987   1.1        pk 			 * If interface is marked up and it is stopped, then
    988   1.1        pk 			 * start it.
    989   1.1        pk 			 */
    990  1.68   tsutsui 			beinit(ifp);
    991  1.60    dyoung 			break;
    992  1.60    dyoung 		default:
    993   1.1        pk 			/*
    994   1.1        pk 			 * Reset the interface to pick up changes in any other
    995   1.1        pk 			 * flags that affect hardware registers.
    996   1.1        pk 			 */
    997  1.68   tsutsui 			bestop(ifp, 0);
    998  1.68   tsutsui 			beinit(ifp);
    999  1.60    dyoung 			break;
   1000   1.1        pk 		}
   1001   1.1        pk #ifdef BEDEBUG
   1002   1.1        pk 		if (ifp->if_flags & IFF_DEBUG)
   1003   1.2        pk 			sc->sc_debug = 1;
   1004   1.1        pk 		else
   1005   1.1        pk 			sc->sc_debug = 0;
   1006   1.1        pk #endif
   1007   1.1        pk 		break;
   1008   1.1        pk 
   1009  1.68   tsutsui 	case SIOCGIFMEDIA:
   1010  1.68   tsutsui 	case SIOCSIFMEDIA:
   1011  1.68   tsutsui 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
   1012  1.68   tsutsui 		break;
   1013  1.68   tsutsui 	default:
   1014  1.54    dyoung 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
   1015   1.1        pk 			/*
   1016   1.1        pk 			 * Multicast list has changed; set the hardware filter
   1017   1.1        pk 			 * accordingly.
   1018   1.1        pk 			 */
   1019  1.44   thorpej 			if (ifp->if_flags & IFF_RUNNING)
   1020  1.68   tsutsui 				error = beinit(ifp);
   1021  1.68   tsutsui 			else
   1022  1.68   tsutsui 				error = 0;
   1023   1.1        pk 		}
   1024   1.1        pk 		break;
   1025   1.1        pk 	}
   1026   1.1        pk 	splx(s);
   1027  1.73   tsutsui 	return error;
   1028   1.1        pk }
   1029   1.1        pk 
   1030   1.1        pk 
   1031  1.68   tsutsui int
   1032  1.68   tsutsui beinit(struct ifnet *ifp)
   1033   1.1        pk {
   1034  1.68   tsutsui 	struct be_softc *sc = ifp->if_softc;
   1035   1.1        pk 	bus_space_tag_t t = sc->sc_bustag;
   1036   1.1        pk 	bus_space_handle_t br = sc->sc_br;
   1037   1.1        pk 	bus_space_handle_t cr = sc->sc_cr;
   1038   1.1        pk 	struct qec_softc *qec = sc->sc_qec;
   1039  1.73   tsutsui 	uint32_t v;
   1040  1.73   tsutsui 	uint32_t qecaddr;
   1041  1.73   tsutsui 	uint8_t *ea;
   1042  1.56    dyoung 	int rc, s;
   1043   1.1        pk 
   1044  1.24   thorpej 	s = splnet();
   1045   1.1        pk 
   1046   1.2        pk 	qec_meminit(&sc->sc_rb, BE_PKT_BUF_SZ);
   1047   1.1        pk 
   1048  1.68   tsutsui 	bestop(ifp, 1);
   1049   1.1        pk 
   1050   1.1        pk 	ea = sc->sc_enaddr;
   1051   1.1        pk 	bus_space_write_4(t, br, BE_BRI_MACADDR0, (ea[0] << 8) | ea[1]);
   1052   1.1        pk 	bus_space_write_4(t, br, BE_BRI_MACADDR1, (ea[2] << 8) | ea[3]);
   1053   1.1        pk 	bus_space_write_4(t, br, BE_BRI_MACADDR2, (ea[4] << 8) | ea[5]);
   1054   1.1        pk 
   1055  1.16        pk 	/* Clear hash table */
   1056   1.1        pk 	bus_space_write_4(t, br, BE_BRI_HASHTAB0, 0);
   1057   1.1        pk 	bus_space_write_4(t, br, BE_BRI_HASHTAB1, 0);
   1058   1.1        pk 	bus_space_write_4(t, br, BE_BRI_HASHTAB2, 0);
   1059   1.1        pk 	bus_space_write_4(t, br, BE_BRI_HASHTAB3, 0);
   1060   1.1        pk 
   1061  1.16        pk 	/* Re-initialize RX configuration */
   1062  1.16        pk 	v = BE_BR_RXCFG_FIFO;
   1063  1.16        pk 	bus_space_write_4(t, br, BE_BRI_RXCFG, v);
   1064  1.16        pk 
   1065   1.5        pk 	be_mcreset(sc);
   1066   1.1        pk 
   1067   1.1        pk 	bus_space_write_4(t, br, BE_BRI_RANDSEED, 0xbd);
   1068   1.1        pk 
   1069  1.73   tsutsui 	bus_space_write_4(t, br,
   1070  1.73   tsutsui 	    BE_BRI_XIFCFG, BE_BR_XCFG_ODENABLE | BE_BR_XCFG_RESV);
   1071   1.1        pk 
   1072   1.1        pk 	bus_space_write_4(t, br, BE_BRI_JSIZE, 4);
   1073   1.1        pk 
   1074   1.1        pk 	/*
   1075   1.1        pk 	 * Turn off counter expiration interrupts as well as
   1076   1.1        pk 	 * 'gotframe' and 'sentframe'
   1077   1.1        pk 	 */
   1078   1.1        pk 	bus_space_write_4(t, br, BE_BRI_IMASK,
   1079  1.73   tsutsui 	    BE_BR_IMASK_GOTFRAME |
   1080  1.73   tsutsui 	    BE_BR_IMASK_RCNTEXP |
   1081  1.73   tsutsui 	    BE_BR_IMASK_ACNTEXP |
   1082  1.73   tsutsui 	    BE_BR_IMASK_CCNTEXP |
   1083  1.73   tsutsui 	    BE_BR_IMASK_LCNTEXP |
   1084  1.73   tsutsui 	    BE_BR_IMASK_CVCNTEXP |
   1085  1.73   tsutsui 	    BE_BR_IMASK_SENTFRAME |
   1086  1.73   tsutsui 	    BE_BR_IMASK_NCNTEXP |
   1087  1.73   tsutsui 	    BE_BR_IMASK_ECNTEXP |
   1088  1.73   tsutsui 	    BE_BR_IMASK_LCCNTEXP |
   1089  1.73   tsutsui 	    BE_BR_IMASK_FCNTEXP |
   1090  1.73   tsutsui 	    BE_BR_IMASK_DTIMEXP);
   1091   1.1        pk 
   1092   1.1        pk 	/* Channel registers: */
   1093  1.73   tsutsui 	bus_space_write_4(t, cr, BE_CRI_RXDS, (uint32_t)sc->sc_rb.rb_rxddma);
   1094  1.73   tsutsui 	bus_space_write_4(t, cr, BE_CRI_TXDS, (uint32_t)sc->sc_rb.rb_txddma);
   1095   1.1        pk 
   1096   1.1        pk 	qecaddr = sc->sc_channel * qec->sc_msize;
   1097   1.1        pk 	bus_space_write_4(t, cr, BE_CRI_RXWBUF, qecaddr);
   1098   1.1        pk 	bus_space_write_4(t, cr, BE_CRI_RXRBUF, qecaddr);
   1099   1.1        pk 	bus_space_write_4(t, cr, BE_CRI_TXWBUF, qecaddr + qec->sc_rsize);
   1100   1.1        pk 	bus_space_write_4(t, cr, BE_CRI_TXRBUF, qecaddr + qec->sc_rsize);
   1101   1.1        pk 
   1102   1.1        pk 	bus_space_write_4(t, cr, BE_CRI_RIMASK, 0);
   1103   1.1        pk 	bus_space_write_4(t, cr, BE_CRI_TIMASK, 0);
   1104   1.1        pk 	bus_space_write_4(t, cr, BE_CRI_QMASK, 0);
   1105   1.1        pk 	bus_space_write_4(t, cr, BE_CRI_BMASK, 0);
   1106   1.1        pk 	bus_space_write_4(t, cr, BE_CRI_CCNT, 0);
   1107  1.40        pk 
   1108  1.40        pk 	/* Set max packet length */
   1109  1.40        pk 	v = ETHER_MAX_LEN;
   1110  1.40        pk 	if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU)
   1111  1.40        pk 		v += ETHER_VLAN_ENCAP_LEN;
   1112  1.40        pk 	bus_space_write_4(t, br, BE_BRI_RXMAX, v);
   1113  1.40        pk 	bus_space_write_4(t, br, BE_BRI_TXMAX, v);
   1114   1.1        pk 
   1115   1.1        pk 	/* Enable transmitter */
   1116  1.73   tsutsui 	bus_space_write_4(t, br,
   1117  1.73   tsutsui 	    BE_BRI_TXCFG, BE_BR_TXCFG_FIFO | BE_BR_TXCFG_ENABLE);
   1118   1.1        pk 
   1119   1.1        pk 	/* Enable receiver */
   1120  1.16        pk 	v = bus_space_read_4(t, br, BE_BRI_RXCFG);
   1121  1.16        pk 	v |= BE_BR_RXCFG_FIFO | BE_BR_RXCFG_ENABLE;
   1122  1.16        pk 	bus_space_write_4(t, br, BE_BRI_RXCFG, v);
   1123   1.1        pk 
   1124  1.56    dyoung 	if ((rc = be_ifmedia_upd(ifp)) != 0)
   1125  1.56    dyoung 		goto out;
   1126  1.56    dyoung 
   1127   1.1        pk 	ifp->if_flags |= IFF_RUNNING;
   1128   1.1        pk 	ifp->if_flags &= ~IFF_OACTIVE;
   1129   1.1        pk 
   1130  1.17   thorpej 	callout_reset(&sc->sc_tick_ch, hz, be_tick, sc);
   1131  1.68   tsutsui 
   1132  1.68   tsutsui 	return 0;
   1133  1.56    dyoung out:
   1134   1.1        pk 	splx(s);
   1135  1.68   tsutsui 	return rc;
   1136   1.1        pk }
   1137   1.1        pk 
   1138   1.1        pk void
   1139  1.62       dsl be_mcreset(struct be_softc *sc)
   1140   1.1        pk {
   1141   1.2        pk 	struct ethercom *ec = &sc->sc_ethercom;
   1142   1.1        pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1143   1.1        pk 	bus_space_tag_t t = sc->sc_bustag;
   1144   1.1        pk 	bus_space_handle_t br = sc->sc_br;
   1145  1.74   tsutsui 	uint32_t v;
   1146  1.73   tsutsui 	uint32_t crc;
   1147  1.73   tsutsui 	uint16_t hash[4];
   1148   1.1        pk 	struct ether_multi *enm;
   1149   1.1        pk 	struct ether_multistep step;
   1150   1.1        pk 
   1151   1.5        pk 	if (ifp->if_flags & IFF_PROMISC) {
   1152   1.5        pk 		v = bus_space_read_4(t, br, BE_BRI_RXCFG);
   1153   1.5        pk 		v |= BE_BR_RXCFG_PMISC;
   1154   1.5        pk 		bus_space_write_4(t, br, BE_BRI_RXCFG, v);
   1155   1.5        pk 		return;
   1156   1.5        pk 	}
   1157   1.5        pk 
   1158   1.1        pk 	if (ifp->if_flags & IFF_ALLMULTI) {
   1159  1.16        pk 		hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
   1160  1.16        pk 		goto chipit;
   1161   1.1        pk 	}
   1162   1.1        pk 
   1163   1.1        pk 	hash[3] = hash[2] = hash[1] = hash[0] = 0;
   1164   1.1        pk 
   1165   1.2        pk 	ETHER_FIRST_MULTI(step, ec, enm);
   1166   1.1        pk 	while (enm != NULL) {
   1167  1.32       wiz 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1168   1.1        pk 			/*
   1169   1.1        pk 			 * We must listen to a range of multicast
   1170   1.1        pk 			 * addresses.  For now, just accept all
   1171   1.1        pk 			 * multicasts, rather than trying to set only
   1172   1.1        pk 			 * those filter bits needed to match the range.
   1173   1.1        pk 			 * (At this time, the only use of address
   1174   1.1        pk 			 * ranges is for IP multicast routing, for
   1175   1.1        pk 			 * which the range is big enough to require
   1176   1.1        pk 			 * all bits set.)
   1177   1.1        pk 			 */
   1178  1.16        pk 			hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
   1179   1.1        pk 			ifp->if_flags |= IFF_ALLMULTI;
   1180  1.16        pk 			goto chipit;
   1181   1.1        pk 		}
   1182   1.1        pk 
   1183  1.74   tsutsui 		crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
   1184  1.74   tsutsui 		/* Just want the 6 most significant bits. */
   1185  1.74   tsutsui 		crc >>= 26;
   1186   1.1        pk 
   1187   1.1        pk 		hash[crc >> 4] |= 1 << (crc & 0xf);
   1188   1.1        pk 		ETHER_NEXT_MULTI(step, enm);
   1189   1.1        pk 	}
   1190   1.1        pk 
   1191  1.16        pk 	ifp->if_flags &= ~IFF_ALLMULTI;
   1192  1.16        pk 
   1193  1.16        pk chipit:
   1194  1.16        pk 	/* Enable the hash filter */
   1195   1.1        pk 	bus_space_write_4(t, br, BE_BRI_HASHTAB0, hash[0]);
   1196   1.1        pk 	bus_space_write_4(t, br, BE_BRI_HASHTAB1, hash[1]);
   1197   1.1        pk 	bus_space_write_4(t, br, BE_BRI_HASHTAB2, hash[2]);
   1198   1.1        pk 	bus_space_write_4(t, br, BE_BRI_HASHTAB3, hash[3]);
   1199  1.16        pk 
   1200  1.16        pk 	v = bus_space_read_4(t, br, BE_BRI_RXCFG);
   1201  1.16        pk 	v &= ~BE_BR_RXCFG_PMISC;
   1202  1.16        pk 	v |= BE_BR_RXCFG_HENABLE;
   1203  1.16        pk 	bus_space_write_4(t, br, BE_BRI_RXCFG, v);
   1204   1.1        pk }
   1205   1.1        pk 
   1206   1.1        pk /*
   1207   1.1        pk  * Set the tcvr to an idle state
   1208   1.1        pk  */
   1209   1.1        pk void
   1210  1.62       dsl be_mii_sync(struct be_softc *sc)
   1211   1.1        pk {
   1212   1.1        pk 	bus_space_tag_t t = sc->sc_bustag;
   1213   1.1        pk 	bus_space_handle_t tr = sc->sc_tr;
   1214  1.10        pk 	int n = 32;
   1215   1.1        pk 
   1216   1.1        pk 	while (n--) {
   1217   1.1        pk 		bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
   1218  1.73   tsutsui 		    MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO | MGMT_PAL_OENAB);
   1219   1.1        pk 		(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
   1220   1.1        pk 		bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
   1221  1.73   tsutsui 		    MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO |
   1222  1.73   tsutsui 		    MGMT_PAL_OENAB | MGMT_PAL_DCLOCK);
   1223   1.1        pk 		(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
   1224   1.1        pk 	}
   1225   1.1        pk }
   1226   1.1        pk 
   1227  1.11        pk void
   1228  1.62       dsl be_pal_gate(struct be_softc *sc, int phy)
   1229  1.11        pk {
   1230  1.11        pk 	bus_space_tag_t t = sc->sc_bustag;
   1231  1.11        pk 	bus_space_handle_t tr = sc->sc_tr;
   1232  1.73   tsutsui 	uint32_t v;
   1233  1.11        pk 
   1234  1.11        pk 	be_mii_sync(sc);
   1235  1.11        pk 
   1236  1.11        pk 	v = ~(TCVR_PAL_EXTLBACK | TCVR_PAL_MSENSE | TCVR_PAL_LTENABLE);
   1237  1.11        pk 	if (phy == BE_PHY_INTERNAL)
   1238  1.11        pk 		v &= ~TCVR_PAL_SERIAL;
   1239  1.11        pk 
   1240  1.11        pk 	bus_space_write_4(t, tr, BE_TRI_TCVRPAL, v);
   1241  1.11        pk 	(void)bus_space_read_4(t, tr, BE_TRI_TCVRPAL);
   1242  1.11        pk }
   1243  1.11        pk 
   1244  1.10        pk static int
   1245  1.62       dsl be_tcvr_read_bit(struct be_softc *sc, int phy)
   1246   1.1        pk {
   1247   1.1        pk 	bus_space_tag_t t = sc->sc_bustag;
   1248   1.1        pk 	bus_space_handle_t tr = sc->sc_tr;
   1249   1.1        pk 	int ret;
   1250   1.1        pk 
   1251   1.1        pk 	if (phy == BE_PHY_INTERNAL) {
   1252   1.1        pk 		bus_space_write_4(t, tr, BE_TRI_MGMTPAL, MGMT_PAL_EXT_MDIO);
   1253   1.1        pk 		(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
   1254  1.73   tsutsui 		bus_space_write_4(t, tr,
   1255  1.73   tsutsui 		    BE_TRI_MGMTPAL, MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK);
   1256   1.1        pk 		(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
   1257   1.1        pk 		ret = (bus_space_read_4(t, tr, BE_TRI_MGMTPAL) &
   1258  1.73   tsutsui 		    MGMT_PAL_INT_MDIO) >> MGMT_PAL_INT_MDIO_SHIFT;
   1259   1.1        pk 	} else {
   1260   1.1        pk 		bus_space_write_4(t, tr, BE_TRI_MGMTPAL, MGMT_PAL_INT_MDIO);
   1261   1.1        pk 		(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
   1262   1.1        pk 		ret = (bus_space_read_4(t, tr, BE_TRI_MGMTPAL) &
   1263  1.73   tsutsui 		    MGMT_PAL_EXT_MDIO) >> MGMT_PAL_EXT_MDIO_SHIFT;
   1264  1.73   tsutsui 		bus_space_write_4(t, tr,
   1265  1.73   tsutsui 		    BE_TRI_MGMTPAL, MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK);
   1266   1.1        pk 		(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
   1267   1.1        pk 	}
   1268   1.1        pk 
   1269  1.73   tsutsui 	return ret;
   1270   1.1        pk }
   1271   1.1        pk 
   1272  1.10        pk static void
   1273  1.62       dsl be_tcvr_write_bit(struct be_softc *sc, int phy, int bit)
   1274   1.1        pk {
   1275   1.1        pk 	bus_space_tag_t t = sc->sc_bustag;
   1276   1.1        pk 	bus_space_handle_t tr = sc->sc_tr;
   1277  1.73   tsutsui 	uint32_t v;
   1278   1.1        pk 
   1279   1.1        pk 	if (phy == BE_PHY_INTERNAL) {
   1280  1.10        pk 		v = ((bit & 1) << MGMT_PAL_INT_MDIO_SHIFT) |
   1281  1.73   tsutsui 		    MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO;
   1282   1.1        pk 	} else {
   1283  1.73   tsutsui 		v = ((bit & 1) << MGMT_PAL_EXT_MDIO_SHIFT) |
   1284  1.73   tsutsui 		    MGMT_PAL_OENAB | MGMT_PAL_INT_MDIO;
   1285   1.1        pk 	}
   1286  1.12        pk 	bus_space_write_4(t, tr, BE_TRI_MGMTPAL, v);
   1287  1.12        pk 	(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
   1288  1.12        pk 	bus_space_write_4(t, tr, BE_TRI_MGMTPAL, v | MGMT_PAL_DCLOCK);
   1289  1.12        pk 	(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
   1290   1.1        pk }
   1291   1.1        pk 
   1292  1.10        pk static void
   1293  1.73   tsutsui be_mii_sendbits(struct be_softc *sc, int phy, uint32_t data, int nbits)
   1294   1.1        pk {
   1295   1.1        pk 	int i;
   1296   1.1        pk 
   1297   1.1        pk 	for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
   1298   1.1        pk 		be_tcvr_write_bit(sc, phy, (data & i) != 0);
   1299   1.1        pk 	}
   1300   1.1        pk }
   1301   1.1        pk 
   1302   1.4        pk static int
   1303  1.65    cegger be_mii_readreg(device_t self, int phy, int reg)
   1304   1.1        pk {
   1305  1.67   tsutsui 	struct be_softc *sc = device_private(self);
   1306   1.1        pk 	int val = 0, i;
   1307   1.1        pk 
   1308   1.1        pk 	/*
   1309   1.1        pk 	 * Read the PHY register by manually driving the MII control lines.
   1310   1.1        pk 	 */
   1311   1.1        pk 	be_mii_sync(sc);
   1312   1.1        pk 	be_mii_sendbits(sc, phy, MII_COMMAND_START, 2);
   1313   1.1        pk 	be_mii_sendbits(sc, phy, MII_COMMAND_READ, 2);
   1314   1.1        pk 	be_mii_sendbits(sc, phy, phy, 5);
   1315   1.1        pk 	be_mii_sendbits(sc, phy, reg, 5);
   1316   1.1        pk 
   1317  1.73   tsutsui 	(void)be_tcvr_read_bit(sc, phy);
   1318  1.73   tsutsui 	(void)be_tcvr_read_bit(sc, phy);
   1319   1.1        pk 
   1320   1.1        pk 	for (i = 15; i >= 0; i--)
   1321   1.1        pk 		val |= (be_tcvr_read_bit(sc, phy) << i);
   1322   1.1        pk 
   1323  1.73   tsutsui 	(void)be_tcvr_read_bit(sc, phy);
   1324  1.73   tsutsui 	(void)be_tcvr_read_bit(sc, phy);
   1325  1.73   tsutsui 	(void)be_tcvr_read_bit(sc, phy);
   1326   1.1        pk 
   1327  1.73   tsutsui 	return val;
   1328   1.1        pk }
   1329   1.1        pk 
   1330   1.1        pk void
   1331  1.65    cegger be_mii_writereg(device_t self, int phy, int reg, int val)
   1332   1.1        pk {
   1333  1.67   tsutsui 	struct be_softc *sc = device_private(self);
   1334   1.1        pk 	int i;
   1335   1.1        pk 
   1336   1.1        pk 	/*
   1337   1.1        pk 	 * Write the PHY register by manually driving the MII control lines.
   1338   1.1        pk 	 */
   1339   1.1        pk 	be_mii_sync(sc);
   1340   1.1        pk 	be_mii_sendbits(sc, phy, MII_COMMAND_START, 2);
   1341   1.1        pk 	be_mii_sendbits(sc, phy, MII_COMMAND_WRITE, 2);
   1342   1.1        pk 	be_mii_sendbits(sc, phy, phy, 5);
   1343   1.1        pk 	be_mii_sendbits(sc, phy, reg, 5);
   1344   1.1        pk 
   1345   1.1        pk 	be_tcvr_write_bit(sc, phy, 1);
   1346   1.1        pk 	be_tcvr_write_bit(sc, phy, 0);
   1347   1.1        pk 
   1348   1.1        pk 	for (i = 15; i >= 0; i--)
   1349   1.1        pk 		be_tcvr_write_bit(sc, phy, (val >> i) & 1);
   1350   1.1        pk }
   1351   1.1        pk 
   1352   1.1        pk int
   1353  1.62       dsl be_mii_reset(struct be_softc *sc, int phy)
   1354   1.1        pk {
   1355  1.69   tsutsui 	device_t self = sc->sc_dev;
   1356   1.1        pk 	int n;
   1357   1.1        pk 
   1358  1.73   tsutsui 	be_mii_writereg(self, phy, MII_BMCR, BMCR_LOOP | BMCR_PDOWN | BMCR_ISO);
   1359  1.67   tsutsui 	be_mii_writereg(self, phy, MII_BMCR, BMCR_RESET);
   1360   1.1        pk 
   1361   1.1        pk 	for (n = 16; n >= 0; n--) {
   1362  1.67   tsutsui 		int bmcr = be_mii_readreg(self, phy, MII_BMCR);
   1363   1.1        pk 		if ((bmcr & BMCR_RESET) == 0)
   1364   1.1        pk 			break;
   1365   1.1        pk 		DELAY(20);
   1366   1.1        pk 	}
   1367   1.1        pk 	if (n == 0) {
   1368  1.67   tsutsui 		aprint_error_dev(self, "bmcr reset failed\n");
   1369  1.73   tsutsui 		return EIO;
   1370   1.1        pk 	}
   1371  1.13        pk 
   1372  1.73   tsutsui 	return 0;
   1373   1.1        pk }
   1374   1.1        pk 
   1375   1.1        pk void
   1376  1.62       dsl be_tick(void *arg)
   1377  1.12        pk {
   1378  1.12        pk 	struct be_softc *sc = arg;
   1379  1.12        pk 	int s = splnet();
   1380  1.12        pk 
   1381  1.12        pk 	mii_tick(&sc->sc_mii);
   1382  1.12        pk 	(void)be_intphy_service(sc, &sc->sc_mii, MII_TICK);
   1383  1.12        pk 
   1384  1.12        pk 	splx(s);
   1385  1.17   thorpej 	callout_reset(&sc->sc_tick_ch, hz, be_tick, sc);
   1386  1.12        pk }
   1387  1.12        pk 
   1388  1.12        pk void
   1389  1.65    cegger be_mii_statchg(device_t self)
   1390   1.1        pk {
   1391  1.67   tsutsui 	struct be_softc *sc = device_private(self);
   1392  1.10        pk 	bus_space_tag_t t = sc->sc_bustag;
   1393  1.10        pk 	bus_space_handle_t br = sc->sc_br;
   1394  1.73   tsutsui 	uint instance;
   1395  1.73   tsutsui 	uint32_t v;
   1396  1.10        pk 
   1397  1.11        pk 	instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
   1398  1.11        pk #ifdef DIAGNOSTIC
   1399  1.11        pk 	if (instance > 1)
   1400  1.11        pk 		panic("be_mii_statchg: instance %d out of range", instance);
   1401  1.11        pk #endif
   1402   1.1        pk 
   1403  1.10        pk 	/* Update duplex mode in TX configuration */
   1404  1.10        pk 	v = bus_space_read_4(t, br, BE_BRI_TXCFG);
   1405  1.10        pk 	if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
   1406  1.10        pk 		v |= BE_BR_TXCFG_FULLDPLX;
   1407  1.10        pk 	else
   1408  1.10        pk 		v &= ~BE_BR_TXCFG_FULLDPLX;
   1409  1.10        pk 	bus_space_write_4(t, br, BE_BRI_TXCFG, v);
   1410  1.11        pk 
   1411  1.11        pk 	/* Change to appropriate gate in transceiver PAL */
   1412  1.11        pk 	be_pal_gate(sc, sc->sc_phys[instance]);
   1413   1.1        pk }
   1414   1.1        pk 
   1415  1.12        pk /*
   1416  1.12        pk  * Get current media settings.
   1417  1.12        pk  */
   1418   1.1        pk void
   1419  1.62       dsl be_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
   1420  1.12        pk {
   1421  1.12        pk 	struct be_softc *sc = ifp->if_softc;
   1422  1.12        pk 
   1423  1.12        pk 	mii_pollstat(&sc->sc_mii);
   1424  1.12        pk 	(void)be_intphy_service(sc, &sc->sc_mii, MII_POLLSTAT);
   1425  1.12        pk 
   1426  1.12        pk 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   1427  1.12        pk 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   1428  1.12        pk }
   1429  1.12        pk 
   1430  1.12        pk /*
   1431  1.12        pk  * Set media options.
   1432  1.12        pk  */
   1433  1.12        pk int
   1434  1.62       dsl be_ifmedia_upd(struct ifnet *ifp)
   1435   1.1        pk {
   1436  1.12        pk 	struct be_softc *sc = ifp->if_softc;
   1437  1.12        pk 	int error;
   1438   1.1        pk 
   1439  1.56    dyoung 	if ((error = mii_mediachg(&sc->sc_mii)) == ENXIO)
   1440  1.56    dyoung 		error = 0;
   1441  1.56    dyoung 	else if (error != 0)
   1442  1.56    dyoung 		return error;
   1443   1.1        pk 
   1444  1.73   tsutsui 	return be_intphy_service(sc, &sc->sc_mii, MII_MEDIACHG);
   1445   1.1        pk }
   1446   1.1        pk 
   1447  1.12        pk /*
   1448  1.12        pk  * Service routine for our pseudo-MII internal transceiver.
   1449  1.12        pk  */
   1450  1.12        pk int
   1451  1.62       dsl be_intphy_service(struct be_softc *sc, struct mii_data *mii, int cmd)
   1452   1.1        pk {
   1453  1.12        pk 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   1454  1.69   tsutsui 	device_t self = sc->sc_dev;
   1455   1.1        pk 	int bmcr, bmsr;
   1456  1.13        pk 	int error;
   1457   1.1        pk 
   1458  1.12        pk 	switch (cmd) {
   1459  1.12        pk 	case MII_POLLSTAT:
   1460  1.12        pk 		/*
   1461  1.12        pk 		 * If we're not polling our PHY instance, just return.
   1462  1.12        pk 		 */
   1463  1.12        pk 		if (IFM_INST(ife->ifm_media) != sc->sc_mii_inst)
   1464  1.73   tsutsui 			return 0;
   1465  1.12        pk 
   1466  1.12        pk 		break;
   1467  1.12        pk 
   1468  1.12        pk 	case MII_MEDIACHG:
   1469  1.12        pk 
   1470  1.12        pk 		/*
   1471  1.12        pk 		 * If the media indicates a different PHY instance,
   1472  1.12        pk 		 * isolate ourselves.
   1473  1.12        pk 		 */
   1474  1.12        pk 		if (IFM_INST(ife->ifm_media) != sc->sc_mii_inst) {
   1475  1.67   tsutsui 			bmcr = be_mii_readreg(self, BE_PHY_INTERNAL, MII_BMCR);
   1476  1.67   tsutsui 			be_mii_writereg(self,
   1477  1.67   tsutsui 			    BE_PHY_INTERNAL, MII_BMCR, bmcr | BMCR_ISO);
   1478  1.13        pk 			sc->sc_mii_flags &= ~MIIF_HAVELINK;
   1479  1.13        pk 			sc->sc_intphy_curspeed = 0;
   1480  1.73   tsutsui 			return 0;
   1481  1.12        pk 		}
   1482  1.12        pk 
   1483  1.12        pk 
   1484  1.13        pk 		if ((error = be_mii_reset(sc, BE_PHY_INTERNAL)) != 0)
   1485  1.73   tsutsui 			return error;
   1486  1.13        pk 
   1487  1.67   tsutsui 		bmcr = be_mii_readreg(self, BE_PHY_INTERNAL, MII_BMCR);
   1488  1.13        pk 
   1489  1.13        pk 		/*
   1490  1.13        pk 		 * Select the new mode and take out of isolation
   1491  1.13        pk 		 */
   1492  1.12        pk 		if (IFM_SUBTYPE(ife->ifm_media) == IFM_100_TX)
   1493  1.12        pk 			bmcr |= BMCR_S100;
   1494  1.12        pk 		else if (IFM_SUBTYPE(ife->ifm_media) == IFM_10_T)
   1495  1.12        pk 			bmcr &= ~BMCR_S100;
   1496  1.13        pk 		else if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
   1497  1.13        pk 			if ((sc->sc_mii_flags & MIIF_HAVELINK) != 0) {
   1498  1.13        pk 				bmcr &= ~BMCR_S100;
   1499  1.13        pk 				bmcr |= sc->sc_intphy_curspeed;
   1500  1.13        pk 			} else {
   1501  1.13        pk 				/* Keep isolated until link is up */
   1502  1.13        pk 				bmcr |= BMCR_ISO;
   1503  1.13        pk 				sc->sc_mii_flags |= MIIF_DOINGAUTO;
   1504  1.13        pk 			}
   1505  1.13        pk 		}
   1506  1.12        pk 
   1507  1.12        pk 		if ((IFM_OPTIONS(ife->ifm_media) & IFM_FDX) != 0)
   1508  1.12        pk 			bmcr |= BMCR_FDX;
   1509  1.12        pk 		else
   1510  1.12        pk 			bmcr &= ~BMCR_FDX;
   1511  1.12        pk 
   1512  1.67   tsutsui 		be_mii_writereg(self, BE_PHY_INTERNAL, MII_BMCR, bmcr);
   1513  1.12        pk 		break;
   1514  1.12        pk 
   1515  1.12        pk 	case MII_TICK:
   1516  1.12        pk 		/*
   1517  1.12        pk 		 * If we're not currently selected, just return.
   1518  1.12        pk 		 */
   1519  1.12        pk 		if (IFM_INST(ife->ifm_media) != sc->sc_mii_inst)
   1520  1.73   tsutsui 			return 0;
   1521  1.12        pk 
   1522  1.12        pk 		/* Only used for automatic media selection */
   1523  1.12        pk 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   1524  1.73   tsutsui 			return 0;
   1525  1.12        pk 
   1526  1.12        pk 		/* Is the interface even up? */
   1527  1.12        pk 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
   1528  1.73   tsutsui 			return 0;
   1529  1.12        pk 
   1530  1.12        pk 		/*
   1531  1.12        pk 		 * Check link status; if we don't have a link, try another
   1532  1.12        pk 		 * speed. We can't detect duplex mode, so half-duplex is
   1533  1.12        pk 		 * what we have to settle for.
   1534  1.12        pk 		 */
   1535   1.1        pk 
   1536  1.12        pk 		/* Read twice in case the register is latched */
   1537  1.73   tsutsui 		bmsr =
   1538  1.73   tsutsui 		    be_mii_readreg(self, BE_PHY_INTERNAL, MII_BMSR) |
   1539  1.73   tsutsui 		    be_mii_readreg(self, BE_PHY_INTERNAL, MII_BMSR);
   1540  1.12        pk 
   1541  1.12        pk 		if ((bmsr & BMSR_LINK) != 0) {
   1542  1.12        pk 			/* We have a carrier */
   1543  1.67   tsutsui 			bmcr = be_mii_readreg(self, BE_PHY_INTERNAL, MII_BMCR);
   1544  1.13        pk 
   1545  1.13        pk 			if ((sc->sc_mii_flags & MIIF_DOINGAUTO) != 0) {
   1546  1.67   tsutsui 				bmcr = be_mii_readreg(self,
   1547  1.67   tsutsui 				    BE_PHY_INTERNAL, MII_BMCR);
   1548  1.13        pk 
   1549  1.13        pk 				sc->sc_mii_flags |= MIIF_HAVELINK;
   1550  1.13        pk 				sc->sc_intphy_curspeed = (bmcr & BMCR_S100);
   1551  1.13        pk 				sc->sc_mii_flags &= ~MIIF_DOINGAUTO;
   1552  1.13        pk 
   1553  1.13        pk 				bmcr &= ~BMCR_ISO;
   1554  1.67   tsutsui 				be_mii_writereg(self,
   1555  1.67   tsutsui 				    BE_PHY_INTERNAL, MII_BMCR, bmcr);
   1556  1.13        pk 
   1557  1.13        pk 				printf("%s: link up at %s Mbps\n",
   1558  1.67   tsutsui 				    device_xname(self),
   1559  1.67   tsutsui 				    (bmcr & BMCR_S100) ? "100" : "10");
   1560  1.13        pk 			}
   1561  1.73   tsutsui 			return 0;
   1562  1.12        pk 		}
   1563   1.1        pk 
   1564  1.13        pk 		if ((sc->sc_mii_flags & MIIF_DOINGAUTO) == 0) {
   1565  1.13        pk 			sc->sc_mii_flags |= MIIF_DOINGAUTO;
   1566  1.13        pk 			sc->sc_mii_flags &= ~MIIF_HAVELINK;
   1567  1.13        pk 			sc->sc_intphy_curspeed = 0;
   1568  1.67   tsutsui 			printf("%s: link down\n", device_xname(self));
   1569  1.13        pk 		}
   1570  1.13        pk 
   1571  1.12        pk 		/* Only retry autonegotiation every 5 seconds. */
   1572  1.13        pk 		if (++sc->sc_mii_ticks < 5)
   1573  1.73   tsutsui 			return 0;
   1574  1.12        pk 
   1575  1.12        pk 		sc->sc_mii_ticks = 0;
   1576  1.67   tsutsui 		bmcr = be_mii_readreg(self, BE_PHY_INTERNAL, MII_BMCR);
   1577  1.12        pk 		/* Just flip the fast speed bit */
   1578  1.12        pk 		bmcr ^= BMCR_S100;
   1579  1.67   tsutsui 		be_mii_writereg(self, BE_PHY_INTERNAL, MII_BMCR, bmcr);
   1580   1.1        pk 
   1581  1.12        pk 		break;
   1582   1.1        pk 
   1583  1.12        pk 	case MII_DOWN:
   1584  1.13        pk 		/* Isolate this phy */
   1585  1.67   tsutsui 		bmcr = be_mii_readreg(self, BE_PHY_INTERNAL, MII_BMCR);
   1586  1.67   tsutsui 		be_mii_writereg(self,
   1587  1.67   tsutsui 		    BE_PHY_INTERNAL, MII_BMCR, bmcr | BMCR_ISO);
   1588  1.73   tsutsui 		return 0;
   1589   1.1        pk 	}
   1590   1.1        pk 
   1591  1.12        pk 	/* Update the media status. */
   1592  1.12        pk 	be_intphy_status(sc);
   1593  1.10        pk 
   1594  1.12        pk 	/* Callback if something changed. */
   1595  1.12        pk 	if (sc->sc_mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
   1596  1.67   tsutsui 		(*mii->mii_statchg)(self);
   1597  1.12        pk 		sc->sc_mii_active = mii->mii_media_active;
   1598  1.12        pk 	}
   1599  1.73   tsutsui 	return 0;
   1600   1.1        pk }
   1601   1.1        pk 
   1602   1.1        pk /*
   1603  1.12        pk  * Determine status of internal transceiver
   1604   1.1        pk  */
   1605   1.1        pk void
   1606  1.62       dsl be_intphy_status(struct be_softc *sc)
   1607   1.1        pk {
   1608  1.12        pk 	struct mii_data *mii = &sc->sc_mii;
   1609  1.69   tsutsui 	device_t self = sc->sc_dev;
   1610  1.10        pk 	int media_active, media_status;
   1611   1.1        pk 	int bmcr, bmsr;
   1612   1.1        pk 
   1613  1.10        pk 	media_status = IFM_AVALID;
   1614  1.10        pk 	media_active = 0;
   1615  1.10        pk 
   1616   1.1        pk 	/*
   1617   1.1        pk 	 * Internal transceiver; do the work here.
   1618   1.1        pk 	 */
   1619  1.67   tsutsui 	bmcr = be_mii_readreg(self, BE_PHY_INTERNAL, MII_BMCR);
   1620   1.1        pk 
   1621   1.1        pk 	switch (bmcr & (BMCR_S100 | BMCR_FDX)) {
   1622   1.1        pk 	case (BMCR_S100 | BMCR_FDX):
   1623  1.10        pk 		media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
   1624   1.1        pk 		break;
   1625   1.1        pk 	case BMCR_S100:
   1626  1.10        pk 		media_active = IFM_ETHER | IFM_100_TX | IFM_HDX;
   1627   1.1        pk 		break;
   1628   1.1        pk 	case BMCR_FDX:
   1629  1.10        pk 		media_active = IFM_ETHER | IFM_10_T | IFM_FDX;
   1630   1.1        pk 		break;
   1631   1.1        pk 	case 0:
   1632  1.10        pk 		media_active = IFM_ETHER | IFM_10_T | IFM_HDX;
   1633   1.1        pk 		break;
   1634   1.1        pk 	}
   1635   1.1        pk 
   1636   1.1        pk 	/* Read twice in case the register is latched */
   1637  1.73   tsutsui 	bmsr =
   1638  1.73   tsutsui 	    be_mii_readreg(self, BE_PHY_INTERNAL, MII_BMSR) |
   1639  1.73   tsutsui 	    be_mii_readreg(self, BE_PHY_INTERNAL, MII_BMSR);
   1640   1.1        pk 	if (bmsr & BMSR_LINK)
   1641  1.11        pk 		media_status |=  IFM_ACTIVE;
   1642  1.10        pk 
   1643  1.12        pk 	mii->mii_media_status = media_status;
   1644  1.12        pk 	mii->mii_media_active = media_active;
   1645   1.1        pk }
   1646