Home | History | Annotate | Line # | Download | only in marvell
if_mvgbe.c revision 1.33
      1  1.33   msaitoh /*	$NetBSD: if_mvgbe.c,v 1.33 2012/12/20 10:01:28 msaitoh Exp $	*/
      2   1.1  kiyohara /*
      3   1.1  kiyohara  * Copyright (c) 2007, 2008 KIYOHARA Takashi
      4   1.1  kiyohara  * All rights reserved.
      5   1.1  kiyohara  *
      6   1.1  kiyohara  * Redistribution and use in source and binary forms, with or without
      7   1.1  kiyohara  * modification, are permitted provided that the following conditions
      8   1.1  kiyohara  * are met:
      9   1.1  kiyohara  * 1. Redistributions of source code must retain the above copyright
     10   1.1  kiyohara  *    notice, this list of conditions and the following disclaimer.
     11   1.1  kiyohara  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1  kiyohara  *    notice, this list of conditions and the following disclaimer in the
     13   1.1  kiyohara  *    documentation and/or other materials provided with the distribution.
     14   1.1  kiyohara  *
     15   1.1  kiyohara  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16   1.1  kiyohara  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17   1.1  kiyohara  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18   1.1  kiyohara  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19   1.1  kiyohara  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20   1.1  kiyohara  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21   1.1  kiyohara  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22   1.1  kiyohara  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23   1.1  kiyohara  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24   1.1  kiyohara  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25   1.1  kiyohara  * POSSIBILITY OF SUCH DAMAGE.
     26   1.1  kiyohara  */
     27   1.1  kiyohara #include <sys/cdefs.h>
     28  1.33   msaitoh __KERNEL_RCSID(0, "$NetBSD: if_mvgbe.c,v 1.33 2012/12/20 10:01:28 msaitoh Exp $");
     29   1.1  kiyohara 
     30   1.1  kiyohara #include <sys/param.h>
     31   1.1  kiyohara #include <sys/bus.h>
     32  1.27   msaitoh #include <sys/callout.h>
     33   1.1  kiyohara #include <sys/device.h>
     34   1.1  kiyohara #include <sys/endian.h>
     35   1.1  kiyohara #include <sys/errno.h>
     36  1.32   msaitoh #include <sys/evcnt.h>
     37  1.27   msaitoh #include <sys/kernel.h>
     38   1.1  kiyohara #include <sys/kmem.h>
     39   1.1  kiyohara #include <sys/mutex.h>
     40   1.1  kiyohara #include <sys/sockio.h>
     41  1.25   msaitoh #include <sys/sysctl.h>
     42   1.1  kiyohara 
     43   1.1  kiyohara #include <dev/marvell/marvellreg.h>
     44   1.1  kiyohara #include <dev/marvell/marvellvar.h>
     45   1.1  kiyohara #include <dev/marvell/mvgbereg.h>
     46   1.1  kiyohara 
     47   1.1  kiyohara #include <net/if.h>
     48   1.1  kiyohara #include <net/if_ether.h>
     49   1.1  kiyohara #include <net/if_media.h>
     50   1.1  kiyohara 
     51   1.1  kiyohara #include <netinet/in.h>
     52   1.1  kiyohara #include <netinet/in_systm.h>
     53   1.1  kiyohara #include <netinet/ip.h>
     54   1.1  kiyohara 
     55   1.1  kiyohara #include <net/bpf.h>
     56   1.1  kiyohara #include <sys/rnd.h>
     57   1.1  kiyohara 
     58   1.1  kiyohara #include <dev/mii/mii.h>
     59   1.1  kiyohara #include <dev/mii/miivar.h>
     60   1.1  kiyohara 
     61   1.1  kiyohara #include "locators.h"
     62   1.1  kiyohara 
     63   1.1  kiyohara /* #define MVGBE_DEBUG 3 */
     64   1.1  kiyohara #ifdef MVGBE_DEBUG
     65   1.1  kiyohara #define DPRINTF(x)	if (mvgbe_debug) printf x
     66   1.1  kiyohara #define DPRINTFN(n,x)	if (mvgbe_debug >= (n)) printf x
     67   1.1  kiyohara int mvgbe_debug = MVGBE_DEBUG;
     68   1.1  kiyohara #else
     69   1.1  kiyohara #define DPRINTF(x)
     70   1.1  kiyohara #define DPRINTFN(n,x)
     71   1.1  kiyohara #endif
     72   1.1  kiyohara 
     73   1.1  kiyohara 
     74   1.1  kiyohara #define MVGBE_READ(sc, reg) \
     75   1.1  kiyohara 	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
     76   1.1  kiyohara #define MVGBE_WRITE(sc, reg, val) \
     77   1.1  kiyohara 	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
     78   1.5  jakllsch #define MVGBE_READ_FILTER(sc, reg, val, c) \
     79   1.5  jakllsch 	bus_space_read_region_4((sc)->sc_iot, (sc)->sc_dafh, (reg), (val), (c))
     80   1.1  kiyohara #define MVGBE_WRITE_FILTER(sc, reg, val, c) \
     81   1.5  jakllsch 	bus_space_write_region_4((sc)->sc_iot, (sc)->sc_dafh, (reg), (val), (c))
     82   1.1  kiyohara 
     83   1.1  kiyohara #define MVGBE_TX_RING_CNT	256
     84   1.4  jakllsch #define MVGBE_TX_RING_MSK	(MVGBE_TX_RING_CNT - 1)
     85   1.4  jakllsch #define MVGBE_TX_RING_NEXT(x)	(((x) + 1) & MVGBE_TX_RING_MSK)
     86   1.1  kiyohara #define MVGBE_RX_RING_CNT	256
     87   1.4  jakllsch #define MVGBE_RX_RING_MSK	(MVGBE_RX_RING_CNT - 1)
     88   1.4  jakllsch #define MVGBE_RX_RING_NEXT(x)	(((x) + 1) & MVGBE_RX_RING_MSK)
     89   1.4  jakllsch 
     90   1.4  jakllsch CTASSERT(MVGBE_TX_RING_CNT > 1 && MVGBE_TX_RING_NEXT(MVGBE_TX_RING_CNT) ==
     91   1.4  jakllsch 	(MVGBE_TX_RING_CNT + 1) % MVGBE_TX_RING_CNT);
     92   1.4  jakllsch CTASSERT(MVGBE_RX_RING_CNT > 1 && MVGBE_RX_RING_NEXT(MVGBE_RX_RING_CNT) ==
     93   1.4  jakllsch 	(MVGBE_RX_RING_CNT + 1) % MVGBE_RX_RING_CNT);
     94   1.1  kiyohara 
     95   1.1  kiyohara #define MVGBE_JSLOTS		384	/* XXXX */
     96  1.28   msaitoh #define MVGBE_JLEN \
     97  1.31   msaitoh     ((MVGBE_MRU + MVGBE_HWHEADER_SIZE + MVGBE_RXBUF_ALIGN - 1) & \
     98  1.31   msaitoh     ~MVGBE_RXBUF_MASK)
     99   1.1  kiyohara #define MVGBE_NTXSEG		30
    100   1.1  kiyohara #define MVGBE_JPAGESZ		PAGE_SIZE
    101   1.1  kiyohara #define MVGBE_RESID \
    102   1.1  kiyohara     (MVGBE_JPAGESZ - (MVGBE_JLEN * MVGBE_JSLOTS) % MVGBE_JPAGESZ)
    103   1.1  kiyohara #define MVGBE_JMEM \
    104   1.1  kiyohara     ((MVGBE_JLEN * MVGBE_JSLOTS) + MVGBE_RESID)
    105   1.1  kiyohara 
    106   1.1  kiyohara #define MVGBE_TX_RING_ADDR(sc, i)		\
    107   1.1  kiyohara     ((sc)->sc_ring_map->dm_segs[0].ds_addr +	\
    108   1.1  kiyohara 			offsetof(struct mvgbe_ring_data, mvgbe_tx_ring[(i)]))
    109   1.1  kiyohara 
    110   1.1  kiyohara #define MVGBE_RX_RING_ADDR(sc, i)		\
    111   1.1  kiyohara     ((sc)->sc_ring_map->dm_segs[0].ds_addr +	\
    112   1.1  kiyohara 			offsetof(struct mvgbe_ring_data, mvgbe_rx_ring[(i)]))
    113   1.1  kiyohara 
    114   1.1  kiyohara #define MVGBE_CDOFF(x)		offsetof(struct mvgbe_ring_data, x)
    115   1.1  kiyohara #define MVGBE_CDTXOFF(x)	MVGBE_CDOFF(mvgbe_tx_ring[(x)])
    116   1.1  kiyohara #define MVGBE_CDRXOFF(x)	MVGBE_CDOFF(mvgbe_rx_ring[(x)])
    117   1.1  kiyohara 
    118   1.1  kiyohara #define MVGBE_CDTXSYNC(sc, x, n, ops)					\
    119   1.1  kiyohara do {									\
    120   1.1  kiyohara 	int __x, __n;							\
    121   1.1  kiyohara 	const int __descsize = sizeof(struct mvgbe_tx_desc);		\
    122   1.1  kiyohara 									\
    123   1.1  kiyohara 	__x = (x);							\
    124   1.1  kiyohara 	__n = (n);							\
    125   1.1  kiyohara 									\
    126   1.1  kiyohara 	/* If it will wrap around, sync to the end of the ring. */	\
    127   1.1  kiyohara 	if ((__x + __n) > MVGBE_TX_RING_CNT) {				\
    128   1.1  kiyohara 		bus_dmamap_sync((sc)->sc_dmat,				\
    129   1.1  kiyohara 		    (sc)->sc_ring_map, MVGBE_CDTXOFF(__x),		\
    130   1.1  kiyohara 		    __descsize * (MVGBE_TX_RING_CNT - __x), (ops));	\
    131   1.1  kiyohara 		__n -= (MVGBE_TX_RING_CNT - __x);			\
    132   1.1  kiyohara 		__x = 0;						\
    133   1.1  kiyohara 	}								\
    134   1.1  kiyohara 									\
    135   1.1  kiyohara 	/* Now sync whatever is left. */				\
    136   1.1  kiyohara 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_ring_map,		\
    137   1.1  kiyohara 	    MVGBE_CDTXOFF((__x)), __descsize * __n, (ops));		\
    138   1.1  kiyohara } while (0 /*CONSTCOND*/)
    139   1.1  kiyohara 
    140   1.1  kiyohara #define MVGBE_CDRXSYNC(sc, x, ops)					\
    141   1.1  kiyohara do {									\
    142   1.1  kiyohara 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_ring_map,		\
    143   1.1  kiyohara 	    MVGBE_CDRXOFF((x)), sizeof(struct mvgbe_rx_desc), (ops));	\
    144   1.1  kiyohara 	} while (/*CONSTCOND*/0)
    145   1.1  kiyohara 
    146  1.25   msaitoh #define MVGBE_IPGINTTX_DEFAULT	768
    147  1.25   msaitoh #define MVGBE_IPGINTRX_DEFAULT	768
    148   1.1  kiyohara 
    149  1.32   msaitoh #ifdef MVGBE_EVENT_COUNTERS
    150  1.32   msaitoh #define	MVGBE_EVCNT_INCR(ev)		(ev)->ev_count++
    151  1.32   msaitoh #define	MVGBE_EVCNT_ADD(ev, val)	(ev)->ev_count += (val)
    152  1.32   msaitoh #else
    153  1.32   msaitoh #define	MVGBE_EVCNT_INCR(ev)		/* nothing */
    154  1.32   msaitoh #define	MVGBE_EVCNT_ADD(ev, val)	/* nothing */
    155  1.32   msaitoh #endif
    156  1.32   msaitoh 
    157   1.1  kiyohara struct mvgbe_jpool_entry {
    158   1.1  kiyohara 	int slot;
    159   1.1  kiyohara 	LIST_ENTRY(mvgbe_jpool_entry) jpool_entries;
    160   1.1  kiyohara };
    161   1.1  kiyohara 
    162   1.1  kiyohara struct mvgbe_chain {
    163   1.1  kiyohara 	void *mvgbe_desc;
    164   1.1  kiyohara 	struct mbuf *mvgbe_mbuf;
    165   1.1  kiyohara 	struct mvgbe_chain *mvgbe_next;
    166   1.1  kiyohara };
    167   1.1  kiyohara 
    168   1.1  kiyohara struct mvgbe_txmap_entry {
    169   1.1  kiyohara 	bus_dmamap_t dmamap;
    170   1.1  kiyohara 	SIMPLEQ_ENTRY(mvgbe_txmap_entry) link;
    171   1.1  kiyohara };
    172   1.1  kiyohara 
    173   1.1  kiyohara struct mvgbe_chain_data {
    174   1.1  kiyohara 	struct mvgbe_chain mvgbe_tx_chain[MVGBE_TX_RING_CNT];
    175   1.1  kiyohara 	struct mvgbe_txmap_entry *mvgbe_tx_map[MVGBE_TX_RING_CNT];
    176   1.1  kiyohara 	int mvgbe_tx_prod;
    177   1.1  kiyohara 	int mvgbe_tx_cons;
    178   1.1  kiyohara 	int mvgbe_tx_cnt;
    179   1.1  kiyohara 
    180   1.1  kiyohara 	struct mvgbe_chain mvgbe_rx_chain[MVGBE_RX_RING_CNT];
    181   1.1  kiyohara 	bus_dmamap_t mvgbe_rx_map[MVGBE_RX_RING_CNT];
    182   1.1  kiyohara 	bus_dmamap_t mvgbe_rx_jumbo_map;
    183   1.1  kiyohara 	int mvgbe_rx_prod;
    184   1.1  kiyohara 	int mvgbe_rx_cons;
    185   1.1  kiyohara 	int mvgbe_rx_cnt;
    186   1.1  kiyohara 
    187   1.1  kiyohara 	/* Stick the jumbo mem management stuff here too. */
    188   1.1  kiyohara 	void *mvgbe_jslots[MVGBE_JSLOTS];
    189   1.1  kiyohara 	void *mvgbe_jumbo_buf;
    190   1.1  kiyohara };
    191   1.1  kiyohara 
    192   1.1  kiyohara struct mvgbe_ring_data {
    193   1.1  kiyohara 	struct mvgbe_tx_desc mvgbe_tx_ring[MVGBE_TX_RING_CNT];
    194   1.1  kiyohara 	struct mvgbe_rx_desc mvgbe_rx_ring[MVGBE_RX_RING_CNT];
    195   1.1  kiyohara };
    196   1.1  kiyohara 
    197   1.1  kiyohara struct mvgbec_softc {
    198   1.1  kiyohara 	device_t sc_dev;
    199   1.1  kiyohara 
    200   1.1  kiyohara 	bus_space_tag_t sc_iot;
    201   1.1  kiyohara 	bus_space_handle_t sc_ioh;
    202   1.1  kiyohara 
    203   1.1  kiyohara 	kmutex_t sc_mtx;
    204   1.3  kiyohara 
    205  1.13       rjs 	int sc_flags;
    206   1.1  kiyohara };
    207   1.1  kiyohara 
    208   1.1  kiyohara struct mvgbe_softc {
    209   1.1  kiyohara 	device_t sc_dev;
    210   1.3  kiyohara 	int sc_port;
    211   1.1  kiyohara 
    212   1.1  kiyohara 	bus_space_tag_t sc_iot;
    213   1.1  kiyohara 	bus_space_handle_t sc_ioh;
    214  1.27   msaitoh 	bus_space_handle_t sc_dafh;	/* dest address filter handle */
    215   1.1  kiyohara 	bus_dma_tag_t sc_dmat;
    216   1.1  kiyohara 
    217   1.1  kiyohara 	struct ethercom sc_ethercom;
    218   1.1  kiyohara 	struct mii_data sc_mii;
    219   1.1  kiyohara 	u_int8_t sc_enaddr[ETHER_ADDR_LEN];	/* station addr */
    220   1.1  kiyohara 
    221  1.27   msaitoh 	callout_t sc_tick_ch;		/* tick callout */
    222  1.27   msaitoh 
    223   1.1  kiyohara 	struct mvgbe_chain_data sc_cdata;
    224   1.1  kiyohara 	struct mvgbe_ring_data *sc_rdata;
    225   1.1  kiyohara 	bus_dmamap_t sc_ring_map;
    226   1.1  kiyohara 	int sc_if_flags;
    227  1.25   msaitoh 	unsigned int sc_ipginttx;
    228  1.25   msaitoh 	unsigned int sc_ipgintrx;
    229  1.20   msaitoh 	int sc_wdogsoft;
    230   1.1  kiyohara 
    231   1.1  kiyohara 	LIST_HEAD(__mvgbe_jfreehead, mvgbe_jpool_entry) sc_jfree_listhead;
    232   1.1  kiyohara 	LIST_HEAD(__mvgbe_jinusehead, mvgbe_jpool_entry) sc_jinuse_listhead;
    233   1.1  kiyohara 	SIMPLEQ_HEAD(__mvgbe_txmaphead, mvgbe_txmap_entry) sc_txmap_head;
    234   1.1  kiyohara 
    235  1.15       tls 	krndsource_t sc_rnd_source;
    236  1.25   msaitoh 	struct sysctllog *mvgbe_clog;
    237  1.32   msaitoh #ifdef MVGBE_EVENT_COUNTERS
    238  1.32   msaitoh 	struct evcnt sc_ev_rxoverrun;
    239  1.32   msaitoh 	struct evcnt sc_ev_wdogsoft;
    240  1.32   msaitoh #endif
    241   1.1  kiyohara };
    242   1.1  kiyohara 
    243   1.1  kiyohara 
    244   1.1  kiyohara /* Gigabit Ethernet Unit Global part functions */
    245   1.1  kiyohara 
    246   1.1  kiyohara static int mvgbec_match(device_t, struct cfdata *, void *);
    247   1.1  kiyohara static void mvgbec_attach(device_t, device_t, void *);
    248   1.1  kiyohara 
    249   1.1  kiyohara static int mvgbec_print(void *, const char *);
    250   1.1  kiyohara static int mvgbec_search(device_t, cfdata_t, const int *, void *);
    251   1.1  kiyohara 
    252   1.1  kiyohara /* MII funcstions */
    253   1.1  kiyohara static int mvgbec_miibus_readreg(device_t, int, int);
    254   1.1  kiyohara static void mvgbec_miibus_writereg(device_t, int, int, int);
    255  1.18      matt static void mvgbec_miibus_statchg(struct ifnet *);
    256   1.1  kiyohara 
    257   1.1  kiyohara static void mvgbec_wininit(struct mvgbec_softc *);
    258   1.1  kiyohara 
    259   1.1  kiyohara /* Gigabit Ethernet Port part functions */
    260   1.1  kiyohara 
    261   1.1  kiyohara static int mvgbe_match(device_t, struct cfdata *, void *);
    262   1.1  kiyohara static void mvgbe_attach(device_t, device_t, void *);
    263   1.1  kiyohara 
    264  1.27   msaitoh static void mvgbe_tick(void *);
    265   1.1  kiyohara static int mvgbe_intr(void *);
    266   1.1  kiyohara 
    267   1.1  kiyohara static void mvgbe_start(struct ifnet *);
    268   1.1  kiyohara static int mvgbe_ioctl(struct ifnet *, u_long, void *);
    269   1.1  kiyohara static int mvgbe_init(struct ifnet *);
    270   1.1  kiyohara static void mvgbe_stop(struct ifnet *, int);
    271   1.1  kiyohara static void mvgbe_watchdog(struct ifnet *);
    272   1.1  kiyohara 
    273   1.5  jakllsch static int mvgbe_ifflags_cb(struct ethercom *);
    274   1.5  jakllsch 
    275   1.5  jakllsch static int mvgbe_mediachange(struct ifnet *);
    276   1.5  jakllsch static void mvgbe_mediastatus(struct ifnet *, struct ifmediareq *);
    277   1.1  kiyohara 
    278   1.1  kiyohara static int mvgbe_init_rx_ring(struct mvgbe_softc *);
    279   1.1  kiyohara static int mvgbe_init_tx_ring(struct mvgbe_softc *);
    280   1.1  kiyohara static int mvgbe_newbuf(struct mvgbe_softc *, int, struct mbuf *, bus_dmamap_t);
    281   1.1  kiyohara static int mvgbe_alloc_jumbo_mem(struct mvgbe_softc *);
    282   1.1  kiyohara static void *mvgbe_jalloc(struct mvgbe_softc *);
    283   1.1  kiyohara static void mvgbe_jfree(struct mbuf *, void *, size_t, void *);
    284   1.1  kiyohara static int mvgbe_encap(struct mvgbe_softc *, struct mbuf *, uint32_t *);
    285   1.1  kiyohara static void mvgbe_rxeof(struct mvgbe_softc *);
    286   1.1  kiyohara static void mvgbe_txeof(struct mvgbe_softc *);
    287   1.5  jakllsch static uint8_t mvgbe_crc8(const uint8_t *, size_t);
    288   1.5  jakllsch static void mvgbe_filter_setup(struct mvgbe_softc *);
    289   1.1  kiyohara #ifdef MVGBE_DEBUG
    290   1.1  kiyohara static void mvgbe_dump_txdesc(struct mvgbe_tx_desc *, int);
    291   1.1  kiyohara #endif
    292  1.28   msaitoh static int mvgbe_ipginttx(struct mvgbec_softc *, struct mvgbe_softc *,
    293  1.28   msaitoh     unsigned int);
    294  1.28   msaitoh static int mvgbe_ipgintrx(struct mvgbec_softc *, struct mvgbe_softc *,
    295  1.28   msaitoh     unsigned int);
    296  1.25   msaitoh static void sysctl_mvgbe_init(struct mvgbe_softc *);
    297  1.25   msaitoh static int mvgbe_sysctl_ipginttx(SYSCTLFN_PROTO);
    298  1.25   msaitoh static int mvgbe_sysctl_ipgintrx(SYSCTLFN_PROTO);
    299   1.1  kiyohara 
    300   1.1  kiyohara CFATTACH_DECL_NEW(mvgbec_gt, sizeof(struct mvgbec_softc),
    301   1.1  kiyohara     mvgbec_match, mvgbec_attach, NULL, NULL);
    302   1.1  kiyohara CFATTACH_DECL_NEW(mvgbec_mbus, sizeof(struct mvgbec_softc),
    303   1.1  kiyohara     mvgbec_match, mvgbec_attach, NULL, NULL);
    304   1.1  kiyohara 
    305   1.1  kiyohara CFATTACH_DECL_NEW(mvgbe, sizeof(struct mvgbe_softc),
    306   1.1  kiyohara     mvgbe_match, mvgbe_attach, NULL, NULL);
    307   1.1  kiyohara 
    308   1.6  christos device_t mvgbec0 = NULL;
    309  1.25   msaitoh static int mvgbe_root_num;
    310   1.1  kiyohara 
    311   1.1  kiyohara struct mvgbe_port {
    312   1.1  kiyohara 	int model;
    313   1.3  kiyohara 	int unit;
    314   1.1  kiyohara 	int ports;
    315   1.3  kiyohara 	int irqs[3];
    316   1.3  kiyohara 	int flags;
    317   1.3  kiyohara #define FLAGS_FIX_TQTB	(1 << 0)
    318  1.13       rjs #define FLAGS_FIX_MTU	(1 << 1)
    319  1.24   msaitoh #define	FLAGS_IPG1	(1 << 2)
    320  1.24   msaitoh #define	FLAGS_IPG2	(1 << 3)
    321   1.1  kiyohara } mvgbe_ports[] = {
    322   1.3  kiyohara 	{ MARVELL_DISCOVERY_II,		0, 3, { 32, 33, 34 }, 0 },
    323   1.3  kiyohara 	{ MARVELL_DISCOVERY_III,	0, 3, { 32, 33, 34 }, 0 },
    324   1.1  kiyohara #if 0
    325   1.3  kiyohara 	{ MARVELL_DISCOVERY_LT,		0, ?, { }, 0 },
    326   1.3  kiyohara 	{ MARVELL_DISCOVERY_V,		0, ?, { }, 0 },
    327   1.3  kiyohara 	{ MARVELL_DISCOVERY_VI,		0, ?, { }, 0 },
    328   1.1  kiyohara #endif
    329  1.13       rjs 	{ MARVELL_ORION_1_88F5082,	0, 1, { 21 }, FLAGS_FIX_MTU },
    330  1.13       rjs 	{ MARVELL_ORION_1_88F5180N,	0, 1, { 21 }, FLAGS_FIX_MTU },
    331  1.24   msaitoh 	{ MARVELL_ORION_1_88F5181,	0, 1, { 21 }, FLAGS_FIX_MTU | FLAGS_IPG1 },
    332  1.24   msaitoh 	{ MARVELL_ORION_1_88F5182,	0, 1, { 21 }, FLAGS_FIX_MTU | FLAGS_IPG1 },
    333  1.24   msaitoh 	{ MARVELL_ORION_2_88F5281,	0, 1, { 21 }, FLAGS_FIX_MTU | FLAGS_IPG1 },
    334  1.13       rjs 	{ MARVELL_ORION_1_88F6082,	0, 1, { 21 }, FLAGS_FIX_MTU },
    335  1.13       rjs 	{ MARVELL_ORION_1_88W8660,	0, 1, { 21 }, FLAGS_FIX_MTU },
    336   1.3  kiyohara 
    337  1.24   msaitoh 	{ MARVELL_KIRKWOOD_88F6180,	0, 1, { 11 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    338  1.24   msaitoh 	{ MARVELL_KIRKWOOD_88F6192,	0, 1, { 11 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    339  1.24   msaitoh 	{ MARVELL_KIRKWOOD_88F6192,	1, 1, { 15 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    340  1.24   msaitoh 	{ MARVELL_KIRKWOOD_88F6281,	0, 1, { 11 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    341  1.24   msaitoh 	{ MARVELL_KIRKWOOD_88F6281,	1, 1, { 15 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    342  1.24   msaitoh 	{ MARVELL_KIRKWOOD_88F6282,	0, 1, { 11 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    343  1.24   msaitoh 	{ MARVELL_KIRKWOOD_88F6282,	1, 1, { 15 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    344  1.24   msaitoh 
    345  1.24   msaitoh 	{ MARVELL_MV78XX0_MV78100,	0, 1, { 40 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    346  1.24   msaitoh 	{ MARVELL_MV78XX0_MV78100,	1, 1, { 44 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    347  1.24   msaitoh 	{ MARVELL_MV78XX0_MV78200,	0, 1, { 40 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    348  1.24   msaitoh 	{ MARVELL_MV78XX0_MV78200,	1, 1, { 44 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    349  1.24   msaitoh 	{ MARVELL_MV78XX0_MV78200,	2, 1, { 48 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    350  1.24   msaitoh 	{ MARVELL_MV78XX0_MV78200,	3, 1, { 52 }, FLAGS_FIX_TQTB | FLAGS_IPG2 },
    351   1.1  kiyohara };
    352   1.1  kiyohara 
    353   1.1  kiyohara 
    354   1.1  kiyohara /* ARGSUSED */
    355   1.1  kiyohara static int
    356   1.9      matt mvgbec_match(device_t parent, cfdata_t match, void *aux)
    357   1.1  kiyohara {
    358   1.1  kiyohara 	struct marvell_attach_args *mva = aux;
    359   1.1  kiyohara 	int i;
    360   1.1  kiyohara 
    361   1.1  kiyohara 	if (strcmp(mva->mva_name, match->cf_name) != 0)
    362   1.1  kiyohara 		return 0;
    363   1.2  kiyohara 	if (mva->mva_offset == MVA_OFFSET_DEFAULT)
    364   1.1  kiyohara 		return 0;
    365   1.1  kiyohara 
    366   1.1  kiyohara 	for (i = 0; i < __arraycount(mvgbe_ports); i++)
    367   1.1  kiyohara 		if (mva->mva_model == mvgbe_ports[i].model) {
    368   1.1  kiyohara 			mva->mva_size = MVGBE_SIZE;
    369   1.1  kiyohara 			return 1;
    370   1.1  kiyohara 		}
    371   1.1  kiyohara 	return 0;
    372   1.1  kiyohara }
    373   1.1  kiyohara 
    374   1.1  kiyohara /* ARGSUSED */
    375   1.1  kiyohara static void
    376   1.1  kiyohara mvgbec_attach(device_t parent, device_t self, void *aux)
    377   1.1  kiyohara {
    378  1.28   msaitoh 	struct mvgbec_softc *csc = device_private(self);
    379   1.1  kiyohara 	struct marvell_attach_args *mva = aux, gbea;
    380   1.1  kiyohara 	struct mvgbe_softc *port;
    381   1.1  kiyohara 	struct mii_softc *mii;
    382   1.1  kiyohara 	device_t child;
    383   1.1  kiyohara 	uint32_t phyaddr;
    384   1.1  kiyohara 	int i, j;
    385   1.1  kiyohara 
    386   1.1  kiyohara 	aprint_naive("\n");
    387   1.1  kiyohara 	aprint_normal(": Marvell Gigabit Ethernet Controller\n");
    388   1.1  kiyohara 
    389  1.28   msaitoh 	csc->sc_dev = self;
    390  1.28   msaitoh 	csc->sc_iot = mva->mva_iot;
    391   1.1  kiyohara 	if (bus_space_subregion(mva->mva_iot, mva->mva_ioh, mva->mva_offset,
    392  1.28   msaitoh 	    mva->mva_size, &csc->sc_ioh)) {
    393   1.1  kiyohara 		aprint_error_dev(self, "Cannot map registers\n");
    394   1.1  kiyohara 		return;
    395   1.1  kiyohara 	}
    396   1.6  christos 
    397   1.7  christos 	if (mvgbec0 == NULL)
    398   1.6  christos 		mvgbec0 = self;
    399  1.17  kiyohara 
    400   1.1  kiyohara 	phyaddr = 0;
    401  1.28   msaitoh 	MVGBE_WRITE(csc, MVGBE_PHYADDR, phyaddr);
    402   1.1  kiyohara 
    403  1.28   msaitoh 	mutex_init(&csc->sc_mtx, MUTEX_DEFAULT, IPL_NET);
    404   1.1  kiyohara 
    405   1.1  kiyohara 	/* Disable and clear Gigabit Ethernet Unit interrupts */
    406  1.28   msaitoh 	MVGBE_WRITE(csc, MVGBE_EUIM, 0);
    407  1.28   msaitoh 	MVGBE_WRITE(csc, MVGBE_EUIC, 0);
    408   1.1  kiyohara 
    409  1.28   msaitoh 	mvgbec_wininit(csc);
    410   1.1  kiyohara 
    411   1.1  kiyohara 	memset(&gbea, 0, sizeof(gbea));
    412   1.1  kiyohara 	for (i = 0; i < __arraycount(mvgbe_ports); i++) {
    413   1.3  kiyohara 		if (mvgbe_ports[i].model != mva->mva_model ||
    414   1.3  kiyohara 		    mvgbe_ports[i].unit != mva->mva_unit)
    415   1.1  kiyohara 			continue;
    416   1.1  kiyohara 
    417  1.28   msaitoh 		csc->sc_flags = mvgbe_ports[i].flags;
    418   1.3  kiyohara 
    419   1.1  kiyohara 		for (j = 0; j < mvgbe_ports[i].ports; j++) {
    420   1.1  kiyohara 			gbea.mva_name = "mvgbe";
    421   1.1  kiyohara 			gbea.mva_model = mva->mva_model;
    422  1.28   msaitoh 			gbea.mva_iot = csc->sc_iot;
    423  1.28   msaitoh 			gbea.mva_ioh = csc->sc_ioh;
    424   1.1  kiyohara 			gbea.mva_unit = j;
    425   1.1  kiyohara 			gbea.mva_dmat = mva->mva_dmat;
    426   1.1  kiyohara 			gbea.mva_irq = mvgbe_ports[i].irqs[j];
    427  1.28   msaitoh 			child = config_found_sm_loc(csc->sc_dev, "mvgbec", NULL,
    428   1.1  kiyohara 			    &gbea, mvgbec_print, mvgbec_search);
    429   1.1  kiyohara 			if (child) {
    430   1.1  kiyohara 				port = device_private(child);
    431   1.1  kiyohara 				mii  = LIST_FIRST(&port->sc_mii.mii_phys);
    432  1.33   msaitoh 				if (mii != NULL)
    433  1.33   msaitoh 					phyaddr |= MVGBE_PHYADDR_PHYAD(j,
    434  1.33   msaitoh 					    mii->mii_phy);
    435   1.1  kiyohara 			}
    436   1.1  kiyohara 		}
    437   1.3  kiyohara 		break;
    438   1.1  kiyohara 	}
    439  1.28   msaitoh 	MVGBE_WRITE(csc, MVGBE_PHYADDR, phyaddr);
    440   1.1  kiyohara }
    441   1.1  kiyohara 
    442   1.1  kiyohara static int
    443   1.1  kiyohara mvgbec_print(void *aux, const char *pnp)
    444   1.1  kiyohara {
    445   1.1  kiyohara 	struct marvell_attach_args *gbea = aux;
    446   1.1  kiyohara 
    447   1.1  kiyohara 	if (pnp)
    448   1.1  kiyohara 		aprint_normal("%s at %s port %d",
    449   1.1  kiyohara 		    gbea->mva_name, pnp, gbea->mva_unit);
    450   1.1  kiyohara 	else {
    451   1.1  kiyohara 		if (gbea->mva_unit != MVGBECCF_PORT_DEFAULT)
    452   1.1  kiyohara 			aprint_normal(" port %d", gbea->mva_unit);
    453   1.1  kiyohara 		if (gbea->mva_irq != MVGBECCF_IRQ_DEFAULT)
    454   1.1  kiyohara 			aprint_normal(" irq %d", gbea->mva_irq);
    455   1.1  kiyohara 	}
    456   1.1  kiyohara 	return UNCONF;
    457   1.1  kiyohara }
    458   1.1  kiyohara 
    459   1.1  kiyohara /* ARGSUSED */
    460   1.1  kiyohara static int
    461   1.1  kiyohara mvgbec_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    462   1.1  kiyohara {
    463   1.1  kiyohara 	struct marvell_attach_args *gbea = aux;
    464   1.1  kiyohara 
    465   1.1  kiyohara 	if (cf->cf_loc[MVGBECCF_PORT] == gbea->mva_unit &&
    466   1.1  kiyohara 	    cf->cf_loc[MVGBECCF_IRQ] != MVGBECCF_IRQ_DEFAULT)
    467   1.1  kiyohara 		gbea->mva_irq = cf->cf_loc[MVGBECCF_IRQ];
    468   1.1  kiyohara 
    469   1.1  kiyohara 	return config_match(parent, cf, aux);
    470   1.1  kiyohara }
    471   1.1  kiyohara 
    472   1.1  kiyohara static int
    473   1.1  kiyohara mvgbec_miibus_readreg(device_t dev, int phy, int reg)
    474   1.1  kiyohara {
    475   1.1  kiyohara 	struct mvgbe_softc *sc = device_private(dev);
    476   1.6  christos 	struct mvgbec_softc *csc;
    477   1.1  kiyohara 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    478   1.1  kiyohara 	uint32_t smi, val;
    479   1.1  kiyohara 	int i;
    480   1.1  kiyohara 
    481   1.6  christos 	if (mvgbec0 == NULL) {
    482   1.6  christos 		aprint_error_ifnet(ifp, "SMI mvgbec0 not found\n");
    483   1.6  christos 		return -1;
    484   1.6  christos 	}
    485   1.6  christos 	csc = device_private(mvgbec0);
    486   1.6  christos 
    487   1.1  kiyohara 	mutex_enter(&csc->sc_mtx);
    488   1.1  kiyohara 
    489   1.1  kiyohara 	for (i = 0; i < MVGBE_PHY_TIMEOUT; i++) {
    490   1.1  kiyohara 		DELAY(1);
    491   1.1  kiyohara 		if (!(MVGBE_READ(csc, MVGBE_SMI) & MVGBE_SMI_BUSY))
    492   1.1  kiyohara 			break;
    493   1.1  kiyohara 	}
    494   1.1  kiyohara 	if (i == MVGBE_PHY_TIMEOUT) {
    495   1.1  kiyohara 		aprint_error_ifnet(ifp, "SMI busy timeout\n");
    496   1.1  kiyohara 		mutex_exit(&csc->sc_mtx);
    497   1.1  kiyohara 		return -1;
    498   1.1  kiyohara 	}
    499   1.1  kiyohara 
    500   1.1  kiyohara 	smi =
    501   1.1  kiyohara 	    MVGBE_SMI_PHYAD(phy) | MVGBE_SMI_REGAD(reg) | MVGBE_SMI_OPCODE_READ;
    502   1.1  kiyohara 	MVGBE_WRITE(csc, MVGBE_SMI, smi);
    503   1.1  kiyohara 
    504   1.1  kiyohara 	for (i = 0; i < MVGBE_PHY_TIMEOUT; i++) {
    505   1.1  kiyohara 		DELAY(1);
    506   1.1  kiyohara 		smi = MVGBE_READ(csc, MVGBE_SMI);
    507   1.1  kiyohara 		if (smi & MVGBE_SMI_READVALID)
    508   1.1  kiyohara 			break;
    509   1.1  kiyohara 	}
    510   1.1  kiyohara 
    511   1.1  kiyohara 	mutex_exit(&csc->sc_mtx);
    512   1.1  kiyohara 
    513   1.1  kiyohara 	DPRINTFN(9, ("mvgbec_miibus_readreg: i=%d, timeout=%d\n",
    514   1.1  kiyohara 	    i, MVGBE_PHY_TIMEOUT));
    515   1.1  kiyohara 
    516   1.1  kiyohara 	val = smi & MVGBE_SMI_DATA_MASK;
    517   1.1  kiyohara 
    518   1.1  kiyohara 	DPRINTFN(9, ("mvgbec_miibus_readreg phy=%d, reg=%#x, val=%#x\n",
    519   1.1  kiyohara 	    phy, reg, val));
    520   1.1  kiyohara 
    521   1.1  kiyohara 	return val;
    522   1.1  kiyohara }
    523   1.1  kiyohara 
    524   1.1  kiyohara static void
    525   1.1  kiyohara mvgbec_miibus_writereg(device_t dev, int phy, int reg, int val)
    526   1.1  kiyohara {
    527   1.1  kiyohara 	struct mvgbe_softc *sc = device_private(dev);
    528   1.6  christos 	struct mvgbec_softc *csc;
    529   1.1  kiyohara 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    530   1.1  kiyohara 	uint32_t smi;
    531   1.1  kiyohara 	int i;
    532   1.1  kiyohara 
    533   1.6  christos 	if (mvgbec0 == NULL) {
    534   1.6  christos 		aprint_error_ifnet(ifp, "SMI mvgbec0 not found\n");
    535   1.6  christos 		return;
    536   1.6  christos 	}
    537   1.6  christos 	csc = device_private(mvgbec0);
    538   1.6  christos 
    539   1.1  kiyohara 	DPRINTFN(9, ("mvgbec_miibus_writereg phy=%d reg=%#x val=%#x\n",
    540   1.1  kiyohara 	     phy, reg, val));
    541   1.1  kiyohara 
    542   1.1  kiyohara 	mutex_enter(&csc->sc_mtx);
    543   1.1  kiyohara 
    544   1.1  kiyohara 	for (i = 0; i < MVGBE_PHY_TIMEOUT; i++) {
    545   1.1  kiyohara 		DELAY(1);
    546   1.1  kiyohara 		if (!(MVGBE_READ(csc, MVGBE_SMI) & MVGBE_SMI_BUSY))
    547   1.1  kiyohara 			break;
    548   1.1  kiyohara 	}
    549   1.1  kiyohara 	if (i == MVGBE_PHY_TIMEOUT) {
    550   1.1  kiyohara 		aprint_error_ifnet(ifp, "SMI busy timeout\n");
    551   1.1  kiyohara 		mutex_exit(&csc->sc_mtx);
    552   1.1  kiyohara 		return;
    553   1.1  kiyohara 	}
    554   1.1  kiyohara 
    555   1.1  kiyohara 	smi = MVGBE_SMI_PHYAD(phy) | MVGBE_SMI_REGAD(reg) |
    556   1.1  kiyohara 	    MVGBE_SMI_OPCODE_WRITE | (val & MVGBE_SMI_DATA_MASK);
    557   1.1  kiyohara 	MVGBE_WRITE(csc, MVGBE_SMI, smi);
    558   1.1  kiyohara 
    559   1.1  kiyohara 	for (i = 0; i < MVGBE_PHY_TIMEOUT; i++) {
    560   1.1  kiyohara 		DELAY(1);
    561   1.1  kiyohara 		if (!(MVGBE_READ(csc, MVGBE_SMI) & MVGBE_SMI_BUSY))
    562   1.1  kiyohara 			break;
    563   1.1  kiyohara 	}
    564   1.1  kiyohara 
    565   1.1  kiyohara 	mutex_exit(&csc->sc_mtx);
    566   1.1  kiyohara 
    567   1.1  kiyohara 	if (i == MVGBE_PHY_TIMEOUT)
    568   1.1  kiyohara 		aprint_error_ifnet(ifp, "phy write timed out\n");
    569   1.1  kiyohara }
    570   1.1  kiyohara 
    571   1.1  kiyohara static void
    572  1.18      matt mvgbec_miibus_statchg(struct ifnet *ifp)
    573   1.1  kiyohara {
    574   1.1  kiyohara 
    575   1.1  kiyohara 	/* nothing to do */
    576   1.1  kiyohara }
    577   1.1  kiyohara 
    578   1.1  kiyohara 
    579   1.1  kiyohara static void
    580   1.1  kiyohara mvgbec_wininit(struct mvgbec_softc *sc)
    581   1.1  kiyohara {
    582   1.1  kiyohara 	device_t pdev = device_parent(sc->sc_dev);
    583   1.1  kiyohara 	uint64_t base;
    584   1.1  kiyohara 	uint32_t en, ac, size;
    585   1.1  kiyohara 	int window, target, attr, rv, i;
    586   1.1  kiyohara 	static int tags[] = {
    587   1.1  kiyohara 		MARVELL_TAG_SDRAM_CS0,
    588   1.1  kiyohara 		MARVELL_TAG_SDRAM_CS1,
    589   1.1  kiyohara 		MARVELL_TAG_SDRAM_CS2,
    590   1.1  kiyohara 		MARVELL_TAG_SDRAM_CS3,
    591   1.1  kiyohara 
    592   1.1  kiyohara 		MARVELL_TAG_UNDEFINED,
    593   1.1  kiyohara 	};
    594   1.1  kiyohara 
    595   1.1  kiyohara 	/* First disable all address decode windows */
    596   1.1  kiyohara 	en = MVGBE_BARE_EN_MASK;
    597   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_BARE, en);
    598   1.1  kiyohara 
    599   1.1  kiyohara 	ac = 0;
    600   1.1  kiyohara 	for (window = 0, i = 0;
    601   1.1  kiyohara 	    tags[i] != MARVELL_TAG_UNDEFINED && window < MVGBE_NWINDOW; i++) {
    602   1.1  kiyohara 		rv = marvell_winparams_by_tag(pdev, tags[i],
    603   1.1  kiyohara 		    &target, &attr, &base, &size);
    604   1.1  kiyohara 		if (rv != 0 || size == 0)
    605   1.1  kiyohara 			continue;
    606   1.1  kiyohara 
    607   1.1  kiyohara 		if (base > 0xffffffffULL) {
    608   1.1  kiyohara 			if (window >= MVGBE_NREMAP) {
    609   1.1  kiyohara 				aprint_error_dev(sc->sc_dev,
    610   1.1  kiyohara 				    "can't remap window %d\n", window);
    611   1.1  kiyohara 				continue;
    612   1.1  kiyohara 			}
    613   1.1  kiyohara 			MVGBE_WRITE(sc, MVGBE_HA(window),
    614   1.1  kiyohara 			    (base >> 32) & 0xffffffff);
    615   1.1  kiyohara 		}
    616   1.1  kiyohara 
    617   1.1  kiyohara 		MVGBE_WRITE(sc, MVGBE_BASEADDR(window),
    618   1.1  kiyohara 		    MVGBE_BASEADDR_TARGET(target)	|
    619   1.1  kiyohara 		    MVGBE_BASEADDR_ATTR(attr)		|
    620   1.1  kiyohara 		    MVGBE_BASEADDR_BASE(base));
    621   1.1  kiyohara 		MVGBE_WRITE(sc, MVGBE_S(window), MVGBE_S_SIZE(size));
    622   1.1  kiyohara 
    623   1.1  kiyohara 		en &= ~(1 << window);
    624   1.1  kiyohara 		/* set full access (r/w) */
    625   1.1  kiyohara 		ac |= MVGBE_EPAP_EPAR(window, MVGBE_EPAP_AC_FA);
    626   1.1  kiyohara 		window++;
    627   1.1  kiyohara 	}
    628   1.1  kiyohara 	/* allow to access decode window */
    629   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_EPAP, ac);
    630   1.1  kiyohara 
    631   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_BARE, en);
    632   1.1  kiyohara }
    633   1.1  kiyohara 
    634   1.1  kiyohara 
    635   1.1  kiyohara /* ARGSUSED */
    636   1.1  kiyohara static int
    637   1.9      matt mvgbe_match(device_t parent, cfdata_t match, void *aux)
    638   1.1  kiyohara {
    639   1.1  kiyohara 	struct marvell_attach_args *mva = aux;
    640   1.1  kiyohara 	uint32_t pbase, maddrh, maddrl;
    641   1.1  kiyohara 
    642   1.1  kiyohara 	pbase = MVGBE_PORTR_BASE + mva->mva_unit * MVGBE_PORTR_SIZE;
    643   1.1  kiyohara 	maddrh =
    644   1.1  kiyohara 	    bus_space_read_4(mva->mva_iot, mva->mva_ioh, pbase + MVGBE_MACAH);
    645   1.1  kiyohara 	maddrl =
    646   1.1  kiyohara 	    bus_space_read_4(mva->mva_iot, mva->mva_ioh, pbase + MVGBE_MACAL);
    647   1.1  kiyohara 	if ((maddrh | maddrl) == 0)
    648   1.1  kiyohara 		return 0;
    649   1.1  kiyohara 
    650   1.1  kiyohara 	return 1;
    651   1.1  kiyohara }
    652   1.1  kiyohara 
    653   1.1  kiyohara /* ARGSUSED */
    654   1.1  kiyohara static void
    655   1.1  kiyohara mvgbe_attach(device_t parent, device_t self, void *aux)
    656   1.1  kiyohara {
    657   1.1  kiyohara 	struct mvgbe_softc *sc = device_private(self);
    658   1.1  kiyohara 	struct marvell_attach_args *mva = aux;
    659   1.1  kiyohara 	struct mvgbe_txmap_entry *entry;
    660   1.1  kiyohara 	struct ifnet *ifp;
    661   1.1  kiyohara 	bus_dma_segment_t seg;
    662   1.1  kiyohara 	bus_dmamap_t dmamap;
    663   1.1  kiyohara 	int rseg, i;
    664   1.1  kiyohara 	uint32_t maddrh, maddrl;
    665   1.1  kiyohara 	void *kva;
    666   1.1  kiyohara 
    667   1.1  kiyohara 	aprint_naive("\n");
    668   1.1  kiyohara 	aprint_normal("\n");
    669   1.1  kiyohara 
    670   1.1  kiyohara 	sc->sc_dev = self;
    671   1.1  kiyohara 	sc->sc_port = mva->mva_unit;
    672   1.1  kiyohara 	sc->sc_iot = mva->mva_iot;
    673  1.27   msaitoh 	callout_init(&sc->sc_tick_ch, 0);
    674  1.27   msaitoh 	callout_setfunc(&sc->sc_tick_ch, mvgbe_tick, sc);
    675   1.1  kiyohara 	if (bus_space_subregion(mva->mva_iot, mva->mva_ioh,
    676   1.1  kiyohara 	    MVGBE_PORTR_BASE + mva->mva_unit * MVGBE_PORTR_SIZE,
    677   1.1  kiyohara 	    MVGBE_PORTR_SIZE, &sc->sc_ioh)) {
    678   1.1  kiyohara 		aprint_error_dev(self, "Cannot map registers\n");
    679   1.1  kiyohara 		return;
    680   1.1  kiyohara 	}
    681   1.1  kiyohara 	if (bus_space_subregion(mva->mva_iot, mva->mva_ioh,
    682   1.1  kiyohara 	    MVGBE_PORTDAFR_BASE + mva->mva_unit * MVGBE_PORTDAFR_SIZE,
    683   1.1  kiyohara 	    MVGBE_PORTDAFR_SIZE, &sc->sc_dafh)) {
    684   1.1  kiyohara 		aprint_error_dev(self,
    685   1.1  kiyohara 		    "Cannot map destination address filter registers\n");
    686   1.1  kiyohara 		return;
    687   1.1  kiyohara 	}
    688   1.1  kiyohara 	sc->sc_dmat = mva->mva_dmat;
    689   1.1  kiyohara 
    690   1.1  kiyohara 	maddrh = MVGBE_READ(sc, MVGBE_MACAH);
    691   1.1  kiyohara 	maddrl = MVGBE_READ(sc, MVGBE_MACAL);
    692   1.1  kiyohara 	sc->sc_enaddr[0] = maddrh >> 24;
    693   1.1  kiyohara 	sc->sc_enaddr[1] = maddrh >> 16;
    694   1.1  kiyohara 	sc->sc_enaddr[2] = maddrh >> 8;
    695   1.1  kiyohara 	sc->sc_enaddr[3] = maddrh >> 0;
    696   1.1  kiyohara 	sc->sc_enaddr[4] = maddrl >> 8;
    697   1.1  kiyohara 	sc->sc_enaddr[5] = maddrl >> 0;
    698   1.1  kiyohara 	aprint_normal_dev(self, "Ethernet address %s\n",
    699   1.1  kiyohara 	    ether_sprintf(sc->sc_enaddr));
    700   1.1  kiyohara 
    701   1.1  kiyohara 	/* clear all ethernet port interrupts */
    702   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_IC, 0);
    703   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_ICE, 0);
    704   1.1  kiyohara 
    705   1.1  kiyohara 	marvell_intr_establish(mva->mva_irq, IPL_NET, mvgbe_intr, sc);
    706   1.1  kiyohara 
    707   1.1  kiyohara 	/* Allocate the descriptor queues. */
    708   1.1  kiyohara 	if (bus_dmamem_alloc(sc->sc_dmat, sizeof(struct mvgbe_ring_data),
    709   1.1  kiyohara 	    PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
    710   1.1  kiyohara 		aprint_error_dev(self, "can't alloc rx buffers\n");
    711   1.1  kiyohara 		return;
    712   1.1  kiyohara 	}
    713   1.1  kiyohara 	if (bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    714   1.1  kiyohara 	    sizeof(struct mvgbe_ring_data), &kva, BUS_DMA_NOWAIT)) {
    715   1.1  kiyohara 		aprint_error_dev(self, "can't map dma buffers (%lu bytes)\n",
    716   1.1  kiyohara 		    (u_long)sizeof(struct mvgbe_ring_data));
    717   1.1  kiyohara 		goto fail1;
    718   1.1  kiyohara 	}
    719   1.1  kiyohara 	if (bus_dmamap_create(sc->sc_dmat, sizeof(struct mvgbe_ring_data), 1,
    720   1.1  kiyohara 	    sizeof(struct mvgbe_ring_data), 0, BUS_DMA_NOWAIT,
    721   1.1  kiyohara 	    &sc->sc_ring_map)) {
    722   1.1  kiyohara 		aprint_error_dev(self, "can't create dma map\n");
    723   1.1  kiyohara 		goto fail2;
    724   1.1  kiyohara 	}
    725   1.1  kiyohara 	if (bus_dmamap_load(sc->sc_dmat, sc->sc_ring_map, kva,
    726   1.1  kiyohara 	    sizeof(struct mvgbe_ring_data), NULL, BUS_DMA_NOWAIT)) {
    727   1.1  kiyohara 		aprint_error_dev(self, "can't load dma map\n");
    728   1.1  kiyohara 		goto fail3;
    729   1.1  kiyohara 	}
    730   1.1  kiyohara 	for (i = 0; i < MVGBE_RX_RING_CNT; i++)
    731   1.1  kiyohara 		sc->sc_cdata.mvgbe_rx_chain[i].mvgbe_mbuf = NULL;
    732   1.1  kiyohara 
    733   1.1  kiyohara 	SIMPLEQ_INIT(&sc->sc_txmap_head);
    734   1.1  kiyohara 	for (i = 0; i < MVGBE_TX_RING_CNT; i++) {
    735   1.1  kiyohara 		sc->sc_cdata.mvgbe_tx_chain[i].mvgbe_mbuf = NULL;
    736   1.1  kiyohara 
    737   1.1  kiyohara 		if (bus_dmamap_create(sc->sc_dmat,
    738   1.1  kiyohara 		    MVGBE_JLEN, MVGBE_NTXSEG, MVGBE_JLEN, 0,
    739   1.1  kiyohara 		    BUS_DMA_NOWAIT, &dmamap)) {
    740   1.1  kiyohara 			aprint_error_dev(self, "Can't create TX dmamap\n");
    741   1.1  kiyohara 			goto fail4;
    742   1.1  kiyohara 		}
    743   1.1  kiyohara 
    744   1.1  kiyohara 		entry = kmem_alloc(sizeof(*entry), KM_SLEEP);
    745   1.1  kiyohara 		if (!entry) {
    746   1.1  kiyohara 			aprint_error_dev(self, "Can't alloc txmap entry\n");
    747   1.1  kiyohara 			bus_dmamap_destroy(sc->sc_dmat, dmamap);
    748   1.1  kiyohara 			goto fail4;
    749   1.1  kiyohara 		}
    750   1.1  kiyohara 		entry->dmamap = dmamap;
    751   1.1  kiyohara 		SIMPLEQ_INSERT_HEAD(&sc->sc_txmap_head, entry, link);
    752   1.1  kiyohara 	}
    753   1.1  kiyohara 
    754   1.1  kiyohara 	sc->sc_rdata = (struct mvgbe_ring_data *)kva;
    755   1.1  kiyohara 	memset(sc->sc_rdata, 0, sizeof(struct mvgbe_ring_data));
    756   1.1  kiyohara 
    757   1.1  kiyohara 	/*
    758   1.1  kiyohara 	 * We can support 802.1Q VLAN-sized frames and jumbo
    759   1.1  kiyohara 	 * Ethernet frames.
    760   1.1  kiyohara 	 */
    761   1.1  kiyohara 	sc->sc_ethercom.ec_capabilities |=
    762   1.5  jakllsch 	    ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU;
    763   1.1  kiyohara 
    764   1.1  kiyohara 	/* Try to allocate memory for jumbo buffers. */
    765   1.1  kiyohara 	if (mvgbe_alloc_jumbo_mem(sc)) {
    766   1.1  kiyohara 		aprint_error_dev(self, "jumbo buffer allocation failed\n");
    767   1.1  kiyohara 		goto fail4;
    768   1.1  kiyohara 	}
    769   1.1  kiyohara 
    770   1.1  kiyohara 	ifp = &sc->sc_ethercom.ec_if;
    771   1.1  kiyohara 	ifp->if_softc = sc;
    772   1.1  kiyohara 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    773   1.1  kiyohara 	ifp->if_start = mvgbe_start;
    774   1.1  kiyohara 	ifp->if_ioctl = mvgbe_ioctl;
    775   1.1  kiyohara 	ifp->if_init = mvgbe_init;
    776   1.1  kiyohara 	ifp->if_stop = mvgbe_stop;
    777   1.1  kiyohara 	ifp->if_watchdog = mvgbe_watchdog;
    778   1.1  kiyohara 	/*
    779   1.1  kiyohara 	 * We can do IPv4/TCPv4/UDPv4 checksums in hardware.
    780   1.1  kiyohara 	 */
    781   1.1  kiyohara 	sc->sc_ethercom.ec_if.if_capabilities |=
    782   1.1  kiyohara 	    IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
    783   1.1  kiyohara 	    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
    784   1.1  kiyohara 	    IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
    785   1.5  jakllsch 	/*
    786   1.5  jakllsch 	 * But, IPv6 packets in the stream can cause incorrect TCPv4 Tx sums.
    787   1.5  jakllsch 	 */
    788   1.5  jakllsch 	sc->sc_ethercom.ec_if.if_capabilities &= ~IFCAP_CSUM_TCPv4_Tx;
    789   1.1  kiyohara 	IFQ_SET_MAXLEN(&ifp->if_snd, max(MVGBE_TX_RING_CNT - 1, IFQ_MAXLEN));
    790   1.1  kiyohara 	IFQ_SET_READY(&ifp->if_snd);
    791   1.1  kiyohara 	strcpy(ifp->if_xname, device_xname(sc->sc_dev));
    792   1.1  kiyohara 
    793   1.1  kiyohara 	mvgbe_stop(ifp, 0);
    794   1.1  kiyohara 
    795   1.1  kiyohara 	/*
    796   1.1  kiyohara 	 * Do MII setup.
    797   1.1  kiyohara 	 */
    798   1.1  kiyohara 	sc->sc_mii.mii_ifp = ifp;
    799   1.1  kiyohara 	sc->sc_mii.mii_readreg = mvgbec_miibus_readreg;
    800   1.1  kiyohara 	sc->sc_mii.mii_writereg = mvgbec_miibus_writereg;
    801   1.1  kiyohara 	sc->sc_mii.mii_statchg = mvgbec_miibus_statchg;
    802   1.1  kiyohara 
    803   1.1  kiyohara 	sc->sc_ethercom.ec_mii = &sc->sc_mii;
    804   1.1  kiyohara 	ifmedia_init(&sc->sc_mii.mii_media, 0,
    805   1.5  jakllsch 	    mvgbe_mediachange, mvgbe_mediastatus);
    806   1.1  kiyohara 	mii_attach(self, &sc->sc_mii, 0xffffffff,
    807   1.8  christos 	    MII_PHY_ANY, parent == mvgbec0 ? 0 : 1, 0);
    808   1.1  kiyohara 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
    809   1.1  kiyohara 		aprint_error_dev(self, "no PHY found!\n");
    810   1.1  kiyohara 		ifmedia_add(&sc->sc_mii.mii_media,
    811   1.1  kiyohara 		    IFM_ETHER|IFM_MANUAL, 0, NULL);
    812   1.1  kiyohara 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_MANUAL);
    813   1.1  kiyohara 	} else
    814   1.1  kiyohara 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    815   1.1  kiyohara 
    816   1.1  kiyohara 	/*
    817   1.1  kiyohara 	 * Call MI attach routines.
    818   1.1  kiyohara 	 */
    819   1.1  kiyohara 	if_attach(ifp);
    820   1.1  kiyohara 
    821   1.1  kiyohara 	ether_ifattach(ifp, sc->sc_enaddr);
    822   1.5  jakllsch 	ether_set_ifflags_cb(&sc->sc_ethercom, mvgbe_ifflags_cb);
    823   1.1  kiyohara 
    824  1.25   msaitoh 	sysctl_mvgbe_init(sc);
    825  1.32   msaitoh #ifdef MVGBE_EVENT_COUNTERS
    826  1.32   msaitoh 	/* Attach event counters. */
    827  1.32   msaitoh 	evcnt_attach_dynamic(&sc->sc_ev_rxoverrun, EVCNT_TYPE_MISC,
    828  1.32   msaitoh 	    NULL, device_xname(sc->sc_dev), "rxoverrrun");
    829  1.32   msaitoh 	evcnt_attach_dynamic(&sc->sc_ev_wdogsoft, EVCNT_TYPE_MISC,
    830  1.32   msaitoh 	    NULL, device_xname(sc->sc_dev), "wdogsoft");
    831  1.32   msaitoh #endif
    832   1.1  kiyohara 	rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
    833   1.1  kiyohara 	    RND_TYPE_NET, 0);
    834   1.1  kiyohara 
    835   1.1  kiyohara 	return;
    836   1.1  kiyohara 
    837   1.1  kiyohara fail4:
    838   1.1  kiyohara 	while ((entry = SIMPLEQ_FIRST(&sc->sc_txmap_head)) != NULL) {
    839   1.1  kiyohara 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txmap_head, link);
    840   1.1  kiyohara 		bus_dmamap_destroy(sc->sc_dmat, entry->dmamap);
    841   1.1  kiyohara 	}
    842   1.1  kiyohara 	bus_dmamap_unload(sc->sc_dmat, sc->sc_ring_map);
    843   1.1  kiyohara fail3:
    844   1.1  kiyohara 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_ring_map);
    845   1.1  kiyohara fail2:
    846   1.1  kiyohara 	bus_dmamem_unmap(sc->sc_dmat, kva, sizeof(struct mvgbe_ring_data));
    847   1.1  kiyohara fail1:
    848   1.1  kiyohara 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    849   1.1  kiyohara 	return;
    850   1.1  kiyohara }
    851   1.1  kiyohara 
    852  1.25   msaitoh static int
    853  1.25   msaitoh mvgbe_ipginttx(struct mvgbec_softc *csc, struct mvgbe_softc *sc,
    854  1.25   msaitoh     unsigned int ipginttx)
    855  1.25   msaitoh {
    856  1.25   msaitoh 	uint32_t reg;
    857  1.25   msaitoh 	reg = MVGBE_READ(sc, MVGBE_PTFUT);
    858  1.25   msaitoh 
    859  1.25   msaitoh 	if (csc->sc_flags & FLAGS_IPG2) {
    860  1.25   msaitoh 		if (ipginttx > MVGBE_PTFUT_IPGINTTX_V2_MAX)
    861  1.25   msaitoh 			return -1;
    862  1.25   msaitoh 		reg &= ~MVGBE_PTFUT_IPGINTTX_V2_MASK;
    863  1.25   msaitoh 		reg |= MVGBE_PTFUT_IPGINTTX_V2(ipginttx);
    864  1.25   msaitoh 	} else if (csc->sc_flags & FLAGS_IPG1) {
    865  1.25   msaitoh 		if (ipginttx > MVGBE_PTFUT_IPGINTTX_V1_MAX)
    866  1.25   msaitoh 			return -1;
    867  1.25   msaitoh 		reg &= ~MVGBE_PTFUT_IPGINTTX_V1_MASK;
    868  1.25   msaitoh 		reg |= MVGBE_PTFUT_IPGINTTX_V1(ipginttx);
    869  1.25   msaitoh 	}
    870  1.25   msaitoh 	MVGBE_WRITE(sc, MVGBE_PTFUT, reg);
    871  1.25   msaitoh 
    872  1.25   msaitoh 	return 0;
    873  1.25   msaitoh }
    874  1.25   msaitoh 
    875  1.25   msaitoh static int
    876  1.25   msaitoh mvgbe_ipgintrx(struct mvgbec_softc *csc, struct mvgbe_softc *sc,
    877  1.25   msaitoh     unsigned int ipgintrx)
    878  1.24   msaitoh {
    879  1.25   msaitoh 	uint32_t reg;
    880  1.25   msaitoh 	reg = MVGBE_READ(sc, MVGBE_SDC);
    881  1.25   msaitoh 
    882  1.25   msaitoh 	if (csc->sc_flags & FLAGS_IPG2) {
    883  1.25   msaitoh 		if (ipgintrx > MVGBE_SDC_IPGINTRX_V2_MAX)
    884  1.25   msaitoh 			return -1;
    885  1.25   msaitoh 		reg &= ~MVGBE_SDC_IPGINTRX_V2_MASK;
    886  1.25   msaitoh 		reg |= MVGBE_SDC_IPGINTRX_V2(ipgintrx);
    887  1.25   msaitoh 	} else if (csc->sc_flags & FLAGS_IPG1) {
    888  1.25   msaitoh 		if (ipgintrx > MVGBE_SDC_IPGINTRX_V1_MAX)
    889  1.25   msaitoh 			return -1;
    890  1.25   msaitoh 		reg &= ~MVGBE_SDC_IPGINTRX_V1_MASK;
    891  1.25   msaitoh 		reg |= MVGBE_SDC_IPGINTRX_V1(ipgintrx);
    892  1.25   msaitoh 	}
    893  1.25   msaitoh 	MVGBE_WRITE(sc, MVGBE_SDC, reg);
    894  1.24   msaitoh 
    895  1.25   msaitoh 	return 0;
    896  1.24   msaitoh }
    897   1.1  kiyohara 
    898  1.27   msaitoh static void
    899  1.27   msaitoh mvgbe_tick(void *arg)
    900  1.27   msaitoh {
    901  1.27   msaitoh 	struct mvgbe_softc *sc = arg;
    902  1.27   msaitoh 	struct mii_data *mii = &sc->sc_mii;
    903  1.27   msaitoh 	int s;
    904  1.27   msaitoh 
    905  1.27   msaitoh 	s = splnet();
    906  1.27   msaitoh 	mii_tick(mii);
    907  1.27   msaitoh 	/* Need more work */
    908  1.32   msaitoh 	MVGBE_EVCNT_ADD(&sc->sc_ev_rxoverrun, MVGBE_READ(sc, MVGBE_POFC));
    909  1.27   msaitoh 	splx(s);
    910  1.27   msaitoh 
    911  1.27   msaitoh 	callout_schedule(&sc->sc_tick_ch, hz);
    912  1.27   msaitoh }
    913  1.27   msaitoh 
    914   1.1  kiyohara static int
    915   1.1  kiyohara mvgbe_intr(void *arg)
    916   1.1  kiyohara {
    917   1.1  kiyohara 	struct mvgbe_softc *sc = arg;
    918   1.1  kiyohara 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    919   1.1  kiyohara 	uint32_t ic, ice, datum = 0;
    920   1.1  kiyohara 	int claimed = 0;
    921   1.1  kiyohara 
    922   1.1  kiyohara 	for (;;) {
    923   1.1  kiyohara 		ice = MVGBE_READ(sc, MVGBE_ICE);
    924   1.1  kiyohara 		ic = MVGBE_READ(sc, MVGBE_IC);
    925   1.1  kiyohara 
    926   1.1  kiyohara 		DPRINTFN(3, ("mvgbe_intr: ic=%#x, ice=%#x\n", ic, ice));
    927   1.1  kiyohara 		if (ic == 0 && ice == 0)
    928   1.1  kiyohara 			break;
    929   1.1  kiyohara 
    930   1.1  kiyohara 		datum = datum ^ ic ^ ice;
    931   1.1  kiyohara 
    932   1.1  kiyohara 		MVGBE_WRITE(sc, MVGBE_IC, ~ic);
    933   1.1  kiyohara 		MVGBE_WRITE(sc, MVGBE_ICE, ~ice);
    934   1.1  kiyohara 
    935   1.1  kiyohara 		claimed = 1;
    936   1.1  kiyohara 
    937  1.26   msaitoh 		if (!(ifp->if_flags & IFF_RUNNING))
    938  1.26   msaitoh 			break;
    939  1.26   msaitoh 
    940   1.1  kiyohara 		if (ice & MVGBE_ICE_LINKCHG) {
    941   1.1  kiyohara 			if (MVGBE_READ(sc, MVGBE_PS) & MVGBE_PS_LINKUP) {
    942   1.1  kiyohara 				/* Enable port RX and TX. */
    943   1.1  kiyohara 				MVGBE_WRITE(sc, MVGBE_RQC, MVGBE_RQC_ENQ(0));
    944   1.1  kiyohara 				MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_ENQ);
    945   1.1  kiyohara 			} else {
    946   1.1  kiyohara 				MVGBE_WRITE(sc, MVGBE_RQC, MVGBE_RQC_DISQ(0));
    947   1.1  kiyohara 				MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_DISQ);
    948   1.1  kiyohara 			}
    949  1.30   msaitoh 
    950  1.30   msaitoh 			/* Notify link change event to mii layer */
    951  1.30   msaitoh 			mii_pollstat(&sc->sc_mii);
    952   1.1  kiyohara 		}
    953   1.1  kiyohara 
    954   1.1  kiyohara 		if (ic & (MVGBE_IC_RXBUF | MVGBE_IC_RXERROR))
    955   1.1  kiyohara 			mvgbe_rxeof(sc);
    956   1.1  kiyohara 
    957   1.1  kiyohara 		if (ice & (MVGBE_ICE_TXBUF | MVGBE_ICE_TXERR))
    958   1.1  kiyohara 			mvgbe_txeof(sc);
    959   1.1  kiyohara 	}
    960   1.1  kiyohara 
    961   1.1  kiyohara 	if (!IFQ_IS_EMPTY(&ifp->if_snd))
    962   1.1  kiyohara 		mvgbe_start(ifp);
    963   1.1  kiyohara 
    964  1.16       tls 	rnd_add_uint32(&sc->sc_rnd_source, datum);
    965   1.1  kiyohara 
    966   1.1  kiyohara 	return claimed;
    967   1.1  kiyohara }
    968   1.1  kiyohara 
    969   1.1  kiyohara static void
    970   1.1  kiyohara mvgbe_start(struct ifnet *ifp)
    971   1.1  kiyohara {
    972   1.1  kiyohara 	struct mvgbe_softc *sc = ifp->if_softc;
    973   1.1  kiyohara 	struct mbuf *m_head = NULL;
    974   1.1  kiyohara 	uint32_t idx = sc->sc_cdata.mvgbe_tx_prod;
    975   1.1  kiyohara 	int pkts = 0;
    976   1.1  kiyohara 
    977   1.1  kiyohara 	DPRINTFN(3, ("mvgbe_start (idx %d, tx_chain[idx] %p)\n", idx,
    978   1.1  kiyohara 	    sc->sc_cdata.mvgbe_tx_chain[idx].mvgbe_mbuf));
    979   1.1  kiyohara 
    980   1.1  kiyohara 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    981   1.1  kiyohara 		return;
    982   1.1  kiyohara 	/* If Link is DOWN, can't start TX */
    983   1.1  kiyohara 	if (!(MVGBE_READ(sc, MVGBE_PS) & MVGBE_PS_LINKUP))
    984   1.1  kiyohara 		return;
    985   1.1  kiyohara 
    986   1.1  kiyohara 	while (sc->sc_cdata.mvgbe_tx_chain[idx].mvgbe_mbuf == NULL) {
    987   1.1  kiyohara 		IFQ_POLL(&ifp->if_snd, m_head);
    988   1.1  kiyohara 		if (m_head == NULL)
    989   1.1  kiyohara 			break;
    990   1.1  kiyohara 
    991   1.1  kiyohara 		/*
    992   1.1  kiyohara 		 * Pack the data into the transmit ring. If we
    993   1.1  kiyohara 		 * don't have room, set the OACTIVE flag and wait
    994   1.1  kiyohara 		 * for the NIC to drain the ring.
    995   1.1  kiyohara 		 */
    996   1.1  kiyohara 		if (mvgbe_encap(sc, m_head, &idx)) {
    997   1.1  kiyohara 			ifp->if_flags |= IFF_OACTIVE;
    998   1.1  kiyohara 			break;
    999   1.1  kiyohara 		}
   1000   1.1  kiyohara 
   1001   1.1  kiyohara 		/* now we are committed to transmit the packet */
   1002   1.1  kiyohara 		IFQ_DEQUEUE(&ifp->if_snd, m_head);
   1003   1.1  kiyohara 		pkts++;
   1004   1.1  kiyohara 
   1005   1.1  kiyohara 		/*
   1006   1.1  kiyohara 		 * If there's a BPF listener, bounce a copy of this frame
   1007   1.1  kiyohara 		 * to him.
   1008   1.1  kiyohara 		 */
   1009  1.10       rjs 		bpf_mtap(ifp, m_head);
   1010   1.1  kiyohara 	}
   1011   1.1  kiyohara 	if (pkts == 0)
   1012   1.1  kiyohara 		return;
   1013   1.1  kiyohara 
   1014   1.1  kiyohara 	/* Transmit at Queue 0 */
   1015   1.1  kiyohara 	if (idx != sc->sc_cdata.mvgbe_tx_prod) {
   1016   1.1  kiyohara 		sc->sc_cdata.mvgbe_tx_prod = idx;
   1017   1.1  kiyohara 		MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_ENQ);
   1018   1.1  kiyohara 
   1019   1.1  kiyohara 		/*
   1020   1.1  kiyohara 		 * Set a timeout in case the chip goes out to lunch.
   1021   1.1  kiyohara 		 */
   1022  1.20   msaitoh 		ifp->if_timer = 1;
   1023  1.20   msaitoh 		sc->sc_wdogsoft = 1;
   1024   1.1  kiyohara 	}
   1025   1.1  kiyohara }
   1026   1.1  kiyohara 
   1027   1.1  kiyohara static int
   1028   1.5  jakllsch mvgbe_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1029   1.1  kiyohara {
   1030   1.1  kiyohara 	struct mvgbe_softc *sc = ifp->if_softc;
   1031   1.1  kiyohara 	struct ifreq *ifr = data;
   1032   1.1  kiyohara 	int s, error = 0;
   1033   1.1  kiyohara 
   1034   1.1  kiyohara 	s = splnet();
   1035   1.1  kiyohara 
   1036   1.5  jakllsch 	switch (cmd) {
   1037   1.1  kiyohara 	case SIOCGIFMEDIA:
   1038   1.1  kiyohara 	case SIOCSIFMEDIA:
   1039   1.1  kiyohara 		DPRINTFN(2, ("mvgbe_ioctl MEDIA\n"));
   1040   1.5  jakllsch 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
   1041   1.1  kiyohara 		break;
   1042   1.1  kiyohara 	default:
   1043   1.1  kiyohara 		DPRINTFN(2, ("mvgbe_ioctl ETHER\n"));
   1044   1.5  jakllsch 		error = ether_ioctl(ifp, cmd, data);
   1045   1.1  kiyohara 		if (error == ENETRESET) {
   1046   1.1  kiyohara 			if (ifp->if_flags & IFF_RUNNING) {
   1047   1.5  jakllsch 				mvgbe_filter_setup(sc);
   1048   1.1  kiyohara 			}
   1049   1.1  kiyohara 			error = 0;
   1050   1.1  kiyohara 		}
   1051   1.1  kiyohara 		break;
   1052   1.1  kiyohara 	}
   1053   1.1  kiyohara 
   1054   1.1  kiyohara 	splx(s);
   1055   1.1  kiyohara 
   1056   1.1  kiyohara 	return error;
   1057   1.1  kiyohara }
   1058   1.1  kiyohara 
   1059   1.1  kiyohara static int
   1060   1.1  kiyohara mvgbe_init(struct ifnet *ifp)
   1061   1.1  kiyohara {
   1062   1.1  kiyohara 	struct mvgbe_softc *sc = ifp->if_softc;
   1063   1.3  kiyohara 	struct mvgbec_softc *csc = device_private(device_parent(sc->sc_dev));
   1064   1.1  kiyohara 	struct mii_data *mii = &sc->sc_mii;
   1065   1.3  kiyohara 	uint32_t reg;
   1066   1.5  jakllsch 	int i;
   1067   1.1  kiyohara 
   1068   1.1  kiyohara 	DPRINTFN(2, ("mvgbe_init\n"));
   1069   1.1  kiyohara 
   1070   1.1  kiyohara 	/* Cancel pending I/O and free all RX/TX buffers. */
   1071   1.1  kiyohara 	mvgbe_stop(ifp, 0);
   1072   1.1  kiyohara 
   1073   1.1  kiyohara 	/* clear all ethernet port interrupts */
   1074   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_IC, 0);
   1075   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_ICE, 0);
   1076   1.1  kiyohara 
   1077   1.1  kiyohara 	/* Init TX/RX descriptors */
   1078   1.1  kiyohara 	if (mvgbe_init_tx_ring(sc) == ENOBUFS) {
   1079   1.1  kiyohara 		aprint_error_ifnet(ifp,
   1080   1.1  kiyohara 		    "initialization failed: no memory for tx buffers\n");
   1081   1.1  kiyohara 		return ENOBUFS;
   1082   1.1  kiyohara 	}
   1083   1.1  kiyohara 	if (mvgbe_init_rx_ring(sc) == ENOBUFS) {
   1084   1.1  kiyohara 		aprint_error_ifnet(ifp,
   1085   1.1  kiyohara 		    "initialization failed: no memory for rx buffers\n");
   1086   1.1  kiyohara 		return ENOBUFS;
   1087   1.1  kiyohara 	}
   1088   1.1  kiyohara 
   1089  1.25   msaitoh 	if ((csc->sc_flags & FLAGS_IPG1) || (csc->sc_flags & FLAGS_IPG2)) {
   1090  1.25   msaitoh 		sc->sc_ipginttx = MVGBE_IPGINTTX_DEFAULT;
   1091  1.25   msaitoh 		sc->sc_ipgintrx = MVGBE_IPGINTRX_DEFAULT;
   1092  1.25   msaitoh 	}
   1093  1.13       rjs 	if (csc->sc_flags & FLAGS_FIX_MTU)
   1094  1.13       rjs 		MVGBE_WRITE(sc, MVGBE_MTU, 0);	/* hw reset value is wrong */
   1095   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_PSC,
   1096   1.1  kiyohara 	    MVGBE_PSC_ANFC |			/* Enable Auto-Neg Flow Ctrl */
   1097   1.1  kiyohara 	    MVGBE_PSC_RESERVED |		/* Must be set to 1 */
   1098   1.1  kiyohara 	    MVGBE_PSC_FLFAIL |			/* Do NOT Force Link Fail */
   1099   1.5  jakllsch 	    MVGBE_PSC_MRU(MVGBE_PSC_MRU_9022) | /* we want 9k */
   1100   1.1  kiyohara 	    MVGBE_PSC_SETFULLDX);		/* Set_FullDx */
   1101   1.3  kiyohara 	/* XXXX: mvgbe(4) always use RGMII. */
   1102   1.3  kiyohara 	MVGBE_WRITE(sc, MVGBE_PSC1,
   1103   1.3  kiyohara 	    MVGBE_READ(sc, MVGBE_PSC1) | MVGBE_PSC1_RGMIIEN);
   1104   1.3  kiyohara 	/* XXXX: Also always Weighted Round-Robin Priority Mode */
   1105   1.3  kiyohara 	MVGBE_WRITE(sc, MVGBE_TQFPC, MVGBE_TQFPC_EN(0));
   1106   1.1  kiyohara 
   1107   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_CRDP(0), MVGBE_RX_RING_ADDR(sc, 0));
   1108   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_TCQDP, MVGBE_TX_RING_ADDR(sc, 0));
   1109   1.1  kiyohara 
   1110  1.13       rjs 	if (csc->sc_flags & FLAGS_FIX_TQTB) {
   1111   1.1  kiyohara 		/*
   1112   1.3  kiyohara 		 * Queue 0 (offset 0x72700) must be programmed to 0x3fffffff.
   1113   1.3  kiyohara 		 * And offset 0x72704 must be programmed to 0x03ffffff.
   1114   1.3  kiyohara 		 * Queue 1 through 7 must be programmed to 0x0.
   1115   1.1  kiyohara 		 */
   1116   1.3  kiyohara 		MVGBE_WRITE(sc, MVGBE_TQTBCOUNT(0), 0x3fffffff);
   1117   1.3  kiyohara 		MVGBE_WRITE(sc, MVGBE_TQTBCONFIG(0), 0x03ffffff);
   1118   1.3  kiyohara 		for (i = 1; i < 8; i++) {
   1119   1.3  kiyohara 			MVGBE_WRITE(sc, MVGBE_TQTBCOUNT(i), 0x0);
   1120   1.3  kiyohara 			MVGBE_WRITE(sc, MVGBE_TQTBCONFIG(i), 0x0);
   1121   1.3  kiyohara 		}
   1122   1.3  kiyohara 	} else
   1123   1.3  kiyohara 		for (i = 1; i < 8; i++) {
   1124   1.3  kiyohara 			MVGBE_WRITE(sc, MVGBE_TQTBCOUNT(i), 0x3fffffff);
   1125   1.3  kiyohara 			MVGBE_WRITE(sc, MVGBE_TQTBCONFIG(i), 0xffff7fff);
   1126   1.3  kiyohara 			MVGBE_WRITE(sc, MVGBE_TQAC(i), 0xfc0000ff);
   1127   1.3  kiyohara 		}
   1128   1.1  kiyohara 
   1129   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_PXC, MVGBE_PXC_RXCS);
   1130   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_PXCX, 0);
   1131  1.25   msaitoh 
   1132  1.25   msaitoh 	/* Set SDC register except IPGINT bits */
   1133   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_SDC,
   1134   1.1  kiyohara 	    MVGBE_SDC_RXBSZ_16_64BITWORDS |
   1135   1.1  kiyohara #if BYTE_ORDER == LITTLE_ENDIAN
   1136   1.4  jakllsch 	    MVGBE_SDC_BLMR |	/* Big/Little Endian Receive Mode: No swap */
   1137   1.4  jakllsch 	    MVGBE_SDC_BLMT |	/* Big/Little Endian Transmit Mode: No swap */
   1138   1.1  kiyohara #endif
   1139   1.1  kiyohara 	    MVGBE_SDC_TXBSZ_16_64BITWORDS);
   1140  1.25   msaitoh 	/* And then set IPGINT bits */
   1141  1.25   msaitoh 	mvgbe_ipgintrx(csc, sc, sc->sc_ipgintrx);
   1142  1.25   msaitoh 
   1143  1.25   msaitoh 	/* Tx side */
   1144  1.25   msaitoh 	MVGBE_WRITE(sc, MVGBE_PTFUT, 0);
   1145  1.25   msaitoh 	mvgbe_ipginttx(csc, sc, sc->sc_ipginttx);
   1146   1.5  jakllsch 
   1147   1.5  jakllsch 	mvgbe_filter_setup(sc);
   1148   1.1  kiyohara 
   1149   1.1  kiyohara 	mii_mediachg(mii);
   1150   1.1  kiyohara 
   1151   1.1  kiyohara 	/* Enable port */
   1152   1.1  kiyohara 	reg = MVGBE_READ(sc, MVGBE_PSC);
   1153   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_PSC, reg | MVGBE_PSC_PORTEN);
   1154   1.1  kiyohara 
   1155   1.1  kiyohara 	/* If Link is UP, Start RX and TX traffic */
   1156   1.1  kiyohara 	if (MVGBE_READ(sc, MVGBE_PS) & MVGBE_PS_LINKUP) {
   1157   1.1  kiyohara 		/* Enable port RX/TX. */
   1158   1.1  kiyohara 		MVGBE_WRITE(sc, MVGBE_RQC, MVGBE_RQC_ENQ(0));
   1159   1.1  kiyohara 		MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_ENQ);
   1160   1.1  kiyohara 	}
   1161   1.1  kiyohara 
   1162   1.1  kiyohara 	/* Enable interrupt masks */
   1163   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_PIM,
   1164   1.1  kiyohara 	    MVGBE_IC_RXBUF |
   1165   1.1  kiyohara 	    MVGBE_IC_EXTEND |
   1166   1.1  kiyohara 	    MVGBE_IC_RXBUFQ_MASK |
   1167   1.1  kiyohara 	    MVGBE_IC_RXERROR |
   1168   1.1  kiyohara 	    MVGBE_IC_RXERRQ_MASK);
   1169   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_PEIM,
   1170   1.1  kiyohara 	    MVGBE_ICE_TXBUF |
   1171   1.1  kiyohara 	    MVGBE_ICE_TXERR |
   1172   1.1  kiyohara 	    MVGBE_ICE_LINKCHG);
   1173   1.1  kiyohara 
   1174  1.27   msaitoh 	callout_schedule(&sc->sc_tick_ch, hz);
   1175  1.27   msaitoh 
   1176   1.1  kiyohara 	ifp->if_flags |= IFF_RUNNING;
   1177   1.1  kiyohara 	ifp->if_flags &= ~IFF_OACTIVE;
   1178   1.1  kiyohara 
   1179   1.1  kiyohara 	return 0;
   1180   1.1  kiyohara }
   1181   1.1  kiyohara 
   1182   1.1  kiyohara /* ARGSUSED */
   1183   1.1  kiyohara static void
   1184   1.1  kiyohara mvgbe_stop(struct ifnet *ifp, int disable)
   1185   1.1  kiyohara {
   1186   1.1  kiyohara 	struct mvgbe_softc *sc = ifp->if_softc;
   1187  1.26   msaitoh 	struct mvgbec_softc *csc = device_private(device_parent(sc->sc_dev));
   1188   1.1  kiyohara 	struct mvgbe_chain_data *cdata = &sc->sc_cdata;
   1189   1.1  kiyohara 	uint32_t reg;
   1190   1.1  kiyohara 	int i, cnt;
   1191   1.1  kiyohara 
   1192   1.1  kiyohara 	DPRINTFN(2, ("mvgbe_stop\n"));
   1193   1.1  kiyohara 
   1194  1.27   msaitoh 	callout_stop(&sc->sc_tick_ch);
   1195  1.27   msaitoh 
   1196   1.1  kiyohara 	/* Stop Rx port activity. Check port Rx activity. */
   1197   1.1  kiyohara 	reg = MVGBE_READ(sc, MVGBE_RQC);
   1198   1.1  kiyohara 	if (reg & MVGBE_RQC_ENQ_MASK)
   1199   1.1  kiyohara 		/* Issue stop command for active channels only */
   1200   1.1  kiyohara 		MVGBE_WRITE(sc, MVGBE_RQC, MVGBE_RQC_DISQ_DISABLE(reg));
   1201   1.1  kiyohara 
   1202   1.1  kiyohara 	/* Stop Tx port activity. Check port Tx activity. */
   1203   1.1  kiyohara 	if (MVGBE_READ(sc, MVGBE_TQC) & MVGBE_TQC_ENQ)
   1204   1.1  kiyohara 		MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_DISQ);
   1205   1.1  kiyohara 
   1206   1.1  kiyohara 	/* Force link down */
   1207   1.1  kiyohara 	reg = MVGBE_READ(sc, MVGBE_PSC);
   1208   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_PSC, reg & ~MVGBE_PSC_FLFAIL);
   1209   1.1  kiyohara 
   1210   1.1  kiyohara #define RX_DISABLE_TIMEOUT          0x1000000
   1211   1.1  kiyohara #define TX_FIFO_EMPTY_TIMEOUT       0x1000000
   1212   1.1  kiyohara 	/* Wait for all Rx activity to terminate. */
   1213   1.1  kiyohara 	cnt = 0;
   1214   1.1  kiyohara 	do {
   1215   1.1  kiyohara 		if (cnt >= RX_DISABLE_TIMEOUT) {
   1216   1.1  kiyohara 			aprint_error_ifnet(ifp,
   1217   1.1  kiyohara 			    "timeout for RX stopped. rqc 0x%x\n", reg);
   1218   1.1  kiyohara 			break;
   1219   1.1  kiyohara 		}
   1220   1.1  kiyohara 		cnt++;
   1221   1.1  kiyohara 
   1222   1.1  kiyohara 		/*
   1223   1.1  kiyohara 		 * Check Receive Queue Command register that all Rx queues
   1224   1.1  kiyohara 		 * are stopped
   1225   1.1  kiyohara 		 */
   1226   1.1  kiyohara 		reg = MVGBE_READ(sc, MVGBE_RQC);
   1227   1.1  kiyohara 	} while (reg & 0xff);
   1228   1.1  kiyohara 
   1229   1.1  kiyohara 	/* Double check to verify that TX FIFO is empty */
   1230   1.1  kiyohara 	cnt = 0;
   1231   1.1  kiyohara 	while (1) {
   1232   1.1  kiyohara 		do {
   1233   1.1  kiyohara 			if (cnt >= TX_FIFO_EMPTY_TIMEOUT) {
   1234   1.1  kiyohara 				aprint_error_ifnet(ifp,
   1235   1.1  kiyohara 				    "timeout for TX FIFO empty. status 0x%x\n",
   1236   1.1  kiyohara 				    reg);
   1237   1.1  kiyohara 				break;
   1238   1.1  kiyohara 			}
   1239   1.1  kiyohara 			cnt++;
   1240   1.1  kiyohara 
   1241   1.1  kiyohara 			reg = MVGBE_READ(sc, MVGBE_PS);
   1242   1.1  kiyohara 		} while
   1243   1.1  kiyohara 		    (!(reg & MVGBE_PS_TXFIFOEMP) || reg & MVGBE_PS_TXINPROG);
   1244   1.1  kiyohara 
   1245   1.1  kiyohara 		if (cnt >= TX_FIFO_EMPTY_TIMEOUT)
   1246   1.1  kiyohara 			break;
   1247   1.1  kiyohara 
   1248   1.1  kiyohara 		/* Double check */
   1249   1.1  kiyohara 		reg = MVGBE_READ(sc, MVGBE_PS);
   1250   1.1  kiyohara 		if (reg & MVGBE_PS_TXFIFOEMP && !(reg & MVGBE_PS_TXINPROG))
   1251   1.1  kiyohara 			break;
   1252   1.1  kiyohara 		else
   1253   1.1  kiyohara 			aprint_error_ifnet(ifp,
   1254   1.1  kiyohara 			    "TX FIFO empty double check failed."
   1255   1.1  kiyohara 			    " %d loops, status 0x%x\n", cnt, reg);
   1256   1.1  kiyohara 	}
   1257   1.1  kiyohara 
   1258   1.1  kiyohara 	/* Reset the Enable bit in the Port Serial Control Register */
   1259   1.1  kiyohara 	reg = MVGBE_READ(sc, MVGBE_PSC);
   1260   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_PSC, reg & ~MVGBE_PSC_PORTEN);
   1261   1.1  kiyohara 
   1262  1.26   msaitoh 	/*
   1263  1.26   msaitoh 	 * Disable and clear interrupts
   1264  1.26   msaitoh 	 * 0) controller interrupt
   1265  1.26   msaitoh 	 * 1) port interrupt cause
   1266  1.26   msaitoh 	 * 2) port interrupt mask
   1267  1.26   msaitoh 	 */
   1268  1.26   msaitoh 	MVGBE_WRITE(csc, MVGBE_EUIM, 0);
   1269  1.26   msaitoh 	MVGBE_WRITE(csc, MVGBE_EUIC, 0);
   1270  1.26   msaitoh 	MVGBE_WRITE(sc, MVGBE_IC, 0);
   1271  1.26   msaitoh 	MVGBE_WRITE(sc, MVGBE_ICE, 0);
   1272   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_PIM, 0);
   1273   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_PEIM, 0);
   1274   1.1  kiyohara 
   1275   1.1  kiyohara 	/* Free RX and TX mbufs still in the queues. */
   1276   1.1  kiyohara 	for (i = 0; i < MVGBE_RX_RING_CNT; i++) {
   1277   1.1  kiyohara 		if (cdata->mvgbe_rx_chain[i].mvgbe_mbuf != NULL) {
   1278   1.1  kiyohara 			m_freem(cdata->mvgbe_rx_chain[i].mvgbe_mbuf);
   1279   1.1  kiyohara 			cdata->mvgbe_rx_chain[i].mvgbe_mbuf = NULL;
   1280   1.1  kiyohara 		}
   1281   1.1  kiyohara 	}
   1282   1.1  kiyohara 	for (i = 0; i < MVGBE_TX_RING_CNT; i++) {
   1283   1.1  kiyohara 		if (cdata->mvgbe_tx_chain[i].mvgbe_mbuf != NULL) {
   1284   1.1  kiyohara 			m_freem(cdata->mvgbe_tx_chain[i].mvgbe_mbuf);
   1285   1.1  kiyohara 			cdata->mvgbe_tx_chain[i].mvgbe_mbuf = NULL;
   1286   1.1  kiyohara 		}
   1287   1.1  kiyohara 	}
   1288   1.1  kiyohara 
   1289   1.1  kiyohara 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1290   1.1  kiyohara }
   1291   1.1  kiyohara 
   1292   1.1  kiyohara static void
   1293   1.1  kiyohara mvgbe_watchdog(struct ifnet *ifp)
   1294   1.1  kiyohara {
   1295   1.1  kiyohara 	struct mvgbe_softc *sc = ifp->if_softc;
   1296   1.1  kiyohara 
   1297   1.1  kiyohara 	/*
   1298   1.1  kiyohara 	 * Reclaim first as there is a possibility of losing Tx completion
   1299   1.1  kiyohara 	 * interrupts.
   1300   1.1  kiyohara 	 */
   1301   1.1  kiyohara 	mvgbe_txeof(sc);
   1302   1.1  kiyohara 	if (sc->sc_cdata.mvgbe_tx_cnt != 0) {
   1303  1.20   msaitoh 		if (sc->sc_wdogsoft) {
   1304  1.20   msaitoh 			/*
   1305  1.20   msaitoh 			 * There is race condition between CPU and DMA
   1306  1.20   msaitoh 			 * engine. When DMA engine encounters queue end,
   1307  1.20   msaitoh 			 * it clears MVGBE_TQC_ENQ bit.
   1308  1.20   msaitoh 			 */
   1309  1.20   msaitoh 			MVGBE_WRITE(sc, MVGBE_TQC, MVGBE_TQC_ENQ);
   1310  1.20   msaitoh 			ifp->if_timer = 5;
   1311  1.20   msaitoh 			sc->sc_wdogsoft = 0;
   1312  1.32   msaitoh 			MVGBE_EVCNT_INCR(&sc->sc_ev_wdogsoft);
   1313  1.20   msaitoh 		} else {
   1314  1.20   msaitoh 			aprint_error_ifnet(ifp, "watchdog timeout\n");
   1315   1.1  kiyohara 
   1316  1.20   msaitoh 			ifp->if_oerrors++;
   1317   1.1  kiyohara 
   1318  1.20   msaitoh 			mvgbe_init(ifp);
   1319  1.20   msaitoh 		}
   1320   1.1  kiyohara 	}
   1321   1.1  kiyohara }
   1322   1.1  kiyohara 
   1323   1.5  jakllsch static int
   1324   1.5  jakllsch mvgbe_ifflags_cb(struct ethercom *ec)
   1325   1.5  jakllsch {
   1326   1.5  jakllsch 	struct ifnet *ifp = &ec->ec_if;
   1327   1.5  jakllsch 	struct mvgbe_softc *sc = ifp->if_softc;
   1328   1.5  jakllsch 	int change = ifp->if_flags ^ sc->sc_if_flags;
   1329   1.5  jakllsch 
   1330   1.5  jakllsch 	if (change != 0)
   1331   1.5  jakllsch 		sc->sc_if_flags = ifp->if_flags;
   1332   1.5  jakllsch 
   1333   1.5  jakllsch 	if ((change & ~(IFF_CANTCHANGE|IFF_DEBUG)) != 0)
   1334   1.5  jakllsch 		return ENETRESET;
   1335   1.5  jakllsch 
   1336   1.5  jakllsch 	if ((change & IFF_PROMISC) != 0)
   1337   1.5  jakllsch 		mvgbe_filter_setup(sc);
   1338   1.5  jakllsch 
   1339   1.5  jakllsch 	return 0;
   1340   1.5  jakllsch }
   1341   1.1  kiyohara 
   1342   1.1  kiyohara /*
   1343   1.1  kiyohara  * Set media options.
   1344   1.1  kiyohara  */
   1345   1.1  kiyohara static int
   1346   1.5  jakllsch mvgbe_mediachange(struct ifnet *ifp)
   1347   1.1  kiyohara {
   1348   1.5  jakllsch 	return ether_mediachange(ifp);
   1349   1.1  kiyohara }
   1350   1.1  kiyohara 
   1351   1.1  kiyohara /*
   1352   1.1  kiyohara  * Report current media status.
   1353   1.1  kiyohara  */
   1354   1.1  kiyohara static void
   1355   1.5  jakllsch mvgbe_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   1356   1.1  kiyohara {
   1357   1.5  jakllsch 	ether_mediastatus(ifp, ifmr);
   1358   1.1  kiyohara }
   1359   1.1  kiyohara 
   1360   1.1  kiyohara 
   1361   1.1  kiyohara static int
   1362   1.1  kiyohara mvgbe_init_rx_ring(struct mvgbe_softc *sc)
   1363   1.1  kiyohara {
   1364   1.1  kiyohara 	struct mvgbe_chain_data *cd = &sc->sc_cdata;
   1365   1.1  kiyohara 	struct mvgbe_ring_data *rd = sc->sc_rdata;
   1366   1.1  kiyohara 	int i;
   1367   1.1  kiyohara 
   1368   1.5  jakllsch 	memset(rd->mvgbe_rx_ring, 0,
   1369   1.1  kiyohara 	    sizeof(struct mvgbe_rx_desc) * MVGBE_RX_RING_CNT);
   1370   1.1  kiyohara 
   1371   1.1  kiyohara 	for (i = 0; i < MVGBE_RX_RING_CNT; i++) {
   1372   1.1  kiyohara 		cd->mvgbe_rx_chain[i].mvgbe_desc =
   1373   1.1  kiyohara 		    &rd->mvgbe_rx_ring[i];
   1374   1.1  kiyohara 		if (i == MVGBE_RX_RING_CNT - 1) {
   1375   1.1  kiyohara 			cd->mvgbe_rx_chain[i].mvgbe_next =
   1376   1.1  kiyohara 			    &cd->mvgbe_rx_chain[0];
   1377   1.1  kiyohara 			rd->mvgbe_rx_ring[i].nextdescptr =
   1378   1.1  kiyohara 			    MVGBE_RX_RING_ADDR(sc, 0);
   1379   1.1  kiyohara 		} else {
   1380   1.1  kiyohara 			cd->mvgbe_rx_chain[i].mvgbe_next =
   1381   1.1  kiyohara 			    &cd->mvgbe_rx_chain[i + 1];
   1382   1.1  kiyohara 			rd->mvgbe_rx_ring[i].nextdescptr =
   1383   1.1  kiyohara 			    MVGBE_RX_RING_ADDR(sc, i + 1);
   1384   1.1  kiyohara 		}
   1385   1.1  kiyohara 	}
   1386   1.1  kiyohara 
   1387   1.1  kiyohara 	for (i = 0; i < MVGBE_RX_RING_CNT; i++) {
   1388   1.1  kiyohara 		if (mvgbe_newbuf(sc, i, NULL,
   1389   1.1  kiyohara 		    sc->sc_cdata.mvgbe_rx_jumbo_map) == ENOBUFS) {
   1390   1.1  kiyohara 			aprint_error_ifnet(&sc->sc_ethercom.ec_if,
   1391   1.1  kiyohara 			    "failed alloc of %dth mbuf\n", i);
   1392   1.1  kiyohara 			return ENOBUFS;
   1393   1.1  kiyohara 		}
   1394   1.1  kiyohara 	}
   1395   1.1  kiyohara 	sc->sc_cdata.mvgbe_rx_prod = 0;
   1396   1.1  kiyohara 	sc->sc_cdata.mvgbe_rx_cons = 0;
   1397   1.1  kiyohara 
   1398   1.1  kiyohara 	return 0;
   1399   1.1  kiyohara }
   1400   1.1  kiyohara 
   1401   1.1  kiyohara static int
   1402   1.1  kiyohara mvgbe_init_tx_ring(struct mvgbe_softc *sc)
   1403   1.1  kiyohara {
   1404   1.1  kiyohara 	struct mvgbe_chain_data *cd = &sc->sc_cdata;
   1405   1.1  kiyohara 	struct mvgbe_ring_data *rd = sc->sc_rdata;
   1406   1.1  kiyohara 	int i;
   1407   1.1  kiyohara 
   1408   1.5  jakllsch 	memset(sc->sc_rdata->mvgbe_tx_ring, 0,
   1409   1.1  kiyohara 	    sizeof(struct mvgbe_tx_desc) * MVGBE_TX_RING_CNT);
   1410   1.1  kiyohara 
   1411   1.1  kiyohara 	for (i = 0; i < MVGBE_TX_RING_CNT; i++) {
   1412   1.1  kiyohara 		cd->mvgbe_tx_chain[i].mvgbe_desc =
   1413   1.1  kiyohara 		    &rd->mvgbe_tx_ring[i];
   1414   1.1  kiyohara 		if (i == MVGBE_TX_RING_CNT - 1) {
   1415   1.1  kiyohara 			cd->mvgbe_tx_chain[i].mvgbe_next =
   1416   1.1  kiyohara 			    &cd->mvgbe_tx_chain[0];
   1417   1.1  kiyohara 			rd->mvgbe_tx_ring[i].nextdescptr =
   1418   1.1  kiyohara 			    MVGBE_TX_RING_ADDR(sc, 0);
   1419   1.1  kiyohara 		} else {
   1420   1.1  kiyohara 			cd->mvgbe_tx_chain[i].mvgbe_next =
   1421   1.1  kiyohara 			    &cd->mvgbe_tx_chain[i + 1];
   1422   1.1  kiyohara 			rd->mvgbe_tx_ring[i].nextdescptr =
   1423   1.1  kiyohara 			    MVGBE_TX_RING_ADDR(sc, i + 1);
   1424   1.1  kiyohara 		}
   1425   1.1  kiyohara 		rd->mvgbe_tx_ring[i].cmdsts = MVGBE_BUFFER_OWNED_BY_HOST;
   1426   1.1  kiyohara 	}
   1427   1.1  kiyohara 
   1428   1.1  kiyohara 	sc->sc_cdata.mvgbe_tx_prod = 0;
   1429   1.1  kiyohara 	sc->sc_cdata.mvgbe_tx_cons = 0;
   1430   1.1  kiyohara 	sc->sc_cdata.mvgbe_tx_cnt = 0;
   1431   1.1  kiyohara 
   1432   1.1  kiyohara 	MVGBE_CDTXSYNC(sc, 0, MVGBE_TX_RING_CNT,
   1433   1.1  kiyohara 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1434   1.1  kiyohara 
   1435   1.1  kiyohara 	return 0;
   1436   1.1  kiyohara }
   1437   1.1  kiyohara 
   1438   1.1  kiyohara static int
   1439   1.1  kiyohara mvgbe_newbuf(struct mvgbe_softc *sc, int i, struct mbuf *m,
   1440   1.1  kiyohara 		bus_dmamap_t dmamap)
   1441   1.1  kiyohara {
   1442   1.1  kiyohara 	struct mbuf *m_new = NULL;
   1443   1.1  kiyohara 	struct mvgbe_chain *c;
   1444   1.1  kiyohara 	struct mvgbe_rx_desc *r;
   1445   1.1  kiyohara 	int align;
   1446  1.19   msaitoh 	vaddr_t offset;
   1447   1.1  kiyohara 
   1448   1.1  kiyohara 	if (m == NULL) {
   1449   1.1  kiyohara 		void *buf = NULL;
   1450   1.1  kiyohara 
   1451   1.1  kiyohara 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
   1452   1.1  kiyohara 		if (m_new == NULL) {
   1453   1.1  kiyohara 			aprint_error_ifnet(&sc->sc_ethercom.ec_if,
   1454   1.1  kiyohara 			    "no memory for rx list -- packet dropped!\n");
   1455   1.1  kiyohara 			return ENOBUFS;
   1456   1.1  kiyohara 		}
   1457   1.1  kiyohara 
   1458   1.1  kiyohara 		/* Allocate the jumbo buffer */
   1459   1.1  kiyohara 		buf = mvgbe_jalloc(sc);
   1460   1.1  kiyohara 		if (buf == NULL) {
   1461   1.1  kiyohara 			m_freem(m_new);
   1462   1.1  kiyohara 			DPRINTFN(1, ("%s jumbo allocation failed -- packet "
   1463   1.1  kiyohara 			    "dropped!\n", sc->sc_ethercom.ec_if.if_xname));
   1464   1.1  kiyohara 			return ENOBUFS;
   1465   1.1  kiyohara 		}
   1466   1.1  kiyohara 
   1467   1.1  kiyohara 		/* Attach the buffer to the mbuf */
   1468   1.1  kiyohara 		m_new->m_len = m_new->m_pkthdr.len = MVGBE_JLEN;
   1469   1.1  kiyohara 		MEXTADD(m_new, buf, MVGBE_JLEN, 0, mvgbe_jfree, sc);
   1470   1.1  kiyohara 	} else {
   1471   1.1  kiyohara 		/*
   1472   1.1  kiyohara 		 * We're re-using a previously allocated mbuf;
   1473   1.1  kiyohara 		 * be sure to re-init pointers and lengths to
   1474   1.1  kiyohara 		 * default values.
   1475   1.1  kiyohara 		 */
   1476   1.1  kiyohara 		m_new = m;
   1477   1.1  kiyohara 		m_new->m_len = m_new->m_pkthdr.len = MVGBE_JLEN;
   1478   1.1  kiyohara 		m_new->m_data = m_new->m_ext.ext_buf;
   1479   1.1  kiyohara 	}
   1480   1.5  jakllsch 	align = (u_long)m_new->m_data & MVGBE_RXBUF_MASK;
   1481   1.5  jakllsch 	if (align != 0) {
   1482   1.5  jakllsch 		DPRINTFN(1,("align = %d\n", align));
   1483   1.5  jakllsch 		m_adj(m_new,  MVGBE_RXBUF_ALIGN - align);
   1484   1.5  jakllsch 	}
   1485   1.1  kiyohara 
   1486   1.1  kiyohara 	c = &sc->sc_cdata.mvgbe_rx_chain[i];
   1487   1.1  kiyohara 	r = c->mvgbe_desc;
   1488   1.1  kiyohara 	c->mvgbe_mbuf = m_new;
   1489  1.19   msaitoh 	offset = (vaddr_t)m_new->m_data - (vaddr_t)sc->sc_cdata.mvgbe_jumbo_buf;
   1490  1.19   msaitoh 	r->bufptr = dmamap->dm_segs[0].ds_addr + offset;
   1491   1.5  jakllsch 	r->bufsize = MVGBE_JLEN & ~MVGBE_RXBUF_MASK;
   1492   1.1  kiyohara 	r->cmdsts = MVGBE_BUFFER_OWNED_BY_DMA | MVGBE_RX_ENABLE_INTERRUPT;
   1493   1.1  kiyohara 
   1494  1.19   msaitoh 	/* Invalidate RX buffer */
   1495  1.19   msaitoh 	bus_dmamap_sync(sc->sc_dmat, dmamap, offset, r->bufsize,
   1496  1.19   msaitoh 	    BUS_DMASYNC_PREREAD);
   1497  1.19   msaitoh 
   1498   1.3  kiyohara 	MVGBE_CDRXSYNC(sc, i, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1499   1.1  kiyohara 
   1500   1.1  kiyohara 	return 0;
   1501   1.1  kiyohara }
   1502   1.1  kiyohara 
   1503   1.1  kiyohara /*
   1504   1.1  kiyohara  * Memory management for jumbo frames.
   1505   1.1  kiyohara  */
   1506   1.1  kiyohara 
   1507   1.1  kiyohara static int
   1508   1.1  kiyohara mvgbe_alloc_jumbo_mem(struct mvgbe_softc *sc)
   1509   1.1  kiyohara {
   1510   1.1  kiyohara 	char *ptr, *kva;
   1511   1.1  kiyohara 	bus_dma_segment_t seg;
   1512   1.1  kiyohara 	int i, rseg, state, error;
   1513   1.1  kiyohara 	struct mvgbe_jpool_entry *entry;
   1514   1.1  kiyohara 
   1515   1.1  kiyohara 	state = error = 0;
   1516   1.1  kiyohara 
   1517   1.1  kiyohara 	/* Grab a big chunk o' storage. */
   1518   1.1  kiyohara 	if (bus_dmamem_alloc(sc->sc_dmat, MVGBE_JMEM, PAGE_SIZE, 0,
   1519   1.1  kiyohara 	    &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
   1520   1.1  kiyohara 		aprint_error_dev(sc->sc_dev, "can't alloc rx buffers\n");
   1521   1.1  kiyohara 		return ENOBUFS;
   1522   1.1  kiyohara 	}
   1523   1.1  kiyohara 
   1524   1.1  kiyohara 	state = 1;
   1525   1.1  kiyohara 	if (bus_dmamem_map(sc->sc_dmat, &seg, rseg, MVGBE_JMEM,
   1526   1.1  kiyohara 	    (void **)&kva, BUS_DMA_NOWAIT)) {
   1527   1.1  kiyohara 		aprint_error_dev(sc->sc_dev,
   1528   1.1  kiyohara 		    "can't map dma buffers (%d bytes)\n", MVGBE_JMEM);
   1529   1.1  kiyohara 		error = ENOBUFS;
   1530   1.1  kiyohara 		goto out;
   1531   1.1  kiyohara 	}
   1532   1.1  kiyohara 
   1533   1.1  kiyohara 	state = 2;
   1534   1.1  kiyohara 	if (bus_dmamap_create(sc->sc_dmat, MVGBE_JMEM, 1, MVGBE_JMEM, 0,
   1535   1.1  kiyohara 	    BUS_DMA_NOWAIT, &sc->sc_cdata.mvgbe_rx_jumbo_map)) {
   1536   1.1  kiyohara 		aprint_error_dev(sc->sc_dev, "can't create dma map\n");
   1537   1.1  kiyohara 		error = ENOBUFS;
   1538   1.1  kiyohara 		goto out;
   1539   1.1  kiyohara 	}
   1540   1.1  kiyohara 
   1541   1.1  kiyohara 	state = 3;
   1542   1.1  kiyohara 	if (bus_dmamap_load(sc->sc_dmat, sc->sc_cdata.mvgbe_rx_jumbo_map,
   1543   1.1  kiyohara 	    kva, MVGBE_JMEM, NULL, BUS_DMA_NOWAIT)) {
   1544   1.1  kiyohara 		aprint_error_dev(sc->sc_dev, "can't load dma map\n");
   1545   1.1  kiyohara 		error = ENOBUFS;
   1546   1.1  kiyohara 		goto out;
   1547   1.1  kiyohara 	}
   1548   1.1  kiyohara 
   1549   1.1  kiyohara 	state = 4;
   1550   1.1  kiyohara 	sc->sc_cdata.mvgbe_jumbo_buf = (void *)kva;
   1551   1.5  jakllsch 	DPRINTFN(1,("mvgbe_jumbo_buf = %p\n", sc->sc_cdata.mvgbe_jumbo_buf));
   1552   1.1  kiyohara 
   1553   1.1  kiyohara 	LIST_INIT(&sc->sc_jfree_listhead);
   1554   1.1  kiyohara 	LIST_INIT(&sc->sc_jinuse_listhead);
   1555   1.1  kiyohara 
   1556   1.1  kiyohara 	/*
   1557   1.1  kiyohara 	 * Now divide it up into 9K pieces and save the addresses
   1558   1.1  kiyohara 	 * in an array.
   1559   1.1  kiyohara 	 */
   1560   1.1  kiyohara 	ptr = sc->sc_cdata.mvgbe_jumbo_buf;
   1561   1.1  kiyohara 	for (i = 0; i < MVGBE_JSLOTS; i++) {
   1562   1.1  kiyohara 		sc->sc_cdata.mvgbe_jslots[i] = ptr;
   1563   1.1  kiyohara 		ptr += MVGBE_JLEN;
   1564   1.1  kiyohara 		entry = kmem_alloc(sizeof(struct mvgbe_jpool_entry), KM_SLEEP);
   1565   1.1  kiyohara 		if (entry == NULL) {
   1566   1.1  kiyohara 			aprint_error_dev(sc->sc_dev,
   1567   1.1  kiyohara 			    "no memory for jumbo buffer queue!\n");
   1568   1.1  kiyohara 			error = ENOBUFS;
   1569   1.1  kiyohara 			goto out;
   1570   1.1  kiyohara 		}
   1571   1.1  kiyohara 		entry->slot = i;
   1572   1.1  kiyohara 		if (i)
   1573   1.1  kiyohara 			LIST_INSERT_HEAD(&sc->sc_jfree_listhead, entry,
   1574   1.1  kiyohara 			    jpool_entries);
   1575   1.1  kiyohara 		else
   1576   1.1  kiyohara 			LIST_INSERT_HEAD(&sc->sc_jinuse_listhead, entry,
   1577   1.1  kiyohara 			    jpool_entries);
   1578   1.1  kiyohara 	}
   1579   1.1  kiyohara out:
   1580   1.1  kiyohara 	if (error != 0) {
   1581   1.1  kiyohara 		switch (state) {
   1582   1.1  kiyohara 		case 4:
   1583   1.1  kiyohara 			bus_dmamap_unload(sc->sc_dmat,
   1584   1.1  kiyohara 			    sc->sc_cdata.mvgbe_rx_jumbo_map);
   1585   1.1  kiyohara 		case 3:
   1586   1.1  kiyohara 			bus_dmamap_destroy(sc->sc_dmat,
   1587   1.1  kiyohara 			    sc->sc_cdata.mvgbe_rx_jumbo_map);
   1588   1.1  kiyohara 		case 2:
   1589   1.1  kiyohara 			bus_dmamem_unmap(sc->sc_dmat, kva, MVGBE_JMEM);
   1590   1.1  kiyohara 		case 1:
   1591   1.1  kiyohara 			bus_dmamem_free(sc->sc_dmat, &seg, rseg);
   1592   1.1  kiyohara 			break;
   1593   1.1  kiyohara 		default:
   1594   1.1  kiyohara 			break;
   1595   1.1  kiyohara 		}
   1596   1.1  kiyohara 	}
   1597   1.1  kiyohara 
   1598   1.1  kiyohara 	return error;
   1599   1.1  kiyohara }
   1600   1.1  kiyohara 
   1601   1.1  kiyohara /*
   1602   1.1  kiyohara  * Allocate a jumbo buffer.
   1603   1.1  kiyohara  */
   1604   1.1  kiyohara static void *
   1605   1.1  kiyohara mvgbe_jalloc(struct mvgbe_softc *sc)
   1606   1.1  kiyohara {
   1607   1.1  kiyohara 	struct mvgbe_jpool_entry *entry;
   1608   1.1  kiyohara 
   1609   1.1  kiyohara 	entry = LIST_FIRST(&sc->sc_jfree_listhead);
   1610   1.1  kiyohara 
   1611   1.1  kiyohara 	if (entry == NULL)
   1612   1.1  kiyohara 		return NULL;
   1613   1.1  kiyohara 
   1614   1.1  kiyohara 	LIST_REMOVE(entry, jpool_entries);
   1615   1.1  kiyohara 	LIST_INSERT_HEAD(&sc->sc_jinuse_listhead, entry, jpool_entries);
   1616   1.1  kiyohara 	return sc->sc_cdata.mvgbe_jslots[entry->slot];
   1617   1.1  kiyohara }
   1618   1.1  kiyohara 
   1619   1.1  kiyohara /*
   1620   1.1  kiyohara  * Release a jumbo buffer.
   1621   1.1  kiyohara  */
   1622   1.1  kiyohara static void
   1623   1.1  kiyohara mvgbe_jfree(struct mbuf *m, void *buf, size_t size, void *arg)
   1624   1.1  kiyohara {
   1625   1.1  kiyohara 	struct mvgbe_jpool_entry *entry;
   1626   1.1  kiyohara 	struct mvgbe_softc *sc;
   1627   1.1  kiyohara 	int i, s;
   1628   1.1  kiyohara 
   1629   1.1  kiyohara 	/* Extract the softc struct pointer. */
   1630   1.1  kiyohara 	sc = (struct mvgbe_softc *)arg;
   1631   1.1  kiyohara 
   1632   1.1  kiyohara 	if (sc == NULL)
   1633   1.1  kiyohara 		panic("%s: can't find softc pointer!", __func__);
   1634   1.1  kiyohara 
   1635   1.1  kiyohara 	/* calculate the slot this buffer belongs to */
   1636   1.1  kiyohara 
   1637   1.1  kiyohara 	i = ((vaddr_t)buf - (vaddr_t)sc->sc_cdata.mvgbe_jumbo_buf) / MVGBE_JLEN;
   1638   1.1  kiyohara 
   1639   1.1  kiyohara 	if ((i < 0) || (i >= MVGBE_JSLOTS))
   1640   1.1  kiyohara 		panic("%s: asked to free buffer that we don't manage!",
   1641   1.1  kiyohara 		    __func__);
   1642   1.1  kiyohara 
   1643   1.1  kiyohara 	s = splvm();
   1644   1.1  kiyohara 	entry = LIST_FIRST(&sc->sc_jinuse_listhead);
   1645   1.1  kiyohara 	if (entry == NULL)
   1646   1.1  kiyohara 		panic("%s: buffer not in use!", __func__);
   1647   1.1  kiyohara 	entry->slot = i;
   1648   1.1  kiyohara 	LIST_REMOVE(entry, jpool_entries);
   1649   1.1  kiyohara 	LIST_INSERT_HEAD(&sc->sc_jfree_listhead, entry, jpool_entries);
   1650   1.1  kiyohara 
   1651   1.1  kiyohara 	if (__predict_true(m != NULL))
   1652   1.1  kiyohara 		pool_cache_put(mb_cache, m);
   1653   1.1  kiyohara 	splx(s);
   1654   1.1  kiyohara }
   1655   1.1  kiyohara 
   1656   1.1  kiyohara static int
   1657   1.1  kiyohara mvgbe_encap(struct mvgbe_softc *sc, struct mbuf *m_head,
   1658   1.1  kiyohara 	      uint32_t *txidx)
   1659   1.1  kiyohara {
   1660   1.1  kiyohara 	struct mvgbe_tx_desc *f = NULL;
   1661   1.1  kiyohara 	struct mvgbe_txmap_entry *entry;
   1662   1.1  kiyohara 	bus_dma_segment_t *txseg;
   1663   1.1  kiyohara 	bus_dmamap_t txmap;
   1664   1.1  kiyohara 	uint32_t first, current, last, cmdsts = 0;
   1665   1.1  kiyohara 	int m_csumflags, i;
   1666  1.14  jakllsch 	bool needs_defrag = false;
   1667   1.1  kiyohara 
   1668   1.1  kiyohara 	DPRINTFN(3, ("mvgbe_encap\n"));
   1669   1.1  kiyohara 
   1670   1.1  kiyohara 	entry = SIMPLEQ_FIRST(&sc->sc_txmap_head);
   1671   1.1  kiyohara 	if (entry == NULL) {
   1672   1.1  kiyohara 		DPRINTFN(2, ("mvgbe_encap: no txmap available\n"));
   1673   1.1  kiyohara 		return ENOBUFS;
   1674   1.1  kiyohara 	}
   1675   1.1  kiyohara 	txmap = entry->dmamap;
   1676   1.1  kiyohara 
   1677   1.1  kiyohara 	first = current = last = *txidx;
   1678   1.1  kiyohara 
   1679   1.1  kiyohara 	/*
   1680   1.1  kiyohara 	 * Preserve m_pkthdr.csum_flags here since m_head might be
   1681   1.1  kiyohara 	 * updated by m_defrag()
   1682   1.1  kiyohara 	 */
   1683   1.1  kiyohara 	m_csumflags = m_head->m_pkthdr.csum_flags;
   1684   1.1  kiyohara 
   1685  1.14  jakllsch do_defrag:
   1686  1.14  jakllsch 	if (__predict_false(needs_defrag == true)) {
   1687  1.14  jakllsch 		/* A small unaligned segment was detected. */
   1688  1.14  jakllsch 		struct mbuf *m_new;
   1689  1.14  jakllsch 		m_new = m_defrag(m_head, M_DONTWAIT);
   1690  1.14  jakllsch 		if (m_new == NULL)
   1691  1.14  jakllsch 			return EFBIG;
   1692  1.14  jakllsch 		m_head = m_new;
   1693  1.14  jakllsch 	}
   1694  1.14  jakllsch 
   1695   1.1  kiyohara 	/*
   1696   1.1  kiyohara 	 * Start packing the mbufs in this chain into
   1697   1.1  kiyohara 	 * the fragment pointers. Stop when we run out
   1698   1.1  kiyohara 	 * of fragments or hit the end of the mbuf chain.
   1699   1.1  kiyohara 	 */
   1700   1.1  kiyohara 	if (bus_dmamap_load_mbuf(sc->sc_dmat, txmap, m_head, BUS_DMA_NOWAIT)) {
   1701   1.1  kiyohara 		DPRINTFN(1, ("mvgbe_encap: dmamap failed\n"));
   1702   1.1  kiyohara 		return ENOBUFS;
   1703   1.1  kiyohara 	}
   1704   1.1  kiyohara 
   1705  1.14  jakllsch 	txseg = txmap->dm_segs;
   1706  1.14  jakllsch 
   1707  1.14  jakllsch 	if (__predict_true(needs_defrag == false)) {
   1708  1.14  jakllsch 		/*
   1709  1.14  jakllsch 		 * Detect rarely encountered DMA limitation.
   1710  1.14  jakllsch 		 */
   1711  1.14  jakllsch 		for (i = 0; i < txmap->dm_nsegs; i++) {
   1712  1.14  jakllsch 			if (((txseg[i].ds_addr & 7) != 0) &&
   1713  1.14  jakllsch 			    (txseg[i].ds_len <= 8) &&
   1714  1.14  jakllsch 			    (txseg[i].ds_len >= 1)
   1715  1.14  jakllsch 			    ) {
   1716  1.14  jakllsch 				txseg = NULL;
   1717  1.14  jakllsch 				bus_dmamap_unload(sc->sc_dmat, txmap);
   1718  1.14  jakllsch 				needs_defrag = true;
   1719  1.14  jakllsch 				goto do_defrag;
   1720  1.14  jakllsch 			}
   1721  1.14  jakllsch 		}
   1722  1.14  jakllsch 	}
   1723  1.14  jakllsch 
   1724   1.1  kiyohara 	/* Sync the DMA map. */
   1725   1.1  kiyohara 	bus_dmamap_sync(sc->sc_dmat, txmap, 0, txmap->dm_mapsize,
   1726   1.1  kiyohara 	    BUS_DMASYNC_PREWRITE);
   1727   1.1  kiyohara 
   1728   1.1  kiyohara 	if (sc->sc_cdata.mvgbe_tx_cnt + txmap->dm_nsegs >=
   1729   1.1  kiyohara 	    MVGBE_TX_RING_CNT) {
   1730   1.1  kiyohara 		DPRINTFN(2, ("mvgbe_encap: too few descriptors free\n"));
   1731   1.1  kiyohara 		bus_dmamap_unload(sc->sc_dmat, txmap);
   1732   1.1  kiyohara 		return ENOBUFS;
   1733   1.1  kiyohara 	}
   1734   1.1  kiyohara 
   1735   1.1  kiyohara 
   1736   1.1  kiyohara 	DPRINTFN(2, ("mvgbe_encap: dm_nsegs=%d\n", txmap->dm_nsegs));
   1737   1.1  kiyohara 
   1738   1.1  kiyohara 	for (i = 0; i < txmap->dm_nsegs; i++) {
   1739   1.1  kiyohara 		f = &sc->sc_rdata->mvgbe_tx_ring[current];
   1740   1.1  kiyohara 		f->bufptr = txseg[i].ds_addr;
   1741   1.1  kiyohara 		f->bytecnt = txseg[i].ds_len;
   1742  1.20   msaitoh 		if (i != 0)
   1743  1.20   msaitoh 			f->cmdsts = MVGBE_BUFFER_OWNED_BY_DMA;
   1744   1.1  kiyohara 		last = current;
   1745   1.4  jakllsch 		current = MVGBE_TX_RING_NEXT(current);
   1746   1.1  kiyohara 	}
   1747   1.1  kiyohara 
   1748   1.1  kiyohara 	if (m_csumflags & M_CSUM_IPv4)
   1749   1.1  kiyohara 		cmdsts |= MVGBE_TX_GENERATE_IP_CHKSUM;
   1750   1.1  kiyohara 	if (m_csumflags & M_CSUM_TCPv4)
   1751   1.1  kiyohara 		cmdsts |=
   1752   1.1  kiyohara 		    MVGBE_TX_GENERATE_L4_CHKSUM | MVGBE_TX_L4_TYPE_TCP;
   1753   1.1  kiyohara 	if (m_csumflags & M_CSUM_UDPv4)
   1754   1.1  kiyohara 		cmdsts |=
   1755   1.1  kiyohara 		    MVGBE_TX_GENERATE_L4_CHKSUM | MVGBE_TX_L4_TYPE_UDP;
   1756   1.1  kiyohara 	if (m_csumflags & (M_CSUM_IPv4 | M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
   1757   1.1  kiyohara 		const int iphdr_unitlen = sizeof(struct ip) / sizeof(uint32_t);
   1758   1.1  kiyohara 
   1759   1.1  kiyohara 		cmdsts |= MVGBE_TX_IP_NO_FRAG |
   1760   1.1  kiyohara 		    MVGBE_TX_IP_HEADER_LEN(iphdr_unitlen);	/* unit is 4B */
   1761   1.1  kiyohara 	}
   1762   1.1  kiyohara 	if (txmap->dm_nsegs == 1)
   1763   1.1  kiyohara 		f->cmdsts = cmdsts		|
   1764   1.1  kiyohara 		    MVGBE_TX_GENERATE_CRC	|
   1765   1.1  kiyohara 		    MVGBE_TX_ENABLE_INTERRUPT	|
   1766   1.1  kiyohara 		    MVGBE_TX_ZERO_PADDING	|
   1767   1.1  kiyohara 		    MVGBE_TX_FIRST_DESC		|
   1768   1.1  kiyohara 		    MVGBE_TX_LAST_DESC;
   1769   1.1  kiyohara 	else {
   1770   1.1  kiyohara 		f = &sc->sc_rdata->mvgbe_tx_ring[first];
   1771   1.1  kiyohara 		f->cmdsts = cmdsts		|
   1772   1.1  kiyohara 		    MVGBE_TX_GENERATE_CRC	|
   1773   1.1  kiyohara 		    MVGBE_TX_FIRST_DESC;
   1774   1.1  kiyohara 
   1775   1.1  kiyohara 		f = &sc->sc_rdata->mvgbe_tx_ring[last];
   1776   1.1  kiyohara 		f->cmdsts =
   1777   1.1  kiyohara 		    MVGBE_BUFFER_OWNED_BY_DMA	|
   1778   1.1  kiyohara 		    MVGBE_TX_ENABLE_INTERRUPT	|
   1779   1.1  kiyohara 		    MVGBE_TX_ZERO_PADDING	|
   1780   1.1  kiyohara 		    MVGBE_TX_LAST_DESC;
   1781  1.20   msaitoh 
   1782  1.20   msaitoh 		/* Sync descriptors except first */
   1783  1.20   msaitoh 		MVGBE_CDTXSYNC(sc,
   1784  1.20   msaitoh 		    (MVGBE_TX_RING_CNT - 1 == *txidx) ? 0 : (*txidx) + 1,
   1785  1.20   msaitoh 		    txmap->dm_nsegs - 1,
   1786  1.20   msaitoh 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1787   1.1  kiyohara 	}
   1788   1.1  kiyohara 
   1789   1.1  kiyohara 	sc->sc_cdata.mvgbe_tx_chain[last].mvgbe_mbuf = m_head;
   1790   1.1  kiyohara 	SIMPLEQ_REMOVE_HEAD(&sc->sc_txmap_head, link);
   1791   1.1  kiyohara 	sc->sc_cdata.mvgbe_tx_map[last] = entry;
   1792   1.1  kiyohara 
   1793  1.20   msaitoh 	/* Finally, sync first descriptor */
   1794  1.20   msaitoh 	sc->sc_rdata->mvgbe_tx_ring[first].cmdsts |=
   1795  1.20   msaitoh 	    MVGBE_BUFFER_OWNED_BY_DMA;
   1796  1.20   msaitoh 	MVGBE_CDTXSYNC(sc, *txidx, 1,
   1797   1.3  kiyohara 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1798   1.1  kiyohara 
   1799   1.1  kiyohara 	sc->sc_cdata.mvgbe_tx_cnt += i;
   1800   1.1  kiyohara 	*txidx = current;
   1801   1.1  kiyohara 
   1802   1.1  kiyohara 	DPRINTFN(3, ("mvgbe_encap: completed successfully\n"));
   1803   1.1  kiyohara 
   1804   1.1  kiyohara 	return 0;
   1805   1.1  kiyohara }
   1806   1.1  kiyohara 
   1807   1.1  kiyohara static void
   1808   1.1  kiyohara mvgbe_rxeof(struct mvgbe_softc *sc)
   1809   1.1  kiyohara {
   1810   1.1  kiyohara 	struct mvgbe_chain_data *cdata = &sc->sc_cdata;
   1811   1.1  kiyohara 	struct mvgbe_rx_desc *cur_rx;
   1812   1.1  kiyohara 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1813   1.1  kiyohara 	struct mbuf *m;
   1814   1.1  kiyohara 	bus_dmamap_t dmamap;
   1815   1.1  kiyohara 	uint32_t rxstat;
   1816  1.21   msaitoh 	uint16_t bufsize;
   1817   1.1  kiyohara 	int idx, cur, total_len;
   1818   1.1  kiyohara 
   1819   1.1  kiyohara 	idx = sc->sc_cdata.mvgbe_rx_prod;
   1820   1.1  kiyohara 
   1821   1.1  kiyohara 	DPRINTFN(3, ("mvgbe_rxeof %d\n", idx));
   1822   1.1  kiyohara 
   1823   1.1  kiyohara 	for (;;) {
   1824   1.1  kiyohara 		cur = idx;
   1825   1.1  kiyohara 
   1826   1.1  kiyohara 		/* Sync the descriptor */
   1827   1.1  kiyohara 		MVGBE_CDRXSYNC(sc, idx,
   1828   1.1  kiyohara 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1829   1.1  kiyohara 
   1830   1.1  kiyohara 		cur_rx = &sc->sc_rdata->mvgbe_rx_ring[idx];
   1831   1.1  kiyohara 
   1832   1.1  kiyohara 		if ((cur_rx->cmdsts & MVGBE_BUFFER_OWNED_MASK) ==
   1833   1.1  kiyohara 		    MVGBE_BUFFER_OWNED_BY_DMA) {
   1834   1.1  kiyohara 			/* Invalidate the descriptor -- it's not ready yet */
   1835   1.1  kiyohara 			MVGBE_CDRXSYNC(sc, idx, BUS_DMASYNC_PREREAD);
   1836   1.1  kiyohara 			sc->sc_cdata.mvgbe_rx_prod = idx;
   1837   1.1  kiyohara 			break;
   1838   1.1  kiyohara 		}
   1839   1.1  kiyohara #ifdef DIAGNOSTIC
   1840   1.1  kiyohara 		if ((cur_rx->cmdsts &
   1841   1.1  kiyohara 		    (MVGBE_RX_LAST_DESC | MVGBE_RX_FIRST_DESC)) !=
   1842   1.1  kiyohara 		    (MVGBE_RX_LAST_DESC | MVGBE_RX_FIRST_DESC))
   1843   1.1  kiyohara 			panic(
   1844   1.1  kiyohara 			    "mvgbe_rxeof: buffer size is smaller than packet");
   1845   1.1  kiyohara #endif
   1846   1.1  kiyohara 
   1847   1.1  kiyohara 		dmamap = sc->sc_cdata.mvgbe_rx_jumbo_map;
   1848   1.1  kiyohara 
   1849   1.1  kiyohara 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
   1850   1.1  kiyohara 		    BUS_DMASYNC_POSTREAD);
   1851   1.1  kiyohara 
   1852   1.1  kiyohara 		m = cdata->mvgbe_rx_chain[idx].mvgbe_mbuf;
   1853   1.1  kiyohara 		cdata->mvgbe_rx_chain[idx].mvgbe_mbuf = NULL;
   1854  1.29   msaitoh 		total_len = cur_rx->bytecnt - ETHER_CRC_LEN;
   1855   1.1  kiyohara 		rxstat = cur_rx->cmdsts;
   1856  1.21   msaitoh 		bufsize = cur_rx->bufsize;
   1857   1.1  kiyohara 
   1858   1.1  kiyohara 		cdata->mvgbe_rx_map[idx] = NULL;
   1859   1.1  kiyohara 
   1860   1.4  jakllsch 		idx = MVGBE_RX_RING_NEXT(idx);
   1861   1.1  kiyohara 
   1862   1.1  kiyohara 		if (rxstat & MVGBE_ERROR_SUMMARY) {
   1863   1.1  kiyohara #if 0
   1864   1.1  kiyohara 			int err = rxstat & MVGBE_RX_ERROR_CODE_MASK;
   1865   1.1  kiyohara 
   1866   1.1  kiyohara 			if (err == MVGBE_RX_CRC_ERROR)
   1867   1.1  kiyohara 				ifp->if_ierrors++;
   1868   1.1  kiyohara 			if (err == MVGBE_RX_OVERRUN_ERROR)
   1869   1.1  kiyohara 				ifp->if_ierrors++;
   1870   1.1  kiyohara 			if (err == MVGBE_RX_MAX_FRAME_LEN_ERROR)
   1871   1.1  kiyohara 				ifp->if_ierrors++;
   1872   1.1  kiyohara 			if (err == MVGBE_RX_RESOURCE_ERROR)
   1873   1.1  kiyohara 				ifp->if_ierrors++;
   1874   1.1  kiyohara #else
   1875   1.1  kiyohara 			ifp->if_ierrors++;
   1876   1.1  kiyohara #endif
   1877   1.1  kiyohara 			mvgbe_newbuf(sc, cur, m, dmamap);
   1878   1.1  kiyohara 			continue;
   1879   1.1  kiyohara 		}
   1880   1.1  kiyohara 
   1881   1.5  jakllsch 		if (rxstat & MVGBE_RX_IP_FRAME_TYPE) {
   1882  1.21   msaitoh 			int flgs = 0;
   1883  1.21   msaitoh 
   1884   1.5  jakllsch 			/* Check IPv4 header checksum */
   1885  1.22   msaitoh 			flgs |= M_CSUM_IPv4;
   1886   1.5  jakllsch 			if (!(rxstat & MVGBE_RX_IP_HEADER_OK))
   1887  1.21   msaitoh 				flgs |= M_CSUM_IPv4_BAD;
   1888  1.23   msaitoh 			else if ((bufsize & MVGBE_RX_IP_FRAGMENT) == 0) {
   1889  1.22   msaitoh 				/*
   1890  1.22   msaitoh 				 * Check TCPv4/UDPv4 checksum for
   1891  1.22   msaitoh 				 * non-fragmented packet only.
   1892  1.22   msaitoh 				 *
   1893  1.22   msaitoh 				 * It seemd that sometimes
   1894  1.22   msaitoh 				 * MVGBE_RX_L4_CHECKSUM_OK bit was set to 0
   1895  1.22   msaitoh 				 * even if the checksum is correct and the
   1896  1.22   msaitoh 				 * packet was not fragmented. So we don't set
   1897  1.22   msaitoh 				 * M_CSUM_TCP_UDP_BAD even if csum bit is 0.
   1898  1.22   msaitoh 				 */
   1899  1.22   msaitoh 
   1900  1.22   msaitoh 				if (((rxstat & MVGBE_RX_L4_TYPE_MASK) ==
   1901  1.22   msaitoh 					MVGBE_RX_L4_TYPE_TCP) &&
   1902  1.22   msaitoh 				    ((rxstat & MVGBE_RX_L4_CHECKSUM_OK) != 0))
   1903  1.21   msaitoh 					flgs |= M_CSUM_TCPv4;
   1904  1.22   msaitoh 				else if (((rxstat & MVGBE_RX_L4_TYPE_MASK) ==
   1905  1.22   msaitoh 					MVGBE_RX_L4_TYPE_UDP) &&
   1906  1.22   msaitoh 				    ((rxstat & MVGBE_RX_L4_CHECKSUM_OK) != 0))
   1907  1.21   msaitoh 					flgs |= M_CSUM_UDPv4;
   1908  1.21   msaitoh 			}
   1909  1.21   msaitoh 			m->m_pkthdr.csum_flags = flgs;
   1910   1.1  kiyohara 		}
   1911   1.1  kiyohara 
   1912   1.1  kiyohara 		/*
   1913   1.1  kiyohara 		 * Try to allocate a new jumbo buffer. If that
   1914   1.1  kiyohara 		 * fails, copy the packet to mbufs and put the
   1915   1.1  kiyohara 		 * jumbo buffer back in the ring so it can be
   1916   1.1  kiyohara 		 * re-used. If allocating mbufs fails, then we
   1917   1.1  kiyohara 		 * have to drop the packet.
   1918   1.1  kiyohara 		 */
   1919   1.1  kiyohara 		if (mvgbe_newbuf(sc, cur, NULL, dmamap) == ENOBUFS) {
   1920   1.1  kiyohara 			struct mbuf *m0;
   1921   1.1  kiyohara 
   1922   1.1  kiyohara 			m0 = m_devget(mtod(m, char *), total_len, 0, ifp, NULL);
   1923   1.1  kiyohara 			mvgbe_newbuf(sc, cur, m, dmamap);
   1924   1.1  kiyohara 			if (m0 == NULL) {
   1925   1.1  kiyohara 				aprint_error_ifnet(ifp,
   1926   1.1  kiyohara 				    "no receive buffers available --"
   1927   1.1  kiyohara 				    " packet dropped!\n");
   1928   1.1  kiyohara 				ifp->if_ierrors++;
   1929   1.1  kiyohara 				continue;
   1930   1.1  kiyohara 			}
   1931   1.1  kiyohara 			m = m0;
   1932   1.1  kiyohara 		} else {
   1933   1.1  kiyohara 			m->m_pkthdr.rcvif = ifp;
   1934   1.1  kiyohara 			m->m_pkthdr.len = m->m_len = total_len;
   1935   1.1  kiyohara 		}
   1936   1.1  kiyohara 
   1937   1.1  kiyohara 		/* Skip on first 2byte (HW header) */
   1938   1.1  kiyohara 		m_adj(m,  MVGBE_HWHEADER_SIZE);
   1939   1.1  kiyohara 
   1940   1.1  kiyohara 		ifp->if_ipackets++;
   1941   1.1  kiyohara 
   1942  1.10       rjs 		bpf_mtap(ifp, m);
   1943   1.1  kiyohara 
   1944   1.1  kiyohara 		/* pass it on. */
   1945   1.1  kiyohara 		(*ifp->if_input)(ifp, m);
   1946   1.1  kiyohara 	}
   1947   1.1  kiyohara }
   1948   1.1  kiyohara 
   1949   1.1  kiyohara static void
   1950   1.1  kiyohara mvgbe_txeof(struct mvgbe_softc *sc)
   1951   1.1  kiyohara {
   1952   1.1  kiyohara 	struct mvgbe_chain_data *cdata = &sc->sc_cdata;
   1953   1.1  kiyohara 	struct mvgbe_tx_desc *cur_tx;
   1954   1.1  kiyohara 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1955   1.1  kiyohara 	struct mvgbe_txmap_entry *entry;
   1956   1.1  kiyohara 	int idx;
   1957   1.1  kiyohara 
   1958   1.1  kiyohara 	DPRINTFN(3, ("mvgbe_txeof\n"));
   1959   1.1  kiyohara 
   1960   1.1  kiyohara 	/*
   1961   1.1  kiyohara 	 * Go through our tx ring and free mbufs for those
   1962   1.1  kiyohara 	 * frames that have been sent.
   1963   1.1  kiyohara 	 */
   1964   1.1  kiyohara 	idx = cdata->mvgbe_tx_cons;
   1965   1.1  kiyohara 	while (idx != cdata->mvgbe_tx_prod) {
   1966   1.1  kiyohara 		MVGBE_CDTXSYNC(sc, idx, 1,
   1967   1.1  kiyohara 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1968   1.1  kiyohara 
   1969   1.1  kiyohara 		cur_tx = &sc->sc_rdata->mvgbe_tx_ring[idx];
   1970   1.1  kiyohara #ifdef MVGBE_DEBUG
   1971   1.1  kiyohara 		if (mvgbe_debug >= 3)
   1972   1.1  kiyohara 			mvgbe_dump_txdesc(cur_tx, idx);
   1973   1.1  kiyohara #endif
   1974   1.1  kiyohara 		if ((cur_tx->cmdsts & MVGBE_BUFFER_OWNED_MASK) ==
   1975   1.1  kiyohara 		    MVGBE_BUFFER_OWNED_BY_DMA) {
   1976   1.1  kiyohara 			MVGBE_CDTXSYNC(sc, idx, 1, BUS_DMASYNC_PREREAD);
   1977   1.1  kiyohara 			break;
   1978   1.1  kiyohara 		}
   1979   1.1  kiyohara 		if (cur_tx->cmdsts & MVGBE_TX_LAST_DESC)
   1980   1.1  kiyohara 			ifp->if_opackets++;
   1981   1.1  kiyohara 		if (cur_tx->cmdsts & MVGBE_ERROR_SUMMARY) {
   1982   1.1  kiyohara 			int err = cur_tx->cmdsts & MVGBE_TX_ERROR_CODE_MASK;
   1983   1.1  kiyohara 
   1984   1.1  kiyohara 			if (err == MVGBE_TX_LATE_COLLISION_ERROR)
   1985   1.1  kiyohara 				ifp->if_collisions++;
   1986   1.1  kiyohara 			if (err == MVGBE_TX_UNDERRUN_ERROR)
   1987   1.1  kiyohara 				ifp->if_oerrors++;
   1988   1.1  kiyohara 			if (err == MVGBE_TX_EXCESSIVE_COLLISION_ERRO)
   1989   1.1  kiyohara 				ifp->if_collisions++;
   1990   1.1  kiyohara 		}
   1991   1.1  kiyohara 		if (cdata->mvgbe_tx_chain[idx].mvgbe_mbuf != NULL) {
   1992   1.1  kiyohara 			entry = cdata->mvgbe_tx_map[idx];
   1993   1.1  kiyohara 
   1994   1.1  kiyohara 			m_freem(cdata->mvgbe_tx_chain[idx].mvgbe_mbuf);
   1995   1.1  kiyohara 			cdata->mvgbe_tx_chain[idx].mvgbe_mbuf = NULL;
   1996   1.1  kiyohara 
   1997   1.1  kiyohara 			bus_dmamap_sync(sc->sc_dmat, entry->dmamap, 0,
   1998   1.1  kiyohara 			    entry->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1999   1.1  kiyohara 
   2000   1.1  kiyohara 			bus_dmamap_unload(sc->sc_dmat, entry->dmamap);
   2001   1.1  kiyohara 			SIMPLEQ_INSERT_TAIL(&sc->sc_txmap_head, entry, link);
   2002   1.1  kiyohara 			cdata->mvgbe_tx_map[idx] = NULL;
   2003   1.1  kiyohara 		}
   2004   1.1  kiyohara 		cdata->mvgbe_tx_cnt--;
   2005   1.4  jakllsch 		idx = MVGBE_TX_RING_NEXT(idx);
   2006   1.1  kiyohara 	}
   2007   1.1  kiyohara 	if (cdata->mvgbe_tx_cnt == 0)
   2008   1.1  kiyohara 		ifp->if_timer = 0;
   2009   1.1  kiyohara 
   2010   1.1  kiyohara 	if (cdata->mvgbe_tx_cnt < MVGBE_TX_RING_CNT - 2)
   2011   1.1  kiyohara 		ifp->if_flags &= ~IFF_OACTIVE;
   2012   1.1  kiyohara 
   2013   1.1  kiyohara 	cdata->mvgbe_tx_cons = idx;
   2014   1.1  kiyohara }
   2015   1.1  kiyohara 
   2016   1.5  jakllsch static uint8_t
   2017   1.5  jakllsch mvgbe_crc8(const uint8_t *data, size_t size)
   2018   1.5  jakllsch {
   2019   1.5  jakllsch 	int bit;
   2020   1.5  jakllsch 	uint8_t byte;
   2021   1.5  jakllsch 	uint8_t crc = 0;
   2022   1.5  jakllsch 	const uint8_t poly = 0x07;
   2023   1.5  jakllsch 
   2024   1.5  jakllsch 	while(size--)
   2025   1.5  jakllsch 	  for (byte = *data++, bit = NBBY-1; bit >= 0; bit--)
   2026   1.5  jakllsch 	    crc = (crc << 1) ^ ((((crc >> 7) ^ (byte >> bit)) & 1) ? poly : 0);
   2027   1.5  jakllsch 
   2028   1.5  jakllsch 	return crc;
   2029   1.5  jakllsch }
   2030   1.5  jakllsch 
   2031   1.5  jakllsch CTASSERT(MVGBE_NDFSMT == MVGBE_NDFOMT);
   2032   1.5  jakllsch 
   2033   1.1  kiyohara static void
   2034   1.5  jakllsch mvgbe_filter_setup(struct mvgbe_softc *sc)
   2035   1.1  kiyohara {
   2036   1.5  jakllsch 	struct ethercom *ec = &sc->sc_ethercom;
   2037   1.1  kiyohara 	struct ifnet *ifp= &sc->sc_ethercom.ec_if;
   2038   1.5  jakllsch 	struct ether_multi *enm;
   2039   1.5  jakllsch 	struct ether_multistep step;
   2040  1.12  jakllsch 	uint32_t dfut[MVGBE_NDFUT], dfsmt[MVGBE_NDFSMT], dfomt[MVGBE_NDFOMT];
   2041   1.5  jakllsch 	uint32_t pxc;
   2042   1.5  jakllsch 	int i;
   2043   1.5  jakllsch 	const uint8_t special[ETHER_ADDR_LEN] = {0x01,0x00,0x5e,0x00,0x00,0x00};
   2044   1.5  jakllsch 
   2045  1.12  jakllsch 	memset(dfut, 0, sizeof(dfut));
   2046  1.12  jakllsch 	memset(dfsmt, 0, sizeof(dfsmt));
   2047  1.12  jakllsch 	memset(dfomt, 0, sizeof(dfomt));
   2048   1.5  jakllsch 
   2049   1.5  jakllsch 	if (ifp->if_flags & (IFF_ALLMULTI|IFF_PROMISC)) {
   2050   1.5  jakllsch 		goto allmulti;
   2051   1.5  jakllsch 	}
   2052   1.5  jakllsch 
   2053   1.5  jakllsch 	ETHER_FIRST_MULTI(step, ec, enm);
   2054   1.5  jakllsch 	while (enm != NULL) {
   2055   1.5  jakllsch 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2056   1.5  jakllsch 			/* ranges are complex and somewhat rare */
   2057   1.5  jakllsch 			goto allmulti;
   2058   1.5  jakllsch 		}
   2059   1.5  jakllsch 		/* chip handles some IPv4 multicast specially */
   2060   1.5  jakllsch 		if (memcmp(enm->enm_addrlo, special, 5) == 0) {
   2061   1.5  jakllsch 			i = enm->enm_addrlo[5];
   2062   1.5  jakllsch 			dfsmt[i>>2] =
   2063   1.5  jakllsch 			    MVGBE_DF(i&3, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS);
   2064   1.5  jakllsch 		} else {
   2065   1.5  jakllsch 			i = mvgbe_crc8(enm->enm_addrlo, ETHER_ADDR_LEN);
   2066   1.5  jakllsch 			dfomt[i>>2] =
   2067   1.5  jakllsch 			    MVGBE_DF(i&3, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS);
   2068   1.5  jakllsch 		}
   2069   1.5  jakllsch 
   2070   1.5  jakllsch 		ETHER_NEXT_MULTI(step, enm);
   2071   1.5  jakllsch 	}
   2072   1.5  jakllsch 	goto set;
   2073   1.1  kiyohara 
   2074   1.5  jakllsch allmulti:
   2075   1.5  jakllsch 	if (ifp->if_flags & (IFF_ALLMULTI|IFF_PROMISC)) {
   2076   1.5  jakllsch 		for (i = 0; i < MVGBE_NDFSMT; i++) {
   2077   1.5  jakllsch 			dfsmt[i] = dfomt[i] =
   2078   1.5  jakllsch 			    MVGBE_DF(0, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS) |
   2079   1.5  jakllsch 			    MVGBE_DF(1, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS) |
   2080   1.5  jakllsch 			    MVGBE_DF(2, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS) |
   2081   1.5  jakllsch 			    MVGBE_DF(3, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS);
   2082   1.5  jakllsch 		}
   2083   1.1  kiyohara 	}
   2084   1.1  kiyohara 
   2085   1.5  jakllsch set:
   2086   1.1  kiyohara 	pxc = MVGBE_READ(sc, MVGBE_PXC);
   2087   1.1  kiyohara 	pxc &= ~MVGBE_PXC_UPM;
   2088   1.5  jakllsch 	pxc |= MVGBE_PXC_RB | MVGBE_PXC_RBIP | MVGBE_PXC_RBARP;
   2089   1.5  jakllsch 	if (ifp->if_flags & IFF_BROADCAST) {
   2090   1.5  jakllsch 		pxc &= ~(MVGBE_PXC_RB | MVGBE_PXC_RBIP | MVGBE_PXC_RBARP);
   2091   1.5  jakllsch 	}
   2092   1.5  jakllsch 	if (ifp->if_flags & IFF_PROMISC) {
   2093   1.5  jakllsch 		pxc |= MVGBE_PXC_UPM;
   2094   1.5  jakllsch 	}
   2095   1.1  kiyohara 	MVGBE_WRITE(sc, MVGBE_PXC, pxc);
   2096   1.1  kiyohara 
   2097   1.5  jakllsch 	/* Set Destination Address Filter Unicast Table */
   2098   1.5  jakllsch 	i = sc->sc_enaddr[5] & 0xf;		/* last nibble */
   2099   1.5  jakllsch 	dfut[i>>2] = MVGBE_DF(i&3, MVGBE_DF_QUEUE(0) | MVGBE_DF_PASS);
   2100   1.5  jakllsch 	MVGBE_WRITE_FILTER(sc, MVGBE_DFUT, dfut, MVGBE_NDFUT);
   2101   1.5  jakllsch 
   2102   1.1  kiyohara 	/* Set Destination Address Filter Multicast Tables */
   2103   1.5  jakllsch 	MVGBE_WRITE_FILTER(sc, MVGBE_DFSMT, dfsmt, MVGBE_NDFSMT);
   2104   1.5  jakllsch 	MVGBE_WRITE_FILTER(sc, MVGBE_DFOMT, dfomt, MVGBE_NDFOMT);
   2105   1.1  kiyohara }
   2106   1.1  kiyohara 
   2107   1.1  kiyohara #ifdef MVGBE_DEBUG
   2108   1.1  kiyohara static void
   2109   1.1  kiyohara mvgbe_dump_txdesc(struct mvgbe_tx_desc *desc, int idx)
   2110   1.1  kiyohara {
   2111   1.1  kiyohara #define DESC_PRINT(X)					\
   2112   1.1  kiyohara 	if (X)						\
   2113   1.1  kiyohara 		printf("txdesc[%d]." #X "=%#x\n", idx, X);
   2114   1.1  kiyohara 
   2115   1.1  kiyohara #if BYTE_ORDER == BIG_ENDIAN
   2116   1.1  kiyohara        DESC_PRINT(desc->bytecnt);
   2117   1.1  kiyohara        DESC_PRINT(desc->l4ichk);
   2118   1.1  kiyohara        DESC_PRINT(desc->cmdsts);
   2119   1.1  kiyohara        DESC_PRINT(desc->nextdescptr);
   2120   1.1  kiyohara        DESC_PRINT(desc->bufptr);
   2121   1.1  kiyohara #else	/* LITTLE_ENDIAN */
   2122   1.1  kiyohara        DESC_PRINT(desc->cmdsts);
   2123   1.1  kiyohara        DESC_PRINT(desc->l4ichk);
   2124   1.1  kiyohara        DESC_PRINT(desc->bytecnt);
   2125   1.1  kiyohara        DESC_PRINT(desc->bufptr);
   2126   1.1  kiyohara        DESC_PRINT(desc->nextdescptr);
   2127   1.1  kiyohara #endif
   2128   1.1  kiyohara #undef DESC_PRINT
   2129   1.1  kiyohara }
   2130   1.1  kiyohara #endif
   2131  1.25   msaitoh 
   2132  1.25   msaitoh SYSCTL_SETUP(sysctl_mvgbe, "sysctl mvgbe subtree setup")
   2133  1.25   msaitoh {
   2134  1.25   msaitoh 	int rc;
   2135  1.25   msaitoh 	const struct sysctlnode *node;
   2136  1.25   msaitoh 
   2137  1.25   msaitoh 	if ((rc = sysctl_createv(clog, 0, NULL, NULL,
   2138  1.25   msaitoh 	    0, CTLTYPE_NODE, "hw", NULL,
   2139  1.25   msaitoh 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
   2140  1.25   msaitoh 		goto err;
   2141  1.25   msaitoh 	}
   2142  1.25   msaitoh 
   2143  1.25   msaitoh 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
   2144  1.25   msaitoh 	    0, CTLTYPE_NODE, "mvgbe",
   2145  1.25   msaitoh 	    SYSCTL_DESCR("mvgbe interface controls"),
   2146  1.25   msaitoh 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
   2147  1.25   msaitoh 		goto err;
   2148  1.25   msaitoh 	}
   2149  1.25   msaitoh 
   2150  1.25   msaitoh 	mvgbe_root_num = node->sysctl_num;
   2151  1.25   msaitoh 	return;
   2152  1.25   msaitoh 
   2153  1.25   msaitoh err:
   2154  1.25   msaitoh 	aprint_error("%s: syctl_createv failed (rc = %d)\n", __func__, rc);
   2155  1.25   msaitoh }
   2156  1.25   msaitoh 
   2157  1.25   msaitoh static void
   2158  1.25   msaitoh sysctl_mvgbe_init(struct mvgbe_softc *sc)
   2159  1.25   msaitoh {
   2160  1.25   msaitoh 	const struct sysctlnode *node;
   2161  1.25   msaitoh 	int mvgbe_nodenum;
   2162  1.25   msaitoh 
   2163  1.25   msaitoh 	if (sysctl_createv(&sc->mvgbe_clog, 0, NULL, &node,
   2164  1.25   msaitoh 		0, CTLTYPE_NODE, device_xname(sc->sc_dev),
   2165  1.25   msaitoh 		SYSCTL_DESCR("mvgbe per-controller controls"),
   2166  1.25   msaitoh 		NULL, 0, NULL, 0, CTL_HW, mvgbe_root_num, CTL_CREATE,
   2167  1.25   msaitoh 		CTL_EOL) != 0) {
   2168  1.25   msaitoh 		aprint_normal_dev(sc->sc_dev, "couldn't create sysctl node\n");
   2169  1.25   msaitoh 		return;
   2170  1.25   msaitoh 	}
   2171  1.25   msaitoh 	mvgbe_nodenum = node->sysctl_num;
   2172  1.25   msaitoh 
   2173  1.25   msaitoh 	/* interrupt moderation sysctls */
   2174  1.25   msaitoh 	if (sysctl_createv(&sc->mvgbe_clog, 0, NULL, &node,
   2175  1.25   msaitoh 		CTLFLAG_READWRITE, CTLTYPE_INT, "ipginttx",
   2176  1.25   msaitoh 		SYSCTL_DESCR("mvgbe TX interrupt moderation timer"),
   2177  1.25   msaitoh 		mvgbe_sysctl_ipginttx, 0, (void *)sc,
   2178  1.25   msaitoh 		0, CTL_HW, mvgbe_root_num, mvgbe_nodenum, CTL_CREATE,
   2179  1.25   msaitoh 		CTL_EOL) != 0) {
   2180  1.25   msaitoh 		aprint_normal_dev(sc->sc_dev,
   2181  1.25   msaitoh 		    "couldn't create ipginttx sysctl node\n");
   2182  1.25   msaitoh 	}
   2183  1.25   msaitoh 	if (sysctl_createv(&sc->mvgbe_clog, 0, NULL, &node,
   2184  1.25   msaitoh 		CTLFLAG_READWRITE, CTLTYPE_INT, "ipgintrx",
   2185  1.25   msaitoh 		SYSCTL_DESCR("mvgbe RX interrupt moderation timer"),
   2186  1.25   msaitoh 		mvgbe_sysctl_ipgintrx, 0, (void *)sc,
   2187  1.25   msaitoh 		0, CTL_HW, mvgbe_root_num, mvgbe_nodenum, CTL_CREATE,
   2188  1.25   msaitoh 		CTL_EOL) != 0) {
   2189  1.25   msaitoh 		aprint_normal_dev(sc->sc_dev,
   2190  1.25   msaitoh 		    "couldn't create ipginttx sysctl node\n");
   2191  1.25   msaitoh 	}
   2192  1.25   msaitoh }
   2193  1.25   msaitoh 
   2194  1.25   msaitoh static int
   2195  1.25   msaitoh mvgbe_sysctl_ipginttx(SYSCTLFN_ARGS)
   2196  1.25   msaitoh {
   2197  1.25   msaitoh 	int error;
   2198  1.25   msaitoh 	unsigned int t;
   2199  1.25   msaitoh 	struct sysctlnode node;
   2200  1.25   msaitoh 	struct mvgbec_softc *csc;
   2201  1.25   msaitoh 	struct mvgbe_softc *sc;
   2202  1.25   msaitoh 
   2203  1.25   msaitoh 	node = *rnode;
   2204  1.25   msaitoh 	sc = node.sysctl_data;
   2205  1.25   msaitoh 	csc = device_private(device_parent(sc->sc_dev));
   2206  1.25   msaitoh 	t = sc->sc_ipginttx;
   2207  1.25   msaitoh 	node.sysctl_data = &t;
   2208  1.25   msaitoh 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2209  1.25   msaitoh 	if (error || newp == NULL)
   2210  1.25   msaitoh 		return error;
   2211  1.25   msaitoh 
   2212  1.25   msaitoh 	if (mvgbe_ipginttx(csc, sc, t) < 0)
   2213  1.25   msaitoh 		return EINVAL;
   2214  1.25   msaitoh 	/*
   2215  1.25   msaitoh 	 * update the softc with sysctl-changed value, and mark
   2216  1.25   msaitoh 	 * for hardware update
   2217  1.25   msaitoh 	 */
   2218  1.25   msaitoh 	sc->sc_ipginttx = t;
   2219  1.25   msaitoh 
   2220  1.25   msaitoh 	return 0;
   2221  1.25   msaitoh }
   2222  1.25   msaitoh 
   2223  1.25   msaitoh static int
   2224  1.25   msaitoh mvgbe_sysctl_ipgintrx(SYSCTLFN_ARGS)
   2225  1.25   msaitoh {
   2226  1.25   msaitoh 	int error;
   2227  1.25   msaitoh 	unsigned int t;
   2228  1.25   msaitoh 	struct sysctlnode node;
   2229  1.25   msaitoh 	struct mvgbec_softc *csc;
   2230  1.25   msaitoh 	struct mvgbe_softc *sc;
   2231  1.25   msaitoh 
   2232  1.25   msaitoh 	node = *rnode;
   2233  1.25   msaitoh 	sc = node.sysctl_data;
   2234  1.25   msaitoh 	csc = device_private(device_parent(sc->sc_dev));
   2235  1.25   msaitoh 	t = sc->sc_ipgintrx;
   2236  1.25   msaitoh 	node.sysctl_data = &t;
   2237  1.25   msaitoh 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2238  1.25   msaitoh 	if (error || newp == NULL)
   2239  1.25   msaitoh 		return error;
   2240  1.25   msaitoh 
   2241  1.25   msaitoh 	if (mvgbe_ipgintrx(csc, sc, t) < 0)
   2242  1.25   msaitoh 		return EINVAL;
   2243  1.25   msaitoh 	/*
   2244  1.25   msaitoh 	 * update the softc with sysctl-changed value, and mark
   2245  1.25   msaitoh 	 * for hardware update
   2246  1.25   msaitoh 	 */
   2247  1.25   msaitoh 	sc->sc_ipgintrx = t;
   2248  1.25   msaitoh 
   2249  1.25   msaitoh 	return 0;
   2250  1.25   msaitoh }
   2251