1 /* $NetBSD: if_aumac.c,v 1.12 2004/10/30 18:08:34 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 2001 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 /* 39 * Device driver for Alchemy Semiconductor Au1x00 Ethernet Media 40 * Access Controller. 41 * 42 * TODO: 43 * 44 * Better Rx buffer management; we want to get new Rx buffers 45 * to the chip more quickly than we currently do. 46 */ 47 48 #include <sys/cdefs.h> 49 __KERNEL_RCSID(0, "$NetBSD: if_aumac.c,v 1.12 2004/10/30 18:08:34 thorpej Exp $"); 50 51 #include "bpfilter.h" 52 53 #include <sys/param.h> 54 #include <sys/systm.h> 55 #include <sys/callout.h> 56 #include <sys/mbuf.h> 57 #include <sys/malloc.h> 58 #include <sys/kernel.h> 59 #include <sys/socket.h> 60 #include <sys/ioctl.h> 61 #include <sys/errno.h> 62 #include <sys/device.h> 63 #include <sys/queue.h> 64 65 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */ 66 67 #include <net/if.h> 68 #include <net/if_dl.h> 69 #include <net/if_media.h> 70 #include <net/if_ether.h> 71 72 #if NBPFILTER > 0 73 #include <net/bpf.h> 74 #endif 75 76 #include <machine/bus.h> 77 #include <machine/intr.h> 78 #include <machine/endian.h> 79 80 #include <dev/mii/mii.h> 81 #include <dev/mii/miivar.h> 82 83 #include <mips/alchemy/include/aureg.h> 84 #include <mips/alchemy/include/auvar.h> 85 #include <mips/alchemy/include/aubusvar.h> 86 #include <mips/alchemy/dev/if_aumacreg.h> 87 88 /* 89 * The Au1X00 MAC has 4 transmit and receive descriptors. Each buffer 90 * must consist of a single DMA segment, and must be aligned to a 2K 91 * boundary. Therefore, this driver does not perform DMA directly 92 * to/from mbufs. Instead, we copy the data to/from buffers allocated 93 * at device attach time. 94 * 95 * We also skip the bus_dma dance. The MAC is built in to the CPU, so 96 * there's little point in not making assumptions based on the CPU type. 97 * We also program the Au1X00 cache to be DMA coherent, so the buffers 98 * are accessed via KSEG0 addresses. 99 */ 100 #define AUMAC_NTXDESC 4 101 #define AUMAC_NTXDESC_MASK (AUMAC_NTXDESC - 1) 102 103 #define AUMAC_NRXDESC 4 104 #define AUMAC_NRXDESC_MASK (AUMAC_NRXDESC - 1) 105 106 #define AUMAC_NEXTTX(x) (((x) + 1) & AUMAC_NTXDESC_MASK) 107 #define AUMAC_NEXTRX(x) (((x) + 1) & AUMAC_NRXDESC_MASK) 108 109 #define AUMAC_TXBUF_OFFSET 0 110 #define AUMAC_RXBUF_OFFSET (MAC_BUFLEN * AUMAC_NTXDESC) 111 #define AUMAC_BUFSIZE (MAC_BUFLEN * (AUMAC_NTXDESC + AUMAC_NRXDESC)) 112 113 struct aumac_buf { 114 caddr_t buf_vaddr; /* virtual address of buffer */ 115 bus_addr_t buf_paddr; /* DMA address of buffer */ 116 }; 117 118 /* 119 * Software state per device. 120 */ 121 struct aumac_softc { 122 struct device sc_dev; /* generic device information */ 123 bus_space_tag_t sc_st; /* bus space tag */ 124 bus_space_handle_t sc_mac_sh; /* MAC space handle */ 125 bus_space_handle_t sc_macen_sh; /* MAC enable space handle */ 126 bus_space_handle_t sc_dma_sh; /* DMA space handle */ 127 struct ethercom sc_ethercom; /* Ethernet common data */ 128 void *sc_sdhook; /* shutdown hook */ 129 130 void *sc_ih; /* interrupt cookie */ 131 132 struct mii_data sc_mii; /* MII/media information */ 133 134 struct callout sc_tick_ch; /* tick callout */ 135 136 /* Transmit and receive buffers */ 137 struct aumac_buf sc_txbufs[AUMAC_NTXDESC]; 138 struct aumac_buf sc_rxbufs[AUMAC_NRXDESC]; 139 caddr_t sc_bufaddr; 140 141 int sc_txfree; /* number of free Tx descriptors */ 142 int sc_txnext; /* next Tx descriptor to use */ 143 int sc_txdirty; /* first dirty Tx descriptor */ 144 145 int sc_rxptr; /* next ready Rx descriptor */ 146 147 #ifdef AUMAC_EVENT_COUNTERS 148 struct evcnt sc_ev_txstall; /* Tx stalled */ 149 struct evcnt sc_ev_rxstall; /* Rx stalled */ 150 struct evcnt sc_ev_txintr; /* Tx interrupts */ 151 struct evcnt sc_ev_rxintr; /* Rx interrupts */ 152 #endif 153 154 uint32_t sc_control; /* MAC_CONTROL contents */ 155 uint32_t sc_flowctrl; /* MAC_FLOWCTRL contents */ 156 }; 157 158 #ifdef AUMAC_EVENT_COUNTERS 159 #define AUMAC_EVCNT_INCR(ev) (ev)->ev_count++ 160 #else 161 #define AUMAC_EVCNT_INCR(ev) /* nothing */ 162 #endif 163 164 #define AUMAC_INIT_RXDESC(sc, x) \ 165 do { \ 166 bus_space_write_4((sc)->sc_st, (sc)->sc_dma_sh, \ 167 MACDMA_RX_STAT((x)), 0); \ 168 bus_space_write_4((sc)->sc_st, (sc)->sc_dma_sh, \ 169 MACDMA_RX_ADDR((x)), \ 170 (sc)->sc_rxbufs[(x)].buf_paddr | RX_ADDR_EN); \ 171 } while (/*CONSTCOND*/0) 172 173 static void aumac_start(struct ifnet *); 174 static void aumac_watchdog(struct ifnet *); 175 static int aumac_ioctl(struct ifnet *, u_long, caddr_t); 176 static int aumac_init(struct ifnet *); 177 static void aumac_stop(struct ifnet *, int); 178 179 static void aumac_shutdown(void *); 180 181 static void aumac_tick(void *); 182 183 static void aumac_set_filter(struct aumac_softc *); 184 185 static void aumac_powerup(struct aumac_softc *); 186 static void aumac_powerdown(struct aumac_softc *); 187 188 static int aumac_intr(void *); 189 static void aumac_txintr(struct aumac_softc *); 190 static void aumac_rxintr(struct aumac_softc *); 191 192 static int aumac_mii_readreg(struct device *, int, int); 193 static void aumac_mii_writereg(struct device *, int, int, int); 194 static void aumac_mii_statchg(struct device *); 195 static int aumac_mii_wait(struct aumac_softc *, const char *); 196 197 static int aumac_mediachange(struct ifnet *); 198 static void aumac_mediastatus(struct ifnet *, struct ifmediareq *); 199 200 static int aumac_match(struct device *, struct cfdata *, void *); 201 static void aumac_attach(struct device *, struct device *, void *); 202 203 int aumac_copy_small = 0; 204 205 CFATTACH_DECL(aumac, sizeof(struct aumac_softc), 206 aumac_match, aumac_attach, NULL, NULL); 207 208 static int 209 aumac_match(struct device *parent, struct cfdata *cf, void *aux) 210 { 211 struct aubus_attach_args *aa = aux; 212 213 if (strcmp(aa->aa_name, cf->cf_name) == 0) 214 return (1); 215 216 return (0); 217 } 218 219 static void 220 aumac_attach(struct device *parent, struct device *self, void *aux) 221 { 222 uint8_t enaddr[ETHER_ADDR_LEN]; 223 struct aumac_softc *sc = (void *) self; 224 struct aubus_attach_args *aa = aux; 225 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 226 struct pglist pglist; 227 paddr_t bufaddr; 228 caddr_t vbufaddr; 229 int i; 230 231 callout_init(&sc->sc_tick_ch); 232 233 printf(": Au1X00 10/100 Ethernet\n"); 234 235 sc->sc_st = aa->aa_st; 236 237 /* Get the MAC address. */ 238 if (prop_get(dev_propdb, &sc->sc_dev, "mac-addr", enaddr, 239 sizeof(enaddr), NULL) != sizeof(enaddr)) { 240 printf("%s: unable to get mac-addr property\n", 241 sc->sc_dev.dv_xname); 242 return; 243 } 244 245 printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname, 246 ether_sprintf(enaddr)); 247 248 /* Map the device. */ 249 if (bus_space_map(sc->sc_st, aa->aa_addrs[AA_MAC_BASE], 250 MACx_SIZE, 0, &sc->sc_mac_sh) != 0) { 251 printf("%s: unable to map MAC registers\n", 252 sc->sc_dev.dv_xname); 253 return; 254 } 255 if (bus_space_map(sc->sc_st, aa->aa_addrs[AA_MAC_ENABLE], 256 MACENx_SIZE, 0, &sc->sc_macen_sh) != 0) { 257 printf("%s: unable to map MACEN registers\n", 258 sc->sc_dev.dv_xname); 259 return; 260 } 261 if (bus_space_map(sc->sc_st, aa->aa_addrs[AA_MAC_DMA_BASE], 262 MACx_DMA_SIZE, 0, &sc->sc_dma_sh) != 0) { 263 printf("%s: unable to map MACDMA registers\n", 264 sc->sc_dev.dv_xname); 265 return; 266 } 267 268 /* Make sure the MAC is powered off. */ 269 aumac_powerdown(sc); 270 271 /* Hook up the interrupt handler. */ 272 sc->sc_ih = au_intr_establish(aa->aa_irq[0], 1, IPL_NET, IST_LEVEL, 273 aumac_intr, sc); 274 if (sc->sc_ih == NULL) { 275 printf("%s: unable to register interrupt handler\n", 276 sc->sc_dev.dv_xname); 277 return; 278 } 279 280 /* 281 * Allocate space for the transmit and receive buffers. 282 */ 283 if (uvm_pglistalloc(AUMAC_BUFSIZE, 0, ctob(physmem), PAGE_SIZE, 0, 284 &pglist, 1, 0)) 285 return; 286 287 bufaddr = TAILQ_FIRST(&pglist)->phys_addr; 288 vbufaddr = (void *)MIPS_PHYS_TO_KSEG0(bufaddr); 289 290 for (i = 0; i < AUMAC_NTXDESC; i++) { 291 int offset = AUMAC_TXBUF_OFFSET + (i * MAC_BUFLEN); 292 293 sc->sc_txbufs[i].buf_vaddr = vbufaddr + offset; 294 sc->sc_txbufs[i].buf_paddr = bufaddr + offset; 295 } 296 297 for (i = 0; i < AUMAC_NRXDESC; i++) { 298 int offset = AUMAC_RXBUF_OFFSET + (i * MAC_BUFLEN); 299 300 sc->sc_rxbufs[i].buf_vaddr = vbufaddr + offset; 301 sc->sc_rxbufs[i].buf_paddr = bufaddr + offset; 302 } 303 304 /* 305 * Power up the MAC before accessing any MAC registers (including 306 * MII configuration. 307 */ 308 aumac_powerup(sc); 309 310 /* 311 * Initialize the media structures and probe the MII. 312 */ 313 sc->sc_mii.mii_ifp = ifp; 314 sc->sc_mii.mii_readreg = aumac_mii_readreg; 315 sc->sc_mii.mii_writereg = aumac_mii_writereg; 316 sc->sc_mii.mii_statchg = aumac_mii_statchg; 317 ifmedia_init(&sc->sc_mii.mii_media, 0, aumac_mediachange, 318 aumac_mediastatus); 319 320 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY, 321 MII_OFFSET_ANY, 0); 322 323 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) { 324 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL); 325 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE); 326 } else 327 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO); 328 329 strcpy(ifp->if_xname, sc->sc_dev.dv_xname); 330 ifp->if_softc = sc; 331 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 332 ifp->if_ioctl = aumac_ioctl; 333 ifp->if_start = aumac_start; 334 ifp->if_watchdog = aumac_watchdog; 335 ifp->if_init = aumac_init; 336 ifp->if_stop = aumac_stop; 337 IFQ_SET_READY(&ifp->if_snd); 338 339 /* Attach the interface. */ 340 if_attach(ifp); 341 ether_ifattach(ifp, enaddr); 342 343 #ifdef AUMAC_EVENT_COUNTERS 344 evcnt_attach_dynamic(&sc->sc_ev_txstall, EVCNT_TYPE_MISC, 345 NULL, sc->sc_dev.dv_xname, "txstall"); 346 evcnt_attach_dynamic(&sc->sc_ev_rxstall, EVCNT_TYPE_MISC, 347 NULL, sc->sc_dev.dv_xname, "rxstall"); 348 evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_MISC, 349 NULL, sc->sc_dev.dv_xname, "txintr"); 350 evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_MISC, 351 NULL, sc->sc_dev.dv_xname, "rxintr"); 352 #endif 353 354 /* Make sure the interface is shutdown during reboot. */ 355 sc->sc_sdhook = shutdownhook_establish(aumac_shutdown, sc); 356 if (sc->sc_sdhook == NULL) 357 printf("%s: WARNING: unable to establish shutdown hook\n", 358 sc->sc_dev.dv_xname); 359 return; 360 } 361 362 /* 363 * aumac_shutdown: 364 * 365 * Make sure the interface is stopped at reboot time. 366 */ 367 static void 368 aumac_shutdown(void *arg) 369 { 370 struct aumac_softc *sc = arg; 371 372 aumac_stop(&sc->sc_ethercom.ec_if, 1); 373 374 /* 375 * XXX aumac_stop leaves device powered up at the moment 376 * XXX but this still isn't enough to keep yamon happy... :-( 377 */ 378 bus_space_write_4(sc->sc_st, sc->sc_macen_sh, 0, 0); 379 } 380 381 /* 382 * aumac_start: [ifnet interface function] 383 * 384 * Start packet transmission on the interface. 385 */ 386 static void 387 aumac_start(struct ifnet *ifp) 388 { 389 struct aumac_softc *sc = ifp->if_softc; 390 struct mbuf *m; 391 int nexttx; 392 393 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING) 394 return; 395 396 /* 397 * Loop through the send queue, setting up transmit descriptors 398 * unitl we drain the queue, or use up all available transmit 399 * descriptors. 400 */ 401 for (;;) { 402 /* Grab a packet off the queue. */ 403 IFQ_POLL(&ifp->if_snd, m); 404 if (m == NULL) 405 return; 406 407 /* Get a spare descriptor. */ 408 if (sc->sc_txfree == 0) { 409 /* No more slots left; notify upper layer. */ 410 ifp->if_flags |= IFF_OACTIVE; 411 AUMAC_EVCNT_INCR(&sc->sc_ev_txstall); 412 return; 413 } 414 nexttx = sc->sc_txnext; 415 416 IFQ_DEQUEUE(&ifp->if_snd, m); 417 418 /* 419 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET. 420 */ 421 422 m_copydata(m, 0, m->m_pkthdr.len, 423 sc->sc_txbufs[nexttx].buf_vaddr); 424 425 /* Zero out the remainder of any short packets. */ 426 if (m->m_pkthdr.len < (ETHER_MIN_LEN - ETHER_CRC_LEN)) 427 memset(sc->sc_txbufs[nexttx].buf_vaddr + 428 m->m_pkthdr.len, 0, 429 ETHER_MIN_LEN - ETHER_CRC_LEN - m->m_pkthdr.len); 430 431 bus_space_write_4(sc->sc_st, sc->sc_dma_sh, 432 MACDMA_TX_STAT(nexttx), 0); 433 bus_space_write_4(sc->sc_st, sc->sc_dma_sh, 434 MACDMA_TX_LEN(nexttx), 435 m->m_pkthdr.len < (ETHER_MIN_LEN - ETHER_CRC_LEN) ? 436 ETHER_MIN_LEN - ETHER_CRC_LEN : m->m_pkthdr.len); 437 bus_space_write_4(sc->sc_st, sc->sc_dma_sh, 438 MACDMA_TX_ADDR(nexttx), 439 sc->sc_txbufs[nexttx].buf_paddr | TX_ADDR_EN); 440 /* XXX - needed?? we should be coherent */ 441 bus_space_barrier(sc->sc_st, sc->sc_dma_sh, 0 /* XXX */, 442 0 /* XXX */, BUS_SPACE_BARRIER_WRITE); 443 444 /* Advance the Tx pointer. */ 445 sc->sc_txfree--; 446 sc->sc_txnext = AUMAC_NEXTTX(nexttx); 447 448 #if NBPFILTER > 0 449 /* Pass the packet to any BPF listeners. */ 450 if (ifp->if_bpf) 451 bpf_mtap(ifp->if_bpf, m); 452 #endif /* NBPFILTER */ 453 454 m_freem(m); 455 456 /* Set a watchdog timer in case the chip flakes out. */ 457 ifp->if_timer = 5; 458 } 459 /* NOTREACHED */ 460 } 461 462 /* 463 * aumac_watchdog: [ifnet interface function] 464 * 465 * Watchdog timer handler. 466 */ 467 static void 468 aumac_watchdog(struct ifnet *ifp) 469 { 470 struct aumac_softc *sc = ifp->if_softc; 471 472 printf("%s: device timeout\n", sc->sc_dev.dv_xname); 473 (void) aumac_init(ifp); 474 475 /* Try to get more packets going. */ 476 aumac_start(ifp); 477 } 478 479 /* 480 * aumac_ioctl: [ifnet interface function] 481 * 482 * Handle control requests from the operator. 483 */ 484 static int 485 aumac_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 486 { 487 struct aumac_softc *sc = ifp->if_softc; 488 struct ifreq *ifr = (struct ifreq *) data; 489 int s, error; 490 491 s = splnet(); 492 493 switch (cmd) { 494 case SIOCSIFMEDIA: 495 case SIOCGIFMEDIA: 496 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd); 497 break; 498 499 default: 500 error = ether_ioctl(ifp, cmd, data); 501 if (error == ENETRESET) { 502 /* 503 * Multicast list has changed; set the hardware filter 504 * accordingly. 505 */ 506 if (ifp->if_flags & IFF_RUNNING) 507 aumac_set_filter(sc); 508 } 509 break; 510 } 511 512 /* Try to get more packets going. */ 513 aumac_start(ifp); 514 515 splx(s); 516 return (error); 517 } 518 519 /* 520 * aumac_intr: 521 * 522 * Interrupt service routine. 523 */ 524 static int 525 aumac_intr(void *arg) 526 { 527 struct aumac_softc *sc = arg; 528 529 /* 530 * There aren't really any interrupt status bits on the 531 * Au1X00 MAC, and each MAC has a dedicated interrupt 532 * in the CPU's built-in interrupt controller. Just 533 * check for new incoming packets, and then Tx completions 534 * (for status updating). 535 */ 536 if ((sc->sc_ethercom.ec_if.if_flags & IFF_RUNNING) == 0) 537 return (0); 538 539 aumac_rxintr(sc); 540 aumac_txintr(sc); 541 542 return (1); 543 } 544 545 /* 546 * aumac_txintr: 547 * 548 * Helper; handle transmit interrupts. 549 */ 550 static void 551 aumac_txintr(struct aumac_softc *sc) 552 { 553 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 554 uint32_t stat; 555 int i; 556 #ifdef AUMAC_EVENT_COUNTERS 557 int gotone = 0; 558 #endif 559 560 for (i = sc->sc_txdirty; sc->sc_txfree != AUMAC_NTXDESC; 561 i = AUMAC_NEXTTX(i)) { 562 if ((bus_space_read_4(sc->sc_st, sc->sc_dma_sh, 563 MACDMA_TX_ADDR(i)) & TX_ADDR_DN) == 0) 564 break; 565 #ifdef AUMAC_EVENT_COUNTERS 566 gotone = 1; 567 #endif 568 569 /* ACK interrupt. */ 570 bus_space_write_4(sc->sc_st, sc->sc_dma_sh, 571 MACDMA_TX_ADDR(i), 0); 572 573 stat = bus_space_read_4(sc->sc_st, sc->sc_dma_sh, 574 MACDMA_TX_STAT(i)); 575 576 if (stat & TX_STAT_FA) { 577 /* XXX STATS */ 578 ifp->if_oerrors++; 579 } else 580 ifp->if_opackets++; 581 582 if (stat & TX_STAT_EC) 583 ifp->if_collisions += 16; 584 else 585 ifp->if_collisions += TX_STAT_CC(stat); 586 587 sc->sc_txfree++; 588 ifp->if_flags &= ~IFF_OACTIVE; 589 590 /* Try to queue more packets. */ 591 aumac_start(ifp); 592 } 593 594 if (gotone) 595 AUMAC_EVCNT_INCR(&sc->sc_ev_txintr); 596 597 /* Update the dirty descriptor pointer. */ 598 sc->sc_txdirty = i; 599 600 /* 601 * If there are no more pending transmissions, cancel the watchdog 602 * timer. 603 */ 604 if (sc->sc_txfree == AUMAC_NTXDESC) 605 ifp->if_timer = 0; 606 } 607 608 /* 609 * aumac_rxintr: 610 * 611 * Helper; handle receive interrupts. 612 */ 613 static void 614 aumac_rxintr(struct aumac_softc *sc) 615 { 616 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 617 struct mbuf *m; 618 uint32_t stat; 619 int i, len; 620 #ifdef AUMAC_EVENT_COUNTERS 621 int pkts = 0; 622 #endif 623 624 for (i = sc->sc_rxptr;; i = AUMAC_NEXTRX(i)) { 625 if ((bus_space_read_4(sc->sc_st, sc->sc_dma_sh, 626 MACDMA_RX_ADDR(i)) & RX_ADDR_DN) == 0) 627 break; 628 #ifdef AUMAC_EVENT_COUNTERS 629 pkts++; 630 #endif 631 632 stat = bus_space_read_4(sc->sc_st, sc->sc_dma_sh, 633 MACDMA_RX_STAT(i)); 634 635 #define PRINTERR(str) \ 636 do { \ 637 error++; \ 638 printf("%s: %s\n", sc->sc_dev.dv_xname, str); \ 639 } while (0) 640 641 if (stat & RX_STAT_ERRS) { 642 int error = 0; 643 644 if (stat & RX_STAT_MI) 645 PRINTERR("missed frame"); 646 if (stat & RX_STAT_UC) 647 PRINTERR("unknown control frame"); 648 if (stat & RX_STAT_LE) 649 PRINTERR("short frame"); 650 if (stat & RX_STAT_CR) 651 PRINTERR("CRC error"); 652 if (stat & RX_STAT_ME) 653 PRINTERR("medium error"); 654 if (stat & RX_STAT_CS) 655 PRINTERR("late collision"); 656 if (stat & RX_STAT_FL) 657 PRINTERR("frame too big"); 658 if (stat & RX_STAT_RF) 659 PRINTERR("runt frame (collision)"); 660 if (stat & RX_STAT_WT) 661 PRINTERR("watch dog"); 662 if (stat & RX_STAT_DB) { 663 if (stat & (RX_STAT_CS | RX_STAT_RF | 664 RX_STAT_CR)) { 665 if (!error) 666 goto pktok; 667 } else 668 PRINTERR("dribbling bit"); 669 } 670 #undef PRINTERR 671 ifp->if_ierrors++; 672 673 dropit: 674 /* reuse the current descriptor */ 675 AUMAC_INIT_RXDESC(sc, i); 676 continue; 677 } 678 pktok: 679 len = RX_STAT_L(stat); 680 681 /* 682 * The Au1X00 MAC includes the CRC with every packet; 683 * trim it off here. 684 */ 685 len -= ETHER_CRC_LEN; 686 687 /* 688 * Truncate the packet if it's too big to fit in 689 * a single mbuf cluster. 690 */ 691 if (len > MCLBYTES - 2) 692 len = MCLBYTES - 2; 693 694 MGETHDR(m, M_DONTWAIT, MT_DATA); 695 if (m == NULL) { 696 printf("%s: unable to allocate Rx mbuf\n", 697 sc->sc_dev.dv_xname); 698 goto dropit; 699 } 700 if (len > MHLEN - 2) { 701 MCLGET(m, M_DONTWAIT); 702 if ((m->m_flags & M_EXT) == 0) { 703 printf("%s: unable to allocate Rx cluster\n", 704 sc->sc_dev.dv_xname); 705 m_freem(m); 706 goto dropit; 707 } 708 } 709 710 m->m_data += 2; /* align payload */ 711 memcpy(mtod(m, caddr_t), 712 sc->sc_rxbufs[i].buf_vaddr, len); 713 AUMAC_INIT_RXDESC(sc, i); 714 715 m->m_pkthdr.rcvif = ifp; 716 m->m_pkthdr.len = m->m_len = len; 717 718 #if NBPFILTER > 0 719 /* Pass this up to any BPF listeners. */ 720 if (ifp->if_bpf) 721 bpf_mtap(ifp->if_bpf, m); 722 #endif /* NBPFILTER > 0 */ 723 724 /* Pass it on. */ 725 (*ifp->if_input)(ifp, m); 726 ifp->if_ipackets++; 727 } 728 if (pkts) 729 AUMAC_EVCNT_INCR(&sc->sc_ev_rxintr); 730 if (pkts == AUMAC_NRXDESC) 731 AUMAC_EVCNT_INCR(&sc->sc_ev_rxstall); 732 733 /* Update the receive pointer. */ 734 sc->sc_rxptr = i; 735 } 736 737 /* 738 * aumac_tick: 739 * 740 * One second timer, used to tick the MII. 741 */ 742 static void 743 aumac_tick(void *arg) 744 { 745 struct aumac_softc *sc = arg; 746 int s; 747 748 s = splnet(); 749 mii_tick(&sc->sc_mii); 750 splx(s); 751 752 callout_reset(&sc->sc_tick_ch, hz, aumac_tick, sc); 753 } 754 755 /* 756 * aumac_init: [ifnet interface function] 757 * 758 * Initialize the interface. Must be called at splnet(). 759 */ 760 static int 761 aumac_init(struct ifnet *ifp) 762 { 763 struct aumac_softc *sc = ifp->if_softc; 764 int i, error = 0; 765 766 /* Cancel any pending I/O, reset MAC. */ 767 aumac_stop(ifp, 0); 768 769 /* Set up the transmit ring. */ 770 for (i = 0; i < AUMAC_NTXDESC; i++) { 771 bus_space_write_4(sc->sc_st, sc->sc_dma_sh, 772 MACDMA_TX_STAT(i), 0); 773 bus_space_write_4(sc->sc_st, sc->sc_dma_sh, 774 MACDMA_TX_LEN(i), 0); 775 bus_space_write_4(sc->sc_st, sc->sc_dma_sh, 776 MACDMA_TX_ADDR(i), sc->sc_txbufs[i].buf_paddr); 777 } 778 sc->sc_txfree = AUMAC_NTXDESC; 779 sc->sc_txnext = TX_ADDR_CB(bus_space_read_4(sc->sc_st, sc->sc_dma_sh, 780 MACDMA_TX_ADDR(0))); 781 sc->sc_txdirty = sc->sc_txnext; 782 783 /* Set up the receive ring. */ 784 for (i = 0; i < AUMAC_NRXDESC; i++) 785 AUMAC_INIT_RXDESC(sc, i); 786 sc->sc_rxptr = RX_ADDR_CB(bus_space_read_4(sc->sc_st, sc->sc_dma_sh, 787 MACDMA_RX_ADDR(0))); 788 789 /* 790 * Power up the MAC. 791 */ 792 aumac_powerup(sc); 793 794 sc->sc_control |= CONTROL_DO | CONTROL_TE | CONTROL_RE; 795 #if _BYTE_ORDER == _BIG_ENDIAN 796 sc->sc_control |= CONTROL_EM; 797 #endif 798 799 /* Set the media. */ 800 aumac_mediachange(ifp); 801 802 /* 803 * Set the receive filter. This will actually start the transmit 804 * and receive processes. 805 */ 806 aumac_set_filter(sc); 807 808 /* Start the one second clock. */ 809 callout_reset(&sc->sc_tick_ch, hz, aumac_tick, sc); 810 811 /* ...all done! */ 812 ifp->if_flags |= IFF_RUNNING; 813 ifp->if_flags &= ~IFF_OACTIVE; 814 815 if (error) 816 printf("%s: interface not running\n", sc->sc_dev.dv_xname); 817 return (error); 818 } 819 820 /* 821 * aumac_stop: [ifnet interface function] 822 * 823 * Stop transmission on the interface. 824 */ 825 static void 826 aumac_stop(struct ifnet *ifp, int disable) 827 { 828 struct aumac_softc *sc = ifp->if_softc; 829 830 /* Stop the one-second clock. */ 831 callout_stop(&sc->sc_tick_ch); 832 833 /* Down the MII. */ 834 mii_down(&sc->sc_mii); 835 836 /* Stop the transmit and receive processes. */ 837 bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_CONTROL, 0); 838 839 /* Power down/reset the MAC. */ 840 aumac_powerdown(sc); 841 842 /* Mark the interface as down and cancel the watchdog timer. */ 843 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 844 ifp->if_timer = 0; 845 } 846 847 /* 848 * aumac_powerdown: 849 * 850 * Power down the MAC. 851 */ 852 static void 853 aumac_powerdown(struct aumac_softc *sc) 854 { 855 856 /* Disable the MAC clocks, and place the device in reset. */ 857 // bus_space_write_4(sc->sc_st, sc->sc_macen_sh, 0, MACEN_JP); 858 859 // delay(10000); 860 } 861 862 /* 863 * aumac_powerup: 864 * 865 * Bring the device out of reset. 866 */ 867 static void 868 aumac_powerup(struct aumac_softc *sc) 869 { 870 871 /* Enable clocks to the MAC. */ 872 bus_space_write_4(sc->sc_st, sc->sc_macen_sh, 0, MACEN_JP|MACEN_CE); 873 874 /* Enable MAC, coherent transactions, pass only valid frames. */ 875 bus_space_write_4(sc->sc_st, sc->sc_macen_sh, 0, 876 MACEN_E2|MACEN_E1|MACEN_E0|MACEN_CE); 877 878 delay(20000); 879 } 880 881 /* 882 * aumac_set_filter: 883 * 884 * Set up the receive filter. 885 */ 886 static void 887 aumac_set_filter(struct aumac_softc *sc) 888 { 889 struct ethercom *ec = &sc->sc_ethercom; 890 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 891 struct ether_multi *enm; 892 struct ether_multistep step; 893 const uint8_t *enaddr = LLADDR(ifp->if_sadl); 894 uint32_t mchash[2], crc; 895 896 sc->sc_control &= ~(CONTROL_PM | CONTROL_PR); 897 898 /* Stop the receiver. */ 899 bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_CONTROL, 900 sc->sc_control & ~CONTROL_RE); 901 902 if (ifp->if_flags & IFF_PROMISC) { 903 sc->sc_control |= CONTROL_PR; 904 goto allmulti; 905 } 906 907 /* Set the station address. */ 908 bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_ADDRHIGH, 909 enaddr[4] | (enaddr[5] << 8)); 910 bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_ADDRLOW, 911 enaddr[0] | (enaddr[1] << 8) | (enaddr[2] << 16) | 912 (enaddr[3] << 24)); 913 914 sc->sc_control |= CONTROL_HP; 915 916 mchash[0] = mchash[1] = 0; 917 918 /* 919 * Set up the multicast address filter by passing all multicast 920 * addresses through a CRC generator, and then using the high 921 * order 6 bits as an index into the 64-bit multicast hash table. 922 * The high order bits select the word, while the rest of the bits 923 * select the bit within the word. 924 */ 925 ETHER_FIRST_MULTI(step, ec, enm); 926 while (enm != NULL) { 927 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) { 928 /* 929 * We must listen to a range of multicast addresses. 930 * For now, just accept all multicasts, rather than 931 * trying to set only those filter bits needed to match 932 * the range. (At this time, the only use of address 933 * ranges is for IP multicast routing, for which the 934 * range is large enough to require all bits set.) 935 */ 936 goto allmulti; 937 } 938 939 crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN); 940 941 /* Just want the 6 most significant bits. */ 942 crc >>= 26; 943 944 /* Set the corresponding bit in the filter. */ 945 mchash[crc >> 5] |= 1U << (crc & 0x1f); 946 947 ETHER_NEXT_MULTI(step, enm); 948 } 949 950 ifp->if_flags &= ~IFF_ALLMULTI; 951 952 bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_HASHHIGH, 953 mchash[1]); 954 bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_HASHLOW, 955 mchash[0]); 956 957 bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_CONTROL, 958 sc->sc_control); 959 return; 960 961 allmulti: 962 sc->sc_control |= CONTROL_PM; 963 bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_CONTROL, 964 sc->sc_control); 965 } 966 967 /* 968 * aumac_mediastatus: [ifmedia interface function] 969 * 970 * Get the current interface media status. 971 */ 972 static void 973 aumac_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 974 { 975 struct aumac_softc *sc = ifp->if_softc; 976 977 mii_pollstat(&sc->sc_mii); 978 ifmr->ifm_status = sc->sc_mii.mii_media_status; 979 ifmr->ifm_active = sc->sc_mii.mii_media_active; 980 } 981 982 /* 983 * aumac_mediachange: [ifmedia interface function] 984 * 985 * Set hardware to newly selected media. 986 */ 987 static int 988 aumac_mediachange(struct ifnet *ifp) 989 { 990 struct aumac_softc *sc = ifp->if_softc; 991 992 if (ifp->if_flags & IFF_UP) 993 mii_mediachg(&sc->sc_mii); 994 return (0); 995 } 996 997 /* 998 * aumac_mii_wait: 999 * 1000 * Wait for the MII interface to not be busy. 1001 */ 1002 static int 1003 aumac_mii_wait(struct aumac_softc *sc, const char *msg) 1004 { 1005 int i; 1006 1007 for (i = 0; i < 10000; i++) { 1008 if ((bus_space_read_4(sc->sc_st, sc->sc_mac_sh, 1009 MAC_MIICTRL) & MIICTRL_MB) == 0) 1010 return (0); 1011 delay(10); 1012 } 1013 1014 printf("%s: MII failed to %s\n", sc->sc_dev.dv_xname, msg); 1015 return (1); 1016 } 1017 1018 /* 1019 * aumac_mii_readreg: [mii interface function] 1020 * 1021 * Read a PHY register on the MII. 1022 */ 1023 static int 1024 aumac_mii_readreg(struct device *self, int phy, int reg) 1025 { 1026 struct aumac_softc *sc = (void *) self; 1027 1028 if (aumac_mii_wait(sc, "become ready")) 1029 return (0); 1030 1031 bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_MIICTRL, 1032 MIICTRL_PHYADDR(phy) | MIICTRL_MIIREG(reg)); 1033 1034 if (aumac_mii_wait(sc, "complete")) 1035 return (0); 1036 1037 return (bus_space_read_4(sc->sc_st, sc->sc_mac_sh, MAC_MIIDATA) & 1038 MIIDATA_MASK); 1039 } 1040 1041 /* 1042 * aumac_mii_writereg: [mii interface function] 1043 * 1044 * Write a PHY register on the MII. 1045 */ 1046 static void 1047 aumac_mii_writereg(struct device *self, int phy, int reg, int val) 1048 { 1049 struct aumac_softc *sc = (void *) self; 1050 1051 if (aumac_mii_wait(sc, "become ready")) 1052 return; 1053 1054 bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_MIIDATA, val); 1055 bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_MIICTRL, 1056 MIICTRL_PHYADDR(phy) | MIICTRL_MIIREG(reg) | MIICTRL_MW); 1057 1058 (void) aumac_mii_wait(sc, "complete"); 1059 } 1060 1061 /* 1062 * aumac_mii_statchg: [mii interface function] 1063 * 1064 * Callback from MII layer when media changes. 1065 */ 1066 static void 1067 aumac_mii_statchg(struct device *self) 1068 { 1069 struct aumac_softc *sc = (void *) self; 1070 1071 if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0) 1072 sc->sc_control |= CONTROL_F; 1073 else 1074 sc->sc_control &= ~CONTROL_F; 1075 1076 bus_space_write_4(sc->sc_st, sc->sc_mac_sh, MAC_CONTROL, 1077 sc->sc_control); 1078 } 1079