Home | History | Annotate | Line # | Download | only in ralink
ralink_eth.c revision 1.18
      1  1.18  msaitoh /*	$NetBSD: ralink_eth.c,v 1.18 2019/05/29 14:03:36 msaitoh Exp $	*/
      2   1.2     matt /*-
      3   1.2     matt  * Copyright (c) 2011 CradlePoint Technology, Inc.
      4   1.2     matt  * All rights reserved.
      5   1.2     matt  *
      6   1.2     matt  *
      7   1.2     matt  * Redistribution and use in source and binary forms, with or without
      8   1.2     matt  * modification, are permitted provided that the following conditions
      9   1.2     matt  * are met:
     10   1.2     matt  * 1. Redistributions of source code must retain the above copyright
     11   1.2     matt  *    notice, this list of conditions and the following disclaimer.
     12   1.2     matt  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.2     matt  *    notice, this list of conditions and the following disclaimer in the
     14   1.2     matt  *    documentation and/or other materials provided with the distribution.
     15   1.2     matt  *
     16   1.2     matt  * THIS SOFTWARE IS PROVIDED BY CRADLEPOINT TECHNOLOGY, INC. AND CONTRIBUTORS
     17   1.2     matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18   1.2     matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19   1.2     matt  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
     20   1.2     matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21   1.2     matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22   1.2     matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23   1.2     matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24   1.2     matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25   1.2     matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26   1.2     matt  * POSSIBILITY OF SUCH DAMAGE.
     27   1.2     matt  */
     28   1.2     matt 
     29   1.2     matt /* ralink_eth.c -- Ralink Ethernet Driver */
     30   1.2     matt 
     31   1.2     matt #include <sys/cdefs.h>
     32  1.18  msaitoh __KERNEL_RCSID(0, "$NetBSD: ralink_eth.c,v 1.18 2019/05/29 14:03:36 msaitoh Exp $");
     33   1.2     matt 
     34   1.2     matt #include <sys/param.h>
     35   1.3     matt #include <sys/bus.h>
     36   1.2     matt #include <sys/callout.h>
     37   1.2     matt #include <sys/device.h>
     38   1.2     matt #include <sys/endian.h>
     39   1.2     matt #include <sys/errno.h>
     40   1.2     matt #include <sys/ioctl.h>
     41   1.3     matt #include <sys/intr.h>
     42   1.2     matt #include <sys/kernel.h>
     43   1.2     matt #include <sys/malloc.h>
     44   1.2     matt #include <sys/mbuf.h>
     45   1.2     matt #include <sys/socket.h>
     46   1.2     matt #include <sys/systm.h>
     47   1.2     matt 
     48   1.2     matt #include <uvm/uvm_extern.h>
     49   1.2     matt 
     50   1.2     matt #include <net/if.h>
     51   1.2     matt #include <net/if_dl.h>
     52   1.2     matt #include <net/if_media.h>
     53   1.2     matt #include <net/if_ether.h>
     54   1.2     matt #include <net/if_vlanvar.h>
     55   1.2     matt 
     56   1.2     matt #include <net/bpf.h>
     57   1.2     matt 
     58   1.2     matt #include <dev/mii/mii.h>
     59   1.2     matt #include <dev/mii/miivar.h>
     60   1.2     matt #include <dev/mii/mii_bitbang.h>
     61   1.2     matt 
     62   1.2     matt #include <mips/ralink/ralink_var.h>
     63   1.2     matt #include <mips/ralink/ralink_reg.h>
     64   1.2     matt #if 0
     65   1.2     matt #define CPDEBUG				/* XXX TMP DEBUG FIXME */
     66  1.10      ryo #define RALINK_ETH_DEBUG		/* XXX TMP DEBUG FIXME */
     67   1.2     matt #define ENABLE_RALINK_DEBUG_ERROR 1
     68   1.2     matt #define ENABLE_RALINK_DEBUG_MISC  1
     69   1.2     matt #define ENABLE_RALINK_DEBUG_INFO  1
     70   1.2     matt #define ENABLE_RALINK_DEBUG_FORCE 1
     71  1.10      ryo #define ENABLE_RALINK_DEBUG_REG   1
     72   1.2     matt #endif
     73   1.2     matt #include <mips/ralink/ralink_debug.h>
     74   1.2     matt 
     75   1.2     matt 
     76   1.2     matt /* PDMA RX Descriptor Format */
     77   1.2     matt struct ralink_rx_desc {
     78   1.2     matt 	uint32_t data_ptr;
     79   1.2     matt 	uint32_t rxd_info1;
     80  1.10      ryo #define RXD_LEN1(x)	(((x) >> 0) & 0x3fff)
     81  1.10      ryo #define RXD_LAST1	(1 << 14)
     82  1.10      ryo #define RXD_LEN0(x)	(((x) >> 16) & 0x3fff)
     83  1.10      ryo #define RXD_LAST0	(1 << 30)
     84  1.10      ryo #define RXD_DDONE	(1 << 31)
     85   1.2     matt 	uint32_t unused;
     86   1.2     matt 	uint32_t rxd_info2;
     87  1.10      ryo #define RXD_FOE(x)	(((x) >> 0) & 0x3fff)
     88  1.10      ryo #define RXD_FVLD	(1 << 14)
     89  1.10      ryo #define RXD_INFO(x)	(((x) >> 16) & 0xff)
     90  1.10      ryo #define RXD_PORT(x)	(((x) >> 24) & 0x7)
     91  1.10      ryo #define RXD_INFO_CPU	(1 << 27)
     92  1.10      ryo #define RXD_L4_FAIL	(1 << 28)
     93  1.10      ryo #define RXD_IP_FAIL	(1 << 29)
     94  1.10      ryo #define RXD_L4_VLD	(1 << 30)
     95  1.10      ryo #define RXD_IP_VLD	(1 << 31)
     96   1.2     matt };
     97   1.2     matt 
     98  1.11      ryo /* PDMA TX Descriptor Format */
     99   1.2     matt struct ralink_tx_desc {
    100   1.2     matt 	uint32_t data_ptr0;
    101   1.2     matt 	uint32_t txd_info1;
    102  1.10      ryo #define TXD_LEN1(x)	(((x) & 0x3fff) << 0)
    103  1.10      ryo #define TXD_LAST1	(1 << 14)
    104  1.10      ryo #define TXD_BURST	(1 << 15)
    105  1.10      ryo #define TXD_LEN0(x)	(((x) & 0x3fff) << 16)
    106  1.10      ryo #define TXD_LAST0	(1 << 30)
    107  1.10      ryo #define TXD_DDONE	(1 << 31)
    108   1.2     matt 	uint32_t data_ptr1;
    109   1.2     matt 	uint32_t txd_info2;
    110  1.10      ryo #define TXD_VIDX(x)	(((x) & 0xf) << 0)
    111  1.10      ryo #define TXD_VPRI(x)	(((x) & 0x7) << 4)
    112  1.10      ryo #define TXD_VEN		(1 << 7)
    113  1.10      ryo #define TXD_SIDX(x)	(((x) & 0xf) << 8)
    114  1.10      ryo #define TXD_SEN(x)	(1 << 13)
    115  1.10      ryo #define TXD_QN(x)	(((x) & 0x7) << 16)
    116  1.10      ryo #define TXD_PN(x)	(((x) & 0x7) << 24)
    117  1.10      ryo #define  TXD_PN_CPU	0
    118  1.10      ryo #define  TXD_PN_GDMA1	1
    119  1.10      ryo #define  TXD_PN_GDMA2	2
    120  1.10      ryo #define TXD_TCP_EN	(1 << 29)
    121  1.10      ryo #define TXD_UDP_EN	(1 << 30)
    122  1.10      ryo #define TXD_IP_EN	(1 << 31)
    123   1.2     matt };
    124   1.2     matt 
    125   1.2     matt /* TODO:
    126   1.2     matt  * try to scale number of descriptors swith size of memory
    127   1.2     matt  * these numbers may have a significant impact on performance/memory/mbuf usage
    128   1.2     matt  */
    129   1.2     matt #if RTMEMSIZE >= 64
    130   1.2     matt #define RALINK_ETH_NUM_RX_DESC 256
    131   1.2     matt #define RALINK_ETH_NUM_TX_DESC 256
    132   1.4     matt #else
    133   1.2     matt #define RALINK_ETH_NUM_RX_DESC 64
    134   1.2     matt #define RALINK_ETH_NUM_TX_DESC 64
    135   1.2     matt #endif
    136   1.2     matt /* maximum segments per packet */
    137   1.2     matt #define RALINK_ETH_MAX_TX_SEGS 1
    138   1.2     matt 
    139   1.2     matt /* define a struct for ease of dma memory allocation */
    140   1.2     matt struct ralink_descs {
    141   1.2     matt 	struct ralink_rx_desc rxdesc[RALINK_ETH_NUM_RX_DESC];
    142   1.2     matt 	struct ralink_tx_desc txdesc[RALINK_ETH_NUM_TX_DESC];
    143   1.2     matt };
    144   1.2     matt 
    145   1.2     matt /* Software state for transmit jobs. */
    146   1.2     matt struct ralink_eth_txstate {
    147   1.2     matt 	struct mbuf *txs_mbuf;		/* head of our mbuf chain */
    148   1.2     matt 	bus_dmamap_t txs_dmamap;	/* our DMA map */
    149   1.2     matt 	int txs_idx;			/* the index in txdesc ring that */
    150   1.2     matt 					/*  this state is tracking */
    151   1.2     matt 	SIMPLEQ_ENTRY(ralink_eth_txstate) txs_q;
    152   1.2     matt };
    153   1.2     matt 
    154   1.2     matt SIMPLEQ_HEAD(ralink_eth_txsq, ralink_eth_txstate);
    155   1.2     matt 
    156   1.2     matt /*
    157   1.2     matt  * Software state for receive jobs.
    158   1.2     matt  */
    159   1.2     matt struct ralink_eth_rxstate {
    160   1.2     matt 	struct mbuf *rxs_mbuf;		/* head of our mbuf chain */
    161   1.2     matt 	bus_dmamap_t rxs_dmamap;	/* our DMA map */
    162   1.2     matt };
    163   1.2     matt 
    164   1.2     matt typedef struct ralink_eth_softc {
    165   1.2     matt 	device_t sc_dev;		/* generic device information */
    166   1.2     matt 	bus_space_tag_t sc_memt;	/* bus space tag */
    167   1.2     matt 	bus_space_handle_t sc_sy_memh;	/* handle at SYSCTL_BASE */
    168   1.2     matt 	bus_space_handle_t sc_fe_memh;	/* handle at FRAME_ENGINE_BASE */
    169   1.2     matt 	bus_space_handle_t sc_sw_memh;	/* handle at ETH_SW_BASE */
    170   1.2     matt 	int sc_sy_size;			/* size of Sysctl regs space */
    171   1.2     matt 	int sc_fe_size;			/* size of Frame Engine regs space */
    172   1.2     matt 	int sc_sw_size;			/* size of Ether Switch regs space */
    173   1.2     matt 	bus_dma_tag_t sc_dmat;		/* bus DMA tag */
    174   1.2     matt 	void *sc_ih;			/* interrupt handle */
    175   1.2     matt 
    176   1.2     matt 	/* tx/rx dma mapping */
    177   1.2     matt 	bus_dma_segment_t sc_dseg;
    178   1.2     matt 	int sc_ndseg;
    179   1.2     matt 	bus_dmamap_t sc_pdmamap;	/* PDMA DMA map */
    180   1.2     matt #define sc_pdma sc_pdmamap->dm_segs[0].ds_addr
    181   1.2     matt 
    182   1.2     matt 	struct ralink_descs *sc_descs;
    183   1.2     matt #define sc_rxdesc sc_descs->rxdesc
    184   1.2     matt #define sc_txdesc sc_descs->txdesc
    185   1.2     matt 
    186   1.2     matt #define RALINK_MIN_BUF 64
    187   1.2     matt 	char ralink_zero_buf[RALINK_MIN_BUF];
    188   1.2     matt 
    189   1.2     matt 	struct ralink_eth_txstate sc_txstate[RALINK_ETH_NUM_TX_DESC];
    190   1.2     matt 	struct ralink_eth_rxstate sc_rxstate[RALINK_ETH_NUM_RX_DESC];
    191   1.2     matt 
    192   1.2     matt 	struct ralink_eth_txsq sc_txfreeq;	/* free Tx descsofts */
    193   1.2     matt 	struct ralink_eth_txsq sc_txdirtyq;	/* dirty Tx descsofts */
    194   1.2     matt 
    195   1.2     matt 	struct ethercom sc_ethercom;		/* ethernet common data */
    196   1.2     matt 	u_int sc_pending_tx;
    197   1.2     matt 
    198   1.2     matt 	/* mii */
    199   1.2     matt 	struct mii_data sc_mii;
    200   1.2     matt 	struct callout sc_tick_callout;
    201   1.2     matt 
    202   1.2     matt 	struct evcnt sc_evcnt_spurious_intr;
    203   1.2     matt 	struct evcnt sc_evcnt_rxintr;
    204   1.2     matt 	struct evcnt sc_evcnt_rxintr_skip_len;
    205   1.2     matt 	struct evcnt sc_evcnt_rxintr_skip_tag_none;
    206   1.2     matt 	struct evcnt sc_evcnt_rxintr_skip_tag_inval;
    207   1.2     matt 	struct evcnt sc_evcnt_rxintr_skip_inact;
    208   1.2     matt 	struct evcnt sc_evcnt_txintr;
    209   1.2     matt 	struct evcnt sc_evcnt_input;
    210   1.2     matt 	struct evcnt sc_evcnt_output;
    211   1.2     matt 	struct evcnt sc_evcnt_watchdog;
    212   1.2     matt 	struct evcnt sc_evcnt_wd_reactivate;
    213   1.2     matt 	struct evcnt sc_evcnt_wd_tx;
    214   1.2     matt 	struct evcnt sc_evcnt_wd_spurious;
    215   1.2     matt 	struct evcnt sc_evcnt_add_rxbuf_hdr_fail;
    216   1.2     matt 	struct evcnt sc_evcnt_add_rxbuf_mcl_fail;
    217   1.2     matt } ralink_eth_softc_t;
    218   1.2     matt 
    219   1.2     matt /* alignment so the IP header is aligned */
    220   1.2     matt #define RALINK_ETHER_ALIGN 2
    221   1.2     matt 
    222   1.2     matt /* device functions */
    223   1.2     matt static int  ralink_eth_match(device_t, cfdata_t, void *);
    224   1.2     matt static void ralink_eth_attach(device_t, device_t, void *);
    225   1.2     matt static int  ralink_eth_detach(device_t, int);
    226   1.2     matt static int  ralink_eth_activate(device_t, enum devact);
    227   1.2     matt 
    228   1.2     matt /* local driver functions */
    229   1.2     matt static void ralink_eth_hw_init(ralink_eth_softc_t *);
    230   1.2     matt static int  ralink_eth_intr(void *);
    231   1.2     matt static void ralink_eth_reset(ralink_eth_softc_t *);
    232   1.2     matt static void ralink_eth_rxintr(ralink_eth_softc_t *);
    233   1.2     matt static void ralink_eth_txintr(ralink_eth_softc_t *);
    234   1.2     matt 
    235   1.2     matt /* partition functions */
    236   1.2     matt static int  ralink_eth_enable(ralink_eth_softc_t *);
    237   1.2     matt static void ralink_eth_disable(ralink_eth_softc_t *);
    238   1.2     matt 
    239   1.2     matt /* ifnet functions */
    240   1.2     matt static int  ralink_eth_init(struct ifnet *);
    241   1.2     matt static void ralink_eth_rxdrain(ralink_eth_softc_t *);
    242   1.2     matt static void ralink_eth_stop(struct ifnet *, int);
    243   1.2     matt static int  ralink_eth_add_rxbuf(ralink_eth_softc_t *, int);
    244   1.2     matt static void ralink_eth_start(struct ifnet *);
    245   1.2     matt static void ralink_eth_watchdog(struct ifnet *);
    246   1.2     matt static int  ralink_eth_ioctl(struct ifnet *, u_long, void *);
    247   1.2     matt 
    248   1.2     matt /* mii functions */
    249   1.2     matt #if defined(RT3050) || defined(RT3052)
    250   1.2     matt static void ralink_eth_mdio_enable(ralink_eth_softc_t *, bool);
    251   1.2     matt #endif
    252   1.6     matt static void ralink_eth_mii_statchg(struct ifnet *);
    253   1.2     matt static void ralink_eth_mii_tick(void *);
    254  1.15  msaitoh static int  ralink_eth_mii_read(device_t, int, int, uint16_t *);
    255  1.15  msaitoh static int  ralink_eth_mii_write(device_t, int, int, uint16_t);
    256   1.2     matt 
    257   1.2     matt CFATTACH_DECL_NEW(reth, sizeof(struct ralink_eth_softc),
    258  1.10      ryo     ralink_eth_match, ralink_eth_attach, ralink_eth_detach,
    259  1.10      ryo     ralink_eth_activate);
    260   1.2     matt 
    261   1.2     matt static inline uint32_t
    262   1.2     matt sy_read(const ralink_eth_softc_t *sc, const bus_size_t off)
    263   1.2     matt {
    264   1.2     matt 	return bus_space_read_4(sc->sc_memt, sc->sc_sy_memh, off);
    265   1.2     matt }
    266   1.2     matt 
    267   1.2     matt static inline void
    268   1.2     matt sy_write(const ralink_eth_softc_t *sc, const bus_size_t off, const uint32_t val)
    269   1.2     matt {
    270   1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sy_memh, off, val);
    271   1.2     matt }
    272   1.2     matt 
    273   1.2     matt static inline uint32_t
    274   1.2     matt fe_read(const ralink_eth_softc_t *sc, const bus_size_t off)
    275   1.2     matt {
    276   1.2     matt 	return bus_space_read_4(sc->sc_memt, sc->sc_fe_memh, off);
    277   1.2     matt }
    278   1.2     matt 
    279   1.2     matt static inline void
    280   1.2     matt fe_write(const ralink_eth_softc_t *sc, const bus_size_t off, const uint32_t val)
    281   1.2     matt {
    282   1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_fe_memh, off, val);
    283   1.2     matt }
    284   1.2     matt 
    285   1.2     matt static inline uint32_t
    286   1.2     matt sw_read(const ralink_eth_softc_t *sc, const bus_size_t off)
    287   1.2     matt {
    288   1.2     matt 	return bus_space_read_4(sc->sc_memt, sc->sc_sw_memh, off);
    289   1.2     matt }
    290   1.2     matt 
    291   1.2     matt static inline void
    292   1.2     matt sw_write(const ralink_eth_softc_t *sc, const bus_size_t off, const uint32_t val)
    293   1.2     matt {
    294   1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sw_memh, off, val);
    295   1.2     matt }
    296   1.2     matt 
    297   1.2     matt /*
    298   1.2     matt  * ralink_eth_match
    299   1.2     matt  */
    300   1.2     matt int
    301   1.2     matt ralink_eth_match(device_t parent, cfdata_t cf, void *aux)
    302   1.2     matt {
    303   1.2     matt 	return 1;
    304   1.2     matt }
    305   1.2     matt 
    306   1.2     matt /*
    307   1.2     matt  * ralink_eth_attach
    308   1.2     matt  */
    309   1.2     matt void
    310   1.2     matt ralink_eth_attach(device_t parent, device_t self, void *aux)
    311   1.2     matt {
    312   1.2     matt 	ralink_eth_softc_t * const sc = device_private(self);
    313   1.2     matt 	const struct mainbus_attach_args *ma = aux;
    314  1.17  msaitoh 	struct mii_data *mii = &sc->sc_mii;
    315   1.2     matt 	int error;
    316   1.2     matt 	int i;
    317   1.2     matt 
    318   1.2     matt 	aprint_naive(": Ralink Ethernet\n");
    319   1.2     matt 	aprint_normal(": Ralink Ethernet\n");
    320   1.2     matt 
    321   1.2     matt 	evcnt_attach_dynamic(&sc->sc_evcnt_spurious_intr, EVCNT_TYPE_INTR, NULL,
    322  1.10      ryo 	    device_xname(self), "spurious intr");
    323   1.2     matt 	evcnt_attach_dynamic(&sc->sc_evcnt_rxintr, EVCNT_TYPE_INTR, NULL,
    324  1.10      ryo 	    device_xname(self), "rxintr");
    325   1.2     matt 	evcnt_attach_dynamic(&sc->sc_evcnt_rxintr_skip_len,
    326  1.10      ryo 	    EVCNT_TYPE_INTR, &sc->sc_evcnt_rxintr,
    327  1.10      ryo 	    device_xname(self), "rxintr skip: no room for VLAN header");
    328   1.2     matt 	evcnt_attach_dynamic(&sc->sc_evcnt_rxintr_skip_tag_none,
    329  1.10      ryo 	    EVCNT_TYPE_INTR, &sc->sc_evcnt_rxintr,
    330  1.10      ryo 	    device_xname(self), "rxintr skip: no VLAN tag");
    331   1.2     matt 	evcnt_attach_dynamic(&sc->sc_evcnt_rxintr_skip_tag_inval,
    332  1.10      ryo 	    EVCNT_TYPE_INTR, &sc->sc_evcnt_rxintr,
    333  1.10      ryo 	    device_xname(self), "rxintr skip: invalid VLAN tag");
    334   1.2     matt 	evcnt_attach_dynamic(&sc->sc_evcnt_rxintr_skip_inact,
    335  1.10      ryo 	    EVCNT_TYPE_INTR, &sc->sc_evcnt_rxintr,
    336  1.10      ryo 	    device_xname(self), "rxintr skip: partition inactive");
    337   1.2     matt 	evcnt_attach_dynamic(&sc->sc_evcnt_txintr, EVCNT_TYPE_INTR, NULL,
    338  1.10      ryo 	    device_xname(self), "txintr");
    339   1.2     matt 	evcnt_attach_dynamic(&sc->sc_evcnt_input, EVCNT_TYPE_INTR, NULL,
    340  1.10      ryo 	    device_xname(self), "input");
    341   1.2     matt 	evcnt_attach_dynamic(&sc->sc_evcnt_output, EVCNT_TYPE_INTR, NULL,
    342  1.10      ryo 	    device_xname(self), "output");
    343   1.2     matt 	evcnt_attach_dynamic(&sc->sc_evcnt_watchdog, EVCNT_TYPE_INTR, NULL,
    344  1.10      ryo 	    device_xname(self), "watchdog");
    345   1.2     matt 	evcnt_attach_dynamic(&sc->sc_evcnt_wd_tx,
    346  1.10      ryo 	    EVCNT_TYPE_INTR, &sc->sc_evcnt_watchdog,
    347  1.10      ryo 	    device_xname(self), "watchdog TX timeout");
    348   1.2     matt 	evcnt_attach_dynamic(&sc->sc_evcnt_wd_spurious,
    349  1.10      ryo 	    EVCNT_TYPE_INTR, &sc->sc_evcnt_watchdog,
    350  1.10      ryo 	    device_xname(self), "watchdog spurious");
    351   1.2     matt 	evcnt_attach_dynamic(&sc->sc_evcnt_wd_reactivate,
    352  1.10      ryo 	    EVCNT_TYPE_INTR, &sc->sc_evcnt_watchdog,
    353  1.10      ryo 	    device_xname(self), "watchdog reactivate");
    354   1.2     matt 	evcnt_attach_dynamic(&sc->sc_evcnt_add_rxbuf_hdr_fail,
    355  1.10      ryo 	    EVCNT_TYPE_INTR, NULL,
    356  1.10      ryo 	    device_xname(self), "add rxbuf hdr fail");
    357   1.2     matt 	evcnt_attach_dynamic(&sc->sc_evcnt_add_rxbuf_mcl_fail,
    358  1.10      ryo 	    EVCNT_TYPE_INTR, NULL,
    359  1.10      ryo 	    device_xname(self), "add rxbuf mcl fail");
    360   1.2     matt 
    361   1.2     matt 	/*
    362   1.2     matt 	 * In order to obtain unique initial Ethernet address on a host,
    363   1.2     matt 	 * do some randomisation using the current uptime.  It's not meant
    364   1.2     matt 	 * for anything but avoiding hard-coding an address.
    365   1.2     matt 	 */
    366  1.11      ryo #ifdef RALINK_ETH_MACADDR
    367  1.11      ryo 	uint8_t enaddr[ETHER_ADDR_LEN];
    368  1.11      ryo 	ether_aton_r(enaddr, sizeof(enaddr), ___STRING(RALINK_ETH_MACADDR));
    369  1.11      ryo #else
    370   1.2     matt 	uint8_t enaddr[ETHER_ADDR_LEN] = { 0x00, 0x30, 0x44, 0x00, 0x00, 0x00 };
    371  1.11      ryo #endif
    372   1.2     matt 
    373   1.2     matt 	sc->sc_dev = self;
    374   1.2     matt 	sc->sc_dmat = ma->ma_dmat;
    375   1.2     matt 	sc->sc_memt = ma->ma_memt;
    376   1.2     matt 	sc->sc_sy_size = 0x10000;
    377   1.2     matt 	sc->sc_fe_size = 0x10000;
    378   1.2     matt 	sc->sc_sw_size = 0x08000;
    379   1.2     matt 
    380   1.2     matt 	/*
    381   1.2     matt 	 * map the registers
    382   1.2     matt 	 *
    383   1.2     matt 	 * we map the Sysctl, Frame Engine and Ether Switch registers
    384   1.2     matt 	 * seperately so we can use the defined register offsets sanely
    385   1.2     matt 	 */
    386   1.2     matt 	if ((error = bus_space_map(sc->sc_memt, RA_SYSCTL_BASE,
    387   1.2     matt 	    sc->sc_sy_size, 0, &sc->sc_sy_memh)) != 0) {
    388   1.2     matt 		aprint_error_dev(self, "unable to map Sysctl registers, "
    389  1.10      ryo 		    "error=%d\n", error);
    390   1.2     matt 		goto fail_0a;
    391   1.2     matt 	}
    392   1.2     matt 	if ((error = bus_space_map(sc->sc_memt, RA_FRAME_ENGINE_BASE,
    393   1.2     matt 	    sc->sc_fe_size, 0, &sc->sc_fe_memh)) != 0) {
    394   1.2     matt 		aprint_error_dev(self, "unable to map Frame Engine registers, "
    395  1.10      ryo 		    "error=%d\n", error);
    396   1.2     matt 		goto fail_0b;
    397   1.2     matt 	}
    398   1.2     matt 	if ((error = bus_space_map(sc->sc_memt, RA_ETH_SW_BASE,
    399   1.2     matt 	    sc->sc_sw_size, 0, &sc->sc_sw_memh)) != 0) {
    400   1.2     matt 		aprint_error_dev(self, "unable to map Ether Switch registers, "
    401  1.10      ryo 		    "error=%d\n", error);
    402   1.2     matt 		goto fail_0c;
    403   1.2     matt 	}
    404   1.2     matt 
    405   1.2     matt 	/* Allocate desc structures, and create & load the DMA map for them */
    406   1.2     matt 	if ((error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct ralink_descs),
    407   1.2     matt 	    PAGE_SIZE, 0, &sc->sc_dseg, 1, &sc->sc_ndseg, 0)) != 0) {
    408   1.2     matt 		aprint_error_dev(self, "unable to allocate transmit descs, "
    409  1.10      ryo 		    "error=%d\n", error);
    410   1.2     matt 		goto fail_1;
    411   1.2     matt 	}
    412   1.2     matt 
    413   1.2     matt 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dseg, sc->sc_ndseg,
    414  1.10      ryo 	    sizeof(struct ralink_descs), (void **)&sc->sc_descs,
    415  1.10      ryo 	    BUS_DMA_COHERENT)) != 0) {
    416   1.2     matt 		aprint_error_dev(self, "unable to map control data, "
    417  1.10      ryo 		    "error=%d\n", error);
    418   1.2     matt 		goto fail_2;
    419   1.2     matt 	}
    420   1.2     matt 
    421  1.10      ryo 	if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct ralink_descs),
    422  1.10      ryo 	    1, sizeof(struct ralink_descs), 0, 0, &sc->sc_pdmamap)) != 0) {
    423   1.2     matt 		aprint_error_dev(self, "unable to create control data DMA map, "
    424  1.10      ryo 		    "error=%d\n", error);
    425   1.2     matt 		goto fail_3;
    426   1.2     matt 	}
    427   1.2     matt 
    428   1.2     matt 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_pdmamap, sc->sc_descs,
    429   1.2     matt 	    sizeof(struct ralink_descs), NULL, 0)) != 0) {
    430   1.2     matt 		aprint_error_dev(self, "unable to load control data DMA map, "
    431  1.10      ryo 		    "error=%d\n", error);
    432   1.2     matt 		goto fail_4;
    433   1.2     matt 	}
    434   1.2     matt 
    435   1.2     matt 	/* Create the transmit buffer DMA maps.  */
    436   1.2     matt 	for (i = 0; i < RALINK_ETH_NUM_TX_DESC; i++) {
    437   1.2     matt 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    438   1.2     matt 		    RALINK_ETH_MAX_TX_SEGS, MCLBYTES, 0, 0,
    439   1.2     matt 		    &sc->sc_txstate[i].txs_dmamap)) != 0) {
    440  1.10      ryo 			aprint_error_dev(self,
    441  1.10      ryo 			    "unable to create tx DMA map %d, error=%d\n",
    442  1.10      ryo 			    i, error);
    443   1.2     matt 			goto fail_5;
    444   1.2     matt 		}
    445   1.2     matt 	}
    446   1.2     matt 
    447   1.2     matt 	/* Create the receive buffer DMA maps.  */
    448   1.2     matt 	for (i = 0; i < RALINK_ETH_NUM_RX_DESC; i++) {
    449   1.2     matt 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    450   1.2     matt 		    MCLBYTES, 0, 0, &sc->sc_rxstate[i].rxs_dmamap)) != 0) {
    451  1.10      ryo 			aprint_error_dev(self,
    452  1.10      ryo 			    "unable to create rx DMA map %d, error=%d\n",
    453  1.10      ryo 			    i, error);
    454   1.2     matt 			goto fail_6;
    455   1.2     matt 		}
    456   1.2     matt 		sc->sc_rxstate[i].rxs_mbuf = NULL;
    457   1.2     matt 	}
    458   1.2     matt 
    459   1.2     matt 	/* this is a zero buffer used for zero'ing out short packets */
    460   1.2     matt 	memset(sc->ralink_zero_buf, 0, RALINK_MIN_BUF);
    461   1.2     matt 
    462   1.2     matt 	/* setup some address in hardware */
    463   1.2     matt 	fe_write(sc, RA_FE_GDMA1_MAC_LSB,
    464  1.10      ryo 	    (enaddr[5] | (enaddr[4] << 8) |
    465  1.10      ryo 	    (enaddr[3] << 16) | (enaddr[2] << 24)));
    466   1.2     matt 	fe_write(sc, RA_FE_GDMA1_MAC_MSB,
    467  1.10      ryo 	    (enaddr[1] | (enaddr[0] << 8)));
    468   1.2     matt 
    469   1.2     matt 	/*
    470   1.2     matt 	 * iterate through ports
    471   1.2     matt 	 *  slickrock must use specific non-linear sequence
    472   1.2     matt 	 *  others are linear
    473   1.2     matt 	 */
    474   1.2     matt 	struct ifnet * const ifp = &sc->sc_ethercom.ec_if;
    475   1.2     matt 
    476   1.2     matt 	strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
    477   1.2     matt 
    478   1.2     matt 	/*
    479   1.2     matt 	 * Initialize our media structures.
    480   1.2     matt 	 * This may probe the PHY, if present.
    481   1.2     matt 	 */
    482  1.17  msaitoh 	mii->mii_ifp = ifp;
    483  1.17  msaitoh 	mii->mii_readreg = ralink_eth_mii_read;
    484  1.17  msaitoh 	mii->mii_writereg = ralink_eth_mii_write;
    485  1.17  msaitoh 	mii->mii_statchg = ralink_eth_mii_statchg;
    486  1.17  msaitoh 	sc->sc_ethercom.ec_mii = mii;
    487  1.18  msaitoh 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
    488  1.17  msaitoh 	mii_attach(sc->sc_dev, mii, ~0, MII_PHY_ANY, MII_OFFSET_ANY,
    489  1.17  msaitoh 	    MIIF_FORCEANEG | MIIF_DOPAUSE | MIIF_NOISOLATE);
    490   1.2     matt 
    491  1.17  msaitoh 	if (LIST_EMPTY(&mii->mii_phys)) {
    492   1.2     matt #if 1
    493  1.17  msaitoh 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_1000_T |
    494  1.17  msaitoh 		    IFM_FDX | IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE, 0, NULL);
    495  1.17  msaitoh 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_1000_T |
    496  1.17  msaitoh 		    IFM_FDX | IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE);
    497   1.2     matt #else
    498  1.17  msaitoh 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_MANUAL, 0, NULL);
    499  1.17  msaitoh 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_MANUAL);
    500   1.2     matt #endif
    501   1.2     matt 	} else {
    502  1.10      ryo 		/* Ensure we mask ok for the switch multiple phy's */
    503  1.17  msaitoh 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    504   1.2     matt 	}
    505   1.2     matt 
    506   1.2     matt 	ifp->if_softc = sc;
    507   1.2     matt 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    508   1.2     matt 	ifp->if_init = ralink_eth_init;
    509   1.2     matt 	ifp->if_start = ralink_eth_start;
    510   1.2     matt 	ifp->if_ioctl = ralink_eth_ioctl;
    511   1.2     matt 	ifp->if_stop = ralink_eth_stop;
    512   1.2     matt 	ifp->if_watchdog = ralink_eth_watchdog;
    513   1.2     matt 	IFQ_SET_READY(&ifp->if_snd);
    514   1.2     matt 
    515   1.2     matt 	/* We can support 802.1Q VLAN-sized frames. */
    516   1.2     matt 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    517   1.2     matt 
    518   1.2     matt 	/* We support IPV4 CRC Offload */
    519   1.2     matt 	ifp->if_capabilities |=
    520  1.10      ryo 	    (IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
    521  1.10      ryo 	    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
    522  1.10      ryo 	    IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx);
    523   1.2     matt 
    524   1.2     matt 	/* Attach the interface. */
    525   1.2     matt 	if_attach(ifp);
    526  1.13    ozaki 	if_deferred_start_init(ifp, NULL);
    527   1.2     matt 	ether_ifattach(ifp, enaddr);
    528   1.2     matt 
    529   1.2     matt 	/* init our mii ticker */
    530   1.2     matt 	callout_init(&sc->sc_tick_callout, 0);
    531   1.2     matt 	callout_reset(&sc->sc_tick_callout, hz, ralink_eth_mii_tick, sc);
    532   1.2     matt 
    533   1.2     matt 	return;
    534   1.2     matt 
    535   1.2     matt 	/*
    536   1.2     matt 	 * Free any resources we've allocated during the failed attach
    537   1.2     matt 	 * attempt.  Do this in reverse order and fall through.
    538   1.2     matt 	 */
    539   1.2     matt  fail_6:
    540   1.2     matt 	for (i = 0; i < RALINK_ETH_NUM_RX_DESC; i++) {
    541   1.2     matt 		if (sc->sc_rxstate[i].rxs_dmamap != NULL)
    542   1.2     matt 			bus_dmamap_destroy(sc->sc_dmat,
    543  1.10      ryo 			    sc->sc_rxstate[i].rxs_dmamap);
    544   1.2     matt 	}
    545   1.2     matt  fail_5:
    546   1.2     matt 	for (i = 0; i < RALINK_ETH_NUM_TX_DESC; i++) {
    547   1.2     matt 		if (sc->sc_txstate[i].txs_dmamap != NULL)
    548   1.2     matt 			bus_dmamap_destroy(sc->sc_dmat,
    549  1.10      ryo 			    sc->sc_txstate[i].txs_dmamap);
    550   1.2     matt 	}
    551   1.2     matt 	bus_dmamap_unload(sc->sc_dmat, sc->sc_pdmamap);
    552   1.2     matt  fail_4:
    553   1.2     matt 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_pdmamap);
    554   1.2     matt  fail_3:
    555   1.2     matt 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_descs,
    556  1.10      ryo 	    sizeof(struct ralink_descs));
    557   1.2     matt  fail_2:
    558   1.2     matt 	bus_dmamem_free(sc->sc_dmat, &sc->sc_dseg, sc->sc_ndseg);
    559   1.2     matt  fail_1:
    560   1.2     matt 	bus_space_unmap(sc->sc_memt, sc->sc_sw_memh, sc->sc_sw_size);
    561   1.2     matt  fail_0c:
    562   1.2     matt 	bus_space_unmap(sc->sc_memt, sc->sc_fe_memh, sc->sc_fe_size);
    563   1.2     matt  fail_0b:
    564   1.2     matt 	bus_space_unmap(sc->sc_memt, sc->sc_sy_memh, sc->sc_fe_size);
    565   1.2     matt  fail_0a:
    566   1.2     matt 	return;
    567   1.2     matt }
    568   1.2     matt 
    569   1.2     matt /*
    570   1.2     matt  * ralink_eth_activate:
    571   1.2     matt  *
    572   1.2     matt  *	Handle device activation/deactivation requests.
    573   1.2     matt  */
    574   1.2     matt int
    575   1.2     matt ralink_eth_activate(device_t self, enum devact act)
    576   1.2     matt {
    577   1.2     matt 	ralink_eth_softc_t * const sc = device_private(self);
    578   1.2     matt 	int error = 0;
    579   1.2     matt 	int s;
    580   1.2     matt 
    581   1.2     matt 	s = splnet();
    582   1.2     matt 	switch (act) {
    583   1.2     matt 	case DVACT_DEACTIVATE:
    584   1.2     matt 		if_deactivate(&sc->sc_ethercom.ec_if);
    585   1.2     matt 		break;
    586   1.2     matt 	}
    587   1.2     matt 	splx(s);
    588   1.2     matt 
    589   1.2     matt 	return error;
    590   1.2     matt }
    591   1.2     matt 
    592   1.2     matt /*
    593   1.2     matt  * ralink_eth_partition_enable
    594   1.2     matt  */
    595   1.2     matt static int
    596   1.2     matt ralink_eth_enable(ralink_eth_softc_t *sc)
    597   1.2     matt {
    598   1.2     matt 	RALINK_DEBUG_FUNC_ENTRY();
    599   1.2     matt 
    600   1.2     matt 	if (sc->sc_ih != NULL) {
    601   1.2     matt 		RALINK_DEBUG(RALINK_DEBUG_MISC, "%s() already active",
    602   1.2     matt 			__func__);
    603   1.2     matt 		return EALREADY;
    604   1.2     matt 	}
    605   1.2     matt 
    606   1.2     matt 	sc->sc_pending_tx = 0;
    607   1.2     matt 
    608   1.2     matt 	int s = splnet();
    609   1.2     matt 	ralink_eth_hw_init(sc);
    610   1.2     matt 	sc->sc_ih = ra_intr_establish(RA_IRQ_FENGINE,
    611  1.10      ryo 	    ralink_eth_intr, sc, 1);
    612   1.2     matt 	splx(s);
    613   1.2     matt 	if (sc->sc_ih == NULL) {
    614   1.2     matt 		RALINK_DEBUG(RALINK_DEBUG_ERROR,
    615  1.10      ryo 		    "%s: unable to establish interrupt\n",
    616  1.10      ryo 		    device_xname(sc->sc_dev));
    617   1.2     matt 		return EIO;
    618   1.2     matt 	}
    619   1.2     matt 
    620   1.2     matt 	return 0;
    621   1.2     matt }
    622   1.2     matt 
    623   1.2     matt /*
    624   1.2     matt  * ralink_eth_partition_disable
    625   1.2     matt  */
    626   1.2     matt static void
    627   1.2     matt ralink_eth_disable(ralink_eth_softc_t *sc)
    628   1.2     matt {
    629   1.2     matt 	RALINK_DEBUG_FUNC_ENTRY();
    630   1.2     matt 
    631   1.2     matt 	int s = splnet();
    632   1.2     matt 	ralink_eth_rxdrain(sc);
    633   1.2     matt 	ra_intr_disestablish(sc->sc_ih);
    634   1.2     matt 	sc->sc_ih = NULL;
    635   1.2     matt 
    636   1.2     matt 	/* stop the mii ticker */
    637   1.2     matt 	callout_stop(&sc->sc_tick_callout);
    638   1.2     matt 
    639   1.2     matt 	/* quiesce the block */
    640   1.2     matt 	ralink_eth_reset(sc);
    641   1.2     matt 	splx(s);
    642   1.2     matt }
    643   1.2     matt 
    644   1.2     matt /*
    645   1.2     matt  * ralink_eth_detach
    646   1.2     matt  */
    647   1.2     matt static int
    648   1.2     matt ralink_eth_detach(device_t self, int flags)
    649   1.2     matt {
    650   1.2     matt 	RALINK_DEBUG_FUNC_ENTRY();
    651   1.2     matt 	ralink_eth_softc_t * const sc = device_private(self);
    652   1.2     matt 	struct ifnet * const ifp = &sc->sc_ethercom.ec_if;
    653   1.2     matt 	struct ralink_eth_rxstate *rxs;
    654   1.2     matt 	struct ralink_eth_txstate *txs;
    655   1.2     matt 	int i;
    656   1.2     matt 
    657   1.2     matt 	ralink_eth_disable(sc);
    658   1.2     matt 	mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    659   1.2     matt 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
    660   1.2     matt 	ether_ifdetach(ifp);
    661   1.2     matt 	if_detach(ifp);
    662   1.2     matt 
    663   1.2     matt 	for (i = 0; i < RALINK_ETH_NUM_RX_DESC; i++) {
    664   1.2     matt 		rxs = &sc->sc_rxstate[i];
    665   1.2     matt 		if (rxs->rxs_mbuf != NULL) {
    666   1.2     matt 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
    667   1.2     matt 			m_freem(rxs->rxs_mbuf);
    668   1.2     matt 			rxs->rxs_mbuf = NULL;
    669   1.2     matt 		}
    670   1.2     matt 		bus_dmamap_destroy(sc->sc_dmat, rxs->rxs_dmamap);
    671   1.2     matt 	}
    672   1.2     matt 
    673   1.2     matt 	for (i = 0; i < RALINK_ETH_NUM_TX_DESC; i++) {
    674   1.2     matt 		txs = &sc->sc_txstate[i];
    675   1.2     matt 		if (txs->txs_mbuf != NULL) {
    676   1.2     matt 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
    677   1.2     matt 			m_freem(txs->txs_mbuf);
    678   1.2     matt 			txs->txs_mbuf = NULL;
    679   1.2     matt 		}
    680   1.2     matt 		bus_dmamap_destroy(sc->sc_dmat, txs->txs_dmamap);
    681   1.2     matt 	}
    682   1.2     matt 
    683   1.2     matt 	bus_dmamap_unload(sc->sc_dmat, sc->sc_pdmamap);
    684   1.2     matt 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_pdmamap);
    685   1.2     matt 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_descs,
    686  1.10      ryo 	    sizeof(struct ralink_descs));
    687   1.2     matt 	bus_dmamem_free(sc->sc_dmat, &sc->sc_dseg, sc->sc_ndseg);
    688   1.2     matt 
    689   1.2     matt 	bus_space_unmap(sc->sc_memt, sc->sc_sw_memh, sc->sc_sw_size);
    690   1.2     matt 	bus_space_unmap(sc->sc_memt, sc->sc_fe_memh, sc->sc_fe_size);
    691   1.2     matt 
    692   1.2     matt 	return 0;
    693   1.2     matt }
    694   1.2     matt 
    695   1.2     matt /*
    696   1.2     matt  * ralink_eth_reset
    697   1.2     matt  */
    698   1.2     matt static void
    699   1.2     matt ralink_eth_reset(ralink_eth_softc_t *sc)
    700   1.2     matt {
    701   1.2     matt 	RALINK_DEBUG_FUNC_ENTRY();
    702   1.2     matt 	uint32_t r;
    703   1.2     matt 
    704   1.2     matt 	/* Reset the frame engine */
    705   1.2     matt 	r = sy_read(sc, RA_SYSCTL_RST);
    706   1.2     matt 	r |= RST_FE;
    707   1.2     matt 	sy_write(sc, RA_SYSCTL_RST, r);
    708   1.2     matt 	r ^= RST_FE;
    709   1.2     matt 	sy_write(sc, RA_SYSCTL_RST, r);
    710   1.2     matt 
    711  1.11      ryo 	/* Wait until the PDMA is quiescent */
    712   1.2     matt 	for (;;) {
    713   1.2     matt 		r = fe_read(sc, RA_FE_PDMA_GLOBAL_CFG);
    714   1.2     matt 		if (r & FE_PDMA_GLOBAL_CFG_RX_DMA_BUSY) {
    715   1.2     matt 			aprint_normal_dev(sc->sc_dev, "RX DMA BUSY\n");
    716   1.2     matt 			continue;
    717   1.2     matt 		}
    718   1.2     matt 		if (r & FE_PDMA_GLOBAL_CFG_TX_DMA_BUSY) {
    719   1.2     matt 			aprint_normal_dev(sc->sc_dev, "TX DMA BUSY\n");
    720   1.2     matt 			continue;
    721   1.2     matt 		}
    722   1.2     matt 		break;
    723   1.2     matt 	}
    724   1.2     matt }
    725   1.2     matt 
    726   1.2     matt /*
    727   1.2     matt  * ralink_eth_hw_init
    728   1.2     matt  */
    729   1.2     matt static void
    730   1.2     matt ralink_eth_hw_init(ralink_eth_softc_t *sc)
    731   1.2     matt {
    732   1.2     matt 	RALINK_DEBUG_FUNC_ENTRY();
    733   1.2     matt 	struct ralink_eth_txstate *txs;
    734   1.2     matt 	uint32_t r;
    735   1.2     matt 	int i;
    736   1.2     matt 
    737   1.2     matt 	/* reset to a known good state */
    738   1.2     matt 	ralink_eth_reset(sc);
    739   1.2     matt 
    740  1.11      ryo #if defined(RT3050) || defined(RT3052) || defined(MT7628)
    741   1.2     matt 	/* Bring the switch to a sane default state (from linux driver) */
    742   1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sw_memh, RA_ETH_SW_SGC2,
    743  1.10      ryo 	    0x00000000);
    744   1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sw_memh, RA_ETH_SW_PFC1,
    745  1.10      ryo 	    0x00405555);	/* check VLAN tag on port forward */
    746   1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sw_memh, RA_ETH_SW_VLANI0,
    747  1.10      ryo 	    0x00002001);
    748   1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sw_memh, RA_ETH_SW_PVIDC0,
    749  1.10      ryo 	    0x00001002);
    750   1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sw_memh, RA_ETH_SW_PVIDC1,
    751  1.10      ryo 	    0x00001001);
    752   1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sw_memh, RA_ETH_SW_PVIDC2,
    753  1.10      ryo 	    0x00001001);
    754  1.11      ryo #if defined(MT7628)
    755  1.11      ryo 	bus_space_write_4(sc->sc_memt, sc->sc_sw_memh, RA_ETH_SW_VMSC0,
    756  1.11      ryo 	    0xffffffff);
    757  1.11      ryo 	bus_space_write_4(sc->sc_memt, sc->sc_sw_memh, RA_ETH_SW_POC0,
    758  1.11      ryo 	    0x10007f7f);
    759  1.11      ryo 	bus_space_write_4(sc->sc_memt, sc->sc_sw_memh, RA_ETH_SW_POC2,
    760  1.11      ryo 	    0x00007f7f);
    761  1.11      ryo 	bus_space_write_4(sc->sc_memt, sc->sc_sw_memh, RA_ETH_SW_FTC2,
    762  1.11      ryo 	    0x0002500c);
    763  1.11      ryo #else
    764   1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sw_memh, RA_ETH_SW_VMSC0,
    765  1.10      ryo 	    0xffff417e);
    766   1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sw_memh, RA_ETH_SW_POC0,
    767  1.10      ryo 	    0x00007f7f);
    768   1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sw_memh, RA_ETH_SW_POC2,
    769  1.10      ryo 	    0x00007f3f);
    770   1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sw_memh, RA_ETH_SW_FTC2,
    771  1.10      ryo 	    0x00d6500c);
    772  1.11      ryo #endif
    773   1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sw_memh, RA_ETH_SW_SWGC,
    774  1.10      ryo 	    0x0008a301);	/* hashing algorithm=XOR48 */
    775   1.2     matt 				/*  aging interval=300sec  */
    776   1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sw_memh, RA_ETH_SW_SOCPC,
    777  1.10      ryo 	    0x02404040);
    778   1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sw_memh, RA_ETH_SW_FPORT,
    779  1.10      ryo 	    0x3f502b28);	/* Change polling Ext PHY Addr=0x0 */
    780   1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sw_memh, RA_ETH_SW_FPA,
    781  1.10      ryo 	    0x00000000);
    782   1.2     matt 
    783   1.2     matt 	/* do some mii magic  TODO: define these registers/bits */
    784   1.2     matt 	/* lower down PHY 10Mbps mode power */
    785   1.2     matt 	/* select local register */
    786   1.5      oki 	ralink_eth_mii_write(sc->sc_dev, 0, 31, 0x8000);
    787   1.2     matt 
    788  1.10      ryo 	for (i=0; i < 5; i++) {
    789   1.2     matt 		/* set TX10 waveform coefficient */
    790   1.5      oki 		ralink_eth_mii_write(sc->sc_dev, i, 26, 0x1601);
    791   1.2     matt 
    792   1.2     matt 		/* set TX100/TX10 AD/DA current bias */
    793   1.5      oki 		ralink_eth_mii_write(sc->sc_dev, i, 29, 0x7058);
    794   1.2     matt 
    795   1.2     matt 		/* set TX100 slew rate control */
    796   1.5      oki 		ralink_eth_mii_write(sc->sc_dev, i, 30, 0x0018);
    797   1.2     matt 	}
    798   1.2     matt 
    799   1.2     matt 	/* PHY IOT */
    800   1.2     matt 
    801   1.2     matt 	/* select global register */
    802   1.5      oki 	ralink_eth_mii_write(sc->sc_dev, 0, 31, 0x0);
    803   1.2     matt 
    804   1.2     matt 	/* tune TP_IDL tail and head waveform */
    805   1.5      oki 	ralink_eth_mii_write(sc->sc_dev, 0, 22, 0x052f);
    806   1.2     matt 
    807   1.2     matt 	/* set TX10 signal amplitude threshold to minimum */
    808   1.5      oki 	ralink_eth_mii_write(sc->sc_dev, 0, 17, 0x0fe0);
    809   1.2     matt 
    810   1.2     matt 	/* set squelch amplitude to higher threshold */
    811   1.5      oki 	ralink_eth_mii_write(sc->sc_dev, 0, 18, 0x40ba);
    812   1.2     matt 
    813   1.2     matt 	/* longer TP_IDL tail length */
    814   1.5      oki 	ralink_eth_mii_write(sc->sc_dev, 0, 14, 0x65);
    815   1.2     matt 
    816   1.2     matt 	/* select local register */
    817   1.5      oki 	ralink_eth_mii_write(sc->sc_dev, 0, 31, 0x8000);
    818   1.2     matt #else
    819   1.2     matt 	/* GE1 + GigSW */
    820   1.2     matt 	fe_write(sc, RA_FE_MDIO_CFG1,
    821  1.10      ryo 	    MDIO_CFG_PHY_ADDR(0x1f) |
    822  1.10      ryo 	    MDIO_CFG_BP_EN |
    823  1.10      ryo 	    MDIO_CFG_FORCE_CFG |
    824  1.10      ryo 	    MDIO_CFG_SPEED(MDIO_CFG_SPEED_1000M) |
    825  1.10      ryo 	    MDIO_CFG_FULL_DUPLEX |
    826  1.10      ryo 	    MDIO_CFG_FC_TX |
    827  1.10      ryo 	    MDIO_CFG_FC_RX |
    828  1.10      ryo 	    MDIO_CFG_TX_CLK_MODE(MDIO_CFG_TX_CLK_MODE_3COM));
    829   1.2     matt #endif
    830   1.2     matt 
    831   1.2     matt 	/*
    832   1.2     matt 	 * TODO: QOS - RT3052 has 4 TX queues for QOS,
    833   1.2     matt 	 * forgoing for 1 for simplicity
    834   1.2     matt 	 */
    835   1.2     matt 
    836   1.2     matt 	/*
    837   1.2     matt 	 * Allocate DMA accessible memory for TX/RX descriptor rings
    838   1.2     matt 	 */
    839   1.2     matt 
    840   1.2     matt 	/* Initialize the TX queues. */
    841   1.2     matt 	SIMPLEQ_INIT(&sc->sc_txfreeq);
    842   1.2     matt 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
    843   1.2     matt 
    844   1.2     matt 	/* Initialize the TX descriptor ring. */
    845   1.2     matt 	memset(sc->sc_txdesc, 0, sizeof(sc->sc_txdesc));
    846   1.2     matt 	for (i = 0; i < RALINK_ETH_NUM_TX_DESC; i++) {
    847   1.2     matt 
    848   1.2     matt 		sc->sc_txdesc[i].txd_info1 = TXD_LAST0 | TXD_DDONE;
    849   1.2     matt 
    850   1.2     matt 		/* setup the freeq as well */
    851   1.2     matt 		txs = &sc->sc_txstate[i];
    852   1.2     matt 		txs->txs_mbuf = NULL;
    853   1.2     matt 		txs->txs_idx = i;
    854   1.2     matt 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
    855   1.2     matt 	}
    856   1.2     matt 
    857   1.2     matt 	/*
    858   1.2     matt 	 * Flush the TX descriptors
    859   1.2     matt 	 *  - TODO: can we just access descriptors via KSEG1
    860   1.2     matt 	 *    to avoid the flush?
    861   1.2     matt 	 */
    862   1.2     matt 	bus_dmamap_sync(sc->sc_dmat, sc->sc_pdmamap,
    863  1.10      ryo 	    (int)&sc->sc_txdesc - (int)sc->sc_descs, sizeof(sc->sc_txdesc),
    864  1.17  msaitoh 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    865   1.2     matt 
    866   1.2     matt 	/* Initialize the RX descriptor ring */
    867   1.2     matt 	memset(sc->sc_rxdesc, 0, sizeof(sc->sc_rxdesc));
    868   1.2     matt 	for (i = 0; i < RALINK_ETH_NUM_RX_DESC; i++) {
    869   1.2     matt 		if (ralink_eth_add_rxbuf(sc, i)) {
    870   1.2     matt 			panic("Can't allocate rx mbuf\n");
    871   1.2     matt 		}
    872   1.2     matt 	}
    873   1.2     matt 
    874   1.2     matt 	/*
    875   1.2     matt 	 * Flush the RX descriptors
    876   1.2     matt 	 * - TODO: can we just access descriptors via KSEG1
    877   1.2     matt 	 *   to avoid the flush?
    878   1.2     matt 	 */
    879   1.2     matt 	bus_dmamap_sync(sc->sc_dmat, sc->sc_pdmamap,
    880  1.10      ryo 	    (int)&sc->sc_rxdesc - (int)sc->sc_descs, sizeof(sc->sc_rxdesc),
    881  1.17  msaitoh 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    882   1.2     matt 
    883   1.2     matt 	/* Clear the PDMA state */
    884   1.2     matt 	r = fe_read(sc, RA_FE_PDMA_GLOBAL_CFG);
    885   1.2     matt 	r &= 0xff;
    886   1.2     matt 	fe_write(sc, RA_FE_PDMA_GLOBAL_CFG, r);
    887   1.2     matt 	(void) fe_read(sc, RA_FE_PDMA_GLOBAL_CFG);
    888   1.2     matt 
    889  1.11      ryo #if !defined(MT7628)
    890   1.2     matt 	/* Setup the PDMA VLAN ID's */
    891   1.2     matt 	fe_write(sc, RA_FE_VLAN_ID_0001, 0x00010000);
    892   1.2     matt 	fe_write(sc, RA_FE_VLAN_ID_0203, 0x00030002);
    893   1.2     matt 	fe_write(sc, RA_FE_VLAN_ID_0405, 0x00050004);
    894   1.2     matt 	fe_write(sc, RA_FE_VLAN_ID_0607, 0x00070006);
    895   1.2     matt 	fe_write(sc, RA_FE_VLAN_ID_0809, 0x00090008);
    896   1.2     matt 	fe_write(sc, RA_FE_VLAN_ID_1011, 0x000b000a);
    897   1.2     matt 	fe_write(sc, RA_FE_VLAN_ID_1213, 0x000d000c);
    898   1.2     matt 	fe_write(sc, RA_FE_VLAN_ID_1415, 0x000f000e);
    899  1.11      ryo #endif
    900   1.2     matt 
    901   1.2     matt 	/* Give the TX and TX rings to the chip. */
    902   1.2     matt 	fe_write(sc, RA_FE_PDMA_TX0_PTR,
    903  1.10      ryo 	    htole32(MIPS_KSEG0_TO_PHYS(&sc->sc_txdesc)));
    904   1.2     matt 	fe_write(sc, RA_FE_PDMA_TX0_COUNT, htole32(RALINK_ETH_NUM_TX_DESC));
    905   1.2     matt 	fe_write(sc, RA_FE_PDMA_TX0_CPU_IDX, 0);
    906  1.11      ryo #if !defined(MT7628)
    907   1.2     matt 	fe_write(sc, RA_FE_PDMA_RESET_IDX, PDMA_RST_TX0);
    908  1.11      ryo #endif
    909   1.2     matt 
    910   1.2     matt 	fe_write(sc, RA_FE_PDMA_RX0_PTR,
    911  1.10      ryo 	    htole32(MIPS_KSEG0_TO_PHYS(&sc->sc_rxdesc)));
    912   1.2     matt 	fe_write(sc, RA_FE_PDMA_RX0_COUNT, htole32(RALINK_ETH_NUM_RX_DESC));
    913   1.2     matt 	fe_write(sc, RA_FE_PDMA_RX0_CPU_IDX,
    914  1.10      ryo 	    htole32(RALINK_ETH_NUM_RX_DESC - 1));
    915  1.11      ryo #if !defined(MT7628)
    916   1.2     matt 	fe_write(sc, RA_FE_PDMA_RESET_IDX, PDMA_RST_RX0);
    917  1.11      ryo #endif
    918   1.2     matt 	fe_write(sc, RA_FE_PDMA_RX0_CPU_IDX,
    919  1.10      ryo 	    htole32(RALINK_ETH_NUM_RX_DESC - 1));
    920   1.2     matt 
    921   1.2     matt 	/* Start PDMA */
    922   1.2     matt 	fe_write(sc, RA_FE_PDMA_GLOBAL_CFG,
    923  1.10      ryo 	    FE_PDMA_GLOBAL_CFG_TX_WB_DDONE |
    924  1.10      ryo 	    FE_PDMA_GLOBAL_CFG_RX_DMA_EN |
    925  1.10      ryo 	    FE_PDMA_GLOBAL_CFG_TX_DMA_EN |
    926  1.10      ryo 	    FE_PDMA_GLOBAL_CFG_BURST_SZ_4);
    927   1.2     matt 
    928   1.2     matt 	/* Setup the clock for the Frame Engine */
    929  1.11      ryo #if defined(MT7628)
    930  1.11      ryo 	fe_write(sc, RA_FE_SDM_CON, 0x8100);
    931  1.11      ryo #else
    932   1.2     matt 	fe_write(sc, RA_FE_GLOBAL_CFG,
    933  1.10      ryo 	    FE_GLOBAL_CFG_EXT_VLAN(0x8100) |
    934  1.10      ryo 	    FE_GLOBAL_CFG_US_CLK(RA_BUS_FREQ / 1000000) |
    935  1.10      ryo 	    FE_GLOBAL_CFG_L2_SPACE(0x8));
    936  1.11      ryo #endif
    937   1.2     matt 
    938   1.2     matt 	/* Turn on all interrupts */
    939  1.11      ryo #if defined(MT7628)
    940  1.11      ryo 	fe_write(sc, RA_FE_INT_MASK,
    941  1.11      ryo 	    RA_FE_INT_RX_DONE_INT1 |
    942  1.11      ryo 	    RA_FE_INT_RX_DONE_INT0 |
    943  1.11      ryo 	    RA_FE_INT_TX_DONE_INT3 |
    944  1.11      ryo 	    RA_FE_INT_TX_DONE_INT2 |
    945  1.11      ryo 	    RA_FE_INT_TX_DONE_INT1 |
    946  1.11      ryo 	    RA_FE_INT_TX_DONE_INT0);
    947  1.11      ryo #else
    948   1.2     matt 	fe_write(sc, RA_FE_INT_ENABLE,
    949  1.10      ryo 	    FE_INT_RX | FE_INT_TX3 | FE_INT_TX2 | FE_INT_TX1 | FE_INT_TX0);
    950  1.11      ryo #endif
    951   1.2     matt 
    952   1.2     matt 	/*
    953   1.2     matt 	 * Configure GDMA forwarding
    954   1.2     matt 	 * - default all packets to CPU
    955   1.2     matt 	 * - Turn on auto-CRC
    956   1.2     matt 	 */
    957   1.2     matt #if 0
    958   1.2     matt 	fe_write(sc, RA_FE_GDMA1_FWD_CFG,
    959  1.10      ryo 	    (FE_GDMA_FWD_CFG_DIS_TX_CRC | FE_GDMA_FWD_CFG_DIS_TX_PAD));
    960   1.2     matt #endif
    961  1.11      ryo 
    962  1.11      ryo #if !defined(MT7628)
    963   1.2     matt 	fe_write(sc, RA_FE_GDMA1_FWD_CFG,
    964  1.10      ryo 	    FE_GDMA_FWD_CFG_JUMBO_LEN(MCLBYTES/1024) |
    965  1.10      ryo 	    FE_GDMA_FWD_CFG_STRIP_RX_CRC |
    966  1.10      ryo 	    FE_GDMA_FWD_CFG_IP4_CRC_EN |
    967  1.10      ryo 	    FE_GDMA_FWD_CFG_TCP_CRC_EN |
    968  1.10      ryo 	    FE_GDMA_FWD_CFG_UDP_CRC_EN);
    969  1.11      ryo #endif
    970   1.2     matt 
    971   1.2     matt 	/* CDMA also needs CRCs turned on */
    972  1.11      ryo #if !defined(MT7628)
    973   1.2     matt 	r = fe_read(sc, RA_FE_CDMA_CSG_CFG);
    974   1.2     matt 	r |= (FE_CDMA_CSG_CFG_IP4_CRC_EN | FE_CDMA_CSG_CFG_UDP_CRC_EN |
    975  1.10      ryo 	    FE_CDMA_CSG_CFG_TCP_CRC_EN);
    976   1.2     matt 	fe_write(sc, RA_FE_CDMA_CSG_CFG, r);
    977  1.11      ryo #endif
    978   1.2     matt 
    979   1.2     matt 	/* Configure Flow Control Thresholds */
    980  1.11      ryo #if defined(MT7628)
    981  1.11      ryo 	sw_write(sc, RA_ETH_SW_FCT0,
    982  1.11      ryo 	    RA_ETH_SW_FCT0_FC_RLS_TH(0xc8) |
    983  1.11      ryo 	    RA_ETH_SW_FCT0_FC_SET_TH(0xa0) |
    984  1.11      ryo 	    RA_ETH_SW_FCT0_DROP_RLS_TH(0x78) |
    985  1.11      ryo 	    RA_ETH_SW_FCT0_DROP_SET_TH(0x50));
    986  1.11      ryo 	sw_write(sc, RA_ETH_SW_FCT1,
    987  1.11      ryo 	    RA_ETH_SW_FCT1_PORT_TH(0x14));
    988  1.11      ryo #elif defined(RT3883)
    989   1.2     matt 	fe_write(sc, RA_FE_PSE_FQ_CFG,
    990  1.10      ryo 	    FE_PSE_FQ_MAX_COUNT(0xff) |
    991  1.10      ryo 	    FE_PSE_FQ_FC_RELEASE(0x90) |
    992  1.10      ryo 	    FE_PSE_FQ_FC_ASSERT(0x80));
    993   1.2     matt #else
    994   1.2     matt 	fe_write(sc, RA_FE_PSE_FQ_CFG,
    995  1.10      ryo 	    FE_PSE_FQ_MAX_COUNT(0x80) |
    996  1.10      ryo 	    FE_PSE_FQ_FC_RELEASE(0x50) |
    997  1.10      ryo 	    FE_PSE_FQ_FC_ASSERT(0x40));
    998   1.2     matt #endif
    999   1.2     matt 
   1000   1.2     matt #ifdef RALINK_ETH_DEBUG
   1001  1.11      ryo #ifdef RA_FE_MDIO_CFG1
   1002   1.2     matt 	printf("FE_MDIO_CFG1: 0x%08x\n", fe_read(sc, RA_FE_MDIO_CFG1));
   1003  1.11      ryo #endif
   1004  1.11      ryo #ifdef RA_FE_MDIO_CFG2
   1005   1.2     matt 	printf("FE_MDIO_CFG2: 0x%08x\n", fe_read(sc, RA_FE_MDIO_CFG2));
   1006  1.11      ryo #endif
   1007   1.2     matt 	printf("FE_PDMA_TX0_PTR: %08x\n", fe_read(sc, RA_FE_PDMA_TX0_PTR));
   1008   1.2     matt 	printf("FE_PDMA_TX0_COUNT: %08x\n",
   1009  1.10      ryo 	    fe_read(sc, RA_FE_PDMA_TX0_COUNT));
   1010   1.2     matt 	printf("FE_PDMA_TX0_CPU_IDX: %08x\n",
   1011  1.10      ryo 	    fe_read(sc, RA_FE_PDMA_TX0_CPU_IDX));
   1012   1.2     matt 	printf("FE_PDMA_TX0_DMA_IDX: %08x\n",
   1013  1.10      ryo 	    fe_read(sc, RA_FE_PDMA_TX0_DMA_IDX));
   1014   1.2     matt 	printf("FE_PDMA_RX0_PTR: %08x\n", fe_read(sc, RA_FE_PDMA_RX0_PTR));
   1015   1.2     matt 	printf("FE_PDMA_RX0_COUNT: %08x\n",
   1016  1.10      ryo 	    fe_read(sc, RA_FE_PDMA_RX0_COUNT));
   1017   1.2     matt 	printf("FE_PDMA_RX0_CPU_IDX: %08x\n",
   1018  1.10      ryo 	    fe_read(sc, RA_FE_PDMA_RX0_CPU_IDX));
   1019   1.2     matt 	printf("FE_PDMA_RX0_DMA_IDX: %08x\n",
   1020  1.10      ryo 	    fe_read(sc, RA_FE_PDMA_RX0_DMA_IDX));
   1021   1.2     matt 	printf("FE_PDMA_GLOBAL_CFG: %08x\n",
   1022  1.10      ryo 	    fe_read(sc, RA_FE_PDMA_GLOBAL_CFG));
   1023  1.11      ryo #ifdef RA_FE_GLOBAL_CFG
   1024   1.2     matt 	printf("FE_GLOBAL_CFG: %08x\n", fe_read(sc, RA_FE_GLOBAL_CFG));
   1025  1.11      ryo #endif
   1026  1.11      ryo #ifdef RA_FE_GDMA1_FWD_CFG
   1027   1.2     matt 	printf("FE_GDMA1_FWD_CFG: %08x\n",
   1028  1.10      ryo 	    fe_read(sc, RA_FE_GDMA1_FWD_CFG));
   1029  1.11      ryo #endif
   1030  1.11      ryo #ifdef RA_FE_CDMA_CSG_CFG
   1031   1.2     matt 	printf("FE_CDMA_CSG_CFG: %08x\n", fe_read(sc, RA_FE_CDMA_CSG_CFG));
   1032  1.11      ryo #endif
   1033  1.11      ryo #ifdef RA_FE_PSE_FQ_CFG
   1034   1.2     matt 	printf("FE_PSE_FQ_CFG: %08x\n", fe_read(sc, RA_FE_PSE_FQ_CFG));
   1035   1.2     matt #endif
   1036  1.11      ryo #endif
   1037   1.2     matt 
   1038   1.2     matt 	/* Force PSE Reset to get everything finalized */
   1039  1.11      ryo #if defined(MT7628)
   1040  1.11      ryo #else
   1041   1.2     matt 	fe_write(sc, RA_FE_GLOBAL_RESET, FE_GLOBAL_RESET_PSE);
   1042   1.2     matt 	fe_write(sc, RA_FE_GLOBAL_RESET, 0);
   1043  1.11      ryo #endif
   1044   1.2     matt }
   1045   1.2     matt 
   1046   1.2     matt /*
   1047   1.2     matt  * ralink_eth_init
   1048   1.2     matt  */
   1049   1.2     matt static int
   1050   1.2     matt ralink_eth_init(struct ifnet *ifp)
   1051   1.2     matt {
   1052   1.2     matt 	RALINK_DEBUG_FUNC_ENTRY();
   1053   1.2     matt 	ralink_eth_softc_t * const sc = ifp->if_softc;
   1054   1.2     matt 	int error;
   1055   1.2     matt 
   1056   1.2     matt 	error = ralink_eth_enable(sc);
   1057   1.2     matt 	if (!error) {
   1058   1.2     matt 		/* Note that the interface is now running. */
   1059   1.2     matt 		ifp->if_flags |= IFF_RUNNING;
   1060   1.2     matt 		ifp->if_flags &= ~IFF_OACTIVE;
   1061   1.2     matt 	}
   1062   1.2     matt 
   1063   1.2     matt 	return error;
   1064   1.2     matt }
   1065   1.2     matt 
   1066   1.2     matt /*
   1067   1.2     matt  * ralink_eth_rxdrain
   1068   1.2     matt  *
   1069   1.2     matt  *  Drain the receive queue.
   1070   1.2     matt  */
   1071   1.2     matt static void
   1072   1.2     matt ralink_eth_rxdrain(ralink_eth_softc_t *sc)
   1073   1.2     matt {
   1074   1.2     matt 	RALINK_DEBUG_FUNC_ENTRY();
   1075   1.2     matt 
   1076   1.2     matt 	for (int i = 0; i < RALINK_ETH_NUM_RX_DESC; i++) {
   1077   1.2     matt 		struct ralink_eth_rxstate *rxs = &sc->sc_rxstate[i];
   1078   1.2     matt 		if (rxs->rxs_mbuf != NULL) {
   1079   1.2     matt 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1080   1.2     matt 			m_freem(rxs->rxs_mbuf);
   1081   1.2     matt 			rxs->rxs_mbuf = NULL;
   1082   1.2     matt 		}
   1083   1.2     matt 	}
   1084   1.2     matt }
   1085   1.2     matt 
   1086   1.2     matt /*
   1087   1.2     matt  * ralink_eth_stop
   1088   1.2     matt  */
   1089   1.2     matt static void
   1090   1.2     matt ralink_eth_stop(struct ifnet *ifp, int disable)
   1091   1.2     matt {
   1092   1.2     matt 	RALINK_DEBUG_FUNC_ENTRY();
   1093   1.2     matt 	ralink_eth_softc_t * const sc = ifp->if_softc;
   1094   1.2     matt 
   1095   1.2     matt 	ralink_eth_disable(sc);
   1096   1.2     matt 
   1097   1.2     matt 	/* Mark the interface down and cancel the watchdog timer.  */
   1098   1.2     matt 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1099   1.2     matt 	ifp->if_timer = 0;
   1100   1.2     matt }
   1101   1.2     matt 
   1102   1.2     matt /*
   1103   1.2     matt  * ralink_eth_add_rxbuf
   1104   1.2     matt  */
   1105   1.2     matt static int
   1106   1.2     matt ralink_eth_add_rxbuf(ralink_eth_softc_t *sc, int idx)
   1107   1.2     matt {
   1108   1.2     matt 	RALINK_DEBUG_FUNC_ENTRY();
   1109   1.2     matt 	struct ralink_eth_rxstate * const rxs = &sc->sc_rxstate[idx];
   1110   1.2     matt 	struct mbuf *m;
   1111   1.2     matt 	int error;
   1112   1.2     matt 
   1113   1.2     matt 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1114   1.2     matt 	if (m == NULL) {
   1115   1.2     matt 		printf("MGETHDR failed\n");
   1116   1.2     matt 		sc->sc_evcnt_add_rxbuf_hdr_fail.ev_count++;
   1117   1.2     matt 		return ENOBUFS;
   1118   1.2     matt 	}
   1119   1.2     matt 
   1120   1.2     matt 	MCLGET(m, M_DONTWAIT);
   1121   1.2     matt 	if ((m->m_flags & M_EXT) == 0) {
   1122   1.2     matt 		m_freem(m);
   1123   1.2     matt 		printf("MCLGET failed\n");
   1124   1.2     matt 		sc->sc_evcnt_add_rxbuf_mcl_fail.ev_count++;
   1125   1.2     matt 		return ENOBUFS;
   1126   1.2     matt 	}
   1127   1.2     matt 
   1128   1.2     matt 	m->m_data = m->m_ext.ext_buf;
   1129   1.2     matt 	rxs->rxs_mbuf = m;
   1130   1.2     matt 
   1131   1.2     matt 	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap, m->m_ext.ext_buf,
   1132  1.17  msaitoh 	    m->m_ext.ext_size, NULL, BUS_DMA_READ | BUS_DMA_NOWAIT);
   1133   1.2     matt 	if (error) {
   1134   1.2     matt 		aprint_error_dev(sc->sc_dev, "can't load rx DMA map %d, "
   1135  1.10      ryo 		    "error=%d\n", idx, error);
   1136   1.2     matt 		panic(__func__);  /* XXX */
   1137   1.2     matt 	}
   1138   1.2     matt 
   1139   1.2     matt 	sc->sc_rxdesc[idx].data_ptr = MIPS_KSEG0_TO_PHYS(
   1140  1.10      ryo 	    rxs->rxs_dmamap->dm_segs[0].ds_addr + RALINK_ETHER_ALIGN);
   1141   1.2     matt 	sc->sc_rxdesc[idx].rxd_info1 = RXD_LAST0;
   1142   1.2     matt 
   1143   1.2     matt 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1144  1.10      ryo 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1145   1.2     matt 
   1146   1.2     matt 	return 0;
   1147   1.2     matt }
   1148   1.2     matt 
   1149   1.2     matt 
   1150   1.2     matt /*
   1151   1.2     matt  * ralink_eth_start
   1152   1.2     matt  */
   1153   1.2     matt static void
   1154   1.2     matt ralink_eth_start(struct ifnet *ifp)
   1155   1.2     matt {
   1156   1.2     matt 	RALINK_DEBUG_FUNC_ENTRY();
   1157   1.2     matt 	ralink_eth_softc_t * const sc = ifp->if_softc;
   1158   1.2     matt 	struct mbuf *m0, *m = NULL;
   1159   1.2     matt 	struct ralink_eth_txstate *txs;
   1160   1.2     matt 	bus_dmamap_t dmamap;
   1161   1.2     matt 	int tx_cpu_idx;
   1162   1.2     matt 	int error;
   1163   1.2     matt 	int s;
   1164   1.2     matt 
   1165  1.17  msaitoh 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   1166   1.2     matt 		return;
   1167   1.2     matt 
   1168   1.2     matt 	s = splnet();
   1169   1.2     matt 
   1170   1.2     matt 	tx_cpu_idx = fe_read(sc, RA_FE_PDMA_TX0_CPU_IDX);
   1171   1.2     matt 
   1172   1.2     matt 	/*
   1173   1.2     matt 	 * Loop through the send queue, setting up transmit descriptors
   1174   1.2     matt 	 * until we drain the queue, or use up all available
   1175   1.2     matt 	 * transmit descriptors.
   1176   1.2     matt 	 */
   1177   1.2     matt 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL) {
   1178   1.2     matt 		/* Grab a packet off the queue.  */
   1179   1.2     matt 		IFQ_POLL(&ifp->if_snd, m0);
   1180   1.2     matt 		if (m0 == NULL)
   1181   1.2     matt 			break;
   1182   1.2     matt 
   1183   1.2     matt 		dmamap = txs->txs_dmamap;
   1184   1.2     matt 
   1185   1.2     matt 		if (m0->m_pkthdr.len < RALINK_MIN_BUF) {
   1186   1.2     matt 			int padlen = 64 - m0->m_pkthdr.len;
   1187   1.2     matt 			m_copyback(m0, m0->m_pkthdr.len, padlen,
   1188  1.10      ryo 			    sc->ralink_zero_buf);
   1189   1.2     matt 			/* TODO : need some checking here */
   1190   1.2     matt 		}
   1191   1.2     matt 
   1192   1.2     matt 		/*
   1193   1.2     matt 		 * Do we need to align the buffer
   1194   1.2     matt 		 * or does the DMA map load fail?
   1195   1.2     matt 		 */
   1196   1.2     matt 		if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
   1197  1.17  msaitoh 		    BUS_DMA_WRITE | BUS_DMA_NOWAIT) != 0) {
   1198   1.2     matt 
   1199   1.2     matt 			/* Allocate a new mbuf for re-alignment */
   1200   1.2     matt 			MGETHDR(m, M_DONTWAIT, MT_DATA);
   1201   1.2     matt 			if (m == NULL) {
   1202   1.2     matt 				aprint_error_dev(sc->sc_dev,
   1203  1.10      ryo 				    "unable to allocate aligned Tx mbuf\n");
   1204   1.2     matt 				break;
   1205   1.2     matt 			}
   1206   1.2     matt 			MCLAIM(m, &sc->sc_ethercom.ec_tx_mowner);
   1207   1.2     matt 			if (m0->m_pkthdr.len > MHLEN) {
   1208   1.2     matt 				MCLGET(m, M_DONTWAIT);
   1209   1.2     matt 				if ((m->m_flags & M_EXT) == 0) {
   1210   1.2     matt 					aprint_error_dev(sc->sc_dev,
   1211  1.10      ryo 					    "unable to allocate Tx cluster\n");
   1212   1.2     matt 					m_freem(m);
   1213   1.2     matt 					break;
   1214   1.2     matt 				}
   1215   1.2     matt 			}
   1216   1.2     matt 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
   1217   1.2     matt 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
   1218   1.2     matt 			error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m,
   1219  1.17  msaitoh 			    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1220   1.2     matt 			if (error) {
   1221   1.2     matt 				aprint_error_dev(sc->sc_dev,
   1222  1.10      ryo 				    "unable to load Tx buffer error=%d\n",
   1223  1.10      ryo 				    error);
   1224   1.2     matt 				m_freem(m);
   1225   1.2     matt 				break;
   1226   1.2     matt 			}
   1227   1.2     matt 		}
   1228   1.2     matt 
   1229   1.2     matt 		IFQ_DEQUEUE(&ifp->if_snd, m0);
   1230   1.2     matt 		/* did we copy the buffer out already? */
   1231   1.2     matt 		if (m != NULL) {
   1232   1.2     matt 			m_freem(m0);
   1233   1.2     matt 			m0 = m;
   1234   1.2     matt 		}
   1235   1.2     matt 
   1236   1.2     matt 		/* Sync the DMA map. */
   1237   1.2     matt 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
   1238  1.10      ryo 		    BUS_DMASYNC_PREWRITE);
   1239   1.2     matt 
   1240   1.2     matt 		/* Initialize the transmit descriptor */
   1241   1.2     matt 		sc->sc_txdesc[tx_cpu_idx].data_ptr0 =
   1242  1.10      ryo 		    MIPS_KSEG0_TO_PHYS(dmamap->dm_segs[0].ds_addr);
   1243   1.2     matt 		sc->sc_txdesc[tx_cpu_idx].txd_info1 =
   1244  1.10      ryo 		    TXD_LEN0(dmamap->dm_segs[0].ds_len) | TXD_LAST0;
   1245   1.2     matt 		sc->sc_txdesc[tx_cpu_idx].txd_info2 =
   1246  1.10      ryo 		    TXD_QN(3) | TXD_PN(TXD_PN_GDMA1);
   1247   1.2     matt 		sc->sc_txdesc[tx_cpu_idx].txd_info2 = TXD_QN(3) |
   1248  1.10      ryo 		    TXD_PN(TXD_PN_GDMA1) | TXD_VEN |
   1249  1.10      ryo 		    // TXD_VIDX(pt->vlan_id) |
   1250  1.10      ryo 		    TXD_TCP_EN | TXD_UDP_EN | TXD_IP_EN;
   1251   1.2     matt 
   1252   1.2     matt 		RALINK_DEBUG(RALINK_DEBUG_REG,"+tx(%d) 0x%08x: 0x%08x\n",
   1253  1.10      ryo 		    tx_cpu_idx, (int)&sc->sc_txdesc[tx_cpu_idx].data_ptr0,
   1254  1.10      ryo 		    sc->sc_txdesc[tx_cpu_idx].data_ptr0);
   1255   1.2     matt 		RALINK_DEBUG(RALINK_DEBUG_REG,"+tx(%d) 0x%08x: 0x%08x\n",
   1256  1.10      ryo 		    tx_cpu_idx, (int)&sc->sc_txdesc[tx_cpu_idx].txd_info1,
   1257  1.10      ryo 		    sc->sc_txdesc[tx_cpu_idx].txd_info1);
   1258   1.2     matt 		RALINK_DEBUG(RALINK_DEBUG_REG,"+tx(%d) 0x%08x: 0x%08x\n",
   1259  1.10      ryo 		    tx_cpu_idx, (int)&sc->sc_txdesc[tx_cpu_idx].data_ptr1,
   1260  1.10      ryo 		    sc->sc_txdesc[tx_cpu_idx].data_ptr1);
   1261  1.10      ryo 		RALINK_DEBUG(RALINK_DEBUG_REG,"+tx(%d) 0x%08x: 0x%08x\n",
   1262  1.10      ryo 		    tx_cpu_idx, (int)&sc->sc_txdesc[tx_cpu_idx].txd_info2,
   1263  1.10      ryo 		    sc->sc_txdesc[tx_cpu_idx].txd_info2);
   1264   1.2     matt 
   1265   1.2     matt 		/* sync the descriptor we're using. */
   1266   1.2     matt 		bus_dmamap_sync(sc->sc_dmat, sc->sc_pdmamap,
   1267  1.10      ryo 		    (int)&sc->sc_txdesc[tx_cpu_idx] - (int)sc->sc_descs,
   1268  1.10      ryo 		    sizeof(struct ralink_tx_desc),
   1269  1.17  msaitoh 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1270   1.2     matt 
   1271   1.2     matt 		/*
   1272   1.2     matt 		 * Store a pointer to the packet so we can free it later,
   1273   1.2     matt 		 * and remember what txdirty will be once the packet is
   1274   1.2     matt 		 * done.
   1275   1.2     matt 		 */
   1276   1.2     matt 		txs->txs_mbuf = m0;
   1277   1.2     matt 		sc->sc_pending_tx++;
   1278   1.2     matt 		if (txs->txs_idx != tx_cpu_idx) {
   1279   1.2     matt 			panic("txs_idx doesn't match %d != %d\n",
   1280  1.10      ryo 			    txs->txs_idx, tx_cpu_idx);
   1281   1.2     matt 		}
   1282   1.2     matt 
   1283   1.2     matt 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
   1284   1.2     matt 		SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
   1285   1.2     matt 
   1286   1.2     matt 		/* Pass the packet to any BPF listeners. */
   1287  1.14  msaitoh 		bpf_mtap(ifp, m0, BPF_D_OUT);
   1288   1.2     matt 
   1289   1.2     matt 		/* Set a watchdog timer in case the chip flakes out. */
   1290   1.2     matt 		ifp->if_timer = 5;
   1291   1.2     matt 
   1292   1.2     matt 		tx_cpu_idx = (tx_cpu_idx + 1) % RALINK_ETH_NUM_TX_DESC;
   1293   1.2     matt 
   1294   1.2     matt 		/* Write back the tx_cpu_idx */
   1295   1.2     matt 		fe_write(sc, RA_FE_PDMA_TX0_CPU_IDX, tx_cpu_idx);
   1296   1.2     matt 	}
   1297   1.2     matt 
   1298   1.2     matt 	if (txs == NULL) {
   1299   1.2     matt 		/* No more slots left; notify upper layer. */
   1300   1.2     matt 		ifp->if_flags |= IFF_OACTIVE;
   1301   1.2     matt 	}
   1302   1.2     matt 
   1303   1.2     matt 	splx(s);
   1304   1.2     matt }
   1305   1.2     matt 
   1306   1.2     matt /*
   1307   1.2     matt  * ralink_eth_watchdog
   1308   1.2     matt  *
   1309   1.2     matt  *	Watchdog timer handler.
   1310   1.2     matt  */
   1311   1.2     matt static void
   1312   1.2     matt ralink_eth_watchdog(struct ifnet *ifp)
   1313   1.2     matt {
   1314   1.2     matt 	RALINK_DEBUG_FUNC_ENTRY();
   1315   1.2     matt 	ralink_eth_softc_t * const sc = ifp->if_softc;
   1316   1.2     matt 	bool doing_transmit;
   1317   1.2     matt 
   1318   1.2     matt 	sc->sc_evcnt_watchdog.ev_count++;
   1319   1.2     matt 	doing_transmit = !SIMPLEQ_EMPTY(&sc->sc_txdirtyq);
   1320   1.2     matt 
   1321   1.2     matt 	if (doing_transmit) {
   1322   1.2     matt 		RALINK_DEBUG(RALINK_DEBUG_ERROR, "%s: transmit timeout\n",
   1323  1.10      ryo 		    ifp->if_xname);
   1324   1.2     matt 		ifp->if_oerrors++;
   1325   1.2     matt 		sc->sc_evcnt_wd_tx.ev_count++;
   1326   1.2     matt 	} else {
   1327  1.10      ryo 		RALINK_DEBUG(RALINK_DEBUG_ERROR,
   1328  1.10      ryo 		    "%s: spurious watchog timeout\n", ifp->if_xname);
   1329   1.2     matt 		sc->sc_evcnt_wd_spurious.ev_count++;
   1330   1.2     matt 		return;
   1331   1.2     matt 	}
   1332   1.2     matt 
   1333   1.2     matt 	sc->sc_evcnt_wd_reactivate.ev_count++;
   1334   1.2     matt 	const int s = splnet();
   1335   1.2     matt 	/* deactive the active partitions, retaining the active information */
   1336   1.2     matt 	ralink_eth_disable(sc);
   1337   1.2     matt 	ralink_eth_enable(sc);
   1338   1.2     matt 	splx(s);
   1339   1.2     matt 
   1340   1.2     matt 	/* Try to get more packets going. */
   1341   1.2     matt 	ralink_eth_start(ifp);
   1342   1.2     matt }
   1343   1.2     matt 
   1344   1.2     matt /*
   1345   1.2     matt  * ralink_eth_ioctl
   1346   1.2     matt  *
   1347   1.2     matt  *	Handle control requests from the operator.
   1348   1.2     matt  */
   1349   1.2     matt static int
   1350   1.2     matt ralink_eth_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1351   1.2     matt {
   1352   1.2     matt 	RALINK_DEBUG_FUNC_ENTRY();
   1353   1.2     matt 	struct ifdrv * const ifd = (struct ifdrv *) data;
   1354   1.2     matt 	ralink_eth_softc_t * const sc = ifp->if_softc;
   1355   1.2     matt 	int s, error = 0;
   1356   1.2     matt 
   1357   1.2     matt 	RALINK_DEBUG(RALINK_DEBUG_INFO, "ifp: %p  cmd: %lu  data: %p\n",
   1358   1.2     matt 		ifp, cmd, data);
   1359   1.2     matt 
   1360   1.2     matt 	s = splnet();
   1361   1.2     matt 
   1362   1.2     matt 	switch (cmd) {
   1363   1.2     matt 	case SIOCSDRVSPEC:
   1364   1.2     matt 		switch (ifd->ifd_cmd) {
   1365   1.2     matt #if 0
   1366   1.2     matt 		case ETH_SWITCH_CMD_PORT_MODE:
   1367   1.2     matt 			/* len parameter is the mode */
   1368   1.2     matt 			pt->mode = (int) ifd->ifd_len;
   1369   1.2     matt 			ralink_eth_configure_switch(pt->sc_reth);
   1370   1.2     matt 			break;
   1371   1.2     matt #endif
   1372   1.2     matt 		default:
   1373   1.2     matt 			error = EINVAL;
   1374   1.2     matt 		}
   1375   1.2     matt 		break;
   1376   1.2     matt 	default:
   1377   1.2     matt 		error = ether_ioctl(ifp, cmd, data);
   1378   1.2     matt 		if (error == ENETRESET) {
   1379   1.2     matt 			if (ifp->if_flags & IFF_RUNNING) {
   1380   1.2     matt 				/*
   1381   1.2     matt 				 * Multicast list has changed.  Set the
   1382   1.2     matt 				 * hardware filter accordingly.
   1383   1.2     matt 				 */
   1384   1.2     matt 				RALINK_DEBUG(RALINK_DEBUG_INFO, "TODO!!!");
   1385   1.2     matt #if 0
   1386   1.2     matt 				ralink_eth_filter_setup(sc);
   1387   1.2     matt #endif
   1388   1.2     matt 			}
   1389   1.2     matt 			error = 0;
   1390   1.2     matt 		}
   1391   1.2     matt 		break;
   1392   1.2     matt 	}
   1393   1.2     matt 
   1394   1.2     matt 	splx(s);
   1395   1.2     matt 
   1396   1.2     matt 	/* Try to get more packets going. */
   1397   1.2     matt 	if (sc->sc_ih != NULL)
   1398   1.2     matt 		ralink_eth_start(ifp);
   1399   1.2     matt 
   1400   1.2     matt 	return error;
   1401   1.2     matt }
   1402   1.2     matt 
   1403   1.2     matt /*
   1404   1.2     matt  * ralink_eth_intr
   1405   1.2     matt  *
   1406   1.2     matt  */
   1407   1.2     matt static int
   1408   1.2     matt ralink_eth_intr(void *arg)
   1409   1.2     matt {
   1410   1.2     matt 	RALINK_DEBUG_FUNC_ENTRY();
   1411   1.2     matt 	ralink_eth_softc_t * const sc = arg;
   1412   1.2     matt 
   1413  1.10      ryo 	for (u_int n = 0;; n = 1) {
   1414   1.2     matt 		u_int32_t status = fe_read(sc, RA_FE_INT_STATUS);
   1415   1.2     matt 		fe_write(sc, RA_FE_INT_STATUS, ~0);
   1416   1.2     matt 		RALINK_DEBUG(RALINK_DEBUG_REG,"%s() status: 0x%08x\n",
   1417  1.10      ryo 		    __func__, status);
   1418  1.11      ryo #if defined(MT7628)
   1419  1.11      ryo 		if ((status & (RA_FE_INT_RX_DONE_INT1 | RA_FE_INT_RX_DONE_INT0 |
   1420  1.11      ryo 		    RA_FE_INT_TX_DONE_INT3 | RA_FE_INT_TX_DONE_INT2 |
   1421  1.11      ryo 		    RA_FE_INT_TX_DONE_INT1 | RA_FE_INT_TX_DONE_INT0)) == 0) {
   1422  1.11      ryo 			if (n == 0)
   1423  1.11      ryo 				sc->sc_evcnt_spurious_intr.ev_count++;
   1424  1.11      ryo 			return (n != 0);
   1425  1.11      ryo 		}
   1426  1.11      ryo 
   1427  1.17  msaitoh 		if (status & (RA_FE_INT_RX_DONE_INT1 | RA_FE_INT_RX_DONE_INT0))
   1428  1.11      ryo 			ralink_eth_rxintr(sc);
   1429   1.2     matt 
   1430  1.11      ryo 		if (status & (RA_FE_INT_TX_DONE_INT3 | RA_FE_INT_TX_DONE_INT2 |
   1431  1.11      ryo 		    RA_FE_INT_TX_DONE_INT1 | RA_FE_INT_TX_DONE_INT0))
   1432  1.11      ryo 			ralink_eth_txintr(sc);
   1433  1.11      ryo #else
   1434   1.2     matt 		if ((status & (FE_INT_RX | FE_INT_TX0)) == 0) {
   1435   1.2     matt 			if (n == 0)
   1436   1.2     matt 				sc->sc_evcnt_spurious_intr.ev_count++;
   1437   1.2     matt 			return (n != 0);
   1438   1.2     matt 		}
   1439   1.2     matt 
   1440   1.2     matt 		if (status & FE_INT_RX)
   1441   1.2     matt 			ralink_eth_rxintr(sc);
   1442   1.2     matt 
   1443   1.2     matt 		if (status & FE_INT_TX0)
   1444   1.2     matt 			ralink_eth_txintr(sc);
   1445  1.11      ryo #endif
   1446   1.2     matt 	}
   1447   1.2     matt 
   1448   1.2     matt 	/* Try to get more packets going. */
   1449  1.13    ozaki 	if_schedule_deferred_start(&sc->sc_ethercom.ec_if);
   1450   1.2     matt 
   1451   1.2     matt 	return 1;
   1452   1.2     matt }
   1453   1.2     matt 
   1454   1.2     matt /*
   1455   1.2     matt  * ralink_eth_rxintr
   1456   1.2     matt  */
   1457   1.2     matt static void
   1458   1.2     matt ralink_eth_rxintr(ralink_eth_softc_t *sc)
   1459   1.2     matt {
   1460   1.2     matt 	RALINK_DEBUG_FUNC_ENTRY();
   1461   1.2     matt 	struct ifnet * const ifp = &sc->sc_ethercom.ec_if;
   1462   1.2     matt 	struct ralink_eth_rxstate *rxs;
   1463   1.2     matt 	struct mbuf *m;
   1464   1.2     matt 	int len;
   1465   1.2     matt 	int rx_cpu_idx;
   1466   1.2     matt 
   1467   1.2     matt 	KASSERT(curcpu()->ci_cpl >= IPL_NET);
   1468   1.2     matt 	sc->sc_evcnt_rxintr.ev_count++;
   1469   1.2     matt 	rx_cpu_idx = fe_read(sc, RA_FE_PDMA_RX0_CPU_IDX);
   1470   1.2     matt 
   1471  1.16  msaitoh 	for (;;) {
   1472   1.2     matt 		rx_cpu_idx = (rx_cpu_idx + 1) % RALINK_ETH_NUM_RX_DESC;
   1473   1.2     matt 
   1474   1.2     matt 		rxs = &sc->sc_rxstate[rx_cpu_idx];
   1475   1.2     matt 
   1476   1.2     matt 		bus_dmamap_sync(sc->sc_dmat, sc->sc_pdmamap,
   1477  1.10      ryo 		    (int)&sc->sc_rxdesc[rx_cpu_idx] - (int)sc->sc_descs,
   1478  1.10      ryo 		    sizeof(struct ralink_rx_desc),
   1479  1.17  msaitoh 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1480   1.2     matt 
   1481   1.2     matt 		RALINK_DEBUG(RALINK_DEBUG_REG,"rx(%d) 0x%08x: 0x%08x\n",
   1482  1.10      ryo 		    rx_cpu_idx, (int)&sc->sc_rxdesc[rx_cpu_idx].data_ptr,
   1483  1.10      ryo 		    sc->sc_rxdesc[rx_cpu_idx].data_ptr);
   1484   1.2     matt 		RALINK_DEBUG(RALINK_DEBUG_REG,"rx(%d) 0x%08x: 0x%08x\n",
   1485  1.10      ryo 		    rx_cpu_idx, (int)&sc->sc_rxdesc[rx_cpu_idx].rxd_info1,
   1486  1.10      ryo 		    sc->sc_rxdesc[rx_cpu_idx].rxd_info1);
   1487   1.2     matt 		RALINK_DEBUG(RALINK_DEBUG_REG,"rx(%d) 0x%08x: 0x%08x\n",
   1488  1.10      ryo 		    rx_cpu_idx, (int)&sc->sc_rxdesc[rx_cpu_idx].unused,
   1489  1.10      ryo 		    sc->sc_rxdesc[rx_cpu_idx].unused);
   1490  1.10      ryo 		RALINK_DEBUG(RALINK_DEBUG_REG,"rx(%d) 0x%08x: 0x%08x\n",
   1491  1.10      ryo 		    rx_cpu_idx, (int)&sc->sc_rxdesc[rx_cpu_idx].rxd_info2,
   1492  1.10      ryo 		    sc->sc_rxdesc[rx_cpu_idx].rxd_info2);
   1493   1.2     matt 
   1494   1.2     matt 		if (!(sc->sc_rxdesc[rx_cpu_idx].rxd_info1 & RXD_DDONE))
   1495   1.2     matt 			break;
   1496   1.2     matt 
   1497   1.2     matt 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1498   1.2     matt 			rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1499   1.2     matt 
   1500   1.2     matt 		/*
   1501   1.2     matt 		 * No errors; receive the packet.
   1502   1.2     matt 		 * Note the chip includes the CRC with every packet.
   1503   1.2     matt 		 */
   1504   1.2     matt 		len = RXD_LEN0(sc->sc_rxdesc[rx_cpu_idx].rxd_info1);
   1505   1.2     matt 
   1506   1.2     matt 		RALINK_DEBUG(RALINK_DEBUG_REG,"rx(%d) packet rx %d bytes\n",
   1507  1.10      ryo 		    rx_cpu_idx, len);
   1508   1.2     matt 
   1509   1.2     matt 		/*
   1510   1.2     matt 		 * Allocate a new mbuf cluster.  If that fails, we are
   1511   1.2     matt 		 * out of memory, and must drop the packet and recycle
   1512   1.2     matt 		 * the buffer that's already attached to this descriptor.
   1513   1.2     matt 		 */
   1514   1.2     matt 		m = rxs->rxs_mbuf;
   1515   1.2     matt 		if (ralink_eth_add_rxbuf(sc, rx_cpu_idx) != 0)
   1516   1.2     matt 			break;
   1517   1.2     matt 		m->m_data += RALINK_ETHER_ALIGN;
   1518   1.2     matt 		m->m_pkthdr.len = m->m_len = len;
   1519   1.2     matt 
   1520   1.2     matt #ifdef RALINK_ETH_DEBUG
   1521   1.2     matt  {
   1522   1.2     matt 		struct ether_header *eh = mtod(m, struct ether_header *);
   1523   1.2     matt 		printf("rx: eth_dst: %s ", ether_sprintf(eh->ether_dhost));
   1524   1.2     matt 		printf("rx: eth_src: %s type: 0x%04x \n",
   1525  1.10      ryo 		    ether_sprintf(eh->ether_shost), ntohs(eh->ether_type));
   1526   1.2     matt 		printf("0x14: %08x\n", *(volatile unsigned int *)(0xb0110014));
   1527   1.2     matt 		printf("0x98: %08x\n", *(volatile unsigned int *)(0xb0110098));
   1528   1.2     matt 
   1529   1.2     matt 		unsigned char * s = mtod(m, unsigned char *);
   1530   1.2     matt 		for (int j = 0; j < 32; j++)
   1531   1.2     matt 			printf("%02x%c", *(s + j),
   1532   1.2     matt 				(j == 15 || j == 31) ? '\n' : ' ');
   1533   1.2     matt  }
   1534   1.2     matt #endif
   1535   1.2     matt 
   1536   1.2     matt 		/*
   1537   1.2     matt 		 * claim the buffer here since we can't do it at
   1538   1.2     matt 		 * allocation time due to the SW partitions
   1539   1.2     matt 		 */
   1540   1.2     matt 		MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
   1541   1.2     matt 
   1542   1.2     matt 		/* push it up the inteface */
   1543   1.9    ozaki 		m_set_rcvif(m, ifp);
   1544   1.2     matt 
   1545   1.2     matt #ifdef RALINK_ETH_DEBUG
   1546   1.2     matt  {
   1547   1.2     matt 		struct ether_header *eh = mtod(m, struct ether_header *);
   1548   1.2     matt 		printf("rx: eth_dst: %s ", ether_sprintf(eh->ether_dhost));
   1549   1.2     matt 		printf("rx: eth_src: %s type: 0x%04x\n",
   1550  1.10      ryo 		    ether_sprintf(eh->ether_shost), ntohs(eh->ether_type));
   1551   1.2     matt 		printf("0x14: %08x\n", *(volatile unsigned int *)(0xb0110014));
   1552   1.2     matt 		printf("0x98: %08x\n", *(volatile unsigned int *)(0xb0110098));
   1553   1.2     matt 
   1554   1.2     matt 		unsigned char * s = mtod(m, unsigned char *);
   1555   1.2     matt 		for (int j = 0; j < 32; j++)
   1556   1.2     matt 			printf("%02x%c", *(s + j),
   1557  1.10      ryo 			    (j == 15 || j == 31) ? '\n' : ' ');
   1558   1.2     matt  }
   1559   1.2     matt #endif
   1560   1.2     matt 
   1561   1.2     matt 		/*
   1562   1.2     matt 		 * XXX: M_CSUM_TCPv4 and M_CSUM_UDPv4 do not currently work when
   1563   1.2     matt 		 * using PF's ROUTETO option for load balancing.
   1564   1.2     matt 		 */
   1565   1.2     matt 		m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
   1566   1.2     matt 
   1567   1.2     matt 		/* Pass it on. */
   1568   1.2     matt 		sc->sc_evcnt_input.ev_count++;
   1569   1.7    ozaki 		if_percpuq_enqueue(ifp->if_percpuq, m);
   1570   1.2     matt 
   1571   1.2     matt 		fe_write(sc, RA_FE_PDMA_RX0_CPU_IDX, rx_cpu_idx);
   1572   1.2     matt 	}
   1573   1.2     matt }
   1574   1.2     matt 
   1575   1.2     matt /*
   1576   1.2     matt  * ralink_eth_txintr
   1577   1.2     matt  */
   1578   1.2     matt static void
   1579   1.2     matt ralink_eth_txintr(ralink_eth_softc_t *sc)
   1580   1.2     matt {
   1581   1.2     matt 	RALINK_DEBUG_FUNC_ENTRY();
   1582   1.2     matt 	struct ralink_eth_txstate *txs;
   1583   1.2     matt 
   1584   1.2     matt 	KASSERT(curcpu()->ci_cpl >= IPL_NET);
   1585   1.2     matt 	sc->sc_evcnt_txintr.ev_count++;
   1586   1.2     matt 
   1587   1.2     matt 	/*
   1588   1.2     matt 	 * Go through our Tx list and free mbufs for those
   1589   1.2     matt 	 * frames that have been transmitted.
   1590   1.2     matt 	 */
   1591   1.2     matt 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   1592   1.2     matt 		bus_dmamap_sync(sc->sc_dmat, sc->sc_pdmamap,
   1593  1.10      ryo 		    (int)&sc->sc_txdesc[txs->txs_idx] - (int)sc->sc_descs,
   1594  1.10      ryo 		    sizeof(struct ralink_tx_desc),
   1595  1.17  msaitoh 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1596  1.10      ryo 
   1597  1.10      ryo 		RALINK_DEBUG(RALINK_DEBUG_REG,"-tx(%d) 0x%08x: 0x%08x\n",
   1598  1.10      ryo 		    txs->txs_idx, (int)&sc->sc_txdesc[txs->txs_idx].data_ptr0,
   1599  1.10      ryo 		    sc->sc_txdesc[txs->txs_idx].data_ptr0);
   1600  1.10      ryo 		RALINK_DEBUG(RALINK_DEBUG_REG,"-tx(%d) 0x%08x: 0x%08x\n",
   1601  1.10      ryo 		    txs->txs_idx, (int)&sc->sc_txdesc[txs->txs_idx].txd_info1,
   1602  1.10      ryo 		    sc->sc_txdesc[txs->txs_idx].txd_info1);
   1603  1.10      ryo 		RALINK_DEBUG(RALINK_DEBUG_REG,"-tx(%d) 0x%08x: 0x%08x\n",
   1604  1.10      ryo 		    txs->txs_idx, (int)&sc->sc_txdesc[txs->txs_idx].data_ptr1,
   1605  1.10      ryo 		    sc->sc_txdesc[txs->txs_idx].data_ptr1);
   1606  1.10      ryo 		RALINK_DEBUG(RALINK_DEBUG_REG,"-tx(%d) 0x%08x: 0x%08x\n",
   1607  1.10      ryo 		    txs->txs_idx, (int)&sc->sc_txdesc[txs->txs_idx].txd_info2,
   1608  1.10      ryo 		    sc->sc_txdesc[txs->txs_idx].txd_info2);
   1609   1.2     matt 
   1610   1.2     matt 		/* we're finished if the current tx isn't done */
   1611   1.2     matt 		if (!(sc->sc_txdesc[txs->txs_idx].txd_info1 & TXD_DDONE))
   1612   1.2     matt 			break;
   1613   1.2     matt 
   1614  1.10      ryo 		RALINK_DEBUG(RALINK_DEBUG_REG,"-tx(%d) transmitted\n",
   1615  1.11      ryo 		   txs->txs_idx);
   1616   1.2     matt 
   1617   1.2     matt 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
   1618   1.2     matt 
   1619   1.2     matt 		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap, 0,
   1620  1.10      ryo 		    txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1621   1.2     matt 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1622   1.2     matt 		m_freem(txs->txs_mbuf);
   1623   1.2     matt 		txs->txs_mbuf = NULL;
   1624   1.2     matt 
   1625   1.2     matt 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1626   1.2     matt 
   1627   1.2     matt 		struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1628   1.2     matt 		ifp->if_flags &= ~IFF_OACTIVE;
   1629   1.2     matt 		ifp->if_opackets++;
   1630   1.2     matt 		sc->sc_evcnt_output.ev_count++;
   1631   1.2     matt 
   1632   1.2     matt 		if (--sc->sc_pending_tx == 0)
   1633   1.2     matt 			ifp->if_timer = 0;
   1634   1.2     matt 	}
   1635   1.2     matt }
   1636   1.2     matt 
   1637   1.2     matt /*
   1638   1.2     matt  * ralink_eth_mdio_enable
   1639   1.2     matt  */
   1640  1.11      ryo #if defined(RT3050) || defined(RT3052)
   1641   1.2     matt static void
   1642   1.2     matt ralink_eth_mdio_enable(ralink_eth_softc_t *sc, bool enable)
   1643   1.2     matt {
   1644   1.2     matt 	uint32_t data = sy_read(sc, RA_SYSCTL_GPIOMODE);
   1645   1.2     matt 
   1646   1.2     matt 	if (enable)
   1647   1.2     matt 		data &= ~GPIOMODE_MDIO;
   1648   1.2     matt 	else
   1649   1.2     matt 		data |= GPIOMODE_MDIO;
   1650   1.2     matt 
   1651   1.5      oki 	sy_write(sc, RA_SYSCTL_GPIOMODE, data);
   1652   1.2     matt }
   1653   1.2     matt #else
   1654   1.2     matt #define ralink_eth_mdio_enable(sc, enable)
   1655   1.2     matt #endif
   1656   1.2     matt 
   1657   1.2     matt /*
   1658   1.2     matt  * ralink_eth_mii_statchg
   1659   1.2     matt  */
   1660   1.2     matt static void
   1661   1.6     matt ralink_eth_mii_statchg(struct ifnet *ifp)
   1662   1.2     matt {
   1663   1.2     matt #if 0
   1664   1.6     matt 	ralink_eth_softc_t * const sc = ifp->if_softc;
   1665   1.2     matt 
   1666   1.2     matt #endif
   1667   1.2     matt }
   1668   1.2     matt 
   1669   1.2     matt /*
   1670   1.2     matt  * ralink_eth_mii_tick
   1671   1.2     matt  *
   1672   1.2     matt  *	One second timer, used to tick the MIIs.
   1673   1.2     matt  */
   1674   1.2     matt static void
   1675   1.2     matt ralink_eth_mii_tick(void *arg)
   1676   1.2     matt {
   1677   1.2     matt 	ralink_eth_softc_t * const sc = arg;
   1678   1.2     matt 
   1679   1.2     matt 	const int s = splnet();
   1680   1.2     matt 	mii_tick(&sc->sc_mii);
   1681   1.2     matt 	splx(s);
   1682   1.2     matt 
   1683   1.2     matt 	callout_reset(&sc->sc_tick_callout, hz, ralink_eth_mii_tick, sc);
   1684   1.2     matt }
   1685   1.2     matt 
   1686   1.2     matt /*
   1687   1.2     matt  * ralink_eth_mii_read
   1688   1.2     matt  */
   1689   1.2     matt static int
   1690  1.15  msaitoh ralink_eth_mii_read(device_t self, int phy_addr, int phy_reg, uint16_t *val)
   1691   1.2     matt {
   1692   1.5      oki 	ralink_eth_softc_t *sc = device_private(self);
   1693   1.2     matt 	KASSERT(sc != NULL);
   1694   1.2     matt #if 0
   1695   1.2     matt 	printf("%s() phy_addr: %d  phy_reg: %d\n", __func__, phy_addr, phy_reg);
   1696   1.2     matt #endif
   1697  1.11      ryo #if defined(RT3050) || defined(RT3052) || defined(MT7628)
   1698   1.2     matt 	if (phy_addr > 5)
   1699  1.15  msaitoh 		return -1;
   1700   1.2     matt #endif
   1701   1.2     matt 
   1702   1.2     matt 	/* We enable mdio gpio purpose register, and disable it when exit. */
   1703   1.2     matt 	ralink_eth_mdio_enable(sc, true);
   1704   1.2     matt 
   1705   1.2     matt 	/*
   1706   1.2     matt 	 * make sure previous read operation is complete
   1707   1.2     matt 	 * TODO: timeout (linux uses jiffies to measure 5 seconds)
   1708   1.2     matt 	 */
   1709   1.2     matt 	for (;;) {
   1710   1.2     matt 		/* rd_rdy: read operation is complete */
   1711  1.11      ryo #if defined(RT3050) || defined(RT3052) || defined(MT7628)
   1712   1.2     matt 		if ((sw_read(sc, RA_ETH_SW_PCTL1) & PCTL1_RD_DONE) == 0)
   1713   1.2     matt 			break;
   1714   1.2     matt #else
   1715   1.2     matt 		if ((fe_read(sc, RA_FE_MDIO_ACCESS) & MDIO_ACCESS_TRG) == 0)
   1716   1.2     matt 			break;
   1717   1.2     matt #endif
   1718   1.2     matt 	}
   1719   1.2     matt 
   1720  1.11      ryo #if defined(RT3050) || defined(RT3052) || defined(MT7628)
   1721   1.2     matt 	sw_write(sc, RA_ETH_SW_PCTL0,
   1722  1.10      ryo 	    PCTL0_RD_CMD | PCTL0_REG(phy_reg) | PCTL0_ADDR(phy_addr));
   1723   1.2     matt #else
   1724   1.2     matt 	fe_write(sc, RA_FE_MDIO_ACCESS,
   1725  1.10      ryo 	    MDIO_ACCESS_PHY_ADDR(phy_addr) | MDIO_ACCESS_REG(phy_reg));
   1726   1.2     matt 	fe_write(sc, RA_FE_MDIO_ACCESS,
   1727  1.10      ryo 	    MDIO_ACCESS_PHY_ADDR(phy_addr) | MDIO_ACCESS_REG(phy_reg) |
   1728  1.10      ryo 	    MDIO_ACCESS_TRG);
   1729   1.2     matt #endif
   1730   1.2     matt 
   1731   1.2     matt 	/*
   1732   1.2     matt 	 * make sure read operation is complete
   1733   1.2     matt 	 * TODO: timeout (linux uses jiffies to measure 5 seconds)
   1734   1.2     matt 	 */
   1735   1.2     matt 	for (;;) {
   1736  1.11      ryo #if defined(RT3050) || defined(RT3052) || defined(MT7628)
   1737   1.2     matt 		if ((sw_read(sc, RA_ETH_SW_PCTL1) & PCTL1_RD_DONE) != 0) {
   1738  1.15  msaitoh 			*val = PCTL1_RD_VAL(
   1739  1.10      ryo 			    sw_read(sc, RA_ETH_SW_PCTL1));
   1740   1.2     matt 			ralink_eth_mdio_enable(sc, false);
   1741  1.15  msaitoh 			return 0;
   1742   1.2     matt 		}
   1743   1.2     matt #else
   1744   1.2     matt 		if ((fe_read(sc, RA_FE_MDIO_ACCESS) & MDIO_ACCESS_TRG) == 0) {
   1745  1.15  msaitoh 			*val = MDIO_ACCESS_DATA(
   1746  1.10      ryo 			    fe_read(sc, RA_FE_MDIO_ACCESS));
   1747   1.2     matt 			ralink_eth_mdio_enable(sc, false);
   1748  1.15  msaitoh 			return 0;
   1749   1.2     matt 		}
   1750   1.2     matt #endif
   1751   1.2     matt 	}
   1752   1.2     matt }
   1753   1.2     matt 
   1754   1.2     matt /*
   1755   1.2     matt  * ralink_eth_mii_write
   1756   1.2     matt  */
   1757  1.15  msaitoh static int
   1758  1.15  msaitoh ralink_eth_mii_write(device_t self, int phy_addr, int phy_reg, uint16_t val)
   1759   1.2     matt {
   1760   1.5      oki 	ralink_eth_softc_t *sc = device_private(self);
   1761   1.2     matt 	KASSERT(sc != NULL);
   1762   1.2     matt #if 0
   1763   1.2     matt 	printf("%s() phy_addr: %d  phy_reg: %d  val: 0x%04x\n",
   1764  1.10      ryo 	    __func__, phy_addr, phy_reg, val);
   1765   1.2     matt #endif
   1766   1.2     matt 	ralink_eth_mdio_enable(sc, true);
   1767   1.2     matt 
   1768   1.2     matt 	/*
   1769   1.2     matt 	 * make sure previous write operation is complete
   1770   1.2     matt 	 * TODO: timeout (linux uses jiffies to measure 5 seconds)
   1771   1.2     matt 	 */
   1772   1.2     matt 	for (;;) {
   1773  1.11      ryo #if defined(RT3050) || defined(RT3052) || defined(MT7628)
   1774   1.2     matt 		if ((sw_read(sc, RA_ETH_SW_PCTL1) & PCTL1_RD_DONE) == 0)
   1775   1.2     matt 			break;
   1776   1.2     matt #else
   1777   1.2     matt 		if ((fe_read(sc, RA_FE_MDIO_ACCESS) & MDIO_ACCESS_TRG) == 0)
   1778   1.2     matt 			break;
   1779   1.2     matt #endif
   1780   1.2     matt 	}
   1781   1.2     matt 
   1782  1.11      ryo #if defined(RT3050) || defined(RT3052) || defined(MT7628)
   1783   1.2     matt 	sw_write(sc, RA_ETH_SW_PCTL0,
   1784  1.10      ryo 	    PCTL0_WR_CMD | PCTL0_WR_VAL(val) | PCTL0_REG(phy_reg) |
   1785  1.10      ryo 	    PCTL0_ADDR(phy_addr));
   1786   1.2     matt #else
   1787   1.2     matt 	fe_write(sc, RA_FE_MDIO_ACCESS,
   1788  1.10      ryo 	    MDIO_ACCESS_WR | MDIO_ACCESS_PHY_ADDR(phy_addr) |
   1789  1.10      ryo 	    MDIO_ACCESS_REG(phy_reg) | MDIO_ACCESS_DATA(val));
   1790   1.2     matt 	fe_write(sc, RA_FE_MDIO_ACCESS,
   1791  1.10      ryo 	    MDIO_ACCESS_WR | MDIO_ACCESS_PHY_ADDR(phy_addr) |
   1792  1.10      ryo 	    MDIO_ACCESS_REG(phy_reg) | MDIO_ACCESS_DATA(val) |
   1793  1.10      ryo 	    MDIO_ACCESS_TRG);
   1794   1.2     matt #endif
   1795   1.2     matt 
   1796   1.2     matt 
   1797   1.2     matt 	/* make sure write operation is complete */
   1798   1.2     matt 	for (;;) {
   1799  1.11      ryo #if defined(RT3050) || defined(RT3052) || defined(MT7628)
   1800   1.2     matt 		if ((sw_read(sc, RA_ETH_SW_PCTL1) & PCTL1_WR_DONE) != 0) {
   1801   1.2     matt 			ralink_eth_mdio_enable(sc, false);
   1802  1.15  msaitoh 			return 0;
   1803   1.2     matt 		}
   1804   1.2     matt #else
   1805  1.16  msaitoh 		if ((fe_read(sc, RA_FE_MDIO_ACCESS) & MDIO_ACCESS_TRG) == 0) {
   1806   1.2     matt 			ralink_eth_mdio_enable(sc, false);
   1807  1.15  msaitoh 			return 0;
   1808   1.2     matt 		}
   1809   1.2     matt #endif
   1810   1.2     matt 	}
   1811   1.2     matt }
   1812