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