Home | History | Annotate | Line # | Download | only in dev
if_emac.c revision 1.35.2.1
      1  1.35.2.1  uebayasi /*	$NetBSD: if_emac.c,v 1.35.2.1 2010/04/30 14:39:42 uebayasi Exp $	*/
      2       1.1    simonb 
      3       1.1    simonb /*
      4       1.3    simonb  * Copyright 2001, 2002 Wasabi Systems, Inc.
      5       1.1    simonb  * All rights reserved.
      6       1.1    simonb  *
      7       1.3    simonb  * Written by Simon Burge and Jason Thorpe for Wasabi Systems, Inc.
      8       1.1    simonb  *
      9       1.1    simonb  * Redistribution and use in source and binary forms, with or without
     10       1.1    simonb  * modification, are permitted provided that the following conditions
     11       1.1    simonb  * are met:
     12       1.1    simonb  * 1. Redistributions of source code must retain the above copyright
     13       1.1    simonb  *    notice, this list of conditions and the following disclaimer.
     14       1.1    simonb  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1    simonb  *    notice, this list of conditions and the following disclaimer in the
     16       1.1    simonb  *    documentation and/or other materials provided with the distribution.
     17       1.1    simonb  * 3. All advertising materials mentioning features or use of this software
     18       1.1    simonb  *    must display the following acknowledgement:
     19       1.1    simonb  *      This product includes software developed for the NetBSD Project by
     20       1.1    simonb  *      Wasabi Systems, Inc.
     21       1.1    simonb  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22       1.1    simonb  *    or promote products derived from this software without specific prior
     23       1.1    simonb  *    written permission.
     24       1.1    simonb  *
     25       1.1    simonb  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26       1.1    simonb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27       1.1    simonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28       1.1    simonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29       1.1    simonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30       1.1    simonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31       1.1    simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32       1.1    simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33       1.1    simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34       1.1    simonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35       1.1    simonb  * POSSIBILITY OF SUCH DAMAGE.
     36       1.1    simonb  */
     37      1.15     lukem 
     38  1.35.2.1  uebayasi /*
     39  1.35.2.1  uebayasi  * emac(4) supports following ibm4xx's EMACs.
     40  1.35.2.1  uebayasi  *   XXXX: ZMII and 'TCP Accelaration Hardware' not support yet...
     41  1.35.2.1  uebayasi  *
     42  1.35.2.1  uebayasi  *            tested
     43  1.35.2.1  uebayasi  *            ------
     44  1.35.2.1  uebayasi  * 405EP	-  10/100 x2
     45  1.35.2.1  uebayasi  * 405EX/EXr	o  10/100/1000 x2 (EXr x1), STA v2, 256bit hash-Table, RGMII
     46  1.35.2.1  uebayasi  * 405GP/GPr	o  10/100
     47  1.35.2.1  uebayasi  * 440EP	-  10/100 x2, ZMII
     48  1.35.2.1  uebayasi  * 440GP	-  10/100 x2, ZMII
     49  1.35.2.1  uebayasi  * 440GX	-  10/100/1000 x4, ZMII/RGMII(ch 2, 3), TAH(ch 2, 3)
     50  1.35.2.1  uebayasi  * 440SP	-  10/100/1000
     51  1.35.2.1  uebayasi  * 440SPe	-  10/100/1000, STA v2
     52  1.35.2.1  uebayasi  */
     53  1.35.2.1  uebayasi 
     54      1.15     lukem #include <sys/cdefs.h>
     55  1.35.2.1  uebayasi __KERNEL_RCSID(0, "$NetBSD: if_emac.c,v 1.35.2.1 2010/04/30 14:39:42 uebayasi Exp $");
     56       1.1    simonb 
     57  1.35.2.1  uebayasi #include "opt_emac.h"
     58       1.1    simonb 
     59       1.1    simonb #include <sys/param.h>
     60       1.1    simonb #include <sys/systm.h>
     61       1.1    simonb #include <sys/mbuf.h>
     62       1.1    simonb #include <sys/kernel.h>
     63       1.1    simonb #include <sys/socket.h>
     64       1.1    simonb #include <sys/ioctl.h>
     65       1.1    simonb 
     66       1.3    simonb #include <uvm/uvm_extern.h>		/* for PAGE_SIZE */
     67       1.1    simonb 
     68       1.1    simonb #include <net/if.h>
     69       1.1    simonb #include <net/if_dl.h>
     70       1.1    simonb #include <net/if_media.h>
     71       1.1    simonb #include <net/if_ether.h>
     72       1.1    simonb 
     73       1.1    simonb #include <net/bpf.h>
     74       1.1    simonb 
     75  1.35.2.1  uebayasi #include <powerpc/ibm4xx/dcr4xx.h>
     76       1.3    simonb #include <powerpc/ibm4xx/mal405gp.h>
     77       1.7    simonb #include <powerpc/ibm4xx/dev/emacreg.h>
     78       1.3    simonb #include <powerpc/ibm4xx/dev/if_emacreg.h>
     79  1.35.2.1  uebayasi #include <powerpc/ibm4xx/dev/if_emacvar.h>
     80  1.35.2.1  uebayasi #include <powerpc/ibm4xx/dev/malvar.h>
     81  1.35.2.1  uebayasi #include <powerpc/ibm4xx/dev/opbreg.h>
     82  1.35.2.1  uebayasi #include <powerpc/ibm4xx/dev/opbvar.h>
     83  1.35.2.1  uebayasi #include <powerpc/ibm4xx/dev/plbvar.h>
     84  1.35.2.1  uebayasi #if defined(EMAC_ZMII_PHY) || defined(EMAC_RGMII_PHY)
     85  1.35.2.1  uebayasi #include <powerpc/ibm4xx/dev/rmiivar.h>
     86  1.35.2.1  uebayasi #endif
     87       1.1    simonb 
     88       1.1    simonb #include <dev/mii/miivar.h>
     89       1.1    simonb 
     90  1.35.2.1  uebayasi #include "locators.h"
     91  1.35.2.1  uebayasi 
     92  1.35.2.1  uebayasi 
     93       1.3    simonb /*
     94       1.3    simonb  * Transmit descriptor list size.  There are two Tx channels, each with
     95       1.3    simonb  * up to 256 hardware descriptors available.  We currently use one Tx
     96       1.3    simonb  * channel.  We tell the upper layers that they can queue a lot of
     97       1.3    simonb  * packets, and we go ahead and manage up to 64 of them at a time.  We
     98       1.3    simonb  * allow up to 16 DMA segments per packet.
     99       1.3    simonb  */
    100       1.3    simonb #define	EMAC_NTXSEGS		16
    101       1.3    simonb #define	EMAC_TXQUEUELEN		64
    102       1.3    simonb #define	EMAC_TXQUEUELEN_MASK	(EMAC_TXQUEUELEN - 1)
    103       1.3    simonb #define	EMAC_TXQUEUE_GC		(EMAC_TXQUEUELEN / 4)
    104       1.3    simonb #define	EMAC_NTXDESC		256
    105       1.3    simonb #define	EMAC_NTXDESC_MASK	(EMAC_NTXDESC - 1)
    106       1.3    simonb #define	EMAC_NEXTTX(x)		(((x) + 1) & EMAC_NTXDESC_MASK)
    107       1.3    simonb #define	EMAC_NEXTTXS(x)		(((x) + 1) & EMAC_TXQUEUELEN_MASK)
    108       1.3    simonb 
    109       1.3    simonb /*
    110       1.3    simonb  * Receive descriptor list size.  There is one Rx channel with up to 256
    111       1.3    simonb  * hardware descriptors available.  We allocate 64 receive descriptors,
    112       1.3    simonb  * each with a 2k buffer (MCLBYTES).
    113       1.3    simonb  */
    114       1.3    simonb #define	EMAC_NRXDESC		64
    115       1.3    simonb #define	EMAC_NRXDESC_MASK	(EMAC_NRXDESC - 1)
    116       1.3    simonb #define	EMAC_NEXTRX(x)		(((x) + 1) & EMAC_NRXDESC_MASK)
    117       1.3    simonb #define	EMAC_PREVRX(x)		(((x) - 1) & EMAC_NRXDESC_MASK)
    118       1.3    simonb 
    119       1.3    simonb /*
    120       1.3    simonb  * Transmit/receive descriptors that are DMA'd to the EMAC.
    121       1.3    simonb  */
    122       1.3    simonb struct emac_control_data {
    123       1.3    simonb 	struct mal_descriptor ecd_txdesc[EMAC_NTXDESC];
    124       1.3    simonb 	struct mal_descriptor ecd_rxdesc[EMAC_NRXDESC];
    125       1.3    simonb };
    126       1.3    simonb 
    127       1.3    simonb #define	EMAC_CDOFF(x)		offsetof(struct emac_control_data, x)
    128       1.3    simonb #define	EMAC_CDTXOFF(x)		EMAC_CDOFF(ecd_txdesc[(x)])
    129       1.3    simonb #define	EMAC_CDRXOFF(x)		EMAC_CDOFF(ecd_rxdesc[(x)])
    130       1.3    simonb 
    131       1.3    simonb /*
    132       1.3    simonb  * Software state for transmit jobs.
    133       1.3    simonb  */
    134       1.3    simonb struct emac_txsoft {
    135       1.3    simonb 	struct mbuf *txs_mbuf;		/* head of mbuf chain */
    136       1.3    simonb 	bus_dmamap_t txs_dmamap;	/* our DMA map */
    137       1.3    simonb 	int txs_firstdesc;		/* first descriptor in packet */
    138       1.3    simonb 	int txs_lastdesc;		/* last descriptor in packet */
    139       1.3    simonb 	int txs_ndesc;			/* # of descriptors used */
    140       1.3    simonb };
    141       1.3    simonb 
    142       1.3    simonb /*
    143       1.3    simonb  * Software state for receive descriptors.
    144       1.3    simonb  */
    145       1.3    simonb struct emac_rxsoft {
    146       1.3    simonb 	struct mbuf *rxs_mbuf;		/* head of mbuf chain */
    147       1.3    simonb 	bus_dmamap_t rxs_dmamap;	/* our DMA map */
    148       1.3    simonb };
    149       1.3    simonb 
    150       1.3    simonb /*
    151       1.3    simonb  * Software state per device.
    152       1.3    simonb  */
    153       1.1    simonb struct emac_softc {
    154  1.35.2.1  uebayasi 	device_t sc_dev;		/* generic device information */
    155  1.35.2.1  uebayasi 	int sc_instance;		/* instance no. */
    156       1.1    simonb 	bus_space_tag_t sc_st;		/* bus space tag */
    157       1.1    simonb 	bus_space_handle_t sc_sh;	/* bus space handle */
    158       1.1    simonb 	bus_dma_tag_t sc_dmat;		/* bus DMA tag */
    159       1.1    simonb 	struct ethercom sc_ethercom;	/* ethernet common data */
    160       1.1    simonb 	void *sc_sdhook;		/* shutdown hook */
    161       1.3    simonb 	void *sc_powerhook;		/* power management hook */
    162       1.3    simonb 
    163       1.3    simonb 	struct mii_data sc_mii;		/* MII/media information */
    164       1.3    simonb 	struct callout sc_callout;	/* tick callout */
    165       1.3    simonb 
    166  1.35.2.1  uebayasi 	uint32_t sc_mr1;		/* copy of Mode Register 1 */
    167  1.35.2.1  uebayasi 	uint32_t sc_stacr_read;		/* Read opcode of STAOPC of STACR */
    168  1.35.2.1  uebayasi 	uint32_t sc_stacr_write;	/* Write opcode of STAOPC of STACR */
    169  1.35.2.1  uebayasi 	uint32_t sc_stacr_bits;		/* misc bits of STACR */
    170  1.35.2.1  uebayasi 	bool sc_stacr_completed;	/* Operation completed of STACR */
    171  1.35.2.1  uebayasi 	int sc_htsize;			/* Hash Table size */
    172       1.3    simonb 
    173       1.3    simonb 	bus_dmamap_t sc_cddmamap;	/* control data dma map */
    174       1.3    simonb #define	sc_cddma	sc_cddmamap->dm_segs[0].ds_addr
    175       1.3    simonb 
    176       1.3    simonb 	/* Software state for transmit/receive descriptors. */
    177       1.3    simonb 	struct emac_txsoft sc_txsoft[EMAC_TXQUEUELEN];
    178       1.3    simonb 	struct emac_rxsoft sc_rxsoft[EMAC_NRXDESC];
    179       1.3    simonb 
    180       1.3    simonb 	/* Control data structures. */
    181       1.3    simonb 	struct emac_control_data *sc_control_data;
    182       1.3    simonb #define	sc_txdescs	sc_control_data->ecd_txdesc
    183       1.3    simonb #define	sc_rxdescs	sc_control_data->ecd_rxdesc
    184       1.3    simonb 
    185       1.3    simonb #ifdef EMAC_EVENT_COUNTERS
    186       1.3    simonb 	struct evcnt sc_ev_rxintr;	/* Rx interrupts */
    187       1.3    simonb 	struct evcnt sc_ev_txintr;	/* Tx interrupts */
    188       1.3    simonb 	struct evcnt sc_ev_rxde;	/* Rx descriptor interrupts */
    189       1.3    simonb 	struct evcnt sc_ev_txde;	/* Tx descriptor interrupts */
    190       1.3    simonb 	struct evcnt sc_ev_intr;	/* General EMAC interrupts */
    191       1.3    simonb 
    192       1.3    simonb 	struct evcnt sc_ev_txreap;	/* Calls to Tx descriptor reaper */
    193       1.3    simonb 	struct evcnt sc_ev_txsstall;	/* Tx stalled due to no txs */
    194       1.3    simonb 	struct evcnt sc_ev_txdstall;	/* Tx stalled due to no txd */
    195       1.3    simonb 	struct evcnt sc_ev_txdrop;	/* Tx packets dropped (too many segs) */
    196       1.3    simonb 	struct evcnt sc_ev_tu;		/* Tx underrun */
    197       1.3    simonb #endif /* EMAC_EVENT_COUNTERS */
    198       1.3    simonb 
    199       1.3    simonb 	int sc_txfree;			/* number of free Tx descriptors */
    200       1.3    simonb 	int sc_txnext;			/* next ready Tx descriptor */
    201       1.3    simonb 
    202       1.3    simonb 	int sc_txsfree;			/* number of free Tx jobs */
    203       1.3    simonb 	int sc_txsnext;			/* next ready Tx job */
    204       1.3    simonb 	int sc_txsdirty;		/* dirty Tx jobs */
    205       1.3    simonb 
    206       1.3    simonb 	int sc_rxptr;			/* next ready RX descriptor/descsoft */
    207  1.35.2.1  uebayasi 
    208  1.35.2.1  uebayasi 	void (*sc_rmii_enable)(device_t, int);		/* reduced MII enable */
    209  1.35.2.1  uebayasi 	void (*sc_rmii_disable)(device_t, int);		/* reduced MII disable*/
    210  1.35.2.1  uebayasi 	void (*sc_rmii_speed)(device_t, int, int);	/* reduced MII speed */
    211       1.1    simonb };
    212       1.1    simonb 
    213       1.3    simonb #ifdef EMAC_EVENT_COUNTERS
    214       1.3    simonb #define	EMAC_EVCNT_INCR(ev)	(ev)->ev_count++
    215       1.3    simonb #else
    216       1.3    simonb #define	EMAC_EVCNT_INCR(ev)	/* nothing */
    217       1.3    simonb #endif
    218       1.3    simonb 
    219       1.3    simonb #define	EMAC_CDTXADDR(sc, x)	((sc)->sc_cddma + EMAC_CDTXOFF((x)))
    220       1.3    simonb #define	EMAC_CDRXADDR(sc, x)	((sc)->sc_cddma + EMAC_CDRXOFF((x)))
    221       1.3    simonb 
    222       1.3    simonb #define	EMAC_CDTXSYNC(sc, x, n, ops)					\
    223       1.3    simonb do {									\
    224       1.3    simonb 	int __x, __n;							\
    225       1.3    simonb 									\
    226       1.3    simonb 	__x = (x);							\
    227       1.3    simonb 	__n = (n);							\
    228       1.3    simonb 									\
    229       1.3    simonb 	/* If it will wrap around, sync to the end of the ring. */	\
    230       1.3    simonb 	if ((__x + __n) > EMAC_NTXDESC) {				\
    231       1.3    simonb 		bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,	\
    232       1.3    simonb 		    EMAC_CDTXOFF(__x), sizeof(struct mal_descriptor) *	\
    233       1.3    simonb 		    (EMAC_NTXDESC - __x), (ops));			\
    234       1.3    simonb 		__n -= (EMAC_NTXDESC - __x);				\
    235       1.3    simonb 		__x = 0;						\
    236       1.3    simonb 	}								\
    237       1.3    simonb 									\
    238       1.3    simonb 	/* Now sync whatever is left. */				\
    239       1.3    simonb 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
    240       1.3    simonb 	    EMAC_CDTXOFF(__x), sizeof(struct mal_descriptor) * __n, (ops)); \
    241       1.3    simonb } while (/*CONSTCOND*/0)
    242       1.3    simonb 
    243       1.3    simonb #define	EMAC_CDRXSYNC(sc, x, ops)					\
    244       1.3    simonb do {									\
    245       1.3    simonb 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
    246       1.3    simonb 	    EMAC_CDRXOFF((x)), sizeof(struct mal_descriptor), (ops));	\
    247       1.3    simonb } while (/*CONSTCOND*/0)
    248       1.3    simonb 
    249       1.3    simonb #define	EMAC_INIT_RXDESC(sc, x)						\
    250       1.3    simonb do {									\
    251       1.3    simonb 	struct emac_rxsoft *__rxs = &(sc)->sc_rxsoft[(x)];		\
    252       1.3    simonb 	struct mal_descriptor *__rxd = &(sc)->sc_rxdescs[(x)];		\
    253       1.3    simonb 	struct mbuf *__m = __rxs->rxs_mbuf;				\
    254       1.3    simonb 									\
    255       1.3    simonb 	/*								\
    256       1.3    simonb 	 * Note: We scoot the packet forward 2 bytes in the buffer	\
    257       1.3    simonb 	 * so that the payload after the Ethernet header is aligned	\
    258       1.3    simonb 	 * to a 4-byte boundary.					\
    259       1.3    simonb 	 */								\
    260       1.3    simonb 	__m->m_data = __m->m_ext.ext_buf + 2;				\
    261       1.3    simonb 									\
    262       1.3    simonb 	__rxd->md_data = __rxs->rxs_dmamap->dm_segs[0].ds_addr + 2;	\
    263       1.3    simonb 	__rxd->md_data_len = __m->m_ext.ext_size - 2;			\
    264       1.3    simonb 	__rxd->md_stat_ctrl = MAL_RX_EMPTY | MAL_RX_INTERRUPT |		\
    265       1.3    simonb 	    /* Set wrap on last descriptor. */				\
    266       1.3    simonb 	    (((x) == EMAC_NRXDESC - 1) ? MAL_RX_WRAP : 0);		\
    267       1.3    simonb 	EMAC_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \
    268       1.3    simonb } while (/*CONSTCOND*/0)
    269       1.3    simonb 
    270       1.3    simonb #define	EMAC_WRITE(sc, reg, val) \
    271       1.3    simonb 	bus_space_write_stream_4((sc)->sc_st, (sc)->sc_sh, (reg), (val))
    272       1.3    simonb #define	EMAC_READ(sc, reg) \
    273       1.3    simonb 	bus_space_read_stream_4((sc)->sc_st, (sc)->sc_sh, (reg))
    274       1.3    simonb 
    275  1.35.2.1  uebayasi #define	EMAC_SET_FILTER(aht, crc) \
    276  1.35.2.1  uebayasi do {									\
    277  1.35.2.1  uebayasi 	(aht)[3 - (((crc) >> 26) >> 4)] |= 1 << (((crc) >> 26) & 0xf);	\
    278  1.35.2.1  uebayasi } while (/*CONSTCOND*/0)
    279  1.35.2.1  uebayasi #define	EMAC_SET_FILTER256(aht, crc) \
    280      1.18    simonb do {									\
    281  1.35.2.1  uebayasi 	(aht)[7 - (((crc) >> 24) >> 5)] |= 1 << (((crc) >> 24) & 0x1f);	\
    282      1.18    simonb } while (/*CONSTCOND*/0)
    283      1.18    simonb 
    284  1.35.2.1  uebayasi static int	emac_match(device_t, cfdata_t, void *);
    285  1.35.2.1  uebayasi static void	emac_attach(device_t, device_t, void *);
    286       1.3    simonb 
    287  1.35.2.1  uebayasi static int	emac_intr(void *);
    288       1.3    simonb static void	emac_shutdown(void *);
    289  1.35.2.1  uebayasi 
    290       1.3    simonb static void	emac_start(struct ifnet *);
    291  1.35.2.1  uebayasi static int	emac_ioctl(struct ifnet *, u_long, void *);
    292  1.35.2.1  uebayasi static int	emac_init(struct ifnet *);
    293       1.3    simonb static void	emac_stop(struct ifnet *, int);
    294       1.3    simonb static void	emac_watchdog(struct ifnet *);
    295  1.35.2.1  uebayasi 
    296  1.35.2.1  uebayasi static int	emac_add_rxbuf(struct emac_softc *, int);
    297  1.35.2.1  uebayasi static void	emac_rxdrain(struct emac_softc *);
    298      1.18    simonb static int	emac_set_filter(struct emac_softc *);
    299  1.35.2.1  uebayasi static int	emac_txreap(struct emac_softc *);
    300       1.3    simonb 
    301  1.35.2.1  uebayasi static void	emac_soft_reset(struct emac_softc *);
    302  1.35.2.1  uebayasi static void	emac_smart_reset(struct emac_softc *);
    303       1.1    simonb 
    304  1.35.2.1  uebayasi static int	emac_mii_readreg(device_t, int, int);
    305  1.35.2.1  uebayasi static void	emac_mii_writereg(device_t, int, int, int);
    306  1.35.2.1  uebayasi static void	emac_mii_statchg(device_t);
    307       1.3    simonb static uint32_t	emac_mii_wait(struct emac_softc *);
    308  1.35.2.1  uebayasi static void	emac_mii_tick(void *);
    309       1.3    simonb 
    310       1.3    simonb int		emac_copy_small = 0;
    311       1.3    simonb 
    312  1.35.2.1  uebayasi CFATTACH_DECL_NEW(emac, sizeof(struct emac_softc),
    313      1.13   thorpej     emac_match, emac_attach, NULL, NULL);
    314       1.1    simonb 
    315  1.35.2.1  uebayasi 
    316       1.1    simonb static int
    317  1.35.2.1  uebayasi emac_match(device_t parent, cfdata_t cf, void *aux)
    318       1.1    simonb {
    319       1.5    simonb 	struct opb_attach_args *oaa = aux;
    320       1.1    simonb 
    321       1.3    simonb 	/* match only on-chip ethernet devices */
    322      1.10   thorpej 	if (strcmp(oaa->opb_name, cf->cf_name) == 0)
    323  1.35.2.1  uebayasi 		return 1;
    324       1.1    simonb 
    325  1.35.2.1  uebayasi 	return 0;
    326       1.1    simonb }
    327       1.1    simonb 
    328       1.1    simonb static void
    329  1.35.2.1  uebayasi emac_attach(device_t parent, device_t self, void *aux)
    330       1.1    simonb {
    331       1.5    simonb 	struct opb_attach_args *oaa = aux;
    332  1.35.2.1  uebayasi 	struct emac_softc *sc = device_private(self);
    333       1.3    simonb 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    334       1.3    simonb 	struct mii_data *mii = &sc->sc_mii;
    335       1.3    simonb 	bus_dma_segment_t seg;
    336  1.35.2.1  uebayasi 	int error, i, nseg, opb_freq, opbc, mii_phy = MII_PHY_ANY;
    337      1.26   thorpej 	const uint8_t *enaddr;
    338  1.35.2.1  uebayasi 	prop_dictionary_t dict = device_properties(self);
    339      1.26   thorpej 	prop_data_t ea;
    340       1.1    simonb 
    341      1.27  kiyohara 	bus_space_map(oaa->opb_bt, oaa->opb_addr, EMAC_NREG, 0, &sc->sc_sh);
    342  1.35.2.1  uebayasi 
    343  1.35.2.1  uebayasi 	sc->sc_dev = self;
    344  1.35.2.1  uebayasi 	sc->sc_instance = oaa->opb_instance;
    345       1.9    simonb 	sc->sc_st = oaa->opb_bt;
    346       1.5    simonb 	sc->sc_dmat = oaa->opb_dmat;
    347       1.1    simonb 
    348      1.33  kiyohara 	callout_init(&sc->sc_callout, 0);
    349      1.33  kiyohara 
    350  1.35.2.1  uebayasi 	aprint_naive("\n");
    351  1.35.2.1  uebayasi 	aprint_normal(": Ethernet Media Access Controller\n");
    352  1.35.2.1  uebayasi 
    353  1.35.2.1  uebayasi 	/* Fetch the Ethernet address. */
    354  1.35.2.1  uebayasi 	ea = prop_dictionary_get(dict, "mac-address");
    355  1.35.2.1  uebayasi 	if (ea == NULL) {
    356  1.35.2.1  uebayasi 		aprint_error_dev(self, "unable to get mac-address property\n");
    357  1.35.2.1  uebayasi 		return;
    358  1.35.2.1  uebayasi 	}
    359  1.35.2.1  uebayasi 	KASSERT(prop_object_type(ea) == PROP_TYPE_DATA);
    360  1.35.2.1  uebayasi 	KASSERT(prop_data_size(ea) == ETHER_ADDR_LEN);
    361  1.35.2.1  uebayasi 	enaddr = prop_data_data_nocopy(ea);
    362  1.35.2.1  uebayasi 	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(enaddr));
    363  1.35.2.1  uebayasi 
    364  1.35.2.1  uebayasi #if defined(EMAC_ZMII_PHY) || defined(EMAC_RGMII_PHY)
    365  1.35.2.1  uebayasi 	/* Fetch the MII offset. */
    366  1.35.2.1  uebayasi 	prop_dictionary_get_uint32(dict, "mii-phy", &mii_phy);
    367  1.35.2.1  uebayasi 
    368  1.35.2.1  uebayasi #ifdef EMAC_ZMII_PHY
    369  1.35.2.1  uebayasi 	if (oaa->opb_flags & OPB_FLAGS_EMAC_RMII_ZMII)
    370  1.35.2.1  uebayasi 		zmii_attach(parent, sc->sc_instance, &sc->sc_rmii_enable,
    371  1.35.2.1  uebayasi 		    &sc->sc_rmii_disable, &sc->sc_rmii_speed);
    372  1.35.2.1  uebayasi #endif
    373  1.35.2.1  uebayasi #ifdef EMAC_RGMII_PHY
    374  1.35.2.1  uebayasi 	if (oaa->opb_flags & OPB_FLAGS_EMAC_RMII_RGMII)
    375  1.35.2.1  uebayasi 		rgmii_attach(parent, sc->sc_instance, &sc->sc_rmii_enable,
    376  1.35.2.1  uebayasi 		    &sc->sc_rmii_disable, &sc->sc_rmii_speed);
    377  1.35.2.1  uebayasi #endif
    378  1.35.2.1  uebayasi #endif
    379       1.3    simonb 
    380       1.3    simonb 	/*
    381       1.3    simonb 	 * Allocate the control data structures, and create and load the
    382       1.3    simonb 	 * DMA map for it.
    383       1.3    simonb 	 */
    384       1.3    simonb 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    385       1.3    simonb 	    sizeof(struct emac_control_data), 0, 0, &seg, 1, &nseg, 0)) != 0) {
    386  1.35.2.1  uebayasi 		aprint_error_dev(self,
    387  1.35.2.1  uebayasi 		    "unable to allocate control data, error = %d\n", error);
    388       1.3    simonb 		goto fail_0;
    389       1.3    simonb 	}
    390       1.3    simonb 
    391       1.3    simonb 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, nseg,
    392      1.28  christos 	    sizeof(struct emac_control_data), (void **)&sc->sc_control_data,
    393       1.3    simonb 	    BUS_DMA_COHERENT)) != 0) {
    394  1.35.2.1  uebayasi 		aprint_error_dev(self,
    395  1.35.2.1  uebayasi 		    "unable to map control data, error = %d\n", error);
    396       1.3    simonb 		goto fail_1;
    397       1.3    simonb 	}
    398       1.3    simonb 
    399       1.3    simonb 	if ((error = bus_dmamap_create(sc->sc_dmat,
    400       1.3    simonb 	    sizeof(struct emac_control_data), 1,
    401       1.3    simonb 	    sizeof(struct emac_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
    402  1.35.2.1  uebayasi 		aprint_error_dev(self,
    403  1.35.2.1  uebayasi 		    "unable to create control data DMA map, error = %d\n",
    404  1.35.2.1  uebayasi 		    error);
    405       1.3    simonb 		goto fail_2;
    406       1.3    simonb 	}
    407       1.3    simonb 
    408       1.3    simonb 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
    409       1.3    simonb 	    sc->sc_control_data, sizeof(struct emac_control_data), NULL,
    410       1.3    simonb 	    0)) != 0) {
    411  1.35.2.1  uebayasi 		aprint_error_dev(self,
    412  1.35.2.1  uebayasi 		    "unable to load control data DMA map, error = %d\n", error);
    413       1.3    simonb 		goto fail_3;
    414       1.3    simonb 	}
    415       1.3    simonb 
    416       1.3    simonb 	/*
    417       1.3    simonb 	 * Create the transmit buffer DMA maps.
    418       1.3    simonb 	 */
    419       1.3    simonb 	for (i = 0; i < EMAC_TXQUEUELEN; i++) {
    420       1.3    simonb 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    421       1.3    simonb 		    EMAC_NTXSEGS, MCLBYTES, 0, 0,
    422       1.3    simonb 		    &sc->sc_txsoft[i].txs_dmamap)) != 0) {
    423  1.35.2.1  uebayasi 			aprint_error_dev(self,
    424  1.35.2.1  uebayasi 			    "unable to create tx DMA map %d, error = %d\n",
    425  1.35.2.1  uebayasi 			    i, error);
    426       1.3    simonb 			goto fail_4;
    427       1.3    simonb 		}
    428       1.3    simonb 	}
    429       1.3    simonb 
    430       1.3    simonb 	/*
    431       1.3    simonb 	 * Create the receive buffer DMA maps.
    432       1.3    simonb 	 */
    433       1.3    simonb 	for (i = 0; i < EMAC_NRXDESC; i++) {
    434       1.3    simonb 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    435       1.3    simonb 		    MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
    436  1.35.2.1  uebayasi 			aprint_error_dev(self,
    437  1.35.2.1  uebayasi 			    "unable to create rx DMA map %d, error = %d\n",
    438  1.35.2.1  uebayasi 			    i, error);
    439       1.3    simonb 			goto fail_5;
    440       1.3    simonb 		}
    441       1.3    simonb 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
    442       1.3    simonb 	}
    443       1.3    simonb 
    444  1.35.2.1  uebayasi 	/* Soft Reset the EMAC.  The chip to a known state. */
    445  1.35.2.1  uebayasi 	emac_soft_reset(sc);
    446       1.3    simonb 
    447  1.35.2.1  uebayasi 	opb_freq = opb_get_frequency();
    448  1.35.2.1  uebayasi 	switch (opb_freq) {
    449  1.35.2.1  uebayasi 	case  50000000: opbc =  STACR_OPBC_50MHZ; break;
    450  1.35.2.1  uebayasi 	case  66666666: opbc =  STACR_OPBC_66MHZ; break;
    451  1.35.2.1  uebayasi 	case  83333333: opbc =  STACR_OPBC_83MHZ; break;
    452  1.35.2.1  uebayasi 	case 100000000: opbc = STACR_OPBC_100MHZ; break;
    453  1.35.2.1  uebayasi 
    454  1.35.2.1  uebayasi 	default:
    455  1.35.2.1  uebayasi 		if (opb_freq > 100000000) {
    456  1.35.2.1  uebayasi 			opbc = STACR_OPBC_A100MHZ;
    457  1.35.2.1  uebayasi 			break;
    458  1.35.2.1  uebayasi 		}
    459  1.35.2.1  uebayasi 		aprint_error_dev(self, "unsupport OPB frequency %dMHz\n",
    460  1.35.2.1  uebayasi 		    opb_freq / 1000 / 1000);
    461  1.35.2.1  uebayasi 		goto fail_5;
    462  1.35.2.1  uebayasi 	}
    463  1.35.2.1  uebayasi 	if (oaa->opb_flags & OPB_FLAGS_EMAC_GBE) {
    464  1.35.2.1  uebayasi 		sc->sc_mr1 =
    465  1.35.2.1  uebayasi 		    MR1_RFS_GBE(MR1__FS_16KB)	|
    466  1.35.2.1  uebayasi 		    MR1_TFS_GBE(MR1__FS_16KB)	|
    467  1.35.2.1  uebayasi 		    MR1_TR0_MULTIPLE		|
    468  1.35.2.1  uebayasi 		    MR1_OBCI(opbc);
    469  1.35.2.1  uebayasi 		sc->sc_ethercom.ec_capabilities |= ETHERCAP_JUMBO_MTU;
    470  1.35.2.1  uebayasi 
    471  1.35.2.1  uebayasi 		if (oaa->opb_flags & OPB_FLAGS_EMAC_STACV2) {
    472  1.35.2.1  uebayasi 			sc->sc_stacr_read = STACR_STAOPC_READ;
    473  1.35.2.1  uebayasi 			sc->sc_stacr_write = STACR_STAOPC_WRITE;
    474  1.35.2.1  uebayasi 			sc->sc_stacr_bits = STACR_OC;
    475  1.35.2.1  uebayasi 			sc->sc_stacr_completed = false;
    476  1.35.2.1  uebayasi 		} else {
    477  1.35.2.1  uebayasi 			sc->sc_stacr_read = STACR_READ;
    478  1.35.2.1  uebayasi 			sc->sc_stacr_write = STACR_WRITE;
    479  1.35.2.1  uebayasi 			sc->sc_stacr_completed = true;
    480  1.35.2.1  uebayasi 		}
    481  1.35.2.1  uebayasi 	} else {
    482  1.35.2.1  uebayasi 		/*
    483  1.35.2.1  uebayasi 		 * Set up Mode Register 1 - set receive and transmit FIFOs to
    484  1.35.2.1  uebayasi 		 * maximum size, allow transmit of multiple packets (only
    485  1.35.2.1  uebayasi 		 * channel 0 is used).
    486  1.35.2.1  uebayasi 		 *
    487  1.35.2.1  uebayasi 		 * XXX: Allow pause packets??
    488  1.35.2.1  uebayasi 		 */
    489  1.35.2.1  uebayasi 		sc->sc_mr1 =
    490  1.35.2.1  uebayasi 		    MR1_RFS(MR1__FS_4KB) |
    491  1.35.2.1  uebayasi 		    MR1_TFS(MR1__FS_2KB) |
    492  1.35.2.1  uebayasi 		    MR1_TR0_MULTIPLE;
    493  1.35.2.1  uebayasi 
    494  1.35.2.1  uebayasi 		sc->sc_stacr_read = STACR_READ;
    495  1.35.2.1  uebayasi 		sc->sc_stacr_write = STACR_WRITE;
    496  1.35.2.1  uebayasi 		sc->sc_stacr_bits = STACR_OPBC(opbc);
    497  1.35.2.1  uebayasi 		sc->sc_stacr_completed = true;
    498      1.14   thorpej 	}
    499      1.14   thorpej 
    500  1.35.2.1  uebayasi 	intr_establish(oaa->opb_irq, IST_LEVEL, IPL_NET, emac_intr, sc);
    501  1.35.2.1  uebayasi 	mal_intr_establish(sc->sc_instance, sc);
    502  1.35.2.1  uebayasi 
    503  1.35.2.1  uebayasi 	if (oaa->opb_flags & OPB_FLAGS_EMAC_HT256)
    504  1.35.2.1  uebayasi 		sc->sc_htsize = 256;
    505  1.35.2.1  uebayasi 	else
    506  1.35.2.1  uebayasi 		sc->sc_htsize = 64;
    507  1.35.2.1  uebayasi 
    508  1.35.2.1  uebayasi 	/* Clear all interrupts */
    509  1.35.2.1  uebayasi 	EMAC_WRITE(sc, EMAC_ISR, ISR_ALL);
    510       1.1    simonb 
    511       1.3    simonb 	/*
    512       1.3    simonb 	 * Initialise the media structures.
    513       1.3    simonb 	 */
    514       1.3    simonb 	mii->mii_ifp = ifp;
    515       1.3    simonb 	mii->mii_readreg = emac_mii_readreg;
    516       1.3    simonb 	mii->mii_writereg = emac_mii_writereg;
    517       1.3    simonb 	mii->mii_statchg = emac_mii_statchg;
    518       1.3    simonb 
    519      1.31    dyoung 	sc->sc_ethercom.ec_mii = mii;
    520      1.31    dyoung 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
    521  1.35.2.1  uebayasi 	mii_attach(self, mii, 0xffffffff, mii_phy, MII_OFFSET_ANY, 0);
    522       1.3    simonb 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    523       1.3    simonb 		ifmedia_add(&mii->mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    524       1.3    simonb 		ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_NONE);
    525       1.3    simonb 	} else
    526       1.3    simonb 		ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_AUTO);
    527       1.3    simonb 
    528       1.3    simonb 	ifp = &sc->sc_ethercom.ec_if;
    529  1.35.2.1  uebayasi 	strcpy(ifp->if_xname, self->dv_xname);
    530       1.3    simonb 	ifp->if_softc = sc;
    531       1.3    simonb 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    532       1.3    simonb 	ifp->if_start = emac_start;
    533  1.35.2.1  uebayasi 	ifp->if_ioctl = emac_ioctl;
    534       1.3    simonb 	ifp->if_init = emac_init;
    535       1.3    simonb 	ifp->if_stop = emac_stop;
    536  1.35.2.1  uebayasi 	ifp->if_watchdog = emac_watchdog;
    537       1.3    simonb 	IFQ_SET_READY(&ifp->if_snd);
    538       1.3    simonb 
    539       1.3    simonb 	/*
    540       1.3    simonb 	 * We can support 802.1Q VLAN-sized frames.
    541       1.3    simonb 	 */
    542       1.3    simonb 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    543       1.3    simonb 
    544       1.3    simonb 	/*
    545       1.3    simonb 	 * Attach the interface.
    546       1.3    simonb 	 */
    547       1.3    simonb 	if_attach(ifp);
    548      1.14   thorpej 	ether_ifattach(ifp, enaddr);
    549       1.3    simonb 
    550       1.3    simonb #ifdef EMAC_EVENT_COUNTERS
    551       1.3    simonb 	/*
    552       1.3    simonb 	 * Attach the event counters.
    553       1.3    simonb 	 */
    554       1.3    simonb 	evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_INTR,
    555  1.35.2.1  uebayasi 	    NULL, self->dv_xname, "txintr");
    556  1.35.2.1  uebayasi 	evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
    557  1.35.2.1  uebayasi 	    NULL, self->dv_xname, "rxintr");
    558       1.3    simonb 	evcnt_attach_dynamic(&sc->sc_ev_txde, EVCNT_TYPE_INTR,
    559  1.35.2.1  uebayasi 	    NULL, self->dv_xname, "txde");
    560  1.35.2.1  uebayasi 	evcnt_attach_dynamic(&sc->sc_ev_rxde, EVCNT_TYPE_INTR,
    561  1.35.2.1  uebayasi 	    NULL, self->dv_xname, "rxde");
    562       1.3    simonb 	evcnt_attach_dynamic(&sc->sc_ev_intr, EVCNT_TYPE_INTR,
    563  1.35.2.1  uebayasi 	    NULL, self->dv_xname, "intr");
    564       1.3    simonb 
    565       1.3    simonb 	evcnt_attach_dynamic(&sc->sc_ev_txreap, EVCNT_TYPE_MISC,
    566  1.35.2.1  uebayasi 	    NULL, self->dv_xname, "txreap");
    567       1.3    simonb 	evcnt_attach_dynamic(&sc->sc_ev_txsstall, EVCNT_TYPE_MISC,
    568  1.35.2.1  uebayasi 	    NULL, self->dv_xname, "txsstall");
    569       1.3    simonb 	evcnt_attach_dynamic(&sc->sc_ev_txdstall, EVCNT_TYPE_MISC,
    570  1.35.2.1  uebayasi 	    NULL, self->dv_xname, "txdstall");
    571       1.3    simonb 	evcnt_attach_dynamic(&sc->sc_ev_txdrop, EVCNT_TYPE_MISC,
    572  1.35.2.1  uebayasi 	    NULL, self->dv_xname, "txdrop");
    573       1.3    simonb 	evcnt_attach_dynamic(&sc->sc_ev_tu, EVCNT_TYPE_MISC,
    574  1.35.2.1  uebayasi 	    NULL, self->dv_xname, "tu");
    575       1.3    simonb #endif /* EMAC_EVENT_COUNTERS */
    576       1.3    simonb 
    577       1.3    simonb 	/*
    578       1.3    simonb 	 * Make sure the interface is shutdown during reboot.
    579       1.3    simonb 	 */
    580       1.3    simonb 	sc->sc_sdhook = shutdownhook_establish(emac_shutdown, sc);
    581       1.3    simonb 	if (sc->sc_sdhook == NULL)
    582  1.35.2.1  uebayasi 		aprint_error_dev(self,
    583  1.35.2.1  uebayasi 		    "WARNING: unable to establish shutdown hook\n");
    584       1.3    simonb 
    585       1.3    simonb 	return;
    586       1.3    simonb 
    587       1.3    simonb 	/*
    588       1.3    simonb 	 * Free any resources we've allocated during the failed attach
    589       1.3    simonb 	 * attempt.  Do this in reverse order and fall through.
    590       1.3    simonb 	 */
    591       1.3    simonb fail_5:
    592       1.3    simonb 	for (i = 0; i < EMAC_NRXDESC; i++) {
    593       1.3    simonb 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
    594       1.3    simonb 			bus_dmamap_destroy(sc->sc_dmat,
    595       1.3    simonb 			    sc->sc_rxsoft[i].rxs_dmamap);
    596       1.3    simonb 	}
    597       1.3    simonb fail_4:
    598       1.3    simonb 	for (i = 0; i < EMAC_TXQUEUELEN; i++) {
    599       1.3    simonb 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
    600       1.3    simonb 			bus_dmamap_destroy(sc->sc_dmat,
    601       1.3    simonb 			    sc->sc_txsoft[i].txs_dmamap);
    602       1.3    simonb 	}
    603       1.3    simonb 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    604       1.3    simonb fail_3:
    605       1.3    simonb 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    606       1.3    simonb fail_2:
    607      1.28  christos 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
    608       1.3    simonb 	    sizeof(struct emac_control_data));
    609       1.3    simonb fail_1:
    610       1.3    simonb 	bus_dmamem_free(sc->sc_dmat, &seg, nseg);
    611       1.3    simonb fail_0:
    612       1.3    simonb 	return;
    613       1.3    simonb }
    614       1.3    simonb 
    615       1.3    simonb /*
    616  1.35.2.1  uebayasi  * EMAC General interrupt handler
    617       1.3    simonb  */
    618  1.35.2.1  uebayasi static int
    619  1.35.2.1  uebayasi emac_intr(void *arg)
    620  1.35.2.1  uebayasi {
    621  1.35.2.1  uebayasi 	struct emac_softc *sc = arg;
    622  1.35.2.1  uebayasi 	uint32_t status;
    623  1.35.2.1  uebayasi 
    624  1.35.2.1  uebayasi 	EMAC_EVCNT_INCR(&sc->sc_ev_intr);
    625  1.35.2.1  uebayasi 	status = EMAC_READ(sc, EMAC_ISR);
    626  1.35.2.1  uebayasi 
    627  1.35.2.1  uebayasi 	/* Clear the interrupt status bits. */
    628  1.35.2.1  uebayasi 	EMAC_WRITE(sc, EMAC_ISR, status);
    629  1.35.2.1  uebayasi 
    630  1.35.2.1  uebayasi 	return 1;
    631  1.35.2.1  uebayasi }
    632  1.35.2.1  uebayasi 
    633       1.3    simonb static void
    634       1.3    simonb emac_shutdown(void *arg)
    635       1.3    simonb {
    636       1.3    simonb 	struct emac_softc *sc = arg;
    637       1.3    simonb 
    638       1.3    simonb 	emac_stop(&sc->sc_ethercom.ec_if, 0);
    639       1.3    simonb }
    640       1.3    simonb 
    641  1.35.2.1  uebayasi 
    642  1.35.2.1  uebayasi /*
    643  1.35.2.1  uebayasi  * ifnet interface functions
    644  1.35.2.1  uebayasi  */
    645  1.35.2.1  uebayasi 
    646       1.3    simonb static void
    647       1.3    simonb emac_start(struct ifnet *ifp)
    648       1.3    simonb {
    649       1.3    simonb 	struct emac_softc *sc = ifp->if_softc;
    650       1.3    simonb 	struct mbuf *m0;
    651       1.3    simonb 	struct emac_txsoft *txs;
    652       1.3    simonb 	bus_dmamap_t dmamap;
    653       1.3    simonb 	int error, firsttx, nexttx, lasttx, ofree, seg;
    654      1.17    simonb 
    655      1.17    simonb 	lasttx = 0;	/* XXX gcc */
    656       1.3    simonb 
    657       1.3    simonb 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    658       1.3    simonb 		return;
    659       1.3    simonb 
    660       1.3    simonb 	/*
    661       1.3    simonb 	 * Remember the previous number of free descriptors.
    662       1.3    simonb 	 */
    663       1.3    simonb 	ofree = sc->sc_txfree;
    664       1.3    simonb 
    665       1.3    simonb 	/*
    666       1.3    simonb 	 * Loop through the send queue, setting up transmit descriptors
    667       1.3    simonb 	 * until we drain the queue, or use up all available transmit
    668       1.3    simonb 	 * descriptors.
    669       1.3    simonb 	 */
    670       1.3    simonb 	for (;;) {
    671       1.3    simonb 		/* Grab a packet off the queue. */
    672       1.3    simonb 		IFQ_POLL(&ifp->if_snd, m0);
    673       1.3    simonb 		if (m0 == NULL)
    674       1.3    simonb 			break;
    675       1.3    simonb 
    676       1.3    simonb 		/*
    677       1.3    simonb 		 * Get a work queue entry.  Reclaim used Tx descriptors if
    678       1.3    simonb 		 * we are running low.
    679       1.3    simonb 		 */
    680       1.3    simonb 		if (sc->sc_txsfree < EMAC_TXQUEUE_GC) {
    681       1.3    simonb 			emac_txreap(sc);
    682       1.3    simonb 			if (sc->sc_txsfree == 0) {
    683       1.3    simonb 				EMAC_EVCNT_INCR(&sc->sc_ev_txsstall);
    684       1.3    simonb 				break;
    685       1.3    simonb 			}
    686       1.3    simonb 		}
    687       1.3    simonb 
    688       1.3    simonb 		txs = &sc->sc_txsoft[sc->sc_txsnext];
    689       1.3    simonb 		dmamap = txs->txs_dmamap;
    690       1.3    simonb 
    691       1.3    simonb 		/*
    692       1.3    simonb 		 * Load the DMA map.  If this fails, the packet either
    693       1.3    simonb 		 * didn't fit in the alloted number of segments, or we
    694       1.3    simonb 		 * were short on resources.  In this case, we'll copy
    695       1.3    simonb 		 * and try again.
    696       1.3    simonb 		 */
    697       1.3    simonb 		error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    698       1.3    simonb 		    BUS_DMA_WRITE|BUS_DMA_NOWAIT);
    699       1.3    simonb 		if (error) {
    700       1.3    simonb 			if (error == EFBIG) {
    701       1.3    simonb 				EMAC_EVCNT_INCR(&sc->sc_ev_txdrop);
    702  1.35.2.1  uebayasi 				aprint_error_ifnet(ifp,
    703  1.35.2.1  uebayasi 				    "Tx packet consumes too many "
    704  1.35.2.1  uebayasi 				    "DMA segments, dropping...\n");
    705       1.3    simonb 				    IFQ_DEQUEUE(&ifp->if_snd, m0);
    706       1.3    simonb 				    m_freem(m0);
    707       1.3    simonb 				    continue;
    708       1.3    simonb 			}
    709       1.3    simonb 			/* Short on resources, just stop for now. */
    710       1.3    simonb 			break;
    711       1.3    simonb 		}
    712       1.3    simonb 
    713       1.3    simonb 		/*
    714       1.3    simonb 		 * Ensure we have enough descriptors free to describe
    715       1.3    simonb 		 * the packet.
    716       1.3    simonb 		 */
    717       1.3    simonb 		if (dmamap->dm_nsegs > sc->sc_txfree) {
    718       1.3    simonb 			/*
    719       1.3    simonb 			 * Not enough free descriptors to transmit this
    720       1.3    simonb 			 * packet.  We haven't committed anything yet,
    721       1.3    simonb 			 * so just unload the DMA map, put the packet
    722       1.3    simonb 			 * back on the queue, and punt.  Notify the upper
    723       1.3    simonb 			 * layer that there are not more slots left.
    724       1.3    simonb 			 *
    725       1.3    simonb 			 */
    726       1.3    simonb 			ifp->if_flags |= IFF_OACTIVE;
    727       1.3    simonb 			bus_dmamap_unload(sc->sc_dmat, dmamap);
    728       1.3    simonb 			EMAC_EVCNT_INCR(&sc->sc_ev_txdstall);
    729       1.3    simonb 			break;
    730       1.3    simonb 		}
    731       1.3    simonb 
    732       1.3    simonb 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    733       1.3    simonb 
    734       1.3    simonb 		/*
    735       1.3    simonb 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
    736       1.3    simonb 		 */
    737       1.3    simonb 
    738       1.3    simonb 		/* Sync the DMA map. */
    739       1.3    simonb 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
    740       1.3    simonb 		    BUS_DMASYNC_PREWRITE);
    741       1.3    simonb 
    742       1.3    simonb 		/*
    743       1.3    simonb 		 * Store a pointer to the packet so that we can free it
    744       1.3    simonb 		 * later.
    745       1.3    simonb 		 */
    746       1.3    simonb 		txs->txs_mbuf = m0;
    747       1.3    simonb 		txs->txs_firstdesc = sc->sc_txnext;
    748       1.3    simonb 		txs->txs_ndesc = dmamap->dm_nsegs;
    749       1.3    simonb 
    750       1.3    simonb 		/*
    751       1.3    simonb 		 * Initialize the transmit descriptor.
    752       1.3    simonb 		 */
    753       1.3    simonb 		firsttx = sc->sc_txnext;
    754       1.3    simonb 		for (nexttx = sc->sc_txnext, seg = 0;
    755       1.3    simonb 		     seg < dmamap->dm_nsegs;
    756       1.3    simonb 		     seg++, nexttx = EMAC_NEXTTX(nexttx)) {
    757  1.35.2.1  uebayasi 			struct mal_descriptor *txdesc =
    758  1.35.2.1  uebayasi 			    &sc->sc_txdescs[nexttx];
    759  1.35.2.1  uebayasi 
    760       1.3    simonb 			/*
    761       1.3    simonb 			 * If this is the first descriptor we're
    762       1.3    simonb 			 * enqueueing, don't set the TX_READY bit just
    763       1.3    simonb 			 * yet.  That could cause a race condition.
    764       1.3    simonb 			 * We'll do it below.
    765       1.3    simonb 			 */
    766  1.35.2.1  uebayasi 			txdesc->md_data = dmamap->dm_segs[seg].ds_addr;
    767  1.35.2.1  uebayasi 			txdesc->md_data_len = dmamap->dm_segs[seg].ds_len;
    768  1.35.2.1  uebayasi 			txdesc->md_stat_ctrl =
    769  1.35.2.1  uebayasi 			    (txdesc->md_stat_ctrl & MAL_TX_WRAP) |
    770       1.3    simonb 			    (nexttx == firsttx ? 0 : MAL_TX_READY) |
    771       1.3    simonb 			    EMAC_TXC_GFCS | EMAC_TXC_GPAD;
    772       1.3    simonb 			lasttx = nexttx;
    773       1.3    simonb 		}
    774       1.3    simonb 
    775       1.3    simonb 		/* Set the LAST bit on the last segment. */
    776       1.3    simonb 		sc->sc_txdescs[lasttx].md_stat_ctrl |= MAL_TX_LAST;
    777       1.3    simonb 
    778      1.21    simonb 		/*
    779      1.21    simonb 		 * Set up last segment descriptor to send an interrupt after
    780      1.21    simonb 		 * that descriptor is transmitted, and bypass existing Tx
    781      1.21    simonb 		 * descriptor reaping method (for now...).
    782      1.21    simonb 		 */
    783      1.21    simonb 		sc->sc_txdescs[lasttx].md_stat_ctrl |= MAL_TX_INTERRUPT;
    784      1.21    simonb 
    785      1.21    simonb 
    786       1.3    simonb 		txs->txs_lastdesc = lasttx;
    787       1.3    simonb 
    788       1.3    simonb 		/* Sync the descriptors we're using. */
    789       1.3    simonb 		EMAC_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
    790       1.3    simonb 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    791       1.3    simonb 
    792       1.3    simonb 		/*
    793       1.3    simonb 		 * The entire packet chain is set up.  Give the
    794       1.3    simonb 		 * first descriptor to the chip now.
    795       1.3    simonb 		 */
    796       1.3    simonb 		sc->sc_txdescs[firsttx].md_stat_ctrl |= MAL_TX_READY;
    797       1.3    simonb 		EMAC_CDTXSYNC(sc, firsttx, 1,
    798       1.3    simonb 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    799       1.3    simonb 		/*
    800       1.3    simonb 		 * Tell the EMAC that a new packet is available.
    801       1.3    simonb 		 */
    802  1.35.2.1  uebayasi 		EMAC_WRITE(sc, EMAC_TMR0, TMR0_GNP0 | TMR0_TFAE_2);
    803       1.3    simonb 
    804       1.3    simonb 		/* Advance the tx pointer. */
    805       1.3    simonb 		sc->sc_txfree -= txs->txs_ndesc;
    806       1.3    simonb 		sc->sc_txnext = nexttx;
    807       1.3    simonb 
    808       1.3    simonb 		sc->sc_txsfree--;
    809       1.3    simonb 		sc->sc_txsnext = EMAC_NEXTTXS(sc->sc_txsnext);
    810       1.3    simonb 
    811       1.3    simonb 		/*
    812       1.3    simonb 		 * Pass the packet to any BPF listeners.
    813       1.3    simonb 		 */
    814  1.35.2.1  uebayasi 		bpf_mtap(ifp, m0);
    815       1.3    simonb 	}
    816       1.3    simonb 
    817  1.35.2.1  uebayasi 	if (sc->sc_txfree == 0)
    818       1.3    simonb 		/* No more slots left; notify upper layer. */
    819       1.3    simonb 		ifp->if_flags |= IFF_OACTIVE;
    820       1.3    simonb 
    821  1.35.2.1  uebayasi 	if (sc->sc_txfree != ofree)
    822       1.3    simonb 		/* Set a watchdog timer in case the chip flakes out. */
    823       1.3    simonb 		ifp->if_timer = 5;
    824  1.35.2.1  uebayasi }
    825  1.35.2.1  uebayasi 
    826  1.35.2.1  uebayasi static int
    827  1.35.2.1  uebayasi emac_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    828  1.35.2.1  uebayasi {
    829  1.35.2.1  uebayasi 	struct emac_softc *sc = ifp->if_softc;
    830  1.35.2.1  uebayasi 	int s, error;
    831  1.35.2.1  uebayasi 
    832  1.35.2.1  uebayasi 	s = splnet();
    833  1.35.2.1  uebayasi 
    834  1.35.2.1  uebayasi 	switch (cmd) {
    835  1.35.2.1  uebayasi 	case SIOCSIFMTU:
    836  1.35.2.1  uebayasi 	{
    837  1.35.2.1  uebayasi 		struct ifreq *ifr = (struct ifreq *)data;
    838  1.35.2.1  uebayasi 		int maxmtu;
    839  1.35.2.1  uebayasi 
    840  1.35.2.1  uebayasi 		if (sc->sc_ethercom.ec_capabilities & ETHERCAP_JUMBO_MTU)
    841  1.35.2.1  uebayasi 			maxmtu = EMAC_MAX_MTU;
    842  1.35.2.1  uebayasi 		else
    843  1.35.2.1  uebayasi 			maxmtu = ETHERMTU;
    844  1.35.2.1  uebayasi 
    845  1.35.2.1  uebayasi 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
    846  1.35.2.1  uebayasi 			error = EINVAL;
    847  1.35.2.1  uebayasi 		else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
    848  1.35.2.1  uebayasi 			break;
    849  1.35.2.1  uebayasi 		else if (ifp->if_flags & IFF_UP)
    850  1.35.2.1  uebayasi 			error = emac_init(ifp);
    851  1.35.2.1  uebayasi 		else
    852  1.35.2.1  uebayasi 			error = 0;
    853  1.35.2.1  uebayasi 		break;
    854  1.35.2.1  uebayasi 	}
    855  1.35.2.1  uebayasi 
    856  1.35.2.1  uebayasi 	default:
    857  1.35.2.1  uebayasi 		error = ether_ioctl(ifp, cmd, data);
    858  1.35.2.1  uebayasi 		if (error == ENETRESET) {
    859  1.35.2.1  uebayasi 			/*
    860  1.35.2.1  uebayasi 			 * Multicast list has changed; set the hardware filter
    861  1.35.2.1  uebayasi 			 * accordingly.
    862  1.35.2.1  uebayasi 			 */
    863  1.35.2.1  uebayasi 			if (ifp->if_flags & IFF_RUNNING)
    864  1.35.2.1  uebayasi 				error = emac_set_filter(sc);
    865  1.35.2.1  uebayasi 			else
    866  1.35.2.1  uebayasi 				error = 0;
    867  1.35.2.1  uebayasi 		}
    868       1.3    simonb 	}
    869  1.35.2.1  uebayasi 
    870  1.35.2.1  uebayasi 	/* try to get more packets going */
    871  1.35.2.1  uebayasi 	emac_start(ifp);
    872  1.35.2.1  uebayasi 
    873  1.35.2.1  uebayasi 	splx(s);
    874  1.35.2.1  uebayasi 	return error;
    875       1.3    simonb }
    876       1.3    simonb 
    877       1.3    simonb static int
    878       1.3    simonb emac_init(struct ifnet *ifp)
    879       1.3    simonb {
    880       1.3    simonb 	struct emac_softc *sc = ifp->if_softc;
    881       1.3    simonb 	struct emac_rxsoft *rxs;
    882      1.29    dyoung 	const uint8_t *enaddr = CLLADDR(ifp->if_sadl);
    883       1.3    simonb 	int error, i;
    884       1.3    simonb 
    885       1.3    simonb 	error = 0;
    886       1.3    simonb 
    887       1.3    simonb 	/* Cancel any pending I/O. */
    888       1.3    simonb 	emac_stop(ifp, 0);
    889       1.3    simonb 
    890       1.3    simonb 	/* Reset the chip to a known state. */
    891  1.35.2.1  uebayasi 	emac_soft_reset(sc);
    892       1.3    simonb 
    893  1.35.2.1  uebayasi 	/*
    894       1.3    simonb 	 * Initialise the transmit descriptor ring.
    895       1.3    simonb 	 */
    896       1.3    simonb 	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
    897       1.3    simonb 	/* set wrap on last descriptor */
    898       1.3    simonb 	sc->sc_txdescs[EMAC_NTXDESC - 1].md_stat_ctrl |= MAL_TX_WRAP;
    899       1.3    simonb 	EMAC_CDTXSYNC(sc, 0, EMAC_NTXDESC,
    900  1.35.2.1  uebayasi 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    901       1.3    simonb 	sc->sc_txfree = EMAC_NTXDESC;
    902       1.3    simonb 	sc->sc_txnext = 0;
    903       1.3    simonb 
    904       1.3    simonb 	/*
    905       1.3    simonb 	 * Initialise the transmit job descriptors.
    906       1.3    simonb 	 */
    907       1.3    simonb 	for (i = 0; i < EMAC_TXQUEUELEN; i++)
    908       1.3    simonb 		sc->sc_txsoft[i].txs_mbuf = NULL;
    909       1.3    simonb 	sc->sc_txsfree = EMAC_TXQUEUELEN;
    910       1.3    simonb 	sc->sc_txsnext = 0;
    911       1.3    simonb 	sc->sc_txsdirty = 0;
    912       1.3    simonb 
    913       1.3    simonb 	/*
    914       1.3    simonb 	 * Initialise the receiver descriptor and receive job
    915       1.3    simonb 	 * descriptor rings.
    916       1.3    simonb 	 */
    917       1.3    simonb 	for (i = 0; i < EMAC_NRXDESC; i++) {
    918       1.3    simonb 		rxs = &sc->sc_rxsoft[i];
    919       1.3    simonb 		if (rxs->rxs_mbuf == NULL) {
    920       1.3    simonb 			if ((error = emac_add_rxbuf(sc, i)) != 0) {
    921  1.35.2.1  uebayasi 				aprint_error_ifnet(ifp,
    922  1.35.2.1  uebayasi 				    "unable to allocate or map rx buffer %d,"
    923  1.35.2.1  uebayasi 				    " error = %d\n",
    924  1.35.2.1  uebayasi 				    i, error);
    925       1.3    simonb 				/*
    926       1.3    simonb 				 * XXX Should attempt to run with fewer receive
    927       1.3    simonb 				 * XXX buffers instead of just failing.
    928       1.3    simonb 				 */
    929       1.3    simonb 				emac_rxdrain(sc);
    930       1.3    simonb 				goto out;
    931       1.3    simonb 			}
    932       1.3    simonb 		} else
    933       1.3    simonb 			EMAC_INIT_RXDESC(sc, i);
    934       1.3    simonb 	}
    935       1.3    simonb 	sc->sc_rxptr = 0;
    936       1.3    simonb 
    937       1.3    simonb 	/*
    938       1.3    simonb 	 * Set the current media.
    939       1.3    simonb 	 */
    940      1.31    dyoung 	if ((error = ether_mediachange(ifp)) != 0)
    941      1.31    dyoung 		goto out;
    942       1.3    simonb 
    943       1.3    simonb 	/*
    944       1.3    simonb 	 * Load the MAC address.
    945       1.3    simonb 	 */
    946       1.6    simonb 	EMAC_WRITE(sc, EMAC_IAHR, enaddr[0] << 8 | enaddr[1]);
    947       1.6    simonb 	EMAC_WRITE(sc, EMAC_IALR,
    948       1.3    simonb 	    enaddr[2] << 24 | enaddr[3] << 16 | enaddr[4] << 8 | enaddr[5]);
    949       1.3    simonb 
    950  1.35.2.1  uebayasi 	/* Enable the transmit and receive channel on the MAL. */
    951  1.35.2.1  uebayasi 	error = mal_start(sc->sc_instance,
    952  1.35.2.1  uebayasi 	    EMAC_CDTXADDR(sc, 0), EMAC_CDRXADDR(sc, 0));
    953  1.35.2.1  uebayasi 	if (error)
    954  1.35.2.1  uebayasi 		goto out;
    955  1.35.2.1  uebayasi 
    956  1.35.2.1  uebayasi 	sc->sc_mr1 &= ~MR1_JPSM;
    957  1.35.2.1  uebayasi 	if (ifp->if_mtu > ETHERMTU)
    958  1.35.2.1  uebayasi 		/* Enable Jumbo Packet Support Mode */
    959  1.35.2.1  uebayasi 		sc->sc_mr1 |= MR1_JPSM;
    960       1.3    simonb 
    961       1.3    simonb 	/* Set fifos, media modes. */
    962       1.6    simonb 	EMAC_WRITE(sc, EMAC_MR1, sc->sc_mr1);
    963       1.3    simonb 
    964       1.3    simonb 	/*
    965       1.3    simonb 	 * Enable Individual and (possibly) Broadcast Address modes,
    966       1.3    simonb 	 * runt packets, and strip padding.
    967       1.3    simonb 	 */
    968  1.35.2.1  uebayasi 	EMAC_WRITE(sc, EMAC_RMR, RMR_IAE | RMR_RRP | RMR_SP | RMR_TFAE_2 |
    969      1.18    simonb 	    (ifp->if_flags & IFF_PROMISC ? RMR_PME : 0) |
    970       1.3    simonb 	    (ifp->if_flags & IFF_BROADCAST ? RMR_BAE : 0));
    971       1.3    simonb 
    972       1.3    simonb 	/*
    973      1.27  kiyohara 	 * Set multicast filter.
    974      1.27  kiyohara 	 */
    975      1.27  kiyohara 	emac_set_filter(sc);
    976      1.27  kiyohara 
    977      1.27  kiyohara 	/*
    978       1.3    simonb 	 * Set low- and urgent-priority request thresholds.
    979       1.3    simonb 	 */
    980       1.6    simonb 	EMAC_WRITE(sc, EMAC_TMR1,
    981       1.3    simonb 	    ((7 << TMR1_TLR_SHIFT) & TMR1_TLR_MASK) | /* 16 word burst */
    982       1.3    simonb 	    ((15 << TMR1_TUR_SHIFT) & TMR1_TUR_MASK));
    983       1.3    simonb 	/*
    984       1.3    simonb 	 * Set Transmit Request Threshold Register.
    985       1.3    simonb 	 */
    986       1.6    simonb 	EMAC_WRITE(sc, EMAC_TRTR, TRTR_256);
    987       1.3    simonb 
    988       1.3    simonb 	/*
    989       1.3    simonb 	 * Set high and low receive watermarks.
    990       1.3    simonb 	 */
    991       1.6    simonb 	EMAC_WRITE(sc, EMAC_RWMR,
    992       1.3    simonb 	    30 << RWMR_RLWM_SHIFT | 64 << RWMR_RLWM_SHIFT);
    993       1.3    simonb 
    994       1.3    simonb 	/*
    995       1.3    simonb 	 * Set frame gap.
    996       1.3    simonb 	 */
    997       1.6    simonb 	EMAC_WRITE(sc, EMAC_IPGVR, 8);
    998       1.3    simonb 
    999       1.3    simonb 	/*
   1000  1.35.2.1  uebayasi 	 * Set interrupt status enable bits for EMAC.
   1001       1.3    simonb 	 */
   1002       1.6    simonb 	EMAC_WRITE(sc, EMAC_ISER,
   1003  1.35.2.1  uebayasi 	    ISR_TXPE |		/* TX Parity Error */
   1004  1.35.2.1  uebayasi 	    ISR_RXPE |		/* RX Parity Error */
   1005  1.35.2.1  uebayasi 	    ISR_TXUE |		/* TX Underrun Event */
   1006  1.35.2.1  uebayasi 	    ISR_RXOE |		/* RX Overrun Event */
   1007  1.35.2.1  uebayasi 	    ISR_OVR  |		/* Overrun Error */
   1008  1.35.2.1  uebayasi 	    ISR_PP   |		/* Pause Packet */
   1009  1.35.2.1  uebayasi 	    ISR_BP   |		/* Bad Packet */
   1010  1.35.2.1  uebayasi 	    ISR_RP   |		/* Runt Packet */
   1011  1.35.2.1  uebayasi 	    ISR_SE   |		/* Short Event */
   1012  1.35.2.1  uebayasi 	    ISR_ALE  |		/* Alignment Error */
   1013  1.35.2.1  uebayasi 	    ISR_BFCS |		/* Bad FCS */
   1014  1.35.2.1  uebayasi 	    ISR_PTLE |		/* Packet Too Long Error */
   1015  1.35.2.1  uebayasi 	    ISR_ORE  |		/* Out of Range Error */
   1016  1.35.2.1  uebayasi 	    ISR_IRE  |		/* In Range Error */
   1017  1.35.2.1  uebayasi 	    ISR_SE0  |		/* Signal Quality Error 0 (SQE) */
   1018  1.35.2.1  uebayasi 	    ISR_TE0  |		/* Transmit Error 0 */
   1019  1.35.2.1  uebayasi 	    ISR_MOS  |		/* MMA Operation Succeeded */
   1020  1.35.2.1  uebayasi 	    ISR_MOF);		/* MMA Operation Failed */
   1021       1.3    simonb 
   1022       1.3    simonb 	/*
   1023       1.3    simonb 	 * Enable the transmit and receive channel on the EMAC.
   1024       1.3    simonb 	 */
   1025       1.6    simonb 	EMAC_WRITE(sc, EMAC_MR0, MR0_TXE | MR0_RXE);
   1026       1.3    simonb 
   1027       1.3    simonb 	/*
   1028       1.3    simonb 	 * Start the one second MII clock.
   1029       1.3    simonb 	 */
   1030       1.3    simonb 	callout_reset(&sc->sc_callout, hz, emac_mii_tick, sc);
   1031       1.3    simonb 
   1032       1.3    simonb 	/*
   1033       1.3    simonb 	 * ... all done!
   1034       1.3    simonb 	 */
   1035       1.3    simonb 	ifp->if_flags |= IFF_RUNNING;
   1036       1.3    simonb 	ifp->if_flags &= ~IFF_OACTIVE;
   1037       1.3    simonb 
   1038       1.3    simonb  out:
   1039       1.3    simonb 	if (error) {
   1040       1.3    simonb 		ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1041       1.3    simonb 		ifp->if_timer = 0;
   1042  1.35.2.1  uebayasi 		aprint_error_ifnet(ifp, "interface not running\n");
   1043       1.3    simonb 	}
   1044  1.35.2.1  uebayasi 	return error;
   1045       1.3    simonb }
   1046       1.3    simonb 
   1047       1.3    simonb static void
   1048       1.3    simonb emac_stop(struct ifnet *ifp, int disable)
   1049       1.3    simonb {
   1050       1.3    simonb 	struct emac_softc *sc = ifp->if_softc;
   1051       1.3    simonb 	struct emac_txsoft *txs;
   1052       1.3    simonb 	int i;
   1053       1.3    simonb 
   1054       1.3    simonb 	/* Stop the one second clock. */
   1055       1.3    simonb 	callout_stop(&sc->sc_callout);
   1056       1.3    simonb 
   1057       1.3    simonb 	/* Down the MII */
   1058       1.3    simonb 	mii_down(&sc->sc_mii);
   1059       1.3    simonb 
   1060       1.3    simonb 	/* Disable interrupts. */
   1061       1.6    simonb 	EMAC_WRITE(sc, EMAC_ISER, 0);
   1062       1.3    simonb 
   1063       1.3    simonb 	/* Disable the receive and transmit channels. */
   1064  1.35.2.1  uebayasi 	mal_stop(sc->sc_instance);
   1065       1.3    simonb 
   1066       1.3    simonb 	/* Disable the transmit enable and receive MACs. */
   1067       1.6    simonb 	EMAC_WRITE(sc, EMAC_MR0,
   1068       1.6    simonb 	    EMAC_READ(sc, EMAC_MR0) & ~(MR0_TXE | MR0_RXE));
   1069       1.3    simonb 
   1070       1.3    simonb 	/* Release any queued transmit buffers. */
   1071       1.3    simonb 	for (i = 0; i < EMAC_TXQUEUELEN; i++) {
   1072       1.3    simonb 		txs = &sc->sc_txsoft[i];
   1073       1.3    simonb 		if (txs->txs_mbuf != NULL) {
   1074       1.3    simonb 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1075       1.3    simonb 			m_freem(txs->txs_mbuf);
   1076       1.3    simonb 			txs->txs_mbuf = NULL;
   1077       1.3    simonb 		}
   1078       1.3    simonb 	}
   1079       1.3    simonb 
   1080       1.3    simonb 	if (disable)
   1081       1.3    simonb 		emac_rxdrain(sc);
   1082       1.3    simonb 
   1083       1.3    simonb 	/*
   1084       1.3    simonb 	 * Mark the interface down and cancel the watchdog timer.
   1085       1.3    simonb 	 */
   1086       1.3    simonb 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1087       1.3    simonb 	ifp->if_timer = 0;
   1088       1.3    simonb }
   1089       1.3    simonb 
   1090  1.35.2.1  uebayasi static void
   1091  1.35.2.1  uebayasi emac_watchdog(struct ifnet *ifp)
   1092       1.3    simonb {
   1093       1.3    simonb 	struct emac_softc *sc = ifp->if_softc;
   1094       1.3    simonb 
   1095  1.35.2.1  uebayasi 	/*
   1096  1.35.2.1  uebayasi 	 * Since we're not interrupting every packet, sweep
   1097  1.35.2.1  uebayasi 	 * up before we report an error.
   1098  1.35.2.1  uebayasi 	 */
   1099  1.35.2.1  uebayasi 	emac_txreap(sc);
   1100       1.3    simonb 
   1101  1.35.2.1  uebayasi 	if (sc->sc_txfree != EMAC_NTXDESC) {
   1102  1.35.2.1  uebayasi 		aprint_error_ifnet(ifp,
   1103  1.35.2.1  uebayasi 		    "device timeout (txfree %d txsfree %d txnext %d)\n",
   1104  1.35.2.1  uebayasi 		    sc->sc_txfree, sc->sc_txsfree, sc->sc_txnext);
   1105  1.35.2.1  uebayasi 		ifp->if_oerrors++;
   1106  1.35.2.1  uebayasi 
   1107  1.35.2.1  uebayasi 		/* Reset the interface. */
   1108  1.35.2.1  uebayasi 		(void)emac_init(ifp);
   1109  1.35.2.1  uebayasi 	} else if (ifp->if_flags & IFF_DEBUG)
   1110  1.35.2.1  uebayasi 		aprint_error_ifnet(ifp, "recovered from device timeout\n");
   1111       1.3    simonb 
   1112       1.3    simonb 	/* try to get more packets going */
   1113       1.3    simonb 	emac_start(ifp);
   1114       1.3    simonb }
   1115       1.3    simonb 
   1116  1.35.2.1  uebayasi static int
   1117  1.35.2.1  uebayasi emac_add_rxbuf(struct emac_softc *sc, int idx)
   1118       1.3    simonb {
   1119  1.35.2.1  uebayasi 	struct emac_rxsoft *rxs = &sc->sc_rxsoft[idx];
   1120  1.35.2.1  uebayasi 	struct mbuf *m;
   1121  1.35.2.1  uebayasi 	int error;
   1122       1.3    simonb 
   1123  1.35.2.1  uebayasi 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1124  1.35.2.1  uebayasi 	if (m == NULL)
   1125  1.35.2.1  uebayasi 		return ENOBUFS;
   1126       1.3    simonb 
   1127  1.35.2.1  uebayasi 	MCLGET(m, M_DONTWAIT);
   1128  1.35.2.1  uebayasi 	if ((m->m_flags & M_EXT) == 0) {
   1129  1.35.2.1  uebayasi 		m_freem(m);
   1130  1.35.2.1  uebayasi 		return ENOBUFS;
   1131  1.35.2.1  uebayasi 	}
   1132       1.3    simonb 
   1133  1.35.2.1  uebayasi 	if (rxs->rxs_mbuf != NULL)
   1134  1.35.2.1  uebayasi 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1135  1.35.2.1  uebayasi 
   1136  1.35.2.1  uebayasi 	rxs->rxs_mbuf = m;
   1137  1.35.2.1  uebayasi 
   1138  1.35.2.1  uebayasi 	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
   1139  1.35.2.1  uebayasi 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
   1140  1.35.2.1  uebayasi 	if (error) {
   1141  1.35.2.1  uebayasi 		aprint_error_dev(sc->sc_dev,
   1142  1.35.2.1  uebayasi 		    "can't load rx DMA map %d, error = %d\n", idx, error);
   1143  1.35.2.1  uebayasi 		panic("emac_add_rxbuf");		/* XXX */
   1144  1.35.2.1  uebayasi 	}
   1145  1.35.2.1  uebayasi 
   1146  1.35.2.1  uebayasi 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1147  1.35.2.1  uebayasi 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1148       1.3    simonb 
   1149  1.35.2.1  uebayasi 	EMAC_INIT_RXDESC(sc, idx);
   1150       1.3    simonb 
   1151  1.35.2.1  uebayasi 	return 0;
   1152       1.3    simonb }
   1153       1.3    simonb 
   1154  1.35.2.1  uebayasi static void
   1155  1.35.2.1  uebayasi emac_rxdrain(struct emac_softc *sc)
   1156  1.35.2.1  uebayasi {
   1157  1.35.2.1  uebayasi 	struct emac_rxsoft *rxs;
   1158  1.35.2.1  uebayasi 	int i;
   1159  1.35.2.1  uebayasi 
   1160  1.35.2.1  uebayasi 	for (i = 0; i < EMAC_NRXDESC; i++) {
   1161  1.35.2.1  uebayasi 		rxs = &sc->sc_rxsoft[i];
   1162  1.35.2.1  uebayasi 		if (rxs->rxs_mbuf != NULL) {
   1163  1.35.2.1  uebayasi 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1164  1.35.2.1  uebayasi 			m_freem(rxs->rxs_mbuf);
   1165  1.35.2.1  uebayasi 			rxs->rxs_mbuf = NULL;
   1166  1.35.2.1  uebayasi 		}
   1167  1.35.2.1  uebayasi 	}
   1168  1.35.2.1  uebayasi }
   1169  1.35.2.1  uebayasi 
   1170  1.35.2.1  uebayasi static int
   1171      1.18    simonb emac_set_filter(struct emac_softc *sc)
   1172      1.18    simonb {
   1173      1.18    simonb 	struct ether_multistep step;
   1174      1.18    simonb 	struct ether_multi *enm;
   1175      1.18    simonb 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1176  1.35.2.1  uebayasi 	uint32_t rmr, crc, mask, tmp, reg, gaht[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
   1177  1.35.2.1  uebayasi 	int regs, cnt = 0, i;
   1178  1.35.2.1  uebayasi 
   1179  1.35.2.1  uebayasi 	if (sc->sc_htsize == 256) {
   1180  1.35.2.1  uebayasi 		reg = EMAC_GAHT256(0);
   1181  1.35.2.1  uebayasi 		regs = 8;
   1182  1.35.2.1  uebayasi 	} else {
   1183  1.35.2.1  uebayasi 		reg = EMAC_GAHT64(0);
   1184  1.35.2.1  uebayasi 		regs = 4;
   1185  1.35.2.1  uebayasi 	}
   1186  1.35.2.1  uebayasi 	mask = (1ULL << (sc->sc_htsize / regs)) - 1;
   1187      1.18    simonb 
   1188      1.18    simonb 	rmr = EMAC_READ(sc, EMAC_RMR);
   1189      1.18    simonb 	rmr &= ~(RMR_PMME | RMR_MAE);
   1190      1.18    simonb 	ifp->if_flags &= ~IFF_ALLMULTI;
   1191      1.18    simonb 
   1192      1.18    simonb 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
   1193      1.18    simonb 	while (enm != NULL) {
   1194      1.18    simonb 		if (memcmp(enm->enm_addrlo,
   1195      1.18    simonb 		    enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
   1196      1.18    simonb 			/*
   1197      1.18    simonb 			 * We must listen to a range of multicast addresses.
   1198      1.18    simonb 			 * For now, just accept all multicasts, rather than
   1199      1.18    simonb 			 * trying to set only those filter bits needed to match
   1200      1.18    simonb 			 * the range.  (At this time, the only use of address
   1201      1.18    simonb 			 * ranges is for IP multicast routing, for which the
   1202      1.18    simonb 			 * range is big enough to require all bits set.)
   1203      1.18    simonb 			 */
   1204  1.35.2.1  uebayasi 			gaht[0] = gaht[1] = gaht[2] = gaht[3] =
   1205  1.35.2.1  uebayasi 			    gaht[4] = gaht[5] = gaht[6] = gaht[7] = mask;
   1206      1.18    simonb 			break;
   1207      1.18    simonb 		}
   1208      1.18    simonb 
   1209      1.18    simonb 		crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
   1210      1.18    simonb 
   1211  1.35.2.1  uebayasi 		if (sc->sc_htsize == 256)
   1212  1.35.2.1  uebayasi 			EMAC_SET_FILTER256(gaht, crc);
   1213  1.35.2.1  uebayasi 		else
   1214  1.35.2.1  uebayasi 			EMAC_SET_FILTER(gaht, crc);
   1215      1.18    simonb 
   1216      1.18    simonb 		ETHER_NEXT_MULTI(step, enm);
   1217      1.18    simonb 		cnt++;
   1218      1.18    simonb 	}
   1219      1.18    simonb 
   1220  1.35.2.1  uebayasi 	for (i = 1, tmp = gaht[0]; i < regs; i++)
   1221  1.35.2.1  uebayasi 		tmp &= gaht[i];
   1222  1.35.2.1  uebayasi 	if (tmp == mask) {
   1223      1.18    simonb 		/* All categories are true. */
   1224      1.18    simonb 		ifp->if_flags |= IFF_ALLMULTI;
   1225      1.18    simonb 		rmr |= RMR_PMME;
   1226      1.18    simonb 	} else if (cnt != 0) {
   1227      1.18    simonb 		/* Some categories are true. */
   1228  1.35.2.1  uebayasi 		for (i = 0; i < regs; i++)
   1229  1.35.2.1  uebayasi 			EMAC_WRITE(sc, reg + (i << 2), gaht[i]);
   1230      1.18    simonb 		rmr |= RMR_MAE;
   1231      1.18    simonb 	}
   1232      1.18    simonb 	EMAC_WRITE(sc, EMAC_RMR, rmr);
   1233      1.18    simonb 
   1234      1.18    simonb 	return 0;
   1235      1.18    simonb }
   1236      1.18    simonb 
   1237       1.3    simonb /*
   1238       1.3    simonb  * Reap completed Tx descriptors.
   1239       1.3    simonb  */
   1240       1.3    simonb static int
   1241       1.3    simonb emac_txreap(struct emac_softc *sc)
   1242       1.3    simonb {
   1243       1.3    simonb 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1244       1.3    simonb 	struct emac_txsoft *txs;
   1245      1.20    simonb 	int handled, i;
   1246  1.35.2.1  uebayasi 	uint32_t txstat;
   1247       1.3    simonb 
   1248       1.3    simonb 	EMAC_EVCNT_INCR(&sc->sc_ev_txreap);
   1249      1.20    simonb 	handled = 0;
   1250       1.3    simonb 
   1251       1.3    simonb 	ifp->if_flags &= ~IFF_OACTIVE;
   1252       1.3    simonb 
   1253       1.3    simonb 	/*
   1254       1.3    simonb 	 * Go through our Tx list and free mbufs for those
   1255       1.3    simonb 	 * frames that have been transmitted.
   1256       1.3    simonb 	 */
   1257       1.3    simonb 	for (i = sc->sc_txsdirty; sc->sc_txsfree != EMAC_TXQUEUELEN;
   1258       1.3    simonb 	    i = EMAC_NEXTTXS(i), sc->sc_txsfree++) {
   1259       1.3    simonb 		txs = &sc->sc_txsoft[i];
   1260       1.3    simonb 
   1261       1.3    simonb 		EMAC_CDTXSYNC(sc, txs->txs_lastdesc,
   1262       1.3    simonb 		    txs->txs_dmamap->dm_nsegs,
   1263       1.3    simonb 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1264       1.3    simonb 
   1265       1.3    simonb 		txstat = sc->sc_txdescs[txs->txs_lastdesc].md_stat_ctrl;
   1266       1.3    simonb 		if (txstat & MAL_TX_READY)
   1267       1.3    simonb 			break;
   1268       1.3    simonb 
   1269      1.20    simonb 		handled = 1;
   1270      1.20    simonb 
   1271       1.3    simonb 		/*
   1272       1.3    simonb 		 * Check for errors and collisions.
   1273       1.3    simonb 		 */
   1274       1.3    simonb 		if (txstat & (EMAC_TXS_UR | EMAC_TXS_ED))
   1275       1.3    simonb 			ifp->if_oerrors++;
   1276       1.3    simonb 
   1277       1.3    simonb #ifdef EMAC_EVENT_COUNTERS
   1278       1.3    simonb 		if (txstat & EMAC_TXS_UR)
   1279       1.3    simonb 			EMAC_EVCNT_INCR(&sc->sc_ev_tu);
   1280       1.3    simonb #endif /* EMAC_EVENT_COUNTERS */
   1281       1.3    simonb 
   1282  1.35.2.1  uebayasi 		if (txstat &
   1283  1.35.2.1  uebayasi 		    (EMAC_TXS_EC | EMAC_TXS_MC | EMAC_TXS_SC | EMAC_TXS_LC)) {
   1284       1.3    simonb 			if (txstat & EMAC_TXS_EC)
   1285       1.3    simonb 				ifp->if_collisions += 16;
   1286       1.3    simonb 			else if (txstat & EMAC_TXS_MC)
   1287       1.3    simonb 				ifp->if_collisions += 2;	/* XXX? */
   1288       1.3    simonb 			else if (txstat & EMAC_TXS_SC)
   1289       1.3    simonb 				ifp->if_collisions++;
   1290       1.3    simonb 			if (txstat & EMAC_TXS_LC)
   1291       1.3    simonb 				ifp->if_collisions++;
   1292       1.3    simonb 		} else
   1293       1.3    simonb 			ifp->if_opackets++;
   1294       1.3    simonb 
   1295       1.3    simonb 		if (ifp->if_flags & IFF_DEBUG) {
   1296       1.3    simonb 			if (txstat & EMAC_TXS_ED)
   1297  1.35.2.1  uebayasi 				aprint_error_ifnet(ifp, "excessive deferral\n");
   1298       1.3    simonb 			if (txstat & EMAC_TXS_EC)
   1299  1.35.2.1  uebayasi 				aprint_error_ifnet(ifp,
   1300  1.35.2.1  uebayasi 				    "excessive collisions\n");
   1301       1.3    simonb 		}
   1302       1.3    simonb 
   1303       1.3    simonb 		sc->sc_txfree += txs->txs_ndesc;
   1304       1.3    simonb 		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
   1305       1.3    simonb 		    0, txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1306       1.3    simonb 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1307       1.3    simonb 		m_freem(txs->txs_mbuf);
   1308       1.3    simonb 		txs->txs_mbuf = NULL;
   1309       1.3    simonb 	}
   1310       1.3    simonb 
   1311       1.3    simonb 	/* Update the dirty transmit buffer pointer. */
   1312       1.3    simonb 	sc->sc_txsdirty = i;
   1313       1.3    simonb 
   1314       1.3    simonb 	/*
   1315       1.3    simonb 	 * If there are no more pending transmissions, cancel the watchdog
   1316       1.3    simonb 	 * timer.
   1317       1.3    simonb 	 */
   1318       1.3    simonb 	if (sc->sc_txsfree == EMAC_TXQUEUELEN)
   1319       1.3    simonb 		ifp->if_timer = 0;
   1320       1.3    simonb 
   1321  1.35.2.1  uebayasi 	return handled;
   1322       1.3    simonb }
   1323       1.3    simonb 
   1324  1.35.2.1  uebayasi 
   1325       1.3    simonb /*
   1326  1.35.2.1  uebayasi  * Reset functions
   1327       1.3    simonb  */
   1328  1.35.2.1  uebayasi 
   1329  1.35.2.1  uebayasi static void
   1330  1.35.2.1  uebayasi emac_soft_reset(struct emac_softc *sc)
   1331  1.35.2.1  uebayasi {
   1332  1.35.2.1  uebayasi 	uint32_t sdr;
   1333  1.35.2.1  uebayasi 	int t = 0;
   1334  1.35.2.1  uebayasi 
   1335  1.35.2.1  uebayasi 	/*
   1336  1.35.2.1  uebayasi 	 * The PHY must provide a TX Clk in order perform a soft reset the
   1337  1.35.2.1  uebayasi 	 * EMAC.  If none is present, select the internal clock,
   1338  1.35.2.1  uebayasi 	 * SDR0_MFR[E0CS,E1CS].  After the soft reset, select the external
   1339  1.35.2.1  uebayasi 	 * clock.
   1340  1.35.2.1  uebayasi 	 */
   1341  1.35.2.1  uebayasi 
   1342  1.35.2.1  uebayasi 	sdr = mfsdr(DCR_SDR0_MFR);
   1343  1.35.2.1  uebayasi 	sdr |= SDR0_MFR_ECS(sc->sc_instance);
   1344  1.35.2.1  uebayasi 	mtsdr(DCR_SDR0_MFR, sdr);
   1345  1.35.2.1  uebayasi 
   1346  1.35.2.1  uebayasi 	EMAC_WRITE(sc, EMAC_MR0, MR0_SRST);
   1347  1.35.2.1  uebayasi 
   1348  1.35.2.1  uebayasi 	sdr = mfsdr(DCR_SDR0_MFR);
   1349  1.35.2.1  uebayasi 	sdr &= ~SDR0_MFR_ECS(sc->sc_instance);
   1350  1.35.2.1  uebayasi 	mtsdr(DCR_SDR0_MFR, sdr);
   1351  1.35.2.1  uebayasi 
   1352  1.35.2.1  uebayasi 	delay(5);
   1353  1.35.2.1  uebayasi 
   1354  1.35.2.1  uebayasi 	/* wait finish */
   1355  1.35.2.1  uebayasi 	while (EMAC_READ(sc, EMAC_MR0) & MR0_SRST) {
   1356  1.35.2.1  uebayasi 		if (++t == 1000000 /* 1sec XXXXX */) {
   1357  1.35.2.1  uebayasi 			aprint_error_dev(sc->sc_dev, "Soft Reset failed\n");
   1358  1.35.2.1  uebayasi 			return;
   1359  1.35.2.1  uebayasi 		}
   1360  1.35.2.1  uebayasi 		delay(1);
   1361  1.35.2.1  uebayasi 	}
   1362  1.35.2.1  uebayasi }
   1363  1.35.2.1  uebayasi 
   1364  1.35.2.1  uebayasi static void
   1365  1.35.2.1  uebayasi emac_smart_reset(struct emac_softc *sc)
   1366  1.35.2.1  uebayasi {
   1367  1.35.2.1  uebayasi 	uint32_t mr0;
   1368  1.35.2.1  uebayasi 	int t = 0;
   1369  1.35.2.1  uebayasi 
   1370  1.35.2.1  uebayasi 	mr0 = EMAC_READ(sc, EMAC_MR0);
   1371  1.35.2.1  uebayasi 	if (mr0 & (MR0_TXE | MR0_RXE)) {
   1372  1.35.2.1  uebayasi 		mr0 &= ~(MR0_TXE | MR0_RXE);
   1373  1.35.2.1  uebayasi 		EMAC_WRITE(sc, EMAC_MR0, mr0);
   1374  1.35.2.1  uebayasi 
   1375  1.35.2.1  uebayasi 		/* wait idel state */
   1376  1.35.2.1  uebayasi 		while ((EMAC_READ(sc, EMAC_MR0) & (MR0_TXI | MR0_RXI)) !=
   1377  1.35.2.1  uebayasi 		    (MR0_TXI | MR0_RXI)) {
   1378  1.35.2.1  uebayasi 			if (++t == 1000000 /* 1sec XXXXX */) {
   1379  1.35.2.1  uebayasi 				aprint_error_dev(sc->sc_dev,
   1380  1.35.2.1  uebayasi 				    "Smart Reset failed\n");
   1381  1.35.2.1  uebayasi 				return;
   1382  1.35.2.1  uebayasi 			}
   1383  1.35.2.1  uebayasi 			delay(1);
   1384  1.35.2.1  uebayasi 		}
   1385  1.35.2.1  uebayasi 	}
   1386  1.35.2.1  uebayasi }
   1387  1.35.2.1  uebayasi 
   1388  1.35.2.1  uebayasi 
   1389  1.35.2.1  uebayasi /*
   1390  1.35.2.1  uebayasi  * MII related functions
   1391  1.35.2.1  uebayasi  */
   1392  1.35.2.1  uebayasi 
   1393       1.3    simonb static int
   1394  1.35.2.1  uebayasi emac_mii_readreg(device_t self, int phy, int reg)
   1395  1.35.2.1  uebayasi {
   1396  1.35.2.1  uebayasi 	struct emac_softc *sc = device_private(self);
   1397  1.35.2.1  uebayasi 	uint32_t sta_reg;
   1398  1.35.2.1  uebayasi 
   1399  1.35.2.1  uebayasi 	if (sc->sc_rmii_enable)
   1400  1.35.2.1  uebayasi 		sc->sc_rmii_enable(device_parent(self), sc->sc_instance);
   1401  1.35.2.1  uebayasi 
   1402  1.35.2.1  uebayasi 	/* wait for PHY data transfer to complete */
   1403  1.35.2.1  uebayasi 	if (emac_mii_wait(sc))
   1404  1.35.2.1  uebayasi 		goto fail;
   1405  1.35.2.1  uebayasi 
   1406  1.35.2.1  uebayasi 	sta_reg =
   1407  1.35.2.1  uebayasi 	    sc->sc_stacr_read		|
   1408  1.35.2.1  uebayasi 	    (reg << STACR_PRA_SHIFT)	|
   1409  1.35.2.1  uebayasi 	    (phy << STACR_PCDA_SHIFT)	|
   1410  1.35.2.1  uebayasi 	    sc->sc_stacr_bits;
   1411  1.35.2.1  uebayasi 	EMAC_WRITE(sc, EMAC_STACR, sta_reg);
   1412  1.35.2.1  uebayasi 
   1413  1.35.2.1  uebayasi 	if (emac_mii_wait(sc))
   1414  1.35.2.1  uebayasi 		goto fail;
   1415  1.35.2.1  uebayasi 	sta_reg = EMAC_READ(sc, EMAC_STACR);
   1416  1.35.2.1  uebayasi 
   1417  1.35.2.1  uebayasi 	if (sc->sc_rmii_disable)
   1418  1.35.2.1  uebayasi 		sc->sc_rmii_disable(device_parent(self), sc->sc_instance);
   1419  1.35.2.1  uebayasi 
   1420  1.35.2.1  uebayasi 	if (sta_reg & STACR_PHYE)
   1421  1.35.2.1  uebayasi 		return 0;
   1422  1.35.2.1  uebayasi 	return sta_reg >> STACR_PHYD_SHIFT;
   1423  1.35.2.1  uebayasi 
   1424  1.35.2.1  uebayasi fail:
   1425  1.35.2.1  uebayasi 	if (sc->sc_rmii_disable)
   1426  1.35.2.1  uebayasi 		sc->sc_rmii_disable(device_parent(self), sc->sc_instance);
   1427  1.35.2.1  uebayasi 	return 0;
   1428  1.35.2.1  uebayasi }
   1429  1.35.2.1  uebayasi 
   1430  1.35.2.1  uebayasi static void
   1431  1.35.2.1  uebayasi emac_mii_writereg(device_t self, int phy, int reg, int val)
   1432  1.35.2.1  uebayasi {
   1433  1.35.2.1  uebayasi 	struct emac_softc *sc = device_private(self);
   1434  1.35.2.1  uebayasi 	uint32_t sta_reg;
   1435  1.35.2.1  uebayasi 
   1436  1.35.2.1  uebayasi 	if (sc->sc_rmii_enable)
   1437  1.35.2.1  uebayasi 		sc->sc_rmii_enable(device_parent(self), sc->sc_instance);
   1438  1.35.2.1  uebayasi 
   1439  1.35.2.1  uebayasi 	/* wait for PHY data transfer to complete */
   1440  1.35.2.1  uebayasi 	if (emac_mii_wait(sc))
   1441  1.35.2.1  uebayasi 		goto out;
   1442  1.35.2.1  uebayasi 
   1443  1.35.2.1  uebayasi 	sta_reg =
   1444  1.35.2.1  uebayasi 	    (val << STACR_PHYD_SHIFT)	|
   1445  1.35.2.1  uebayasi 	    sc->sc_stacr_write		|
   1446  1.35.2.1  uebayasi 	    (reg << STACR_PRA_SHIFT)	|
   1447  1.35.2.1  uebayasi 	    (phy << STACR_PCDA_SHIFT)	|
   1448  1.35.2.1  uebayasi 	    sc->sc_stacr_bits;
   1449  1.35.2.1  uebayasi 	EMAC_WRITE(sc, EMAC_STACR, sta_reg);
   1450  1.35.2.1  uebayasi 
   1451  1.35.2.1  uebayasi 	if (emac_mii_wait(sc))
   1452  1.35.2.1  uebayasi 		goto out;
   1453  1.35.2.1  uebayasi 	if (EMAC_READ(sc, EMAC_STACR) & STACR_PHYE)
   1454  1.35.2.1  uebayasi 		aprint_error_dev(sc->sc_dev, "MII PHY Error\n");
   1455  1.35.2.1  uebayasi 
   1456  1.35.2.1  uebayasi out:
   1457  1.35.2.1  uebayasi 	if (sc->sc_rmii_disable)
   1458  1.35.2.1  uebayasi 		sc->sc_rmii_disable(device_parent(self), sc->sc_instance);
   1459  1.35.2.1  uebayasi }
   1460  1.35.2.1  uebayasi 
   1461  1.35.2.1  uebayasi static void
   1462  1.35.2.1  uebayasi emac_mii_statchg(device_t self)
   1463  1.35.2.1  uebayasi {
   1464  1.35.2.1  uebayasi 	struct emac_softc *sc = device_private(self);
   1465  1.35.2.1  uebayasi 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1466  1.35.2.1  uebayasi 	struct mii_data *mii = &sc->sc_mii;
   1467  1.35.2.1  uebayasi 
   1468  1.35.2.1  uebayasi 	/*
   1469  1.35.2.1  uebayasi 	 * MR1 can only be written immediately after a reset...
   1470  1.35.2.1  uebayasi 	 */
   1471  1.35.2.1  uebayasi 	emac_smart_reset(sc);
   1472  1.35.2.1  uebayasi 
   1473  1.35.2.1  uebayasi 	sc->sc_mr1 &= ~(MR1_FDE | MR1_ILE | MR1_EIFC | MR1_MF_MASK | MR1_IST);
   1474  1.35.2.1  uebayasi 	if (mii->mii_media_active & IFM_FDX)
   1475  1.35.2.1  uebayasi 		sc->sc_mr1 |= (MR1_FDE | MR1_EIFC | MR1_IST);
   1476  1.35.2.1  uebayasi 	if (mii->mii_media_active & IFM_FLOW)
   1477  1.35.2.1  uebayasi 		sc->sc_mr1 |= MR1_EIFC;
   1478  1.35.2.1  uebayasi 	if (mii->mii_media_active & IFM_LOOP)
   1479  1.35.2.1  uebayasi 		sc->sc_mr1 |= MR1_ILE;
   1480  1.35.2.1  uebayasi 	switch (IFM_SUBTYPE(mii->mii_media_active)) {
   1481  1.35.2.1  uebayasi 	case IFM_1000_T:
   1482  1.35.2.1  uebayasi 		sc->sc_mr1 |= (MR1_MF_1000MBS | MR1_IST);
   1483  1.35.2.1  uebayasi 		break;
   1484  1.35.2.1  uebayasi 
   1485  1.35.2.1  uebayasi 	case IFM_100_TX:
   1486  1.35.2.1  uebayasi 		sc->sc_mr1 |= (MR1_MF_100MBS | MR1_IST);
   1487  1.35.2.1  uebayasi 		break;
   1488  1.35.2.1  uebayasi 
   1489  1.35.2.1  uebayasi 	case IFM_10_T:
   1490  1.35.2.1  uebayasi 		sc->sc_mr1 |= MR1_MF_10MBS;
   1491  1.35.2.1  uebayasi 		break;
   1492  1.35.2.1  uebayasi 
   1493  1.35.2.1  uebayasi 	case IFM_NONE:
   1494  1.35.2.1  uebayasi 		break;
   1495  1.35.2.1  uebayasi 
   1496  1.35.2.1  uebayasi 	default:
   1497  1.35.2.1  uebayasi 		aprint_error_dev(self, "unknown sub-type %d\n",
   1498  1.35.2.1  uebayasi 		    IFM_SUBTYPE(mii->mii_media_active));
   1499  1.35.2.1  uebayasi 		break;
   1500  1.35.2.1  uebayasi 	}
   1501  1.35.2.1  uebayasi 	if (sc->sc_rmii_speed)
   1502  1.35.2.1  uebayasi 		sc->sc_rmii_speed(device_parent(self), sc->sc_instance,
   1503  1.35.2.1  uebayasi 		    IFM_SUBTYPE(mii->mii_media_active));
   1504  1.35.2.1  uebayasi 
   1505  1.35.2.1  uebayasi 	EMAC_WRITE(sc, EMAC_MR1, sc->sc_mr1);
   1506  1.35.2.1  uebayasi 
   1507  1.35.2.1  uebayasi 	/* Enable TX and RX if already RUNNING */
   1508  1.35.2.1  uebayasi 	if (ifp->if_flags & IFF_RUNNING)
   1509  1.35.2.1  uebayasi 		EMAC_WRITE(sc, EMAC_MR0, MR0_TXE | MR0_RXE);
   1510  1.35.2.1  uebayasi }
   1511  1.35.2.1  uebayasi 
   1512  1.35.2.1  uebayasi static uint32_t
   1513  1.35.2.1  uebayasi emac_mii_wait(struct emac_softc *sc)
   1514  1.35.2.1  uebayasi {
   1515  1.35.2.1  uebayasi 	int i;
   1516  1.35.2.1  uebayasi 	uint32_t oc;
   1517  1.35.2.1  uebayasi 
   1518  1.35.2.1  uebayasi 	/* wait for PHY data transfer to complete */
   1519  1.35.2.1  uebayasi 	i = 0;
   1520  1.35.2.1  uebayasi 	oc = EMAC_READ(sc, EMAC_STACR) & STACR_OC;
   1521  1.35.2.1  uebayasi 	while ((oc == STACR_OC) != sc->sc_stacr_completed) {
   1522  1.35.2.1  uebayasi 		delay(7);
   1523  1.35.2.1  uebayasi 		if (i++ > 5) {
   1524  1.35.2.1  uebayasi 			aprint_error_dev(sc->sc_dev, "MII timed out\n");
   1525  1.35.2.1  uebayasi 			return -1;
   1526  1.35.2.1  uebayasi 		}
   1527  1.35.2.1  uebayasi 		oc = EMAC_READ(sc, EMAC_STACR) & STACR_OC;
   1528  1.35.2.1  uebayasi 	}
   1529  1.35.2.1  uebayasi 	return 0;
   1530  1.35.2.1  uebayasi }
   1531  1.35.2.1  uebayasi 
   1532  1.35.2.1  uebayasi static void
   1533  1.35.2.1  uebayasi emac_mii_tick(void *arg)
   1534  1.35.2.1  uebayasi {
   1535  1.35.2.1  uebayasi 	struct emac_softc *sc = arg;
   1536  1.35.2.1  uebayasi 	int s;
   1537  1.35.2.1  uebayasi 
   1538  1.35.2.1  uebayasi 	if (!device_is_active(sc->sc_dev))
   1539  1.35.2.1  uebayasi 		return;
   1540  1.35.2.1  uebayasi 
   1541  1.35.2.1  uebayasi 	s = splnet();
   1542  1.35.2.1  uebayasi 	mii_tick(&sc->sc_mii);
   1543  1.35.2.1  uebayasi 	splx(s);
   1544  1.35.2.1  uebayasi 
   1545  1.35.2.1  uebayasi 	callout_reset(&sc->sc_callout, hz, emac_mii_tick, sc);
   1546  1.35.2.1  uebayasi }
   1547  1.35.2.1  uebayasi 
   1548  1.35.2.1  uebayasi int
   1549  1.35.2.1  uebayasi emac_txeob_intr(void *arg)
   1550  1.35.2.1  uebayasi {
   1551  1.35.2.1  uebayasi 	struct emac_softc *sc = arg;
   1552  1.35.2.1  uebayasi 	int handled = 0;
   1553  1.35.2.1  uebayasi 
   1554  1.35.2.1  uebayasi 	EMAC_EVCNT_INCR(&sc->sc_ev_txintr);
   1555  1.35.2.1  uebayasi 	handled |= emac_txreap(sc);
   1556  1.35.2.1  uebayasi 
   1557  1.35.2.1  uebayasi 	/* try to get more packets going */
   1558  1.35.2.1  uebayasi 	emac_start(&sc->sc_ethercom.ec_if);
   1559  1.35.2.1  uebayasi 
   1560  1.35.2.1  uebayasi 	return handled;
   1561  1.35.2.1  uebayasi }
   1562  1.35.2.1  uebayasi 
   1563  1.35.2.1  uebayasi int
   1564       1.3    simonb emac_rxeob_intr(void *arg)
   1565       1.3    simonb {
   1566       1.3    simonb 	struct emac_softc *sc = arg;
   1567       1.3    simonb 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1568       1.3    simonb 	struct emac_rxsoft *rxs;
   1569       1.3    simonb 	struct mbuf *m;
   1570  1.35.2.1  uebayasi 	uint32_t rxstat;
   1571       1.3    simonb 	int i, len;
   1572       1.3    simonb 
   1573       1.3    simonb 	EMAC_EVCNT_INCR(&sc->sc_ev_rxintr);
   1574       1.3    simonb 
   1575  1.35.2.1  uebayasi 	for (i = sc->sc_rxptr; ; i = EMAC_NEXTRX(i)) {
   1576       1.3    simonb 		rxs = &sc->sc_rxsoft[i];
   1577       1.3    simonb 
   1578       1.3    simonb 		EMAC_CDRXSYNC(sc, i,
   1579       1.3    simonb 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1580       1.3    simonb 
   1581       1.3    simonb 		rxstat = sc->sc_rxdescs[i].md_stat_ctrl;
   1582       1.3    simonb 
   1583       1.3    simonb 		if (rxstat & MAL_RX_EMPTY)
   1584       1.3    simonb 			/*
   1585       1.3    simonb 			 * We have processed all of the receive buffers.
   1586       1.3    simonb 			 */
   1587       1.3    simonb 			break;
   1588       1.3    simonb 
   1589       1.3    simonb 		/*
   1590       1.3    simonb 		 * If an error occurred, update stats, clear the status
   1591       1.3    simonb 		 * word, and leave the packet buffer in place.  It will
   1592       1.3    simonb 		 * simply be reused the next time the ring comes around.
   1593       1.3    simonb 		 */
   1594       1.3    simonb 		if (rxstat & (EMAC_RXS_OE | EMAC_RXS_BP | EMAC_RXS_SE |
   1595       1.3    simonb 		    EMAC_RXS_AE | EMAC_RXS_BFCS | EMAC_RXS_PTL | EMAC_RXS_ORE |
   1596       1.3    simonb 		    EMAC_RXS_IRE)) {
   1597  1.35.2.1  uebayasi #define	PRINTERR(bit, str)					\
   1598  1.35.2.1  uebayasi 			if (rxstat & (bit))			\
   1599  1.35.2.1  uebayasi 				aprint_error_ifnet(ifp,		\
   1600  1.35.2.1  uebayasi 				    "receive error: %s\n", str)
   1601       1.3    simonb 			ifp->if_ierrors++;
   1602       1.3    simonb 			PRINTERR(EMAC_RXS_OE, "overrun error");
   1603       1.3    simonb 			PRINTERR(EMAC_RXS_BP, "bad packet");
   1604       1.3    simonb 			PRINTERR(EMAC_RXS_RP, "runt packet");
   1605       1.3    simonb 			PRINTERR(EMAC_RXS_SE, "short event");
   1606       1.3    simonb 			PRINTERR(EMAC_RXS_AE, "alignment error");
   1607       1.3    simonb 			PRINTERR(EMAC_RXS_BFCS, "bad FCS");
   1608       1.3    simonb 			PRINTERR(EMAC_RXS_PTL, "packet too long");
   1609       1.3    simonb 			PRINTERR(EMAC_RXS_ORE, "out of range error");
   1610       1.3    simonb 			PRINTERR(EMAC_RXS_IRE, "in range error");
   1611       1.3    simonb #undef PRINTERR
   1612       1.3    simonb 			EMAC_INIT_RXDESC(sc, i);
   1613       1.3    simonb 			continue;
   1614       1.3    simonb 		}
   1615       1.3    simonb 
   1616       1.3    simonb 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1617       1.3    simonb 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1618       1.3    simonb 
   1619       1.3    simonb 		/*
   1620       1.3    simonb 		 * No errors; receive the packet.  Note, the 405GP emac
   1621       1.3    simonb 		 * includes the CRC with every packet.
   1622       1.3    simonb 		 */
   1623      1.22   thorpej 		len = sc->sc_rxdescs[i].md_data_len - ETHER_CRC_LEN;
   1624       1.3    simonb 
   1625       1.3    simonb 		/*
   1626       1.3    simonb 		 * If the packet is small enough to fit in a
   1627       1.3    simonb 		 * single header mbuf, allocate one and copy
   1628       1.3    simonb 		 * the data into it.  This greatly reduces
   1629       1.3    simonb 		 * memory consumption when we receive lots
   1630       1.3    simonb 		 * of small packets.
   1631       1.3    simonb 		 *
   1632       1.3    simonb 		 * Otherwise, we add a new buffer to the receive
   1633       1.3    simonb 		 * chain.  If this fails, we drop the packet and
   1634       1.3    simonb 		 * recycle the old buffer.
   1635       1.3    simonb 		 */
   1636       1.3    simonb 		if (emac_copy_small != 0 && len <= MHLEN) {
   1637       1.3    simonb 			MGETHDR(m, M_DONTWAIT, MT_DATA);
   1638       1.3    simonb 			if (m == NULL)
   1639       1.3    simonb 				goto dropit;
   1640      1.28  christos 			memcpy(mtod(m, void *),
   1641      1.28  christos 			    mtod(rxs->rxs_mbuf, void *), len);
   1642       1.3    simonb 			EMAC_INIT_RXDESC(sc, i);
   1643       1.3    simonb 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1644       1.3    simonb 			    rxs->rxs_dmamap->dm_mapsize,
   1645       1.3    simonb 			    BUS_DMASYNC_PREREAD);
   1646       1.3    simonb 		} else {
   1647       1.3    simonb 			m = rxs->rxs_mbuf;
   1648       1.3    simonb 			if (emac_add_rxbuf(sc, i) != 0) {
   1649       1.3    simonb  dropit:
   1650       1.3    simonb 				ifp->if_ierrors++;
   1651       1.3    simonb 				EMAC_INIT_RXDESC(sc, i);
   1652       1.3    simonb 				bus_dmamap_sync(sc->sc_dmat,
   1653       1.3    simonb 				    rxs->rxs_dmamap, 0,
   1654       1.3    simonb 				    rxs->rxs_dmamap->dm_mapsize,
   1655       1.3    simonb 				    BUS_DMASYNC_PREREAD);
   1656       1.3    simonb 				continue;
   1657       1.3    simonb 			}
   1658       1.3    simonb 		}
   1659       1.3    simonb 
   1660       1.3    simonb 		ifp->if_ipackets++;
   1661       1.3    simonb 		m->m_pkthdr.rcvif = ifp;
   1662       1.3    simonb 		m->m_pkthdr.len = m->m_len = len;
   1663       1.3    simonb 
   1664       1.3    simonb 		/*
   1665       1.3    simonb 		 * Pass this up to any BPF listeners, but only
   1666       1.3    simonb 		 * pass if up the stack if it's for us.
   1667       1.3    simonb 		 */
   1668  1.35.2.1  uebayasi 		bpf_mtap(ifp, m);
   1669       1.3    simonb 
   1670       1.3    simonb 		/* Pass it on. */
   1671       1.3    simonb 		(*ifp->if_input)(ifp, m);
   1672       1.3    simonb 	}
   1673       1.3    simonb 
   1674       1.3    simonb 	/* Update the receive pointer. */
   1675       1.3    simonb 	sc->sc_rxptr = i;
   1676       1.3    simonb 
   1677  1.35.2.1  uebayasi 	return 1;
   1678       1.3    simonb }
   1679       1.3    simonb 
   1680  1.35.2.1  uebayasi int
   1681       1.3    simonb emac_txde_intr(void *arg)
   1682       1.3    simonb {
   1683       1.3    simonb 	struct emac_softc *sc = arg;
   1684       1.3    simonb 
   1685       1.3    simonb 	EMAC_EVCNT_INCR(&sc->sc_ev_txde);
   1686  1.35.2.1  uebayasi 	aprint_error_dev(sc->sc_dev, "emac_txde_intr\n");
   1687  1.35.2.1  uebayasi 	return 1;
   1688       1.3    simonb }
   1689       1.3    simonb 
   1690  1.35.2.1  uebayasi int
   1691       1.3    simonb emac_rxde_intr(void *arg)
   1692       1.3    simonb {
   1693       1.3    simonb 	struct emac_softc *sc = arg;
   1694  1.35.2.1  uebayasi 	int i;
   1695       1.3    simonb 
   1696       1.3    simonb 	EMAC_EVCNT_INCR(&sc->sc_ev_rxde);
   1697  1.35.2.1  uebayasi 	aprint_error_dev(sc->sc_dev, "emac_rxde_intr\n");
   1698       1.3    simonb 	/*
   1699       1.3    simonb 	 * XXX!
   1700       1.3    simonb 	 * This is a bit drastic; we just drop all descriptors that aren't
   1701       1.3    simonb 	 * "clean".  We should probably send any that are up the stack.
   1702       1.3    simonb 	 */
   1703       1.3    simonb 	for (i = 0; i < EMAC_NRXDESC; i++) {
   1704  1.35.2.1  uebayasi 		EMAC_CDRXSYNC(sc, i,
   1705  1.35.2.1  uebayasi 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1706       1.3    simonb 
   1707  1.35.2.1  uebayasi 		if (sc->sc_rxdescs[i].md_data_len != MCLBYTES)
   1708       1.3    simonb 			EMAC_INIT_RXDESC(sc, i);
   1709       1.3    simonb 	}
   1710       1.3    simonb 
   1711  1.35.2.1  uebayasi 	return 1;
   1712       1.3    simonb }
   1713