1 /* $NetBSD: epe.c,v 1.18 2008/05/10 15:31:04 martin 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.18 2008/05/10 15:31:04 martin 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(ifp) 466 struct ifnet *ifp; 467 { 468 if (ifp->if_flags & IFF_UP) 469 epe_ifinit(ifp); 470 return (0); 471 } 472 473 int 474 epe_mii_readreg(self, phy, reg) 475 struct device *self; 476 int phy, reg; 477 { 478 u_int32_t d, v; 479 struct epe_softc *sc; 480 481 sc = (struct epe_softc *)self; 482 d = EPE_READ(SelfCtl); 483 EPE_WRITE(SelfCtl, d & ~SelfCtl_PSPRS); /* no preamble suppress */ 484 EPE_WRITE(MIICmd, (MIICmd_READ | (phy << 5) | reg)); 485 while(EPE_READ(MIISts) & MIISts_BUSY); 486 v = EPE_READ(MIIData); 487 EPE_WRITE(SelfCtl, d); /* restore old value */ 488 return v; 489 } 490 491 void 492 epe_mii_writereg(self, phy, reg, val) 493 struct device *self; 494 int phy, reg, val; 495 { 496 struct epe_softc *sc; 497 u_int32_t d; 498 499 sc = (struct epe_softc *)self; 500 d = EPE_READ(SelfCtl); 501 EPE_WRITE(SelfCtl, d & ~SelfCtl_PSPRS); /* no preamble suppress */ 502 EPE_WRITE(MIIData, val); 503 EPE_WRITE(MIICmd, (MIICmd_WRITE | (phy << 5) | reg)); 504 while(EPE_READ(MIISts) & MIISts_BUSY); 505 EPE_WRITE(SelfCtl, d); /* restore old value */ 506 } 507 508 509 void 510 epe_statchg(self) 511 struct device *self; 512 { 513 struct epe_softc *sc = (struct epe_softc *)self; 514 u_int32_t reg; 515 516 /* 517 * We must keep the MAC and the PHY in sync as 518 * to the status of full-duplex! 519 */ 520 reg = EPE_READ(TestCtl); 521 if (sc->sc_mii.mii_media_active & IFM_FDX) 522 reg |= TestCtl_MFDX; 523 else 524 reg &= ~TestCtl_MFDX; 525 EPE_WRITE(TestCtl, reg); 526 } 527 528 void 529 epe_tick(arg) 530 void *arg; 531 { 532 struct epe_softc* sc = (struct epe_softc *)arg; 533 struct ifnet * ifp = &sc->sc_ec.ec_if; 534 int s; 535 u_int32_t misses; 536 537 ifp->if_collisions += EPE_READ(TXCollCnt); 538 /* These misses are ok, they will happen if the RAM/CPU can't keep up */ 539 misses = EPE_READ(RXMissCnt); 540 if (misses > 0) 541 printf("%s: %d rx misses\n", sc->sc_dev.dv_xname, misses); 542 543 s = splnet(); 544 if (epe_gctx(sc) > 0 && IFQ_IS_EMPTY(&ifp->if_snd) == 0) { 545 epe_ifstart(ifp); 546 } 547 splx(s); 548 549 mii_tick(&sc->sc_mii); 550 callout_reset(&sc->epe_tick_ch, hz, epe_tick, sc); 551 } 552 553 554 static int 555 epe_ifioctl(ifp, cmd, data) 556 struct ifnet *ifp; 557 u_long cmd; 558 void *data; 559 { 560 int s, error; 561 562 s = splnet(); 563 error = ether_ioctl(ifp, cmd, data); 564 if (error == ENETRESET) { 565 if (ifp->if_flags & IFF_RUNNING) 566 epe_setaddr(ifp); 567 error = 0; 568 } 569 splx(s); 570 return error; 571 } 572 573 static void 574 epe_ifstart(ifp) 575 struct ifnet *ifp; 576 { 577 struct epe_softc *sc = (struct epe_softc *)ifp->if_softc; 578 struct mbuf *m; 579 bus_dma_segment_t *segs; 580 int s, bi, err, nsegs, ndq; 581 582 s = splnet(); 583 start: 584 ndq = 0; 585 if (sc->TXDQ_avail == 0) { 586 if (epe_gctx(sc) == 0) { 587 /* Enable End-Of-TX-Chain interrupt */ 588 EPE_WRITE(IntEn, IntEn_REOFIE|IntEn_ECIE); 589 ifp->if_flags |= IFF_OACTIVE; 590 ifp->if_timer = 10; 591 splx(s); 592 return; 593 } 594 } 595 596 bi = sc->TXDQ_cur - sc->TXDQ; 597 598 IFQ_POLL(&ifp->if_snd, m); 599 if (m == NULL) { 600 splx(s); 601 return; 602 } 603 more: 604 if ((err = bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m, 605 BUS_DMA_NOWAIT)) || 606 sc->txq[bi].m_dmamap->dm_segs[0].ds_addr & 0x3 || 607 sc->txq[bi].m_dmamap->dm_nsegs > (sc->TXDQ_avail - ndq)) { 608 /* Copy entire mbuf chain to new and 32-bit aligned storage */ 609 struct mbuf *mn; 610 611 if (err == 0) 612 bus_dmamap_unload(sc->sc_dmat, sc->txq[bi].m_dmamap); 613 614 MGETHDR(mn, M_DONTWAIT, MT_DATA); 615 if (mn == NULL) goto stop; 616 if (m->m_pkthdr.len > (MHLEN & (~0x3))) { 617 MCLGET(mn, M_DONTWAIT); 618 if ((mn->m_flags & M_EXT) == 0) { 619 m_freem(mn); 620 goto stop; 621 } 622 } 623 mn->m_data = (void *)(((u_int32_t)mn->m_data + 0x3) & (~0x3)); 624 m_copydata(m, 0, m->m_pkthdr.len, mtod(mn, void *)); 625 mn->m_pkthdr.len = mn->m_len = m->m_pkthdr.len; 626 IFQ_DEQUEUE(&ifp->if_snd, m); 627 m_freem(m); 628 m = mn; 629 bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m, 630 BUS_DMA_NOWAIT); 631 } else { 632 IFQ_DEQUEUE(&ifp->if_snd, m); 633 } 634 635 #if NBPFILTER > 0 636 if (ifp->if_bpf) 637 bpf_mtap(ifp->if_bpf, m); 638 #endif /* NBPFILTER > 0 */ 639 640 nsegs = sc->txq[bi].m_dmamap->dm_nsegs; 641 segs = sc->txq[bi].m_dmamap->dm_segs; 642 bus_dmamap_sync(sc->sc_dmat, sc->txq[bi].m_dmamap, 0, 643 sc->txq[bi].m_dmamap->dm_mapsize, 644 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 645 646 /* XXX: This driver hasn't been tested w/nsegs > 1 */ 647 while (nsegs > 0) { 648 nsegs--; 649 sc->txq[bi].m = m; 650 sc->TXDQ[bi * 2] = segs->ds_addr; 651 if (nsegs == 0) 652 sc->TXDQ[bi * 2 + 1] = segs->ds_len | (bi << 16) | 653 (1 << 31); 654 else 655 sc->TXDQ[bi * 2 + 1] = segs->ds_len | (bi << 16); 656 segs++; 657 bi = (bi + 1) % TX_QLEN; 658 ndq++; 659 } 660 661 662 /* 663 * Enqueue another. Don't do more than half the available 664 * descriptors before telling the MAC about them 665 */ 666 if ((sc->TXDQ_avail - ndq) > 0 && ndq < TX_QLEN / 2) { 667 IFQ_POLL(&ifp->if_snd, m); 668 if (m != NULL) { 669 goto more; 670 } 671 } 672 stop: 673 if (ndq > 0) { 674 sc->TXDQ_avail -= ndq; 675 sc->TXDQ_cur = &sc->TXDQ[bi]; 676 CTRLPAGE_DMASYNC(0, TX_QLEN * 2 * sizeof(u_int32_t), 677 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 678 EPE_WRITE(TXDEnq, ndq); 679 } 680 681 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 682 goto start; 683 684 splx(s); 685 return; 686 } 687 688 static void 689 epe_ifwatchdog(ifp) 690 struct ifnet *ifp; 691 { 692 struct epe_softc *sc = (struct epe_softc *)ifp->if_softc; 693 694 if ((ifp->if_flags & IFF_RUNNING) == 0) 695 return; 696 printf("%s: device timeout, BMCtl = 0x%08x, BMSts = 0x%08x\n", 697 sc->sc_dev.dv_xname, EPE_READ(BMCtl), EPE_READ(BMSts)); 698 } 699 700 static int 701 epe_ifinit(ifp) 702 struct ifnet *ifp; 703 { 704 struct epe_softc *sc = ifp->if_softc; 705 int rc, s = splnet(); 706 707 callout_stop(&sc->epe_tick_ch); 708 EPE_WRITE(RXCtl, RXCtl_IA0|RXCtl_BA|RXCtl_RCRCA|RXCtl_SRxON); 709 EPE_WRITE(TXCtl, TXCtl_STxON); 710 EPE_WRITE(GIIntMsk, GIIntMsk_INT); /* start interrupting */ 711 712 if ((rc = mii_mediachg(&sc->sc_mii)) == ENXIO) 713 rc = 0; 714 else if (rc != 0) 715 goto out; 716 717 callout_reset(&sc->epe_tick_ch, hz, epe_tick, sc); 718 ifp->if_flags |= IFF_RUNNING; 719 out: 720 splx(s); 721 return 0; 722 } 723 724 static void 725 epe_ifstop(ifp, disable) 726 struct ifnet *ifp; 727 int disable; 728 { 729 struct epe_softc *sc = ifp->if_softc; 730 731 732 EPE_WRITE(RXCtl, 0); 733 EPE_WRITE(TXCtl, 0); 734 EPE_WRITE(GIIntMsk, 0); 735 callout_stop(&sc->epe_tick_ch); 736 737 /* Down the MII. */ 738 mii_down(&sc->sc_mii); 739 740 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 741 ifp->if_timer = 0; 742 sc->sc_mii.mii_media_status &= ~IFM_ACTIVE; 743 } 744 745 static void 746 epe_setaddr(ifp) 747 struct ifnet *ifp; 748 { 749 struct epe_softc *sc = ifp->if_softc; 750 struct ethercom *ac = &sc->sc_ec; 751 struct ether_multi *enm; 752 struct ether_multistep step; 753 u_int8_t ias[2][ETHER_ADDR_LEN]; 754 u_int32_t h, nma = 0, hashes[2] = { 0, 0 }; 755 u_int32_t rxctl = EPE_READ(RXCtl); 756 757 /* disable receiver temporarily */ 758 EPE_WRITE(RXCtl, rxctl & ~RXCtl_SRxON); 759 760 rxctl &= ~(RXCtl_MA|RXCtl_PA|RXCtl_IA2|RXCtl_IA3); 761 762 if (ifp->if_flags & IFF_PROMISC) { 763 rxctl |= RXCtl_PA; 764 } 765 766 ifp->if_flags &= ~IFF_ALLMULTI; 767 768 ETHER_FIRST_MULTI(step, ac, enm); 769 while (enm != NULL) { 770 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) { 771 /* 772 * We must listen to a range of multicast addresses. 773 * For now, just accept all multicasts, rather than 774 * trying to set only those filter bits needed to match 775 * the range. (At this time, the only use of address 776 * ranges is for IP multicast routing, for which the 777 * range is big enough to require all bits set.) 778 */ 779 rxctl &= ~(RXCtl_IA2|RXCtl_IA3); 780 rxctl |= RXCtl_MA; 781 hashes[0] = 0xffffffffUL; 782 hashes[1] = 0xffffffffUL; 783 ifp->if_flags |= IFF_ALLMULTI; 784 break; 785 } 786 787 if (nma < 2) { 788 /* We can program 2 perfect address filters for mcast */ 789 memcpy(ias[nma], enm->enm_addrlo, ETHER_ADDR_LEN); 790 rxctl |= (1 << (nma + 2)); 791 } else { 792 /* 793 * XXX: Datasheet is not very clear here, I'm not sure 794 * if I'm doing this right. --joff 795 */ 796 h = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN); 797 798 /* Just want the 6 most-significant bits. */ 799 h = h >> 26; 800 801 hashes[ h / 32 ] |= (1 << (h % 32)); 802 rxctl |= RXCtl_MA; 803 } 804 ETHER_NEXT_MULTI(step, enm); 805 nma++; 806 } 807 808 EPE_WRITE(AFP, 0); 809 bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd, 810 sc->sc_enaddr, ETHER_ADDR_LEN); 811 if (rxctl & RXCtl_IA2) { 812 EPE_WRITE(AFP, 2); 813 bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd, 814 ias[0], ETHER_ADDR_LEN); 815 } 816 if (rxctl & RXCtl_IA3) { 817 EPE_WRITE(AFP, 3); 818 bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd, 819 ias[1], ETHER_ADDR_LEN); 820 } 821 if (hashes[0] != 0 && hashes[1] != 0) { 822 EPE_WRITE(AFP, 7); 823 EPE_WRITE(HashTbl, hashes[0]); 824 EPE_WRITE(HashTbl + 4, hashes[1]); 825 } 826 EPE_WRITE(RXCtl, rxctl); 827 } 828