Home | History | Annotate | Line # | Download | only in ep93xx
epe.c revision 1.19
      1 /*	$NetBSD: epe.c,v 1.19 2009/03/14 15:36:01 dsl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2004 Jesse Off
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the NetBSD
     18  *	Foundation, Inc. and its contributors.
     19  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  *    contributors may be used to endorse or promote products derived
     21  *    from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  * POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: epe.c,v 1.19 2009/03/14 15:36:01 dsl Exp $");
     38 
     39 #include <sys/types.h>
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/ioctl.h>
     43 #include <sys/kernel.h>
     44 #include <sys/proc.h>
     45 #include <sys/malloc.h>
     46 #include <sys/time.h>
     47 #include <sys/device.h>
     48 #include <uvm/uvm_extern.h>
     49 
     50 #include <machine/bus.h>
     51 #include <machine/intr.h>
     52 
     53 #include <arm/cpufunc.h>
     54 
     55 #include <arm/ep93xx/epsocvar.h>
     56 #include <arm/ep93xx/ep93xxvar.h>
     57 
     58 #include <net/if.h>
     59 #include <net/if_dl.h>
     60 #include <net/if_types.h>
     61 #include <net/if_media.h>
     62 #include <net/if_ether.h>
     63 
     64 #include <dev/mii/mii.h>
     65 #include <dev/mii/miivar.h>
     66 
     67 #ifdef INET
     68 #include <netinet/in.h>
     69 #include <netinet/in_systm.h>
     70 #include <netinet/in_var.h>
     71 #include <netinet/ip.h>
     72 #include <netinet/if_inarp.h>
     73 #endif
     74 
     75 #ifdef NS
     76 #include <netns/ns.h>
     77 #include <netns/ns_if.h>
     78 #endif
     79 
     80 #include "bpfilter.h"
     81 #if NBPFILTER > 0
     82 #include <net/bpf.h>
     83 #include <net/bpfdesc.h>
     84 #endif
     85 
     86 #include <arm/ep93xx/ep93xxreg.h>
     87 #include <arm/ep93xx/epereg.h>
     88 #include <arm/ep93xx/epevar.h>
     89 
     90 #define DEFAULT_MDCDIV	32
     91 
     92 #ifndef EPE_FAST
     93 #define EPE_FAST
     94 #endif
     95 
     96 #ifndef EPE_FAST
     97 #define EPE_READ(x) \
     98 	bus_space_read_4(sc->sc_iot, sc->sc_ioh, (EPE_ ## x))
     99 #define EPE_WRITE(x, y) \
    100 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, (EPE_ ## x), (y))
    101 #define CTRLPAGE_DMASYNC(x, y, z) \
    102 	bus_dmamap_sync(sc->sc_dmat, sc->ctrlpage_dmamap, (x), (y), (z))
    103 #else
    104 #define EPE_READ(x) *(volatile u_int32_t *) \
    105 	(EP93XX_AHB_VBASE + EP93XX_AHB_EPE + (EPE_ ## x))
    106 #define EPE_WRITE(x, y) *(volatile u_int32_t *) \
    107 	(EP93XX_AHB_VBASE + EP93XX_AHB_EPE + (EPE_ ## x)) = y
    108 #define CTRLPAGE_DMASYNC(x, y, z)
    109 #endif /* ! EPE_FAST */
    110 
    111 static int	epe_match(struct device *, struct cfdata *, void *);
    112 static void	epe_attach(struct device *, struct device *, void *);
    113 static void	epe_init(struct epe_softc *);
    114 static int      epe_intr(void* arg);
    115 static int	epe_gctx(struct epe_softc *);
    116 static int	epe_mediachange(struct ifnet *);
    117 int		epe_mii_readreg (struct device *, int, int);
    118 void		epe_mii_writereg (struct device *, int, int, int);
    119 void		epe_statchg (struct device *);
    120 void		epe_tick (void *);
    121 static int	epe_ifioctl (struct ifnet *, u_long, void *);
    122 static void	epe_ifstart (struct ifnet *);
    123 static void	epe_ifwatchdog (struct ifnet *);
    124 static int	epe_ifinit (struct ifnet *);
    125 static void	epe_ifstop (struct ifnet *, int);
    126 static void	epe_setaddr (struct ifnet *);
    127 
    128 CFATTACH_DECL(epe, sizeof(struct epe_softc),
    129     epe_match, epe_attach, NULL, NULL);
    130 
    131 static int
    132 epe_match(struct device *parent, struct cfdata *match, void *aux)
    133 {
    134 	return 2;
    135 }
    136 
    137 static void
    138 epe_attach(struct device *parent, struct device *self, void *aux)
    139 {
    140 	struct epe_softc		*sc;
    141 	struct epsoc_attach_args	*sa;
    142 	prop_data_t			 enaddr;
    143 
    144 	printf("\n");
    145 	sc = (struct epe_softc*) self;
    146 	sa = aux;
    147 	sc->sc_iot = sa->sa_iot;
    148 	sc->sc_intr = sa->sa_intr;
    149 	sc->sc_dmat = sa->sa_dmat;
    150 
    151 	if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size,
    152 		0, &sc->sc_ioh))
    153 		panic("%s: Cannot map registers", self->dv_xname);
    154 
    155 	/* Fetch the Ethernet address from property if set. */
    156 	enaddr = prop_dictionary_get(device_properties(self), "mac-addr");
    157 	if (enaddr != NULL) {
    158 		KASSERT(prop_object_type(enaddr) == PROP_TYPE_DATA);
    159 		KASSERT(prop_data_size(enaddr) == ETHER_ADDR_LEN);
    160 		memcpy(sc->sc_enaddr, prop_data_data_nocopy(enaddr),
    161 		       ETHER_ADDR_LEN);
    162 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, EPE_AFP, 0);
    163 		bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
    164 					 sc->sc_enaddr, ETHER_ADDR_LEN);
    165 	}
    166 
    167         ep93xx_intr_establish(sc->sc_intr, IPL_NET, epe_intr, sc);
    168 	epe_init(sc);
    169 }
    170 
    171 static int
    172 epe_gctx(struct epe_softc *sc)
    173 {
    174 	struct ifnet * ifp = &sc->sc_ec.ec_if;
    175 	u_int32_t *cur, ndq = 0;
    176 
    177 	/* Handle transmit completions */
    178 	cur = (u_int32_t *)(EPE_READ(TXStsQCurAdd) -
    179 		sc->ctrlpage_dsaddr + (char*)sc->ctrlpage);
    180 
    181 	if (sc->TXStsQ_cur != cur) {
    182 		CTRLPAGE_DMASYNC(TX_QLEN * 2 * sizeof(u_int32_t),
    183 			TX_QLEN * sizeof(u_int32_t), BUS_DMASYNC_PREREAD);
    184 	} else {
    185 		return 0;
    186 	}
    187 
    188 	do {
    189 		u_int32_t tbi = *sc->TXStsQ_cur & 0x7fff;
    190 		struct mbuf *m = sc->txq[tbi].m;
    191 
    192 		if ((*sc->TXStsQ_cur & TXStsQ_TxWE) == 0) {
    193 			ifp->if_oerrors++;
    194 		}
    195 		bus_dmamap_unload(sc->sc_dmat, sc->txq[tbi].m_dmamap);
    196 		m_freem(m);
    197 		do {
    198 			sc->txq[tbi].m = NULL;
    199 			ndq++;
    200 			tbi = (tbi + 1) % TX_QLEN;
    201 		} while (sc->txq[tbi].m == m);
    202 
    203 		ifp->if_opackets++;
    204 		sc->TXStsQ_cur++;
    205 		if (sc->TXStsQ_cur >= sc->TXStsQ + TX_QLEN) {
    206 			sc->TXStsQ_cur = sc->TXStsQ;
    207 		}
    208 	} while (sc->TXStsQ_cur != cur);
    209 
    210 	sc->TXDQ_avail += ndq;
    211 	if (ifp->if_flags & IFF_OACTIVE) {
    212 		ifp->if_flags &= ~IFF_OACTIVE;
    213 		/* Disable end-of-tx-chain interrupt */
    214 		EPE_WRITE(IntEn, IntEn_REOFIE);
    215 	}
    216 	return ndq;
    217 }
    218 
    219 static int
    220 epe_intr(void *arg)
    221 {
    222 	struct epe_softc *sc = (struct epe_softc *)arg;
    223 	struct ifnet * ifp = &sc->sc_ec.ec_if;
    224 	u_int32_t ndq = 0, irq, *cur;
    225 
    226 	irq = EPE_READ(IntStsC);
    227 begin:
    228 	cur = (u_int32_t *)(EPE_READ(RXStsQCurAdd) -
    229 		sc->ctrlpage_dsaddr + (char*)sc->ctrlpage);
    230 	CTRLPAGE_DMASYNC(TX_QLEN * 3 * sizeof(u_int32_t),
    231 		RX_QLEN * 4 * sizeof(u_int32_t),
    232 		BUS_DMASYNC_PREREAD);
    233 	while (sc->RXStsQ_cur != cur) {
    234 		if ((sc->RXStsQ_cur[0] & (RXStsQ_RWE|RXStsQ_RFP|RXStsQ_EOB)) ==
    235 			(RXStsQ_RWE|RXStsQ_RFP|RXStsQ_EOB)) {
    236 			u_int32_t bi = (sc->RXStsQ_cur[1] >> 16) & 0x7fff;
    237 			u_int32_t fl = sc->RXStsQ_cur[1] & 0xffff;
    238 			struct mbuf *m;
    239 
    240 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    241 			if (m != NULL) MCLGET(m, M_DONTWAIT);
    242 			if (m != NULL && (m->m_flags & M_EXT)) {
    243 				bus_dmamap_unload(sc->sc_dmat,
    244 					sc->rxq[bi].m_dmamap);
    245 				sc->rxq[bi].m->m_pkthdr.rcvif = ifp;
    246 				sc->rxq[bi].m->m_pkthdr.len =
    247 					sc->rxq[bi].m->m_len = fl;
    248 #if NBPFILTER > 0
    249 				if (ifp->if_bpf)
    250 					bpf_mtap(ifp->if_bpf, sc->rxq[bi].m);
    251 #endif /* NBPFILTER > 0 */
    252                                 (*ifp->if_input)(ifp, sc->rxq[bi].m);
    253 				sc->rxq[bi].m = m;
    254 				bus_dmamap_load(sc->sc_dmat,
    255 					sc->rxq[bi].m_dmamap,
    256 					m->m_ext.ext_buf, MCLBYTES,
    257 					NULL, BUS_DMA_NOWAIT);
    258 				sc->RXDQ[bi * 2] =
    259 					sc->rxq[bi].m_dmamap->dm_segs[0].ds_addr;
    260 			} else {
    261 				/* Drop packets until we can get replacement
    262 				 * empty mbufs for the RXDQ.
    263 				 */
    264 				if (m != NULL) {
    265 					m_freem(m);
    266 				}
    267 				ifp->if_ierrors++;
    268 			}
    269 		} else {
    270 			ifp->if_ierrors++;
    271 		}
    272 
    273 		ndq++;
    274 
    275 		sc->RXStsQ_cur += 2;
    276 		if (sc->RXStsQ_cur >= sc->RXStsQ + (RX_QLEN * 2)) {
    277 			sc->RXStsQ_cur = sc->RXStsQ;
    278 		}
    279 	}
    280 
    281 	if (ndq > 0) {
    282 		ifp->if_ipackets += ndq;
    283 		CTRLPAGE_DMASYNC(TX_QLEN * 3 * sizeof(u_int32_t),
    284  			RX_QLEN * 4 * sizeof(u_int32_t),
    285 			BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
    286 		EPE_WRITE(RXStsEnq, ndq);
    287 		EPE_WRITE(RXDEnq, ndq);
    288 		ndq = 0;
    289 	}
    290 
    291 	if (epe_gctx(sc) > 0 && IFQ_IS_EMPTY(&ifp->if_snd) == 0) {
    292 		epe_ifstart(ifp);
    293 	}
    294 
    295 	irq = EPE_READ(IntStsC);
    296 	if ((irq & (IntSts_RxSQ|IntSts_ECI)) != 0)
    297 		goto begin;
    298 
    299 	return (1);
    300 }
    301 
    302 
    303 static void
    304 epe_init(struct epe_softc *sc)
    305 {
    306 	bus_dma_segment_t segs;
    307 	char *addr;
    308 	int rsegs, err, i;
    309 	struct ifnet * ifp = &sc->sc_ec.ec_if;
    310 	int mdcdiv = DEFAULT_MDCDIV;
    311 
    312 	callout_init(&sc->epe_tick_ch, 0);
    313 
    314 	/* Select primary Individual Address in Address Filter Pointer */
    315 	EPE_WRITE(AFP, 0);
    316 	/* Read ethernet MAC, should already be set by bootrom */
    317 	bus_space_read_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
    318 		sc->sc_enaddr, ETHER_ADDR_LEN);
    319 	printf("%s: MAC address %s\n", sc->sc_dev.dv_xname,
    320 		ether_sprintf(sc->sc_enaddr));
    321 
    322 	/* Soft Reset the MAC */
    323 	EPE_WRITE(SelfCtl, SelfCtl_RESET);
    324 	while(EPE_READ(SelfCtl) & SelfCtl_RESET);
    325 
    326 	/* suggested magic initialization values from datasheet */
    327 	EPE_WRITE(RXBufThrshld, 0x800040);
    328 	EPE_WRITE(TXBufThrshld, 0x200010);
    329 	EPE_WRITE(RXStsThrshld, 0x40002);
    330 	EPE_WRITE(TXStsThrshld, 0x40002);
    331 	EPE_WRITE(RXDThrshld, 0x40002);
    332 	EPE_WRITE(TXDThrshld, 0x40002);
    333 
    334 	/* Allocate a page of memory for descriptor and status queues */
    335 	err = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, 0, PAGE_SIZE,
    336 		&segs, 1, &rsegs, BUS_DMA_WAITOK);
    337 	if (err == 0) {
    338 		err = bus_dmamem_map(sc->sc_dmat, &segs, 1, PAGE_SIZE,
    339 			&sc->ctrlpage, (BUS_DMA_WAITOK|BUS_DMA_COHERENT));
    340 	}
    341 	if (err == 0) {
    342 		err = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE,
    343 			0, BUS_DMA_WAITOK, &sc->ctrlpage_dmamap);
    344 	}
    345 	if (err == 0) {
    346 		err = bus_dmamap_load(sc->sc_dmat, sc->ctrlpage_dmamap,
    347 			sc->ctrlpage, PAGE_SIZE, NULL, BUS_DMA_WAITOK);
    348 	}
    349 	if (err != 0) {
    350 		panic("%s: Cannot get DMA memory", sc->sc_dev.dv_xname);
    351 	}
    352 	sc->ctrlpage_dsaddr = sc->ctrlpage_dmamap->dm_segs[0].ds_addr;
    353 	bzero(sc->ctrlpage, PAGE_SIZE);
    354 
    355 	/* Set up pointers to start of each queue in kernel addr space.
    356 	 * Each descriptor queue or status queue entry uses 2 words
    357 	 */
    358 	sc->TXDQ = (u_int32_t *)sc->ctrlpage;
    359 	sc->TXDQ_cur = sc->TXDQ;
    360 	sc->TXDQ_avail = TX_QLEN - 1;
    361 	sc->TXStsQ = &sc->TXDQ[TX_QLEN * 2];
    362 	sc->TXStsQ_cur = sc->TXStsQ;
    363 	sc->RXDQ = &sc->TXStsQ[TX_QLEN];
    364 	sc->RXStsQ = &sc->RXDQ[RX_QLEN * 2];
    365 	sc->RXStsQ_cur = sc->RXStsQ;
    366 
    367 	/* Program each queue's start addr, cur addr, and len registers
    368 	 * with the physical addresses.
    369 	 */
    370 	addr = (char *)sc->ctrlpage_dmamap->dm_segs[0].ds_addr;
    371 	EPE_WRITE(TXDQBAdd, (u_int32_t)addr);
    372 	EPE_WRITE(TXDQCurAdd, (u_int32_t)addr);
    373 	EPE_WRITE(TXDQBLen, TX_QLEN * 2 * sizeof(u_int32_t));
    374 
    375 	addr += (sc->TXStsQ - sc->TXDQ) * sizeof(u_int32_t);
    376 	EPE_WRITE(TXStsQBAdd, (u_int32_t)addr);
    377 	EPE_WRITE(TXStsQCurAdd, (u_int32_t)addr);
    378 	EPE_WRITE(TXStsQBLen, TX_QLEN * sizeof(u_int32_t));
    379 
    380 	addr += (sc->RXDQ - sc->TXStsQ) * sizeof(u_int32_t);
    381 	EPE_WRITE(RXDQBAdd, (u_int32_t)addr);
    382 	EPE_WRITE(RXDCurAdd, (u_int32_t)addr);
    383 	EPE_WRITE(RXDQBLen, RX_QLEN * 2 * sizeof(u_int32_t));
    384 
    385 	addr += (sc->RXStsQ - sc->RXDQ) * sizeof(u_int32_t);
    386 	EPE_WRITE(RXStsQBAdd, (u_int32_t)addr);
    387 	EPE_WRITE(RXStsQCurAdd, (u_int32_t)addr);
    388 	EPE_WRITE(RXStsQBLen, RX_QLEN * 2 * sizeof(u_int32_t));
    389 
    390 	/* Populate the RXDQ with mbufs */
    391 	for(i = 0; i < RX_QLEN; i++) {
    392 		struct mbuf *m;
    393 
    394 		bus_dmamap_create(sc->sc_dmat, MCLBYTES, TX_QLEN/4, MCLBYTES, 0,
    395 			BUS_DMA_WAITOK, &sc->rxq[i].m_dmamap);
    396 		MGETHDR(m, M_WAIT, MT_DATA);
    397 		MCLGET(m, M_WAIT);
    398 		sc->rxq[i].m = m;
    399 		bus_dmamap_load(sc->sc_dmat, sc->rxq[i].m_dmamap,
    400 			m->m_ext.ext_buf, MCLBYTES, NULL,
    401 			BUS_DMA_WAITOK);
    402 
    403 		sc->RXDQ[i * 2] = sc->rxq[i].m_dmamap->dm_segs[0].ds_addr;
    404 		sc->RXDQ[i * 2 + 1] = (i << 16) | MCLBYTES;
    405 		bus_dmamap_sync(sc->sc_dmat, sc->rxq[i].m_dmamap, 0,
    406 			MCLBYTES, BUS_DMASYNC_PREREAD);
    407 	}
    408 
    409 	for(i = 0; i < TX_QLEN; i++) {
    410 		bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0,
    411 			(BUS_DMA_WAITOK|BUS_DMA_ALLOCNOW),
    412 			&sc->txq[i].m_dmamap);
    413 		sc->txq[i].m = NULL;
    414 		sc->TXDQ[i * 2 + 1] = (i << 16);
    415 	}
    416 
    417 	/* Divide HCLK by 32 for MDC clock */
    418 	if (device_cfdata(&sc->sc_dev)->cf_flags)
    419 		mdcdiv = device_cfdata(&sc->sc_dev)->cf_flags;
    420 	EPE_WRITE(SelfCtl, (SelfCtl_MDCDIV(mdcdiv)|SelfCtl_PSPRS));
    421 
    422 	sc->sc_mii.mii_ifp = ifp;
    423 	sc->sc_mii.mii_readreg = epe_mii_readreg;
    424 	sc->sc_mii.mii_writereg = epe_mii_writereg;
    425 	sc->sc_mii.mii_statchg = epe_statchg;
    426 	sc->sc_ec.ec_mii = &sc->sc_mii;
    427 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, epe_mediachange,
    428 		ether_mediastatus);
    429 	mii_attach((struct device *)sc, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    430 		MII_OFFSET_ANY, 0);
    431 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    432 
    433 	EPE_WRITE(BMCtl, BMCtl_RxEn|BMCtl_TxEn);
    434 	EPE_WRITE(IntEn, IntEn_REOFIE);
    435 	/* maximum valid max frame length */
    436 	EPE_WRITE(MaxFrmLen, (0x7ff << 16)|MHLEN);
    437 	/* wait for receiver ready */
    438 	while((EPE_READ(BMSts) & BMSts_RxAct) == 0);
    439 	/* enqueue the entries in RXStsQ and RXDQ */
    440 	CTRLPAGE_DMASYNC(0, sc->ctrlpage_dmamap->dm_mapsize,
    441 		BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
    442 	EPE_WRITE(RXDEnq, RX_QLEN - 1);
    443 	EPE_WRITE(RXStsEnq, RX_QLEN - 1);
    444 
    445 	/*
    446 	 * We can support 802.1Q VLAN-sized frames.
    447 	 */
    448 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
    449 
    450         strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    451         ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
    452         ifp->if_ioctl = epe_ifioctl;
    453         ifp->if_start = epe_ifstart;
    454         ifp->if_watchdog = epe_ifwatchdog;
    455         ifp->if_init = epe_ifinit;
    456         ifp->if_stop = epe_ifstop;
    457         ifp->if_timer = 0;
    458 	ifp->if_softc = sc;
    459         IFQ_SET_READY(&ifp->if_snd);
    460         if_attach(ifp);
    461         ether_ifattach(ifp, (sc)->sc_enaddr);
    462 }
    463 
    464 static int
    465 epe_mediachange(struct ifnet *ifp)
    466 {
    467 	if (ifp->if_flags & IFF_UP)
    468 		epe_ifinit(ifp);
    469 	return (0);
    470 }
    471 
    472 int
    473 epe_mii_readreg(self, phy, reg)
    474 	struct device *self;
    475 	int phy, reg;
    476 {
    477 	u_int32_t d, v;
    478 	struct epe_softc *sc;
    479 
    480 	sc = (struct epe_softc *)self;
    481 	d = EPE_READ(SelfCtl);
    482 	EPE_WRITE(SelfCtl, d & ~SelfCtl_PSPRS); /* no preamble suppress */
    483 	EPE_WRITE(MIICmd, (MIICmd_READ | (phy << 5) | reg));
    484 	while(EPE_READ(MIISts) & MIISts_BUSY);
    485 	v = EPE_READ(MIIData);
    486 	EPE_WRITE(SelfCtl, d); /* restore old value */
    487 	return v;
    488 }
    489 
    490 void
    491 epe_mii_writereg(self, phy, reg, val)
    492 	struct device *self;
    493 	int phy, reg, val;
    494 {
    495 	struct epe_softc *sc;
    496 	u_int32_t d;
    497 
    498 	sc = (struct epe_softc *)self;
    499 	d = EPE_READ(SelfCtl);
    500 	EPE_WRITE(SelfCtl, d & ~SelfCtl_PSPRS); /* no preamble suppress */
    501 	EPE_WRITE(MIIData, val);
    502 	EPE_WRITE(MIICmd, (MIICmd_WRITE | (phy << 5) | reg));
    503 	while(EPE_READ(MIISts) & MIISts_BUSY);
    504 	EPE_WRITE(SelfCtl, d); /* restore old value */
    505 }
    506 
    507 
    508 void
    509 epe_statchg(struct device *self)
    510 {
    511         struct epe_softc *sc = (struct epe_softc *)self;
    512         u_int32_t reg;
    513 
    514         /*
    515          * We must keep the MAC and the PHY in sync as
    516          * to the status of full-duplex!
    517          */
    518         reg = EPE_READ(TestCtl);
    519         if (sc->sc_mii.mii_media_active & IFM_FDX)
    520                 reg |= TestCtl_MFDX;
    521         else
    522                 reg &= ~TestCtl_MFDX;
    523 	EPE_WRITE(TestCtl, reg);
    524 }
    525 
    526 void
    527 epe_tick(void *arg)
    528 {
    529 	struct epe_softc* sc = (struct epe_softc *)arg;
    530 	struct ifnet * ifp = &sc->sc_ec.ec_if;
    531 	int s;
    532 	u_int32_t misses;
    533 
    534 	ifp->if_collisions += EPE_READ(TXCollCnt);
    535 	/* These misses are ok, they will happen if the RAM/CPU can't keep up */
    536 	misses = EPE_READ(RXMissCnt);
    537 	if (misses > 0)
    538 		printf("%s: %d rx misses\n", sc->sc_dev.dv_xname, misses);
    539 
    540 	s = splnet();
    541 	if (epe_gctx(sc) > 0 && IFQ_IS_EMPTY(&ifp->if_snd) == 0) {
    542 		epe_ifstart(ifp);
    543 	}
    544 	splx(s);
    545 
    546 	mii_tick(&sc->sc_mii);
    547 	callout_reset(&sc->epe_tick_ch, hz, epe_tick, sc);
    548 }
    549 
    550 
    551 static int
    552 epe_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
    553 {
    554 	int s, error;
    555 
    556 	s = splnet();
    557 	error = ether_ioctl(ifp, cmd, data);
    558 	if (error == ENETRESET) {
    559 		if (ifp->if_flags & IFF_RUNNING)
    560 			epe_setaddr(ifp);
    561 		error = 0;
    562 	}
    563 	splx(s);
    564 	return error;
    565 }
    566 
    567 static void
    568 epe_ifstart(struct ifnet *ifp)
    569 {
    570 	struct epe_softc *sc = (struct epe_softc *)ifp->if_softc;
    571 	struct mbuf *m;
    572 	bus_dma_segment_t *segs;
    573 	int s, bi, err, nsegs, ndq;
    574 
    575 	s = splnet();
    576 start:
    577 	ndq = 0;
    578 	if (sc->TXDQ_avail == 0) {
    579 		if (epe_gctx(sc) == 0) {
    580 			/* Enable End-Of-TX-Chain interrupt */
    581 			EPE_WRITE(IntEn, IntEn_REOFIE|IntEn_ECIE);
    582 			ifp->if_flags |= IFF_OACTIVE;
    583 			ifp->if_timer = 10;
    584 			splx(s);
    585 			return;
    586 		}
    587 	}
    588 
    589 	bi = sc->TXDQ_cur - sc->TXDQ;
    590 
    591 	IFQ_POLL(&ifp->if_snd, m);
    592 	if (m == NULL) {
    593 		splx(s);
    594 		return;
    595 	}
    596 more:
    597 	if ((err = bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m,
    598 		BUS_DMA_NOWAIT)) ||
    599 		sc->txq[bi].m_dmamap->dm_segs[0].ds_addr & 0x3 ||
    600 		sc->txq[bi].m_dmamap->dm_nsegs > (sc->TXDQ_avail - ndq)) {
    601 		/* Copy entire mbuf chain to new and 32-bit aligned storage */
    602 		struct mbuf *mn;
    603 
    604 		if (err == 0)
    605 			bus_dmamap_unload(sc->sc_dmat, sc->txq[bi].m_dmamap);
    606 
    607 		MGETHDR(mn, M_DONTWAIT, MT_DATA);
    608 		if (mn == NULL) goto stop;
    609 		if (m->m_pkthdr.len > (MHLEN & (~0x3))) {
    610 			MCLGET(mn, M_DONTWAIT);
    611 			if ((mn->m_flags & M_EXT) == 0) {
    612 				m_freem(mn);
    613 				goto stop;
    614 			}
    615 		}
    616 		mn->m_data = (void *)(((u_int32_t)mn->m_data + 0x3) & (~0x3));
    617 		m_copydata(m, 0, m->m_pkthdr.len, mtod(mn, void *));
    618 		mn->m_pkthdr.len = mn->m_len = m->m_pkthdr.len;
    619 		IFQ_DEQUEUE(&ifp->if_snd, m);
    620 		m_freem(m);
    621 		m = mn;
    622 		bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m,
    623 			BUS_DMA_NOWAIT);
    624 	} else {
    625 		IFQ_DEQUEUE(&ifp->if_snd, m);
    626 	}
    627 
    628 #if NBPFILTER > 0
    629 	if (ifp->if_bpf)
    630 		bpf_mtap(ifp->if_bpf, m);
    631 #endif /* NBPFILTER > 0 */
    632 
    633 	nsegs = sc->txq[bi].m_dmamap->dm_nsegs;
    634 	segs = sc->txq[bi].m_dmamap->dm_segs;
    635 	bus_dmamap_sync(sc->sc_dmat, sc->txq[bi].m_dmamap, 0,
    636 		sc->txq[bi].m_dmamap->dm_mapsize,
    637 		BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    638 
    639 	/* XXX: This driver hasn't been tested w/nsegs > 1 */
    640 	while (nsegs > 0) {
    641 		nsegs--;
    642 		sc->txq[bi].m = m;
    643 		sc->TXDQ[bi * 2] = segs->ds_addr;
    644 		if (nsegs == 0)
    645 			sc->TXDQ[bi * 2 + 1] = segs->ds_len | (bi << 16) |
    646 				(1 << 31);
    647 		else
    648 			sc->TXDQ[bi * 2 + 1] = segs->ds_len | (bi << 16);
    649 		segs++;
    650 		bi = (bi + 1) % TX_QLEN;
    651 		ndq++;
    652 	}
    653 
    654 
    655 	/*
    656 	 * Enqueue another.  Don't do more than half the available
    657 	 * descriptors before telling the MAC about them
    658 	 */
    659 	if ((sc->TXDQ_avail - ndq) > 0 && ndq < TX_QLEN / 2) {
    660 		IFQ_POLL(&ifp->if_snd, m);
    661 		if (m != NULL) {
    662 			goto more;
    663 		}
    664 	}
    665 stop:
    666 	if (ndq > 0) {
    667 		sc->TXDQ_avail -= ndq;
    668 		sc->TXDQ_cur = &sc->TXDQ[bi];
    669 		CTRLPAGE_DMASYNC(0, TX_QLEN * 2 * sizeof(u_int32_t),
    670 			BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
    671 		EPE_WRITE(TXDEnq, ndq);
    672 	}
    673 
    674 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    675 		goto start;
    676 
    677 	splx(s);
    678 	return;
    679 }
    680 
    681 static void
    682 epe_ifwatchdog(struct ifnet *ifp)
    683 {
    684 	struct epe_softc *sc = (struct epe_softc *)ifp->if_softc;
    685 
    686 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    687 		return;
    688        	printf("%s: device timeout, BMCtl = 0x%08x, BMSts = 0x%08x\n",
    689 		sc->sc_dev.dv_xname, EPE_READ(BMCtl), EPE_READ(BMSts));
    690 }
    691 
    692 static int
    693 epe_ifinit(struct ifnet *ifp)
    694 {
    695 	struct epe_softc *sc = ifp->if_softc;
    696 	int rc, s = splnet();
    697 
    698 	callout_stop(&sc->epe_tick_ch);
    699 	EPE_WRITE(RXCtl, RXCtl_IA0|RXCtl_BA|RXCtl_RCRCA|RXCtl_SRxON);
    700 	EPE_WRITE(TXCtl, TXCtl_STxON);
    701 	EPE_WRITE(GIIntMsk, GIIntMsk_INT); /* start interrupting */
    702 
    703 	if ((rc = mii_mediachg(&sc->sc_mii)) == ENXIO)
    704 		rc = 0;
    705 	else if (rc != 0)
    706 		goto out;
    707 
    708 	callout_reset(&sc->epe_tick_ch, hz, epe_tick, sc);
    709         ifp->if_flags |= IFF_RUNNING;
    710 out:
    711 	splx(s);
    712 	return 0;
    713 }
    714 
    715 static void
    716 epe_ifstop(struct ifnet *ifp, int disable)
    717 {
    718 	struct epe_softc *sc = ifp->if_softc;
    719 
    720 
    721 	EPE_WRITE(RXCtl, 0);
    722 	EPE_WRITE(TXCtl, 0);
    723 	EPE_WRITE(GIIntMsk, 0);
    724 	callout_stop(&sc->epe_tick_ch);
    725 
    726 	/* Down the MII. */
    727 	mii_down(&sc->sc_mii);
    728 
    729 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    730 	ifp->if_timer = 0;
    731 	sc->sc_mii.mii_media_status &= ~IFM_ACTIVE;
    732 }
    733 
    734 static void
    735 epe_setaddr(struct ifnet *ifp)
    736 {
    737 	struct epe_softc *sc = ifp->if_softc;
    738 	struct ethercom *ac = &sc->sc_ec;
    739 	struct ether_multi *enm;
    740 	struct ether_multistep step;
    741 	u_int8_t ias[2][ETHER_ADDR_LEN];
    742 	u_int32_t h, nma = 0, hashes[2] = { 0, 0 };
    743 	u_int32_t rxctl = EPE_READ(RXCtl);
    744 
    745 	/* disable receiver temporarily */
    746 	EPE_WRITE(RXCtl, rxctl & ~RXCtl_SRxON);
    747 
    748 	rxctl &= ~(RXCtl_MA|RXCtl_PA|RXCtl_IA2|RXCtl_IA3);
    749 
    750 	if (ifp->if_flags & IFF_PROMISC) {
    751 		rxctl |= RXCtl_PA;
    752 	}
    753 
    754 	ifp->if_flags &= ~IFF_ALLMULTI;
    755 
    756 	ETHER_FIRST_MULTI(step, ac, enm);
    757 	while (enm != NULL) {
    758 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
    759 			/*
    760 			 * We must listen to a range of multicast addresses.
    761 			 * For now, just accept all multicasts, rather than
    762 			 * trying to set only those filter bits needed to match
    763 			 * the range.  (At this time, the only use of address
    764 			 * ranges is for IP multicast routing, for which the
    765 			 * range is big enough to require all bits set.)
    766 			 */
    767 			rxctl &= ~(RXCtl_IA2|RXCtl_IA3);
    768 			rxctl |= RXCtl_MA;
    769 			hashes[0] = 0xffffffffUL;
    770 			hashes[1] = 0xffffffffUL;
    771 			ifp->if_flags |= IFF_ALLMULTI;
    772 			break;
    773 		}
    774 
    775 		if (nma < 2) {
    776 			/* We can program 2 perfect address filters for mcast */
    777 			memcpy(ias[nma], enm->enm_addrlo, ETHER_ADDR_LEN);
    778 			rxctl |= (1 << (nma + 2));
    779 		} else {
    780 			/*
    781 			 * XXX: Datasheet is not very clear here, I'm not sure
    782 			 * if I'm doing this right.  --joff
    783 			 */
    784 			h = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
    785 
    786 			/* Just want the 6 most-significant bits. */
    787 			h = h >> 26;
    788 
    789 			hashes[ h / 32 ] |=  (1 << (h % 32));
    790 			rxctl |= RXCtl_MA;
    791 		}
    792 		ETHER_NEXT_MULTI(step, enm);
    793 		nma++;
    794 	}
    795 
    796 	EPE_WRITE(AFP, 0);
    797 	bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
    798 		sc->sc_enaddr, ETHER_ADDR_LEN);
    799 	if (rxctl & RXCtl_IA2) {
    800 		EPE_WRITE(AFP, 2);
    801 		bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
    802 			ias[0], ETHER_ADDR_LEN);
    803 	}
    804 	if (rxctl & RXCtl_IA3) {
    805 		EPE_WRITE(AFP, 3);
    806 		bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
    807 			ias[1], ETHER_ADDR_LEN);
    808 	}
    809 	if (hashes[0] != 0 && hashes[1] != 0) {
    810 		EPE_WRITE(AFP, 7);
    811 		EPE_WRITE(HashTbl, hashes[0]);
    812 		EPE_WRITE(HashTbl + 4, hashes[1]);
    813 	}
    814 	EPE_WRITE(RXCtl, rxctl);
    815 }
    816