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