if_gem.c revision 106937
1/* 2 * Copyright (C) 2001 Eduardo Horvath. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * from: NetBSD: gem.c,v 1.21 2002/06/01 23:50:58 lukem Exp 27 * 28 * $FreeBSD: head/sys/dev/gem/if_gem.c 106937 2002-11-14 23:54:55Z sam $ 29 */ 30 31/* 32 * Driver for Sun GEM ethernet controllers. 33 */ 34 35#define GEM_DEBUG 36 37#include <sys/param.h> 38#include <sys/systm.h> 39#include <sys/bus.h> 40#include <sys/callout.h> 41#include <sys/endian.h> 42#include <sys/mbuf.h> 43#include <sys/malloc.h> 44#include <sys/kernel.h> 45#include <sys/socket.h> 46#include <sys/sockio.h> 47 48#include <net/bpf.h> 49#include <net/ethernet.h> 50#include <net/if.h> 51#include <net/if_arp.h> 52#include <net/if_dl.h> 53#include <net/if_media.h> 54 55#include <machine/bus.h> 56 57#include <dev/mii/mii.h> 58#include <dev/mii/miivar.h> 59 60#include <gem/if_gemreg.h> 61#include <gem/if_gemvar.h> 62 63#define TRIES 10000 64 65static void gem_start(struct ifnet *); 66static void gem_stop(struct ifnet *, int); 67static int gem_ioctl(struct ifnet *, u_long, caddr_t); 68static void gem_cddma_callback(void *, bus_dma_segment_t *, int, int); 69static void gem_rxdma_callback(void *, bus_dma_segment_t *, int, int); 70static void gem_txdma_callback(void *, bus_dma_segment_t *, int, int); 71static void gem_tick(void *); 72static void gem_watchdog(struct ifnet *); 73static void gem_init(void *); 74static void gem_init_regs(struct gem_softc *sc); 75static int gem_ringsize(int sz); 76static int gem_meminit(struct gem_softc *); 77static int gem_dmamap_load_mbuf(struct gem_softc *, struct mbuf *, 78 bus_dmamap_callback_t *, struct gem_txjob *, int); 79static void gem_dmamap_unload_mbuf(struct gem_softc *, struct gem_txjob *); 80static void gem_dmamap_commit_mbuf(struct gem_softc *, struct gem_txjob *); 81static void gem_mifinit(struct gem_softc *); 82static int gem_bitwait(struct gem_softc *sc, bus_addr_t r, 83 u_int32_t clr, u_int32_t set); 84static int gem_reset_rx(struct gem_softc *); 85static int gem_reset_tx(struct gem_softc *); 86static int gem_disable_rx(struct gem_softc *); 87static int gem_disable_tx(struct gem_softc *); 88static void gem_rxdrain(struct gem_softc *); 89static int gem_add_rxbuf(struct gem_softc *, int); 90static void gem_setladrf(struct gem_softc *); 91 92struct mbuf *gem_get(struct gem_softc *, int, int); 93static void gem_eint(struct gem_softc *, u_int); 94static void gem_rint(struct gem_softc *); 95#if 0 96static void gem_rint_timeout(void *); 97#endif 98static void gem_tint(struct gem_softc *); 99#ifdef notyet 100static void gem_power(int, void *); 101#endif 102 103devclass_t gem_devclass; 104DRIVER_MODULE(miibus, gem, miibus_driver, miibus_devclass, 0, 0); 105MODULE_DEPEND(gem, miibus, 1, 1, 1); 106 107#ifdef GEM_DEBUG 108#define DPRINTF(sc, x) if ((sc)->sc_arpcom.ac_if.if_flags & IFF_DEBUG) \ 109 printf x 110#include <sys/ktr.h> 111#define KTR_GEM KTR_CT2 112#else 113#define DPRINTF(sc, x) /* nothing */ 114#endif 115 116#define GEM_NSEGS GEM_NTXSEGS 117 118/* 119 * gem_attach: 120 * 121 * Attach a Gem interface to the system. 122 */ 123int 124gem_attach(sc) 125 struct gem_softc *sc; 126{ 127 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 128 struct mii_softc *child; 129 int i, error; 130 u_int32_t v; 131 132 /* Make sure the chip is stopped. */ 133 ifp->if_softc = sc; 134 gem_reset(sc); 135 136 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT, 137 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, GEM_NSEGS, 138 BUS_SPACE_MAXSIZE_32BIT, 0, &sc->sc_pdmatag); 139 if (error) 140 return (error); 141 142 error = bus_dma_tag_create(sc->sc_pdmatag, 1, 0, 143 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MAXBSIZE, 144 GEM_NSEGS, BUS_SPACE_MAXSIZE_32BIT, BUS_DMA_ALLOCNOW, 145 &sc->sc_dmatag); 146 if (error) 147 goto fail_0; 148 149 error = bus_dma_tag_create(sc->sc_pdmatag, PAGE_SIZE, 0, 150 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 151 sizeof(struct gem_control_data), 1, 152 sizeof(struct gem_control_data), BUS_DMA_ALLOCNOW, 153 &sc->sc_cdmatag); 154 if (error) 155 goto fail_1; 156 157 /* 158 * Allocate the control data structures, and create and load the 159 * DMA map for it. 160 */ 161 if ((error = bus_dmamem_alloc(sc->sc_cdmatag, 162 (void **)&sc->sc_control_data, 0, &sc->sc_cddmamap))) { 163 device_printf(sc->sc_dev, "unable to allocate control data," 164 " error = %d\n", error); 165 goto fail_2; 166 } 167 168 sc->sc_cddma = 0; 169 if ((error = bus_dmamap_load(sc->sc_cdmatag, sc->sc_cddmamap, 170 sc->sc_control_data, sizeof(struct gem_control_data), 171 gem_cddma_callback, sc, 0)) != 0 || sc->sc_cddma == 0) { 172 device_printf(sc->sc_dev, "unable to load control data DMA " 173 "map, error = %d\n", error); 174 goto fail_3; 175 } 176 177 /* 178 * Initialize the transmit job descriptors. 179 */ 180 STAILQ_INIT(&sc->sc_txfreeq); 181 STAILQ_INIT(&sc->sc_txdirtyq); 182 183 /* 184 * Create the transmit buffer DMA maps. 185 */ 186 error = ENOMEM; 187 for (i = 0; i < GEM_TXQUEUELEN; i++) { 188 struct gem_txsoft *txs; 189 190 txs = &sc->sc_txsoft[i]; 191 txs->txs_mbuf = NULL; 192 txs->txs_ndescs = 0; 193 if ((error = bus_dmamap_create(sc->sc_dmatag, 0, 194 &txs->txs_dmamap)) != 0) { 195 device_printf(sc->sc_dev, "unable to create tx DMA map " 196 "%d, error = %d\n", i, error); 197 goto fail_4; 198 } 199 STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 200 } 201 202 /* 203 * Create the receive buffer DMA maps. 204 */ 205 for (i = 0; i < GEM_NRXDESC; i++) { 206 if ((error = bus_dmamap_create(sc->sc_dmatag, 0, 207 &sc->sc_rxsoft[i].rxs_dmamap)) != 0) { 208 device_printf(sc->sc_dev, "unable to create rx DMA map " 209 "%d, error = %d\n", i, error); 210 goto fail_5; 211 } 212 sc->sc_rxsoft[i].rxs_mbuf = NULL; 213 } 214 215 216 gem_mifinit(sc); 217 218 if ((error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus, gem_mediachange, 219 gem_mediastatus)) != 0) { 220 device_printf(sc->sc_dev, "phy probe failed: %d\n", error); 221 goto fail_5; 222 } 223 sc->sc_mii = device_get_softc(sc->sc_miibus); 224 225 /* 226 * From this point forward, the attachment cannot fail. A failure 227 * before this point releases all resources that may have been 228 * allocated. 229 */ 230 231 /* Announce ourselves. */ 232 device_printf(sc->sc_dev, "Ethernet address:"); 233 for (i = 0; i < 6; i++) 234 printf("%c%02x", i > 0 ? ':' : ' ', sc->sc_arpcom.ac_enaddr[i]); 235 236 /* Get RX FIFO size */ 237 sc->sc_rxfifosize = 64 * 238 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_RX_FIFO_SIZE); 239 printf(", %uKB RX fifo", sc->sc_rxfifosize / 1024); 240 241 /* Get TX FIFO size */ 242 v = bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_FIFO_SIZE); 243 printf(", %uKB TX fifo\n", v / 16); 244 245 /* Initialize ifnet structure. */ 246 ifp->if_softc = sc; 247 ifp->if_unit = device_get_unit(sc->sc_dev); 248 ifp->if_name = "gem"; 249 ifp->if_mtu = ETHERMTU; 250 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 251 ifp->if_start = gem_start; 252 ifp->if_ioctl = gem_ioctl; 253 ifp->if_watchdog = gem_watchdog; 254 ifp->if_init = gem_init; 255 ifp->if_output = ether_output; 256 ifp->if_snd.ifq_maxlen = GEM_TXQUEUELEN; 257 /* 258 * Walk along the list of attached MII devices and 259 * establish an `MII instance' to `phy number' 260 * mapping. We'll use this mapping in media change 261 * requests to determine which phy to use to program 262 * the MIF configuration register. 263 */ 264 for (child = LIST_FIRST(&sc->sc_mii->mii_phys); child != NULL; 265 child = LIST_NEXT(child, mii_list)) { 266 /* 267 * Note: we support just two PHYs: the built-in 268 * internal device and an external on the MII 269 * connector. 270 */ 271 if (child->mii_phy > 1 || child->mii_inst > 1) { 272 device_printf(sc->sc_dev, "cannot accomodate " 273 "MII device %s at phy %d, instance %d\n", 274 device_get_name(child->mii_dev), 275 child->mii_phy, child->mii_inst); 276 continue; 277 } 278 279 sc->sc_phys[child->mii_inst] = child->mii_phy; 280 } 281 282 /* 283 * Now select and activate the PHY we will use. 284 * 285 * The order of preference is External (MDI1), 286 * Internal (MDI0), Serial Link (no MII). 287 */ 288 if (sc->sc_phys[1]) { 289#ifdef GEM_DEBUG 290 printf("using external phy\n"); 291#endif 292 sc->sc_mif_config |= GEM_MIF_CONFIG_PHY_SEL; 293 } else { 294#ifdef GEM_DEBUG 295 printf("using internal phy\n"); 296#endif 297 sc->sc_mif_config &= ~GEM_MIF_CONFIG_PHY_SEL; 298 } 299 bus_space_write_4(sc->sc_bustag, sc->sc_h, GEM_MIF_CONFIG, 300 sc->sc_mif_config); 301 /* Attach the interface. */ 302 ether_ifattach(ifp, sc->sc_arpcom.ac_enaddr); 303 304#if notyet 305 /* 306 * Add a suspend hook to make sure we come back up after a 307 * resume. 308 */ 309 sc->sc_powerhook = powerhook_establish(gem_power, sc); 310 if (sc->sc_powerhook == NULL) 311 device_printf(sc->sc_dev, "WARNING: unable to establish power " 312 "hook\n"); 313#endif 314 315 callout_init(&sc->sc_tick_ch, 0); 316 callout_init(&sc->sc_rx_ch, 0); 317 return (0); 318 319 /* 320 * Free any resources we've allocated during the failed attach 321 * attempt. Do this in reverse order and fall through. 322 */ 323fail_5: 324 for (i = 0; i < GEM_NRXDESC; i++) { 325 if (sc->sc_rxsoft[i].rxs_dmamap != NULL) 326 bus_dmamap_destroy(sc->sc_dmatag, 327 sc->sc_rxsoft[i].rxs_dmamap); 328 } 329fail_4: 330 for (i = 0; i < GEM_TXQUEUELEN; i++) { 331 if (sc->sc_txsoft[i].txs_dmamap != NULL) 332 bus_dmamap_destroy(sc->sc_dmatag, 333 sc->sc_txsoft[i].txs_dmamap); 334 } 335 bus_dmamap_unload(sc->sc_dmatag, sc->sc_cddmamap); 336fail_3: 337 bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data, 338 sc->sc_cddmamap); 339fail_2: 340 bus_dma_tag_destroy(sc->sc_cdmatag); 341fail_1: 342 bus_dma_tag_destroy(sc->sc_dmatag); 343fail_0: 344 bus_dma_tag_destroy(sc->sc_pdmatag); 345 return (error); 346} 347 348static void 349gem_cddma_callback(xsc, segs, nsegs, error) 350 void *xsc; 351 bus_dma_segment_t *segs; 352 int nsegs; 353 int error; 354{ 355 struct gem_softc *sc = (struct gem_softc *)xsc; 356 357 if (error != 0) 358 return; 359 if (nsegs != 1) { 360 /* can't happen... */ 361 panic("gem_cddma_callback: bad control buffer segment count"); 362 } 363 sc->sc_cddma = segs[0].ds_addr; 364} 365 366static void 367gem_rxdma_callback(xsc, segs, nsegs, error) 368 void *xsc; 369 bus_dma_segment_t *segs; 370 int nsegs; 371 int error; 372{ 373 struct gem_rxsoft *rxs = (struct gem_rxsoft *)xsc; 374 375 if (error != 0) 376 return; 377 if (nsegs != 1) { 378 /* can't happen... */ 379 panic("gem_rxdma_callback: bad control buffer segment count"); 380 } 381 rxs->rxs_paddr = segs[0].ds_addr; 382} 383 384/* 385 * This is called multiple times in our version of dmamap_load_mbuf, but should 386 * be fit for a generic version that only calls it once. 387 */ 388static void 389gem_txdma_callback(xsc, segs, nsegs, error) 390 void *xsc; 391 bus_dma_segment_t *segs; 392 int nsegs; 393 int error; 394{ 395 struct gem_txdma *tx = (struct gem_txdma *)xsc; 396 int seg; 397 398 tx->txd_error = error; 399 if (error != 0) 400 return; 401 tx->txd_nsegs = nsegs; 402 403 /* 404 * Initialize the transmit descriptors. 405 */ 406 for (seg = 0; seg < nsegs; 407 seg++, tx->txd_nexttx = GEM_NEXTTX(tx->txd_nexttx)) { 408 uint64_t flags; 409 410 DPRINTF(tx->txd_sc, ("txdma_cb: mapping seg %d (txd %d), len " 411 "%lx, addr %#lx (%#lx)\n", seg, tx->txd_nexttx, 412 segs[seg].ds_len, segs[seg].ds_addr, 413 GEM_DMA_WRITE(tx->txd_sc, segs[seg].ds_addr))); 414 CTR5(KTR_GEM, "txdma_cb: mapping seg %d (txd %d), len " 415 "%lx, addr %#lx (%#lx)", seg, tx->txd_nexttx, 416 segs[seg].ds_len, segs[seg].ds_addr, 417 GEM_DMA_WRITE(tx->txd_sc, segs[seg].ds_addr)); 418 /* 419 * If this is the first descriptor we're 420 * enqueueing, set the start of packet flag, 421 * and the checksum stuff if we want the hardware 422 * to do it. 423 */ 424 tx->txd_sc->sc_txdescs[tx->txd_nexttx].gd_addr = 425 GEM_DMA_WRITE(tx->txd_sc, segs[seg].ds_addr); 426 flags = segs[seg].ds_len & GEM_TD_BUFSIZE; 427 if ((tx->txd_flags & GTXD_FIRST) != 0 && seg == 0) { 428 CTR2(KTR_GEM, "txdma_cb: start of packet at seg %d, " 429 "tx %d", seg, tx->txd_nexttx); 430 flags |= GEM_TD_START_OF_PACKET; 431 if (++tx->txd_sc->sc_txwin > GEM_NTXSEGS * 2 / 3) { 432 tx->txd_sc->sc_txwin = 0; 433 flags |= GEM_TD_INTERRUPT_ME; 434 } 435 } 436 if ((tx->txd_flags & GTXD_LAST) != 0 && seg == nsegs - 1) { 437 CTR2(KTR_GEM, "txdma_cb: end of packet at seg %d, " 438 "tx %d", seg, tx->txd_nexttx); 439 flags |= GEM_TD_END_OF_PACKET; 440 } 441 tx->txd_sc->sc_txdescs[tx->txd_nexttx].gd_flags = 442 GEM_DMA_WRITE(tx->txd_sc, flags); 443 tx->txd_lasttx = tx->txd_nexttx; 444 } 445} 446 447static void 448gem_tick(arg) 449 void *arg; 450{ 451 struct gem_softc *sc = arg; 452 int s; 453 454 s = splnet(); 455 mii_tick(sc->sc_mii); 456 splx(s); 457 458 callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc); 459} 460 461static int 462gem_bitwait(sc, r, clr, set) 463 struct gem_softc *sc; 464 bus_addr_t r; 465 u_int32_t clr; 466 u_int32_t set; 467{ 468 int i; 469 u_int32_t reg; 470 471 for (i = TRIES; i--; DELAY(100)) { 472 reg = bus_space_read_4(sc->sc_bustag, sc->sc_h, r); 473 if ((r & clr) == 0 && (r & set) == set) 474 return (1); 475 } 476 return (0); 477} 478 479void 480gem_reset(sc) 481 struct gem_softc *sc; 482{ 483 bus_space_tag_t t = sc->sc_bustag; 484 bus_space_handle_t h = sc->sc_h; 485 int s; 486 487 s = splnet(); 488 DPRINTF(sc, ("%s: gem_reset\n", device_get_name(sc->sc_dev))); 489 CTR1(KTR_GEM, "%s: gem_reset", device_get_name(sc->sc_dev)); 490 gem_reset_rx(sc); 491 gem_reset_tx(sc); 492 493 /* Do a full reset */ 494 bus_space_write_4(t, h, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX); 495 if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 0)) 496 device_printf(sc->sc_dev, "cannot reset device\n"); 497 splx(s); 498} 499 500 501/* 502 * gem_rxdrain: 503 * 504 * Drain the receive queue. 505 */ 506static void 507gem_rxdrain(sc) 508 struct gem_softc *sc; 509{ 510 struct gem_rxsoft *rxs; 511 int i; 512 513 for (i = 0; i < GEM_NRXDESC; i++) { 514 rxs = &sc->sc_rxsoft[i]; 515 if (rxs->rxs_mbuf != NULL) { 516 bus_dmamap_unload(sc->sc_dmatag, rxs->rxs_dmamap); 517 m_freem(rxs->rxs_mbuf); 518 rxs->rxs_mbuf = NULL; 519 } 520 } 521} 522 523/* 524 * Reset the whole thing. 525 */ 526static void 527gem_stop(ifp, disable) 528 struct ifnet *ifp; 529 int disable; 530{ 531 struct gem_softc *sc = (struct gem_softc *)ifp->if_softc; 532 struct gem_txsoft *txs; 533 534 DPRINTF(sc, ("%s: gem_stop\n", device_get_name(sc->sc_dev))); 535 CTR1(KTR_GEM, "%s: gem_stop", device_get_name(sc->sc_dev)); 536 537 callout_stop(&sc->sc_tick_ch); 538 539 /* XXX - Should we reset these instead? */ 540 gem_disable_tx(sc); 541 gem_disable_rx(sc); 542 543 /* 544 * Release any queued transmit buffers. 545 */ 546 while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) { 547 STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q); 548 if (txs->txs_ndescs != 0) { 549 bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap); 550 if (txs->txs_mbuf != NULL) { 551 m_freem(txs->txs_mbuf); 552 txs->txs_mbuf = NULL; 553 } 554 } 555 STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 556 } 557 558 if (disable) 559 gem_rxdrain(sc); 560 561 /* 562 * Mark the interface down and cancel the watchdog timer. 563 */ 564 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 565 ifp->if_timer = 0; 566} 567 568/* 569 * Reset the receiver 570 */ 571int 572gem_reset_rx(sc) 573 struct gem_softc *sc; 574{ 575 bus_space_tag_t t = sc->sc_bustag; 576 bus_space_handle_t h = sc->sc_h; 577 578 /* 579 * Resetting while DMA is in progress can cause a bus hang, so we 580 * disable DMA first. 581 */ 582 gem_disable_rx(sc); 583 bus_space_write_4(t, h, GEM_RX_CONFIG, 0); 584 /* Wait till it finishes */ 585 if (!gem_bitwait(sc, GEM_RX_CONFIG, 1, 0)) 586 device_printf(sc->sc_dev, "cannot disable read dma\n"); 587 588 /* Wait 5ms extra. */ 589 DELAY(5000); 590 591 /* Finally, reset the ERX */ 592 bus_space_write_4(t, h, GEM_RESET, GEM_RESET_RX); 593 /* Wait till it finishes */ 594 if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_TX, 0)) { 595 device_printf(sc->sc_dev, "cannot reset receiver\n"); 596 return (1); 597 } 598 return (0); 599} 600 601 602/* 603 * Reset the transmitter 604 */ 605static int 606gem_reset_tx(sc) 607 struct gem_softc *sc; 608{ 609 bus_space_tag_t t = sc->sc_bustag; 610 bus_space_handle_t h = sc->sc_h; 611 int i; 612 613 /* 614 * Resetting while DMA is in progress can cause a bus hang, so we 615 * disable DMA first. 616 */ 617 gem_disable_tx(sc); 618 bus_space_write_4(t, h, GEM_TX_CONFIG, 0); 619 /* Wait till it finishes */ 620 if (!gem_bitwait(sc, GEM_TX_CONFIG, 1, 0)) 621 device_printf(sc->sc_dev, "cannot disable read dma\n"); 622 623 /* Wait 5ms extra. */ 624 DELAY(5000); 625 626 /* Finally, reset the ETX */ 627 bus_space_write_4(t, h, GEM_RESET, GEM_RESET_TX); 628 /* Wait till it finishes */ 629 for (i = TRIES; i--; DELAY(100)) 630 if ((bus_space_read_4(t, h, GEM_RESET) & GEM_RESET_TX) == 0) 631 break; 632 if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_TX, 0)) { 633 device_printf(sc->sc_dev, "cannot reset receiver\n"); 634 return (1); 635 } 636 return (0); 637} 638 639/* 640 * disable receiver. 641 */ 642static int 643gem_disable_rx(sc) 644 struct gem_softc *sc; 645{ 646 bus_space_tag_t t = sc->sc_bustag; 647 bus_space_handle_t h = sc->sc_h; 648 u_int32_t cfg; 649 650 /* Flip the enable bit */ 651 cfg = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG); 652 cfg &= ~GEM_MAC_RX_ENABLE; 653 bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, cfg); 654 655 /* Wait for it to finish */ 656 return (gem_bitwait(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0)); 657} 658 659/* 660 * disable transmitter. 661 */ 662static int 663gem_disable_tx(sc) 664 struct gem_softc *sc; 665{ 666 bus_space_tag_t t = sc->sc_bustag; 667 bus_space_handle_t h = sc->sc_h; 668 u_int32_t cfg; 669 670 /* Flip the enable bit */ 671 cfg = bus_space_read_4(t, h, GEM_MAC_TX_CONFIG); 672 cfg &= ~GEM_MAC_TX_ENABLE; 673 bus_space_write_4(t, h, GEM_MAC_TX_CONFIG, cfg); 674 675 /* Wait for it to finish */ 676 return (gem_bitwait(sc, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0)); 677} 678 679/* 680 * Initialize interface. 681 */ 682static int 683gem_meminit(sc) 684 struct gem_softc *sc; 685{ 686 struct gem_rxsoft *rxs; 687 int i, error; 688 689 /* 690 * Initialize the transmit descriptor ring. 691 */ 692 memset((void *)sc->sc_txdescs, 0, sizeof(sc->sc_txdescs)); 693 for (i = 0; i < GEM_NTXDESC; i++) { 694 sc->sc_txdescs[i].gd_flags = 0; 695 sc->sc_txdescs[i].gd_addr = 0; 696 } 697 GEM_CDTXSYNC(sc, 0, GEM_NTXDESC, 698 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 699 sc->sc_txfree = GEM_NTXDESC-1; 700 sc->sc_txnext = 0; 701 sc->sc_txwin = 0; 702 703 /* 704 * Initialize the receive descriptor and receive job 705 * descriptor rings. 706 */ 707 for (i = 0; i < GEM_NRXDESC; i++) { 708 rxs = &sc->sc_rxsoft[i]; 709 if (rxs->rxs_mbuf == NULL) { 710 if ((error = gem_add_rxbuf(sc, i)) != 0) { 711 device_printf(sc->sc_dev, "unable to " 712 "allocate or map rx buffer %d, error = " 713 "%d\n", i, error); 714 /* 715 * XXX Should attempt to run with fewer receive 716 * XXX buffers instead of just failing. 717 */ 718 gem_rxdrain(sc); 719 return (1); 720 } 721 } else 722 GEM_INIT_RXDESC(sc, i); 723 } 724 sc->sc_rxptr = 0; 725 726 return (0); 727} 728 729static int 730gem_ringsize(sz) 731 int sz; 732{ 733 int v = 0; 734 735 switch (sz) { 736 case 32: 737 v = GEM_RING_SZ_32; 738 break; 739 case 64: 740 v = GEM_RING_SZ_64; 741 break; 742 case 128: 743 v = GEM_RING_SZ_128; 744 break; 745 case 256: 746 v = GEM_RING_SZ_256; 747 break; 748 case 512: 749 v = GEM_RING_SZ_512; 750 break; 751 case 1024: 752 v = GEM_RING_SZ_1024; 753 break; 754 case 2048: 755 v = GEM_RING_SZ_2048; 756 break; 757 case 4096: 758 v = GEM_RING_SZ_4096; 759 break; 760 case 8192: 761 v = GEM_RING_SZ_8192; 762 break; 763 default: 764 printf("gem: invalid Receive Descriptor ring size\n"); 765 break; 766 } 767 return (v); 768} 769 770/* 771 * Initialization of interface; set up initialization block 772 * and transmit/receive descriptor rings. 773 */ 774static void 775gem_init(xsc) 776 void *xsc; 777{ 778 struct gem_softc *sc = (struct gem_softc *)xsc; 779 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 780 bus_space_tag_t t = sc->sc_bustag; 781 bus_space_handle_t h = sc->sc_h; 782 int s; 783 u_int32_t v; 784 785 s = splnet(); 786 787 DPRINTF(sc, ("%s: gem_init: calling stop\n", device_get_name(sc->sc_dev))); 788 CTR1(KTR_GEM, "%s: gem_init: calling stop", device_get_name(sc->sc_dev)); 789 /* 790 * Initialization sequence. The numbered steps below correspond 791 * to the sequence outlined in section 6.3.5.1 in the Ethernet 792 * Channel Engine manual (part of the PCIO manual). 793 * See also the STP2002-STQ document from Sun Microsystems. 794 */ 795 796 /* step 1 & 2. Reset the Ethernet Channel */ 797 gem_stop(&sc->sc_arpcom.ac_if, 0); 798 gem_reset(sc); 799 DPRINTF(sc, ("%s: gem_init: restarting\n", device_get_name(sc->sc_dev))); 800 CTR1(KTR_GEM, "%s: gem_init: restarting", device_get_name(sc->sc_dev)); 801 802 /* Re-initialize the MIF */ 803 gem_mifinit(sc); 804 805 /* Call MI reset function if any */ 806 if (sc->sc_hwreset) 807 (*sc->sc_hwreset)(sc); 808 809 /* step 3. Setup data structures in host memory */ 810 gem_meminit(sc); 811 812 /* step 4. TX MAC registers & counters */ 813 gem_init_regs(sc); 814 /* XXX: VLAN code from NetBSD temporarily removed. */ 815 bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME, 816 (ETHER_MAX_LEN + sizeof(struct ether_header)) | (0x2000<<16)); 817 818 /* step 5. RX MAC registers & counters */ 819 gem_setladrf(sc); 820 821 /* step 6 & 7. Program Descriptor Ring Base Addresses */ 822 /* NOTE: we use only 32-bit DMA addresses here. */ 823 bus_space_write_4(t, h, GEM_TX_RING_PTR_HI, 0); 824 bus_space_write_4(t, h, GEM_TX_RING_PTR_LO, GEM_CDTXADDR(sc, 0)); 825 826 bus_space_write_4(t, h, GEM_RX_RING_PTR_HI, 0); 827 bus_space_write_4(t, h, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0)); 828 DPRINTF(sc, ("loading rx ring %lx, tx ring %lx, cddma %lx\n", 829 GEM_CDRXADDR(sc, 0), GEM_CDTXADDR(sc, 0), sc->sc_cddma)); 830 CTR3(KTR_GEM, "loading rx ring %lx, tx ring %lx, cddma %lx", 831 GEM_CDRXADDR(sc, 0), GEM_CDTXADDR(sc, 0), sc->sc_cddma); 832 833 /* step 8. Global Configuration & Interrupt Mask */ 834 bus_space_write_4(t, h, GEM_INTMASK, 835 ~(GEM_INTR_TX_INTME| 836 GEM_INTR_TX_EMPTY| 837 GEM_INTR_RX_DONE|GEM_INTR_RX_NOBUF| 838 GEM_INTR_RX_TAG_ERR|GEM_INTR_PCS| 839 GEM_INTR_MAC_CONTROL|GEM_INTR_MIF| 840 GEM_INTR_BERR)); 841 bus_space_write_4(t, h, GEM_MAC_RX_MASK, 842 GEM_MAC_RX_DONE|GEM_MAC_RX_FRAME_CNT); 843 bus_space_write_4(t, h, GEM_MAC_TX_MASK, 0xffff); /* XXXX */ 844 bus_space_write_4(t, h, GEM_MAC_CONTROL_MASK, 0); /* XXXX */ 845 846 /* step 9. ETX Configuration: use mostly default values */ 847 848 /* Enable DMA */ 849 v = gem_ringsize(GEM_NTXDESC /*XXX*/); 850 bus_space_write_4(t, h, GEM_TX_CONFIG, 851 v|GEM_TX_CONFIG_TXDMA_EN| 852 ((0x400<<10)&GEM_TX_CONFIG_TXFIFO_TH)); 853 854 /* step 10. ERX Configuration */ 855 856 /* Encode Receive Descriptor ring size: four possible values */ 857 v = gem_ringsize(GEM_NRXDESC /*XXX*/); 858 859 /* Enable DMA */ 860 bus_space_write_4(t, h, GEM_RX_CONFIG, 861 v|(GEM_THRSH_1024<<GEM_RX_CONFIG_FIFO_THRS_SHIFT)| 862 (2<<GEM_RX_CONFIG_FBOFF_SHFT)|GEM_RX_CONFIG_RXDMA_EN| 863 (0<<GEM_RX_CONFIG_CXM_START_SHFT)); 864 /* 865 * The following value is for an OFF Threshold of about 3/4 full 866 * and an ON Threshold of 1/4 full. 867 */ 868 bus_space_write_4(t, h, GEM_RX_PAUSE_THRESH, 869 (3 * sc->sc_rxfifosize / 256) | 870 ( (sc->sc_rxfifosize / 256) << 12)); 871 bus_space_write_4(t, h, GEM_RX_BLANKING, (6<<12)|6); 872 873 /* step 11. Configure Media */ 874 mii_mediachg(sc->sc_mii); 875 876 /* step 12. RX_MAC Configuration Register */ 877 v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG); 878 v |= GEM_MAC_RX_ENABLE; 879 bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v); 880 881 /* step 14. Issue Transmit Pending command */ 882 883 /* Call MI initialization function if any */ 884 if (sc->sc_hwinit) 885 (*sc->sc_hwinit)(sc); 886 887 /* step 15. Give the reciever a swift kick */ 888 bus_space_write_4(t, h, GEM_RX_KICK, GEM_NRXDESC-4); 889 890 /* Start the one second timer. */ 891 callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc); 892 893 ifp->if_flags |= IFF_RUNNING; 894 ifp->if_flags &= ~IFF_OACTIVE; 895 ifp->if_timer = 0; 896 sc->sc_ifflags = ifp->if_flags; 897 splx(s); 898} 899 900/* 901 * XXX: This is really a substitute for bus_dmamap_load_mbuf(), which FreeBSD 902 * does not yet have, with some adaptions for this driver. 903 * Some changes are mandated by the fact that multiple maps may needed to map 904 * a single mbuf. 905 * It should be removed once generic support is available. 906 * 907 * This is derived from NetBSD (syssrc/sys/arch/sparc64/sparc64/machdep.c), for 908 * a copyright notice see sparc64/sparc64/bus_machdep.c. 909 * 910 * Not every error condition is passed to the callback in this version, and the 911 * callback may be called more than once. 912 * It also gropes in the entails of the callback arg... 913 */ 914static int 915gem_dmamap_load_mbuf(sc, m0, cb, txj, flags) 916 struct gem_softc *sc; 917 struct mbuf *m0; 918 bus_dmamap_callback_t *cb; 919 struct gem_txjob *txj; 920 int flags; 921{ 922 struct gem_txdma txd; 923 struct gem_txsoft *txs; 924 struct mbuf *m; 925 void *vaddr; 926 int error, first = 1, len, totlen; 927 928 if ((m0->m_flags & M_PKTHDR) == 0) 929 panic("gem_dmamap_load_mbuf: no packet header"); 930 totlen = m0->m_pkthdr.len; 931 len = 0; 932 txd.txd_sc = sc; 933 txd.txd_nexttx = txj->txj_nexttx; 934 txj->txj_nsegs = 0; 935 STAILQ_INIT(&txj->txj_txsq); 936 m = m0; 937 while (m != NULL && len < totlen) { 938 if (m->m_len == 0) 939 continue; 940 /* Get a work queue entry. */ 941 if ((txs = STAILQ_FIRST(&sc->sc_txfreeq)) == NULL) { 942 /* 943 * Ran out of descriptors, return a value that 944 * cannot be returned by bus_dmamap_load to notify 945 * the caller. 946 */ 947 error = -1; 948 goto fail; 949 } 950 len += m->m_len; 951 txd.txd_flags = first ? GTXD_FIRST : 0; 952 if (m->m_next == NULL || len >= totlen) 953 txd.txd_flags |= GTXD_LAST; 954 vaddr = mtod(m, void *); 955 error = bus_dmamap_load(sc->sc_dmatag, txs->txs_dmamap, vaddr, 956 m->m_len, cb, &txd, flags); 957 if (error != 0 || txd.txd_error != 0) 958 goto fail; 959 /* Sync the DMA map. */ 960 bus_dmamap_sync(sc->sc_dmatag, txs->txs_dmamap, 961 BUS_DMASYNC_PREWRITE); 962 m = m->m_next; 963 /* 964 * Store a pointer to the packet so we can free it later, 965 * and remember what txdirty will be once the packet is 966 * done. 967 */ 968 txs->txs_mbuf = first ? m0 : NULL; 969 txs->txs_firstdesc = txj->txj_nexttx; 970 txs->txs_lastdesc = txd.txd_lasttx; 971 txs->txs_ndescs = txd.txd_nsegs; 972 CTR3(KTR_GEM, "load_mbuf: setting firstdesc=%d, lastdesc=%d, " 973 "ndescs=%d", txs->txs_firstdesc, txs->txs_lastdesc, 974 txs->txs_ndescs); 975 STAILQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q); 976 STAILQ_INSERT_TAIL(&txj->txj_txsq, txs, txs_q); 977 txj->txj_nexttx = txd.txd_nexttx; 978 txj->txj_nsegs += txd.txd_nsegs; 979 first = 0; 980 } 981 txj->txj_lasttx = txd.txd_lasttx; 982 return (0); 983 984fail: 985 CTR1(KTR_GEM, "gem_dmamap_load_mbuf failed (%d)", error); 986 gem_dmamap_unload_mbuf(sc, txj); 987 return (error); 988} 989 990/* 991 * Unload an mbuf using the txd the information was placed in. 992 * The tx interrupt code frees the tx segments one by one, because the txd is 993 * not available any more. 994 */ 995static void 996gem_dmamap_unload_mbuf(sc, txj) 997 struct gem_softc *sc; 998 struct gem_txjob *txj; 999{ 1000 struct gem_txsoft *txs; 1001 1002 /* Readd the removed descriptors and unload the segments. */ 1003 while ((txs = STAILQ_FIRST(&txj->txj_txsq)) != NULL) { 1004 bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap); 1005 STAILQ_REMOVE_HEAD(&txj->txj_txsq, txs_q); 1006 STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 1007 } 1008} 1009 1010static void 1011gem_dmamap_commit_mbuf(sc, txj) 1012 struct gem_softc *sc; 1013 struct gem_txjob *txj; 1014{ 1015 struct gem_txsoft *txs; 1016 1017 /* Commit the txjob by transfering the txsoft's to the txdirtyq. */ 1018 while ((txs = STAILQ_FIRST(&txj->txj_txsq)) != NULL) { 1019 STAILQ_REMOVE_HEAD(&txj->txj_txsq, txs_q); 1020 STAILQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q); 1021 } 1022} 1023 1024static void 1025gem_init_regs(sc) 1026 struct gem_softc *sc; 1027{ 1028 bus_space_tag_t t = sc->sc_bustag; 1029 bus_space_handle_t h = sc->sc_h; 1030 const u_char *laddr = sc->sc_arpcom.ac_enaddr; 1031 u_int32_t v; 1032 1033 /* These regs are not cleared on reset */ 1034 if (!sc->sc_inited) { 1035 1036 /* Wooo. Magic values. */ 1037 bus_space_write_4(t, h, GEM_MAC_IPG0, 0); 1038 bus_space_write_4(t, h, GEM_MAC_IPG1, 8); 1039 bus_space_write_4(t, h, GEM_MAC_IPG2, 4); 1040 1041 bus_space_write_4(t, h, GEM_MAC_MAC_MIN_FRAME, ETHER_MIN_LEN); 1042 /* Max frame and max burst size */ 1043 bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME, 1044 ETHER_MAX_LEN | (0x2000<<16)); 1045 1046 bus_space_write_4(t, h, GEM_MAC_PREAMBLE_LEN, 0x7); 1047 bus_space_write_4(t, h, GEM_MAC_JAM_SIZE, 0x4); 1048 bus_space_write_4(t, h, GEM_MAC_ATTEMPT_LIMIT, 0x10); 1049 /* Dunno.... */ 1050 bus_space_write_4(t, h, GEM_MAC_CONTROL_TYPE, 0x8088); 1051 bus_space_write_4(t, h, GEM_MAC_RANDOM_SEED, 1052 ((laddr[5]<<8)|laddr[4])&0x3ff); 1053 1054 /* Secondary MAC addr set to 0:0:0:0:0:0 */ 1055 bus_space_write_4(t, h, GEM_MAC_ADDR3, 0); 1056 bus_space_write_4(t, h, GEM_MAC_ADDR4, 0); 1057 bus_space_write_4(t, h, GEM_MAC_ADDR5, 0); 1058 1059 /* MAC control addr set to 01:80:c2:00:00:01 */ 1060 bus_space_write_4(t, h, GEM_MAC_ADDR6, 0x0001); 1061 bus_space_write_4(t, h, GEM_MAC_ADDR7, 0xc200); 1062 bus_space_write_4(t, h, GEM_MAC_ADDR8, 0x0180); 1063 1064 /* MAC filter addr set to 0:0:0:0:0:0 */ 1065 bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER0, 0); 1066 bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER1, 0); 1067 bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER2, 0); 1068 1069 bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK1_2, 0); 1070 bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK0, 0); 1071 1072 sc->sc_inited = 1; 1073 } 1074 1075 /* Counters need to be zeroed */ 1076 bus_space_write_4(t, h, GEM_MAC_NORM_COLL_CNT, 0); 1077 bus_space_write_4(t, h, GEM_MAC_FIRST_COLL_CNT, 0); 1078 bus_space_write_4(t, h, GEM_MAC_EXCESS_COLL_CNT, 0); 1079 bus_space_write_4(t, h, GEM_MAC_LATE_COLL_CNT, 0); 1080 bus_space_write_4(t, h, GEM_MAC_DEFER_TMR_CNT, 0); 1081 bus_space_write_4(t, h, GEM_MAC_PEAK_ATTEMPTS, 0); 1082 bus_space_write_4(t, h, GEM_MAC_RX_FRAME_COUNT, 0); 1083 bus_space_write_4(t, h, GEM_MAC_RX_LEN_ERR_CNT, 0); 1084 bus_space_write_4(t, h, GEM_MAC_RX_ALIGN_ERR, 0); 1085 bus_space_write_4(t, h, GEM_MAC_RX_CRC_ERR_CNT, 0); 1086 bus_space_write_4(t, h, GEM_MAC_RX_CODE_VIOL, 0); 1087 1088 /* Un-pause stuff */ 1089#if 0 1090 bus_space_write_4(t, h, GEM_MAC_SEND_PAUSE_CMD, 0x1BF0); 1091#else 1092 bus_space_write_4(t, h, GEM_MAC_SEND_PAUSE_CMD, 0); 1093#endif 1094 1095 /* 1096 * Set the station address. 1097 */ 1098 bus_space_write_4(t, h, GEM_MAC_ADDR0, (laddr[4]<<8)|laddr[5]); 1099 bus_space_write_4(t, h, GEM_MAC_ADDR1, (laddr[2]<<8)|laddr[3]); 1100 bus_space_write_4(t, h, GEM_MAC_ADDR2, (laddr[0]<<8)|laddr[1]); 1101 1102 /* 1103 * Enable MII outputs. Enable GMII if there is a gigabit PHY. 1104 */ 1105 sc->sc_mif_config = bus_space_read_4(t, h, GEM_MIF_CONFIG); 1106 v = GEM_MAC_XIF_TX_MII_ENA; 1107 if (sc->sc_mif_config & GEM_MIF_CONFIG_MDI1) { 1108 v |= GEM_MAC_XIF_FDPLX_LED; 1109 if (sc->sc_flags & GEM_GIGABIT) 1110 v |= GEM_MAC_XIF_GMII_MODE; 1111 } 1112 bus_space_write_4(t, h, GEM_MAC_XIF_CONFIG, v); 1113} 1114 1115static void 1116gem_start(ifp) 1117 struct ifnet *ifp; 1118{ 1119 struct gem_softc *sc = (struct gem_softc *)ifp->if_softc; 1120 struct mbuf *m0 = NULL, *m; 1121 struct gem_txjob txj; 1122 int firsttx, ofree, seg, ntx, txmfail; 1123 1124 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) 1125 return; 1126 1127 /* 1128 * Remember the previous number of free descriptors and 1129 * the first descriptor we'll use. 1130 */ 1131 ofree = sc->sc_txfree; 1132 firsttx = sc->sc_txnext; 1133 1134 DPRINTF(sc, ("%s: gem_start: txfree %d, txnext %d\n", 1135 device_get_name(sc->sc_dev), ofree, firsttx)); 1136 CTR3(KTR_GEM, "%s: gem_start: txfree %d, txnext %d", 1137 device_get_name(sc->sc_dev), ofree, firsttx); 1138 1139 txj.txj_nexttx = firsttx; 1140 txj.txj_lasttx = 0; 1141 /* 1142 * Loop through the send queue, setting up transmit descriptors 1143 * until we drain the queue, or use up all available transmit 1144 * descriptors. 1145 */ 1146 txmfail = 0; 1147 for (ntx = 0;; ntx++) { 1148 /* 1149 * Grab a packet off the queue. 1150 */ 1151 IF_DEQUEUE(&ifp->if_snd, m0); 1152 if (m0 == NULL) 1153 break; 1154 m = NULL; 1155 1156 /* 1157 * Load the DMA map. If this fails, the packet either 1158 * didn't fit in the alloted number of segments, or we were 1159 * short on resources. In this case, we'll copy and try 1160 * again. 1161 */ 1162 txmfail = gem_dmamap_load_mbuf(sc, m0, 1163 gem_txdma_callback, &txj, BUS_DMA_NOWAIT); 1164 if (txmfail == -1) { 1165 IF_PREPEND(&ifp->if_snd, m0); 1166 break; 1167 } 1168 if (txmfail > 0) { 1169 MGETHDR(m, M_DONTWAIT, MT_DATA); 1170 if (m == NULL) { 1171 device_printf(sc->sc_dev, "unable to " 1172 "allocate Tx mbuf\n"); 1173 /* Failed; requeue. */ 1174 IF_PREPEND(&ifp->if_snd, m0); 1175 break; 1176 } 1177 if (m0->m_pkthdr.len > MHLEN) { 1178 MCLGET(m, M_DONTWAIT); 1179 if ((m->m_flags & M_EXT) == 0) { 1180 device_printf(sc->sc_dev, "unable to " 1181 "allocate Tx cluster\n"); 1182 IF_PREPEND(&ifp->if_snd, m0); 1183 m_freem(m); 1184 break; 1185 } 1186 } 1187 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t)); 1188 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len; 1189 txmfail = gem_dmamap_load_mbuf(sc, m, 1190 gem_txdma_callback, &txj, BUS_DMA_NOWAIT); 1191 if (txmfail != 0) { 1192 if (txmfail > 0) { 1193 device_printf(sc->sc_dev, "unable to " 1194 "load Tx buffer, error = %d\n", 1195 txmfail); 1196 } 1197 m_freem(m); 1198 IF_PREPEND(&ifp->if_snd, m0); 1199 break; 1200 } 1201 } 1202 1203 /* 1204 * Ensure we have enough descriptors free to describe 1205 * the packet. Note, we always reserve one descriptor 1206 * at the end of the ring as a termination point, to 1207 * prevent wrap-around. 1208 */ 1209 if (txj.txj_nsegs > (sc->sc_txfree - 1)) { 1210 /* 1211 * Not enough free descriptors to transmit this 1212 * packet. We haven't committed to anything yet, 1213 * so just unload the DMA map, put the packet 1214 * back on the queue, and punt. Notify the upper 1215 * layer that there are no more slots left. 1216 * 1217 * XXX We could allocate an mbuf and copy, but 1218 * XXX it is worth it? 1219 */ 1220 ifp->if_flags |= IFF_OACTIVE; 1221 gem_dmamap_unload_mbuf(sc, &txj); 1222 if (m != NULL) 1223 m_freem(m); 1224 IF_PREPEND(&ifp->if_snd, m0); 1225 break; 1226 } 1227 1228 /* 1229 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET. 1230 */ 1231 if (ifp->if_bpf != NULL) 1232 bpf_mtap(ifp, m0); 1233 1234 if (m != NULL) 1235 m_freem(m0); 1236 1237#ifdef GEM_DEBUG 1238 if (ifp->if_flags & IFF_DEBUG) { 1239 printf(" gem_start %p transmit chain:\n", 1240 STAILQ_FIRST(&txj.txj_txsq)); 1241 for (seg = sc->sc_txnext;; seg = GEM_NEXTTX(seg)) { 1242 printf("descriptor %d:\t", seg); 1243 printf("gd_flags: 0x%016llx\t", (long long) 1244 GEM_DMA_READ(sc, sc->sc_txdescs[seg].gd_flags)); 1245 printf("gd_addr: 0x%016llx\n", (long long) 1246 GEM_DMA_READ(sc, sc->sc_txdescs[seg].gd_addr)); 1247 if (seg == txj.txj_lasttx) 1248 break; 1249 } 1250 } 1251#endif 1252 1253 /* Sync the descriptors we're using. */ 1254 GEM_CDTXSYNC(sc, sc->sc_txnext, txj.txj_nsegs, 1255 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 1256 1257 /* Advance the tx pointer. */ 1258 sc->sc_txfree -= txj.txj_nsegs; 1259 sc->sc_txnext = txj.txj_nexttx; 1260 1261 gem_dmamap_commit_mbuf(sc, &txj); 1262 } 1263 1264 if (txmfail == -1 || sc->sc_txfree == 0) { 1265 ifp->if_flags |= IFF_OACTIVE; 1266 /* No more slots left; notify upper layer. */ 1267 } 1268 1269 if (ntx > 0) { 1270 DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n", 1271 device_get_name(sc->sc_dev), txj.txj_lasttx, firsttx)); 1272 CTR3(KTR_GEM, "%s: packets enqueued, IC on %d, OWN on %d", 1273 device_get_name(sc->sc_dev), txj.txj_lasttx, firsttx); 1274 /* 1275 * The entire packet chain is set up. 1276 * Kick the transmitter. 1277 */ 1278 DPRINTF(sc, ("%s: gem_start: kicking tx %d\n", 1279 device_get_name(sc->sc_dev), txj.txj_nexttx)); 1280 CTR3(KTR_GEM, "%s: gem_start: kicking tx %d=%d", 1281 device_get_name(sc->sc_dev), txj.txj_nexttx, 1282 sc->sc_txnext); 1283 bus_space_write_4(sc->sc_bustag, sc->sc_h, GEM_TX_KICK, 1284 sc->sc_txnext); 1285 1286 /* Set a watchdog timer in case the chip flakes out. */ 1287 ifp->if_timer = 5; 1288 DPRINTF(sc, ("%s: gem_start: watchdog %d\n", 1289 device_get_name(sc->sc_dev), ifp->if_timer)); 1290 CTR2(KTR_GEM, "%s: gem_start: watchdog %d", 1291 device_get_name(sc->sc_dev), ifp->if_timer); 1292 } 1293} 1294 1295/* 1296 * Transmit interrupt. 1297 */ 1298static void 1299gem_tint(sc) 1300 struct gem_softc *sc; 1301{ 1302 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 1303 bus_space_tag_t t = sc->sc_bustag; 1304 bus_space_handle_t mac = sc->sc_h; 1305 struct gem_txsoft *txs; 1306 int txlast; 1307 int progress = 0; 1308 1309 1310 DPRINTF(sc, ("%s: gem_tint\n", device_get_name(sc->sc_dev))); 1311 CTR1(KTR_GEM, "%s: gem_tint", device_get_name(sc->sc_dev)); 1312 1313 /* 1314 * Unload collision counters 1315 */ 1316 ifp->if_collisions += 1317 bus_space_read_4(t, mac, GEM_MAC_NORM_COLL_CNT) + 1318 bus_space_read_4(t, mac, GEM_MAC_FIRST_COLL_CNT) + 1319 bus_space_read_4(t, mac, GEM_MAC_EXCESS_COLL_CNT) + 1320 bus_space_read_4(t, mac, GEM_MAC_LATE_COLL_CNT); 1321 1322 /* 1323 * then clear the hardware counters. 1324 */ 1325 bus_space_write_4(t, mac, GEM_MAC_NORM_COLL_CNT, 0); 1326 bus_space_write_4(t, mac, GEM_MAC_FIRST_COLL_CNT, 0); 1327 bus_space_write_4(t, mac, GEM_MAC_EXCESS_COLL_CNT, 0); 1328 bus_space_write_4(t, mac, GEM_MAC_LATE_COLL_CNT, 0); 1329 1330 /* 1331 * Go through our Tx list and free mbufs for those 1332 * frames that have been transmitted. 1333 */ 1334 while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) { 1335 GEM_CDTXSYNC(sc, txs->txs_lastdesc, 1336 txs->txs_ndescs, 1337 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 1338 1339#ifdef GEM_DEBUG 1340 if (ifp->if_flags & IFF_DEBUG) { 1341 int i; 1342 printf(" txsoft %p transmit chain:\n", txs); 1343 for (i = txs->txs_firstdesc;; i = GEM_NEXTTX(i)) { 1344 printf("descriptor %d: ", i); 1345 printf("gd_flags: 0x%016llx\t", (long long) 1346 GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_flags)); 1347 printf("gd_addr: 0x%016llx\n", (long long) 1348 GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_addr)); 1349 if (i == txs->txs_lastdesc) 1350 break; 1351 } 1352 } 1353#endif 1354 1355 /* 1356 * In theory, we could harveast some descriptors before 1357 * the ring is empty, but that's a bit complicated. 1358 * 1359 * GEM_TX_COMPLETION points to the last descriptor 1360 * processed +1. 1361 */ 1362 txlast = bus_space_read_4(t, mac, GEM_TX_COMPLETION); 1363 DPRINTF(sc, 1364 ("gem_tint: txs->txs_lastdesc = %d, txlast = %d\n", 1365 txs->txs_lastdesc, txlast)); 1366 CTR3(KTR_GEM, "gem_tint: txs->txs_firstdesc = %d, " 1367 "txs->txs_lastdesc = %d, txlast = %d", 1368 txs->txs_firstdesc, txs->txs_lastdesc, txlast); 1369 if (txs->txs_firstdesc <= txs->txs_lastdesc) { 1370 if ((txlast >= txs->txs_firstdesc) && 1371 (txlast <= txs->txs_lastdesc)) 1372 break; 1373 } else { 1374 /* Ick -- this command wraps */ 1375 if ((txlast >= txs->txs_firstdesc) || 1376 (txlast <= txs->txs_lastdesc)) 1377 break; 1378 } 1379 1380 DPRINTF(sc, ("gem_tint: releasing a desc\n")); 1381 CTR0(KTR_GEM, "gem_tint: releasing a desc"); 1382 STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q); 1383 1384 sc->sc_txfree += txs->txs_ndescs; 1385 1386 bus_dmamap_sync(sc->sc_dmatag, txs->txs_dmamap, 1387 BUS_DMASYNC_POSTWRITE); 1388 bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap); 1389 if (txs->txs_mbuf != NULL) { 1390 m_freem(txs->txs_mbuf); 1391 txs->txs_mbuf = NULL; 1392 } 1393 1394 STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 1395 1396 ifp->if_opackets++; 1397 progress = 1; 1398 } 1399 1400 DPRINTF(sc, ("gem_tint: GEM_TX_STATE_MACHINE %x " 1401 "GEM_TX_DATA_PTR %llx " 1402 "GEM_TX_COMPLETION %x\n", 1403 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_STATE_MACHINE), 1404 ((long long) bus_space_read_4(sc->sc_bustag, sc->sc_h, 1405 GEM_TX_DATA_PTR_HI) << 32) | 1406 bus_space_read_4(sc->sc_bustag, sc->sc_h, 1407 GEM_TX_DATA_PTR_LO), 1408 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_COMPLETION))); 1409 CTR3(KTR_GEM, "gem_tint: GEM_TX_STATE_MACHINE %x " 1410 "GEM_TX_DATA_PTR %llx " 1411 "GEM_TX_COMPLETION %x", 1412 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_STATE_MACHINE), 1413 ((long long) bus_space_read_4(sc->sc_bustag, sc->sc_h, 1414 GEM_TX_DATA_PTR_HI) << 32) | 1415 bus_space_read_4(sc->sc_bustag, sc->sc_h, 1416 GEM_TX_DATA_PTR_LO), 1417 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_COMPLETION)); 1418 1419 if (progress) { 1420 if (sc->sc_txfree == GEM_NTXDESC - 1) 1421 sc->sc_txwin = 0; 1422 1423 /* Freed some descriptors, so reset IFF_OACTIVE and restart. */ 1424 ifp->if_flags &= ~IFF_OACTIVE; 1425 gem_start(ifp); 1426 1427 if (STAILQ_EMPTY(&sc->sc_txdirtyq)) 1428 ifp->if_timer = 0; 1429 } 1430 1431 DPRINTF(sc, ("%s: gem_tint: watchdog %d\n", 1432 device_get_name(sc->sc_dev), ifp->if_timer)); 1433 CTR2(KTR_GEM, "%s: gem_tint: watchdog %d", 1434 device_get_name(sc->sc_dev), ifp->if_timer); 1435} 1436 1437#if 0 1438static void 1439gem_rint_timeout(arg) 1440 void *arg; 1441{ 1442 1443 gem_rint((struct gem_softc *)arg); 1444} 1445#endif 1446 1447/* 1448 * Receive interrupt. 1449 */ 1450static void 1451gem_rint(sc) 1452 struct gem_softc *sc; 1453{ 1454 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 1455 bus_space_tag_t t = sc->sc_bustag; 1456 bus_space_handle_t h = sc->sc_h; 1457 struct ether_header *eh; 1458 struct gem_rxsoft *rxs; 1459 struct mbuf *m; 1460 u_int64_t rxstat; 1461 u_int32_t rxcomp; 1462 int i, len, progress = 0; 1463 1464 callout_stop(&sc->sc_rx_ch); 1465 DPRINTF(sc, ("%s: gem_rint\n", device_get_name(sc->sc_dev))); 1466 CTR1(KTR_GEM, "%s: gem_rint", device_get_name(sc->sc_dev)); 1467 1468 /* 1469 * Read the completion register once. This limits 1470 * how long the following loop can execute. 1471 */ 1472 rxcomp = bus_space_read_4(t, h, GEM_RX_COMPLETION); 1473 1474 /* 1475 * XXXX Read the lastrx only once at the top for speed. 1476 */ 1477 DPRINTF(sc, ("gem_rint: sc->rxptr %d, complete %d\n", 1478 sc->sc_rxptr, rxcomp)); 1479 CTR2(KTR_GEM, "gem_rint: sc->rxptr %d, complete %d", 1480 sc->sc_rxptr, rxcomp); 1481 for (i = sc->sc_rxptr; i != rxcomp; 1482 i = GEM_NEXTRX(i)) { 1483 rxs = &sc->sc_rxsoft[i]; 1484 1485 GEM_CDRXSYNC(sc, i, 1486 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 1487 1488 rxstat = GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags); 1489 1490 if (rxstat & GEM_RD_OWN) { 1491#if 0 /* XXX: In case of emergency, re-enable this. */ 1492 /* 1493 * The descriptor is still marked as owned, although 1494 * it is supposed to have completed. This has been 1495 * observed on some machines. Just exiting here 1496 * might leave the packet sitting around until another 1497 * one arrives to trigger a new interrupt, which is 1498 * generally undesirable, so set up a timeout. 1499 */ 1500 callout_reset(&sc->sc_rx_ch, GEM_RXOWN_TICKS, 1501 gem_rint_timeout, sc); 1502#endif 1503 break; 1504 } 1505 1506 progress++; 1507 ifp->if_ipackets++; 1508 1509 if (rxstat & GEM_RD_BAD_CRC) { 1510 ifp->if_ierrors++; 1511 device_printf(sc->sc_dev, "receive error: CRC error\n"); 1512 GEM_INIT_RXDESC(sc, i); 1513 continue; 1514 } 1515 1516 bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 1517 BUS_DMASYNC_POSTREAD); 1518#ifdef GEM_DEBUG 1519 if (ifp->if_flags & IFF_DEBUG) { 1520 printf(" rxsoft %p descriptor %d: ", rxs, i); 1521 printf("gd_flags: 0x%016llx\t", (long long) 1522 GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags)); 1523 printf("gd_addr: 0x%016llx\n", (long long) 1524 GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_addr)); 1525 } 1526#endif 1527 1528 /* 1529 * No errors; receive the packet. Note the Gem 1530 * includes the CRC with every packet. 1531 */ 1532 len = GEM_RD_BUFLEN(rxstat); 1533 1534 /* 1535 * Allocate a new mbuf cluster. If that fails, we are 1536 * out of memory, and must drop the packet and recycle 1537 * the buffer that's already attached to this descriptor. 1538 */ 1539 m = rxs->rxs_mbuf; 1540 if (gem_add_rxbuf(sc, i) != 0) { 1541 ifp->if_ierrors++; 1542 GEM_INIT_RXDESC(sc, i); 1543 bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 1544 BUS_DMASYNC_PREREAD); 1545 continue; 1546 } 1547 m->m_data += 2; /* We're already off by two */ 1548 1549 m->m_pkthdr.rcvif = ifp; 1550 m->m_pkthdr.len = m->m_len = len - ETHER_CRC_LEN; 1551 1552 /* Pass it on. */ 1553 (*ifp->if_input)(ifp, m); 1554 } 1555 1556 if (progress) { 1557 /* Update the receive pointer. */ 1558 if (i == sc->sc_rxptr) { 1559 device_printf(sc->sc_dev, "rint: ring wrap\n"); 1560 } 1561 sc->sc_rxptr = i; 1562 bus_space_write_4(t, h, GEM_RX_KICK, GEM_PREVRX(i)); 1563 } 1564 1565 DPRINTF(sc, ("gem_rint: done sc->rxptr %d, complete %d\n", 1566 sc->sc_rxptr, bus_space_read_4(t, h, GEM_RX_COMPLETION))); 1567 CTR2(KTR_GEM, "gem_rint: done sc->rxptr %d, complete %d", 1568 sc->sc_rxptr, bus_space_read_4(t, h, GEM_RX_COMPLETION)); 1569 1570} 1571 1572 1573/* 1574 * gem_add_rxbuf: 1575 * 1576 * Add a receive buffer to the indicated descriptor. 1577 */ 1578static int 1579gem_add_rxbuf(sc, idx) 1580 struct gem_softc *sc; 1581 int idx; 1582{ 1583 struct gem_rxsoft *rxs = &sc->sc_rxsoft[idx]; 1584 struct mbuf *m; 1585 int error; 1586 1587 MGETHDR(m, M_DONTWAIT, MT_DATA); 1588 if (m == NULL) 1589 return (ENOBUFS); 1590 1591 MCLGET(m, M_DONTWAIT); 1592 if ((m->m_flags & M_EXT) == 0) { 1593 m_freem(m); 1594 return (ENOBUFS); 1595 } 1596 1597#ifdef GEM_DEBUG 1598 /* bzero the packet to check dma */ 1599 memset(m->m_ext.ext_buf, 0, m->m_ext.ext_size); 1600#endif 1601 1602 if (rxs->rxs_mbuf != NULL) 1603 bus_dmamap_unload(sc->sc_dmatag, rxs->rxs_dmamap); 1604 1605 rxs->rxs_mbuf = m; 1606 1607 error = bus_dmamap_load(sc->sc_dmatag, rxs->rxs_dmamap, 1608 m->m_ext.ext_buf, m->m_ext.ext_size, gem_rxdma_callback, rxs, 1609 BUS_DMA_NOWAIT); 1610 if (error != 0 || rxs->rxs_paddr == 0) { 1611 device_printf(sc->sc_dev, "can't load rx DMA map %d, error = " 1612 "%d\n", idx, error); 1613 panic("gem_add_rxbuf"); /* XXX */ 1614 } 1615 1616 bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, BUS_DMASYNC_PREREAD); 1617 1618 GEM_INIT_RXDESC(sc, idx); 1619 1620 return (0); 1621} 1622 1623 1624static void 1625gem_eint(sc, status) 1626 struct gem_softc *sc; 1627 u_int status; 1628{ 1629 1630 if ((status & GEM_INTR_MIF) != 0) { 1631 device_printf(sc->sc_dev, "XXXlink status changed\n"); 1632 return; 1633 } 1634 1635 device_printf(sc->sc_dev, "status=%x\n", status); 1636} 1637 1638 1639void 1640gem_intr(v) 1641 void *v; 1642{ 1643 struct gem_softc *sc = (struct gem_softc *)v; 1644 bus_space_tag_t t = sc->sc_bustag; 1645 bus_space_handle_t seb = sc->sc_h; 1646 u_int32_t status; 1647 1648 status = bus_space_read_4(t, seb, GEM_STATUS); 1649 DPRINTF(sc, ("%s: gem_intr: cplt %x, status %x\n", 1650 device_get_name(sc->sc_dev), (status>>19), 1651 (u_int)status)); 1652 CTR3(KTR_GEM, "%s: gem_intr: cplt %x, status %x", 1653 device_get_name(sc->sc_dev), (status>>19), 1654 (u_int)status); 1655 1656 if ((status & (GEM_INTR_RX_TAG_ERR | GEM_INTR_BERR)) != 0) 1657 gem_eint(sc, status); 1658 1659 if ((status & (GEM_INTR_TX_EMPTY | GEM_INTR_TX_INTME)) != 0) 1660 gem_tint(sc); 1661 1662 if ((status & (GEM_INTR_RX_DONE | GEM_INTR_RX_NOBUF)) != 0) 1663 gem_rint(sc); 1664 1665 /* We should eventually do more than just print out error stats. */ 1666 if (status & GEM_INTR_TX_MAC) { 1667 int txstat = bus_space_read_4(t, seb, GEM_MAC_TX_STATUS); 1668 if (txstat & ~GEM_MAC_TX_XMIT_DONE) 1669 device_printf(sc->sc_dev, "MAC tx fault, status %x\n", 1670 txstat); 1671 if (txstat & (GEM_MAC_TX_UNDERRUN | GEM_MAC_TX_PKT_TOO_LONG)) 1672 gem_init(sc); 1673 } 1674 if (status & GEM_INTR_RX_MAC) { 1675 int rxstat = bus_space_read_4(t, seb, GEM_MAC_RX_STATUS); 1676 if (rxstat & ~(GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT)) 1677 device_printf(sc->sc_dev, "MAC rx fault, status %x\n", 1678 rxstat); 1679 if ((rxstat & GEM_MAC_RX_OVERFLOW) != 0) 1680 gem_init(sc); 1681 } 1682} 1683 1684 1685static void 1686gem_watchdog(ifp) 1687 struct ifnet *ifp; 1688{ 1689 struct gem_softc *sc = ifp->if_softc; 1690 1691 DPRINTF(sc, ("gem_watchdog: GEM_RX_CONFIG %x GEM_MAC_RX_STATUS %x " 1692 "GEM_MAC_RX_CONFIG %x\n", 1693 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_RX_CONFIG), 1694 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_STATUS), 1695 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_CONFIG))); 1696 CTR3(KTR_GEM, "gem_watchdog: GEM_RX_CONFIG %x GEM_MAC_RX_STATUS %x " 1697 "GEM_MAC_RX_CONFIG %x", 1698 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_RX_CONFIG), 1699 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_STATUS), 1700 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_CONFIG)); 1701 CTR3(KTR_GEM, "gem_watchdog: GEM_TX_CONFIG %x GEM_MAC_TX_STATUS %x " 1702 "GEM_MAC_TX_CONFIG %x", 1703 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_CONFIG), 1704 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_TX_STATUS), 1705 bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_TX_CONFIG)); 1706 1707 device_printf(sc->sc_dev, "device timeout\n"); 1708 ++ifp->if_oerrors; 1709 1710 /* Try to get more packets going. */ 1711 gem_start(ifp); 1712} 1713 1714/* 1715 * Initialize the MII Management Interface 1716 */ 1717static void 1718gem_mifinit(sc) 1719 struct gem_softc *sc; 1720{ 1721 bus_space_tag_t t = sc->sc_bustag; 1722 bus_space_handle_t mif = sc->sc_h; 1723 1724 /* Configure the MIF in frame mode */ 1725 sc->sc_mif_config = bus_space_read_4(t, mif, GEM_MIF_CONFIG); 1726 sc->sc_mif_config &= ~GEM_MIF_CONFIG_BB_ENA; 1727 bus_space_write_4(t, mif, GEM_MIF_CONFIG, sc->sc_mif_config); 1728} 1729 1730/* 1731 * MII interface 1732 * 1733 * The GEM MII interface supports at least three different operating modes: 1734 * 1735 * Bitbang mode is implemented using data, clock and output enable registers. 1736 * 1737 * Frame mode is implemented by loading a complete frame into the frame 1738 * register and polling the valid bit for completion. 1739 * 1740 * Polling mode uses the frame register but completion is indicated by 1741 * an interrupt. 1742 * 1743 */ 1744int 1745gem_mii_readreg(dev, phy, reg) 1746 device_t dev; 1747 int phy, reg; 1748{ 1749 struct gem_softc *sc = device_get_softc(dev); 1750 bus_space_tag_t t = sc->sc_bustag; 1751 bus_space_handle_t mif = sc->sc_h; 1752 int n; 1753 u_int32_t v; 1754 1755#ifdef GEM_DEBUG_PHY 1756 printf("gem_mii_readreg: phy %d reg %d\n", phy, reg); 1757#endif 1758 1759#if 0 1760 /* Select the desired PHY in the MIF configuration register */ 1761 v = bus_space_read_4(t, mif, GEM_MIF_CONFIG); 1762 /* Clear PHY select bit */ 1763 v &= ~GEM_MIF_CONFIG_PHY_SEL; 1764 if (phy == GEM_PHYAD_EXTERNAL) 1765 /* Set PHY select bit to get at external device */ 1766 v |= GEM_MIF_CONFIG_PHY_SEL; 1767 bus_space_write_4(t, mif, GEM_MIF_CONFIG, v); 1768#endif 1769 1770 /* Construct the frame command */ 1771 v = (reg << GEM_MIF_REG_SHIFT) | (phy << GEM_MIF_PHY_SHIFT) | 1772 GEM_MIF_FRAME_READ; 1773 1774 bus_space_write_4(t, mif, GEM_MIF_FRAME, v); 1775 for (n = 0; n < 100; n++) { 1776 DELAY(1); 1777 v = bus_space_read_4(t, mif, GEM_MIF_FRAME); 1778 if (v & GEM_MIF_FRAME_TA0) 1779 return (v & GEM_MIF_FRAME_DATA); 1780 } 1781 1782 device_printf(sc->sc_dev, "mii_read timeout\n"); 1783 return (0); 1784} 1785 1786int 1787gem_mii_writereg(dev, phy, reg, val) 1788 device_t dev; 1789 int phy, reg, val; 1790{ 1791 struct gem_softc *sc = device_get_softc(dev); 1792 bus_space_tag_t t = sc->sc_bustag; 1793 bus_space_handle_t mif = sc->sc_h; 1794 int n; 1795 u_int32_t v; 1796 1797#ifdef GEM_DEBUG_PHY 1798 printf("gem_mii_writereg: phy %d reg %d val %x\n", phy, reg, val); 1799#endif 1800 1801#if 0 1802 /* Select the desired PHY in the MIF configuration register */ 1803 v = bus_space_read_4(t, mif, GEM_MIF_CONFIG); 1804 /* Clear PHY select bit */ 1805 v &= ~GEM_MIF_CONFIG_PHY_SEL; 1806 if (phy == GEM_PHYAD_EXTERNAL) 1807 /* Set PHY select bit to get at external device */ 1808 v |= GEM_MIF_CONFIG_PHY_SEL; 1809 bus_space_write_4(t, mif, GEM_MIF_CONFIG, v); 1810#endif 1811 /* Construct the frame command */ 1812 v = GEM_MIF_FRAME_WRITE | 1813 (phy << GEM_MIF_PHY_SHIFT) | 1814 (reg << GEM_MIF_REG_SHIFT) | 1815 (val & GEM_MIF_FRAME_DATA); 1816 1817 bus_space_write_4(t, mif, GEM_MIF_FRAME, v); 1818 for (n = 0; n < 100; n++) { 1819 DELAY(1); 1820 v = bus_space_read_4(t, mif, GEM_MIF_FRAME); 1821 if (v & GEM_MIF_FRAME_TA0) 1822 return (1); 1823 } 1824 1825 device_printf(sc->sc_dev, "mii_write timeout\n"); 1826 return (0); 1827} 1828 1829void 1830gem_mii_statchg(dev) 1831 device_t dev; 1832{ 1833 struct gem_softc *sc = device_get_softc(dev); 1834#ifdef GEM_DEBUG 1835 int instance = IFM_INST(sc->sc_mii->mii_media.ifm_cur->ifm_media); 1836#endif 1837 bus_space_tag_t t = sc->sc_bustag; 1838 bus_space_handle_t mac = sc->sc_h; 1839 u_int32_t v; 1840 1841#ifdef GEM_DEBUG 1842 if (sc->sc_debug) 1843 printf("gem_mii_statchg: status change: phy = %d\n", 1844 sc->sc_phys[instance]); 1845#endif 1846 1847 /* Set tx full duplex options */ 1848 bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, 0); 1849 DELAY(10000); /* reg must be cleared and delay before changing. */ 1850 v = GEM_MAC_TX_ENA_IPG0|GEM_MAC_TX_NGU|GEM_MAC_TX_NGU_LIMIT| 1851 GEM_MAC_TX_ENABLE; 1852 if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0) { 1853 v |= GEM_MAC_TX_IGN_CARRIER|GEM_MAC_TX_IGN_COLLIS; 1854 } 1855 bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, v); 1856 1857 /* XIF Configuration */ 1858 /* We should really calculate all this rather than rely on defaults */ 1859 v = bus_space_read_4(t, mac, GEM_MAC_XIF_CONFIG); 1860 v = GEM_MAC_XIF_LINK_LED; 1861 v |= GEM_MAC_XIF_TX_MII_ENA; 1862 1863 /* If an external transceiver is connected, enable its MII drivers */ 1864 sc->sc_mif_config = bus_space_read_4(t, mac, GEM_MIF_CONFIG); 1865 if ((sc->sc_mif_config & GEM_MIF_CONFIG_MDI1) != 0) { 1866 /* External MII needs echo disable if half duplex. */ 1867 if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0) 1868 /* turn on full duplex LED */ 1869 v |= GEM_MAC_XIF_FDPLX_LED; 1870 else 1871 /* half duplex -- disable echo */ 1872 v |= GEM_MAC_XIF_ECHO_DISABL; 1873 1874 if (IFM_SUBTYPE(sc->sc_mii->mii_media_active) == IFM_1000_T) 1875 v |= GEM_MAC_XIF_GMII_MODE; 1876 else 1877 v &= ~GEM_MAC_XIF_GMII_MODE; 1878 } else { 1879 /* Internal MII needs buf enable */ 1880 v |= GEM_MAC_XIF_MII_BUF_ENA; 1881 } 1882 bus_space_write_4(t, mac, GEM_MAC_XIF_CONFIG, v); 1883} 1884 1885int 1886gem_mediachange(ifp) 1887 struct ifnet *ifp; 1888{ 1889 struct gem_softc *sc = ifp->if_softc; 1890 1891 /* XXX Add support for serial media. */ 1892 1893 return (mii_mediachg(sc->sc_mii)); 1894} 1895 1896void 1897gem_mediastatus(ifp, ifmr) 1898 struct ifnet *ifp; 1899 struct ifmediareq *ifmr; 1900{ 1901 struct gem_softc *sc = ifp->if_softc; 1902 1903 if ((ifp->if_flags & IFF_UP) == 0) 1904 return; 1905 1906 mii_pollstat(sc->sc_mii); 1907 ifmr->ifm_active = sc->sc_mii->mii_media_active; 1908 ifmr->ifm_status = sc->sc_mii->mii_media_status; 1909} 1910 1911/* 1912 * Process an ioctl request. 1913 */ 1914static int 1915gem_ioctl(ifp, cmd, data) 1916 struct ifnet *ifp; 1917 u_long cmd; 1918 caddr_t data; 1919{ 1920 struct gem_softc *sc = ifp->if_softc; 1921 struct ifreq *ifr = (struct ifreq *)data; 1922 int s, error = 0; 1923 1924 switch (cmd) { 1925 case SIOCSIFADDR: 1926 case SIOCGIFADDR: 1927 case SIOCSIFMTU: 1928 error = ether_ioctl(ifp, cmd, data); 1929 break; 1930 case SIOCSIFFLAGS: 1931 if (ifp->if_flags & IFF_UP) { 1932 if ((sc->sc_ifflags ^ ifp->if_flags) == IFF_PROMISC) 1933 gem_setladrf(sc); 1934 else 1935 gem_init(sc); 1936 } else { 1937 if (ifp->if_flags & IFF_RUNNING) 1938 gem_stop(ifp, 0); 1939 } 1940 sc->sc_ifflags = ifp->if_flags; 1941 error = 0; 1942 break; 1943 case SIOCADDMULTI: 1944 case SIOCDELMULTI: 1945 gem_setladrf(sc); 1946 error = 0; 1947 break; 1948 case SIOCGIFMEDIA: 1949 case SIOCSIFMEDIA: 1950 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii->mii_media, cmd); 1951 break; 1952 default: 1953 error = ENOTTY; /* XXX EINVAL??? */ 1954 break; 1955 } 1956 1957 /* Try to get things going again */ 1958 if (ifp->if_flags & IFF_UP) 1959 gem_start(ifp); 1960 splx(s); 1961 return (error); 1962} 1963 1964/* 1965 * Set up the logical address filter. 1966 */ 1967static void 1968gem_setladrf(sc) 1969 struct gem_softc *sc; 1970{ 1971 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 1972 struct ifmultiaddr *inm; 1973 struct sockaddr_dl *sdl; 1974 bus_space_tag_t t = sc->sc_bustag; 1975 bus_space_handle_t h = sc->sc_h; 1976 u_char *cp; 1977 u_int32_t crc; 1978 u_int32_t hash[16]; 1979 u_int32_t v; 1980 int len; 1981 int i; 1982 1983 /* Get current RX configuration */ 1984 v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG); 1985 1986 /* 1987 * Turn off promiscuous mode, promiscuous group mode (all multicast), 1988 * and hash filter. Depending on the case, the right bit will be 1989 * enabled. 1990 */ 1991 v &= ~(GEM_MAC_RX_PROMISCUOUS|GEM_MAC_RX_HASH_FILTER| 1992 GEM_MAC_RX_PROMISC_GRP); 1993 1994 if ((ifp->if_flags & IFF_PROMISC) != 0) { 1995 /* Turn on promiscuous mode */ 1996 v |= GEM_MAC_RX_PROMISCUOUS; 1997 goto chipit; 1998 } 1999 if ((ifp->if_flags & IFF_ALLMULTI) != 0) { 2000 hash[3] = hash[2] = hash[1] = hash[0] = 0xffff; 2001 ifp->if_flags |= IFF_ALLMULTI; 2002 v |= GEM_MAC_RX_PROMISC_GRP; 2003 goto chipit; 2004 } 2005 2006 /* 2007 * Set up multicast address filter by passing all multicast addresses 2008 * through a crc generator, and then using the high order 8 bits as an 2009 * index into the 256 bit logical address filter. The high order 4 2010 * bits selects the word, while the other 4 bits select the bit within 2011 * the word (where bit 0 is the MSB). 2012 */ 2013 2014 /* Clear hash table */ 2015 memset(hash, 0, sizeof(hash)); 2016 2017 TAILQ_FOREACH(inm, &sc->sc_arpcom.ac_if.if_multiaddrs, ifma_link) { 2018 if (inm->ifma_addr->sa_family != AF_LINK) 2019 continue; 2020 sdl = (struct sockaddr_dl *)inm->ifma_addr; 2021 cp = LLADDR(sdl); 2022 crc = 0xffffffff; 2023 for (len = sdl->sdl_alen; --len >= 0;) { 2024 int octet = *cp++; 2025 int i; 2026 2027#define MC_POLY_LE 0xedb88320UL /* mcast crc, little endian */ 2028 for (i = 0; i < 8; i++) { 2029 if ((crc & 1) ^ (octet & 1)) { 2030 crc >>= 1; 2031 crc ^= MC_POLY_LE; 2032 } else { 2033 crc >>= 1; 2034 } 2035 octet >>= 1; 2036 } 2037 } 2038 /* Just want the 8 most significant bits. */ 2039 crc >>= 24; 2040 2041 /* Set the corresponding bit in the filter. */ 2042 hash[crc >> 4] |= 1 << (15 - (crc & 15)); 2043 } 2044 2045 v |= GEM_MAC_RX_HASH_FILTER; 2046 ifp->if_flags &= ~IFF_ALLMULTI; 2047 2048 /* Now load the hash table into the chip (if we are using it) */ 2049 for (i = 0; i < 16; i++) { 2050 bus_space_write_4(t, h, 2051 GEM_MAC_HASH0 + i * (GEM_MAC_HASH1-GEM_MAC_HASH0), 2052 hash[i]); 2053 } 2054 2055chipit: 2056 bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v); 2057} 2058 2059#if notyet 2060 2061/* 2062 * gem_power: 2063 * 2064 * Power management (suspend/resume) hook. 2065 */ 2066void 2067static gem_power(why, arg) 2068 int why; 2069 void *arg; 2070{ 2071 struct gem_softc *sc = arg; 2072 struct ifnet *ifp = &sc->sc_arpcom.ac_if; 2073 int s; 2074 2075 s = splnet(); 2076 switch (why) { 2077 case PWR_SUSPEND: 2078 case PWR_STANDBY: 2079 gem_stop(ifp, 1); 2080 if (sc->sc_power != NULL) 2081 (*sc->sc_power)(sc, why); 2082 break; 2083 case PWR_RESUME: 2084 if (ifp->if_flags & IFF_UP) { 2085 if (sc->sc_power != NULL) 2086 (*sc->sc_power)(sc, why); 2087 gem_init(ifp); 2088 } 2089 break; 2090 case PWR_SOFTSUSPEND: 2091 case PWR_SOFTSTANDBY: 2092 case PWR_SOFTRESUME: 2093 break; 2094 } 2095 splx(s); 2096} 2097#endif 2098