1 /*- 2 * Copyright (c) 2001 Wind River Systems 3 * Copyright (c) 1997, 1998, 1999, 2000, 2001 4 * Bill Paul <william.paul@windriver.com>. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Bill Paul. 17 * 4. Neither the name of the author nor the names of any co-contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 31 * THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 /* 38 * Level 1 LXT1001 gigabit ethernet driver for FreeBSD. Public 39 * documentation not available, but ask me nicely. 40 * 41 * The Level 1 chip is used on some D-Link, SMC and Addtron NICs. 42 * It's a 64-bit PCI part that supports TCP/IP checksum offload, 43 * VLAN tagging/insertion, GMII and TBI (1000baseX) ports. There 44 * are three supported methods for data transfer between host and 45 * NIC: programmed I/O, traditional scatter/gather DMA and Packet 46 * Propulsion Technology (tm) DMA. The latter mechanism is a form 47 * of double buffer DMA where the packet data is copied to a 48 * pre-allocated DMA buffer who's physical address has been loaded 49 * into a table at device initialization time. The rationale is that 50 * the virtual to physical address translation needed for normal 51 * scatter/gather DMA is more expensive than the data copy needed 52 * for double buffering. This may be true in Windows NT and the like, 53 * but it isn't true for us, at least on the x86 arch. This driver 54 * uses the scatter/gather I/O method for both TX and RX. 55 * 56 * The LXT1001 only supports TCP/IP checksum offload on receive. 57 * Also, the VLAN tagging is done using a 16-entry table which allows 58 * the chip to perform hardware filtering based on VLAN tags. Sadly, 59 * our vlan support doesn't currently play well with this kind of 60 * hardware support. 61 * 62 * Special thanks to: 63 * - Jeff James at Intel, for arranging to have the LXT1001 manual 64 * released (at long last) 65 * - Beny Chen at D-Link, for actually sending it to me 66 * - Brad Short and Keith Alexis at SMC, for sending me sample 67 * SMC9462SX and SMC9462TX adapters for testing 68 * - Paul Saab at Y!, for not killing me (though it remains to be seen 69 * if in fact he did me much of a favor) 70 */ 71 72 #include <sys/param.h> 73 #include <sys/systm.h> 74 #include <sys/sockio.h> 75 #include <sys/mbuf.h> 76 #include <sys/malloc.h> 77 #include <sys/kernel.h> 78 #include <sys/module.h> 79 #include <sys/socket.h> 80 81 #include <net/if.h> 82 #include <net/if_var.h> 83 #include <net/if_arp.h> 84 #include <net/ethernet.h> 85 #include <net/if_dl.h> 86 #include <net/if_media.h> 87 #include <net/if_types.h> 88 89 #include <net/bpf.h> 90 91 #include <vm/vm.h> /* for vtophys */ 92 #include <vm/pmap.h> /* for vtophys */ 93 #include <machine/bus.h> 94 #include <machine/resource.h> 95 #include <sys/bus.h> 96 #include <sys/rman.h> 97 98 #include <dev/mii/mii.h> 99 #include <dev/mii/miivar.h> 100 101 #include <dev/pci/pcireg.h> 102 #include <dev/pci/pcivar.h> 103 104 #define LGE_USEIOSPACE 105 106 #include <dev/lge/if_lgereg.h> 107 108 /* "device miibus" required. See GENERIC if you get errors here. */ 109 #include "miibus_if.h" 110 111 /* 112 * Various supported device vendors/types and their names. 113 */ 114 static const struct lge_type lge_devs[] = { 115 { LGE_VENDORID, LGE_DEVICEID, "Level 1 Gigabit Ethernet" }, 116 { 0, 0, NULL } 117 }; 118 119 static int lge_probe(device_t); 120 static int lge_attach(device_t); 121 static int lge_detach(device_t); 122 123 static int lge_alloc_jumbo_mem(struct lge_softc *); 124 static void lge_free_jumbo_mem(struct lge_softc *); 125 static void *lge_jalloc(struct lge_softc *); 126 static void lge_jfree(struct mbuf *); 127 128 static int lge_newbuf(struct lge_softc *, struct lge_rx_desc *, struct mbuf *); 129 static int lge_encap(struct lge_softc *, struct mbuf *, u_int32_t *); 130 static void lge_rxeof(struct lge_softc *, int); 131 static void lge_rxeoc(struct lge_softc *); 132 static void lge_txeof(struct lge_softc *); 133 static void lge_intr(void *); 134 static void lge_tick(void *); 135 static void lge_start(struct ifnet *); 136 static void lge_start_locked(struct ifnet *); 137 static int lge_ioctl(struct ifnet *, u_long, caddr_t); 138 static void lge_init(void *); 139 static void lge_init_locked(struct lge_softc *); 140 static void lge_stop(struct lge_softc *); 141 static void lge_watchdog(struct lge_softc *); 142 static int lge_shutdown(device_t); 143 static int lge_ifmedia_upd(struct ifnet *); 144 static void lge_ifmedia_upd_locked(struct ifnet *); 145 static void lge_ifmedia_sts(struct ifnet *, struct ifmediareq *); 146 147 static void lge_eeprom_getword(struct lge_softc *, int, u_int16_t *); 148 static void lge_read_eeprom(struct lge_softc *, caddr_t, int, int, int); 149 150 static int lge_miibus_readreg(device_t, int, int); 151 static int lge_miibus_writereg(device_t, int, int, int); 152 static void lge_miibus_statchg(device_t); 153 154 static void lge_setmulti(struct lge_softc *); 155 static void lge_reset(struct lge_softc *); 156 static int lge_list_rx_init(struct lge_softc *); 157 static int lge_list_tx_init(struct lge_softc *); 158 159 #ifdef LGE_USEIOSPACE 160 #define LGE_RES SYS_RES_IOPORT 161 #define LGE_RID LGE_PCI_LOIO 162 #else 163 #define LGE_RES SYS_RES_MEMORY 164 #define LGE_RID LGE_PCI_LOMEM 165 #endif 166 167 static device_method_t lge_methods[] = { 168 /* Device interface */ 169 DEVMETHOD(device_probe, lge_probe), 170 DEVMETHOD(device_attach, lge_attach), 171 DEVMETHOD(device_detach, lge_detach), 172 DEVMETHOD(device_shutdown, lge_shutdown), 173 174 /* MII interface */ 175 DEVMETHOD(miibus_readreg, lge_miibus_readreg), 176 DEVMETHOD(miibus_writereg, lge_miibus_writereg), 177 DEVMETHOD(miibus_statchg, lge_miibus_statchg), 178 179 DEVMETHOD_END 180 }; 181 182 static driver_t lge_driver = { 183 "lge", 184 lge_methods, 185 sizeof(struct lge_softc) 186 }; 187 188 static devclass_t lge_devclass; 189 190 DRIVER_MODULE(lge, pci, lge_driver, lge_devclass, 0, 0); 191 DRIVER_MODULE(miibus, lge, miibus_driver, miibus_devclass, 0, 0); 192 MODULE_DEPEND(lge, pci, 1, 1, 1); 193 MODULE_DEPEND(lge, ether, 1, 1, 1); 194 MODULE_DEPEND(lge, miibus, 1, 1, 1); 195 196 #define LGE_SETBIT(sc, reg, x) \ 197 CSR_WRITE_4(sc, reg, \ 198 CSR_READ_4(sc, reg) | (x)) 199 200 #define LGE_CLRBIT(sc, reg, x) \ 201 CSR_WRITE_4(sc, reg, \ 202 CSR_READ_4(sc, reg) & ~(x)) 203 204 #define SIO_SET(x) \ 205 CSR_WRITE_4(sc, LGE_MEAR, CSR_READ_4(sc, LGE_MEAR) | x) 206 207 #define SIO_CLR(x) \ 208 CSR_WRITE_4(sc, LGE_MEAR, CSR_READ_4(sc, LGE_MEAR) & ~x) 209 210 /* 211 * Read a word of data stored in the EEPROM at address 'addr.' 212 */ 213 static void 214 lge_eeprom_getword(sc, addr, dest) 215 struct lge_softc *sc; 216 int addr; 217 u_int16_t *dest; 218 { 219 int i; 220 u_int32_t val; 221 222 CSR_WRITE_4(sc, LGE_EECTL, LGE_EECTL_CMD_READ| 223 LGE_EECTL_SINGLEACCESS|((addr >> 1) << 8)); 224 225 for (i = 0; i < LGE_TIMEOUT; i++) 226 if (!(CSR_READ_4(sc, LGE_EECTL) & LGE_EECTL_CMD_READ)) 227 break; 228 229 if (i == LGE_TIMEOUT) { 230 device_printf(sc->lge_dev, "EEPROM read timed out\n"); 231 return; 232 } 233 234 val = CSR_READ_4(sc, LGE_EEDATA); 235 236 if (addr & 1) 237 *dest = (val >> 16) & 0xFFFF; 238 else 239 *dest = val & 0xFFFF; 240 241 return; 242 } 243 244 /* 245 * Read a sequence of words from the EEPROM. 246 */ 247 static void 248 lge_read_eeprom(sc, dest, off, cnt, swap) 249 struct lge_softc *sc; 250 caddr_t dest; 251 int off; 252 int cnt; 253 int swap; 254 { 255 int i; 256 u_int16_t word = 0, *ptr; 257 258 for (i = 0; i < cnt; i++) { 259 lge_eeprom_getword(sc, off + i, &word); 260 ptr = (u_int16_t *)(dest + (i * 2)); 261 if (swap) 262 *ptr = ntohs(word); 263 else 264 *ptr = word; 265 } 266 267 return; 268 } 269 270 static int 271 lge_miibus_readreg(dev, phy, reg) 272 device_t dev; 273 int phy, reg; 274 { 275 struct lge_softc *sc; 276 int i; 277 278 sc = device_get_softc(dev); 279 280 /* 281 * If we have a non-PCS PHY, pretend that the internal 282 * autoneg stuff at PHY address 0 isn't there so that 283 * the miibus code will find only the GMII PHY. 284 */ 285 if (sc->lge_pcs == 0 && phy == 0) 286 return(0); 287 288 CSR_WRITE_4(sc, LGE_GMIICTL, (phy << 8) | reg | LGE_GMIICMD_READ); 289 290 for (i = 0; i < LGE_TIMEOUT; i++) 291 if (!(CSR_READ_4(sc, LGE_GMIICTL) & LGE_GMIICTL_CMDBUSY)) 292 break; 293 294 if (i == LGE_TIMEOUT) { 295 device_printf(sc->lge_dev, "PHY read timed out\n"); 296 return(0); 297 } 298 299 return(CSR_READ_4(sc, LGE_GMIICTL) >> 16); 300 } 301 302 static int 303 lge_miibus_writereg(dev, phy, reg, data) 304 device_t dev; 305 int phy, reg, data; 306 { 307 struct lge_softc *sc; 308 int i; 309 310 sc = device_get_softc(dev); 311 312 CSR_WRITE_4(sc, LGE_GMIICTL, 313 (data << 16) | (phy << 8) | reg | LGE_GMIICMD_WRITE); 314 315 for (i = 0; i < LGE_TIMEOUT; i++) 316 if (!(CSR_READ_4(sc, LGE_GMIICTL) & LGE_GMIICTL_CMDBUSY)) 317 break; 318 319 if (i == LGE_TIMEOUT) { 320 device_printf(sc->lge_dev, "PHY write timed out\n"); 321 return(0); 322 } 323 324 return(0); 325 } 326 327 static void 328 lge_miibus_statchg(dev) 329 device_t dev; 330 { 331 struct lge_softc *sc; 332 struct mii_data *mii; 333 334 sc = device_get_softc(dev); 335 mii = device_get_softc(sc->lge_miibus); 336 337 LGE_CLRBIT(sc, LGE_GMIIMODE, LGE_GMIIMODE_SPEED); 338 switch (IFM_SUBTYPE(mii->mii_media_active)) { 339 case IFM_1000_T: 340 case IFM_1000_SX: 341 LGE_SETBIT(sc, LGE_GMIIMODE, LGE_SPEED_1000); 342 break; 343 case IFM_100_TX: 344 LGE_SETBIT(sc, LGE_GMIIMODE, LGE_SPEED_100); 345 break; 346 case IFM_10_T: 347 LGE_SETBIT(sc, LGE_GMIIMODE, LGE_SPEED_10); 348 break; 349 default: 350 /* 351 * Choose something, even if it's wrong. Clearing 352 * all the bits will hose autoneg on the internal 353 * PHY. 354 */ 355 LGE_SETBIT(sc, LGE_GMIIMODE, LGE_SPEED_1000); 356 break; 357 } 358 359 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) { 360 LGE_SETBIT(sc, LGE_GMIIMODE, LGE_GMIIMODE_FDX); 361 } else { 362 LGE_CLRBIT(sc, LGE_GMIIMODE, LGE_GMIIMODE_FDX); 363 } 364 365 return; 366 } 367 368 static void 369 lge_setmulti(sc) 370 struct lge_softc *sc; 371 { 372 struct ifnet *ifp; 373 struct ifmultiaddr *ifma; 374 u_int32_t h = 0, hashes[2] = { 0, 0 }; 375 376 ifp = sc->lge_ifp; 377 LGE_LOCK_ASSERT(sc); 378 379 /* Make sure multicast hash table is enabled. */ 380 CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL1|LGE_MODE1_RX_MCAST); 381 382 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 383 CSR_WRITE_4(sc, LGE_MAR0, 0xFFFFFFFF); 384 CSR_WRITE_4(sc, LGE_MAR1, 0xFFFFFFFF); 385 return; 386 } 387 388 /* first, zot all the existing hash bits */ 389 CSR_WRITE_4(sc, LGE_MAR0, 0); 390 CSR_WRITE_4(sc, LGE_MAR1, 0); 391 392 /* now program new ones */ 393 if_maddr_rlock(ifp); 394 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 395 if (ifma->ifma_addr->sa_family != AF_LINK) 396 continue; 397 h = ether_crc32_be(LLADDR((struct sockaddr_dl *) 398 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 399 if (h < 32) 400 hashes[0] |= (1 << h); 401 else 402 hashes[1] |= (1 << (h - 32)); 403 } 404 if_maddr_runlock(ifp); 405 406 CSR_WRITE_4(sc, LGE_MAR0, hashes[0]); 407 CSR_WRITE_4(sc, LGE_MAR1, hashes[1]); 408 409 return; 410 } 411 412 static void 413 lge_reset(sc) 414 struct lge_softc *sc; 415 { 416 int i; 417 418 LGE_SETBIT(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL0|LGE_MODE1_SOFTRST); 419 420 for (i = 0; i < LGE_TIMEOUT; i++) { 421 if (!(CSR_READ_4(sc, LGE_MODE1) & LGE_MODE1_SOFTRST)) 422 break; 423 } 424 425 if (i == LGE_TIMEOUT) 426 device_printf(sc->lge_dev, "reset never completed\n"); 427 428 /* Wait a little while for the chip to get its brains in order. */ 429 DELAY(1000); 430 431 return; 432 } 433 434 /* 435 * Probe for a Level 1 chip. Check the PCI vendor and device 436 * IDs against our list and return a device name if we find a match. 437 */ 438 static int 439 lge_probe(dev) 440 device_t dev; 441 { 442 const struct lge_type *t; 443 444 t = lge_devs; 445 446 while(t->lge_name != NULL) { 447 if ((pci_get_vendor(dev) == t->lge_vid) && 448 (pci_get_device(dev) == t->lge_did)) { 449 device_set_desc(dev, t->lge_name); 450 return(BUS_PROBE_DEFAULT); 451 } 452 t++; 453 } 454 455 return(ENXIO); 456 } 457 458 /* 459 * Attach the interface. Allocate softc structures, do ifmedia 460 * setup and ethernet/BPF attach. 461 */ 462 static int 463 lge_attach(dev) 464 device_t dev; 465 { 466 u_char eaddr[ETHER_ADDR_LEN]; 467 struct lge_softc *sc; 468 struct ifnet *ifp = NULL; 469 int error = 0, rid; 470 471 sc = device_get_softc(dev); 472 sc->lge_dev = dev; 473 474 mtx_init(&sc->lge_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 475 MTX_DEF); 476 callout_init_mtx(&sc->lge_stat_callout, &sc->lge_mtx, 0); 477 478 /* 479 * Map control/status registers. 480 */ 481 pci_enable_busmaster(dev); 482 483 rid = LGE_RID; 484 sc->lge_res = bus_alloc_resource_any(dev, LGE_RES, &rid, RF_ACTIVE); 485 486 if (sc->lge_res == NULL) { 487 device_printf(dev, "couldn't map ports/memory\n"); 488 error = ENXIO; 489 goto fail; 490 } 491 492 sc->lge_btag = rman_get_bustag(sc->lge_res); 493 sc->lge_bhandle = rman_get_bushandle(sc->lge_res); 494 495 /* Allocate interrupt */ 496 rid = 0; 497 sc->lge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 498 RF_SHAREABLE | RF_ACTIVE); 499 500 if (sc->lge_irq == NULL) { 501 device_printf(dev, "couldn't map interrupt\n"); 502 error = ENXIO; 503 goto fail; 504 } 505 506 /* Reset the adapter. */ 507 lge_reset(sc); 508 509 /* 510 * Get station address from the EEPROM. 511 */ 512 lge_read_eeprom(sc, (caddr_t)&eaddr[0], LGE_EE_NODEADDR_0, 1, 0); 513 lge_read_eeprom(sc, (caddr_t)&eaddr[2], LGE_EE_NODEADDR_1, 1, 0); 514 lge_read_eeprom(sc, (caddr_t)&eaddr[4], LGE_EE_NODEADDR_2, 1, 0); 515 516 sc->lge_ldata = contigmalloc(sizeof(struct lge_list_data), M_DEVBUF, 517 M_NOWAIT | M_ZERO, 0, 0xffffffff, PAGE_SIZE, 0); 518 519 if (sc->lge_ldata == NULL) { 520 device_printf(dev, "no memory for list buffers!\n"); 521 error = ENXIO; 522 goto fail; 523 } 524 525 /* Try to allocate memory for jumbo buffers. */ 526 if (lge_alloc_jumbo_mem(sc)) { 527 device_printf(dev, "jumbo buffer allocation failed\n"); 528 error = ENXIO; 529 goto fail; 530 } 531 532 ifp = sc->lge_ifp = if_alloc(IFT_ETHER); 533 if (ifp == NULL) { 534 device_printf(dev, "can not if_alloc()\n"); 535 error = ENOSPC; 536 goto fail; 537 } 538 ifp->if_softc = sc; 539 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 540 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 541 ifp->if_ioctl = lge_ioctl; 542 ifp->if_start = lge_start; 543 ifp->if_init = lge_init; 544 ifp->if_snd.ifq_maxlen = LGE_TX_LIST_CNT - 1; 545 ifp->if_capabilities = IFCAP_RXCSUM; 546 ifp->if_capenable = ifp->if_capabilities; 547 548 if (CSR_READ_4(sc, LGE_GMIIMODE) & LGE_GMIIMODE_PCSENH) 549 sc->lge_pcs = 1; 550 else 551 sc->lge_pcs = 0; 552 553 /* 554 * Do MII setup. 555 */ 556 error = mii_attach(dev, &sc->lge_miibus, ifp, lge_ifmedia_upd, 557 lge_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0); 558 if (error != 0) { 559 device_printf(dev, "attaching PHYs failed\n"); 560 goto fail; 561 } 562 563 /* 564 * Call MI attach routine. 565 */ 566 ether_ifattach(ifp, eaddr); 567 568 error = bus_setup_intr(dev, sc->lge_irq, INTR_TYPE_NET | INTR_MPSAFE, 569 NULL, lge_intr, sc, &sc->lge_intrhand); 570 571 if (error) { 572 ether_ifdetach(ifp); 573 device_printf(dev, "couldn't set up irq\n"); 574 goto fail; 575 } 576 return (0); 577 578 fail: 579 lge_free_jumbo_mem(sc); 580 if (sc->lge_ldata) 581 contigfree(sc->lge_ldata, 582 sizeof(struct lge_list_data), M_DEVBUF); 583 if (ifp) 584 if_free(ifp); 585 if (sc->lge_irq) 586 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->lge_irq); 587 if (sc->lge_res) 588 bus_release_resource(dev, LGE_RES, LGE_RID, sc->lge_res); 589 mtx_destroy(&sc->lge_mtx); 590 return(error); 591 } 592 593 static int 594 lge_detach(dev) 595 device_t dev; 596 { 597 struct lge_softc *sc; 598 struct ifnet *ifp; 599 600 sc = device_get_softc(dev); 601 ifp = sc->lge_ifp; 602 603 LGE_LOCK(sc); 604 lge_reset(sc); 605 lge_stop(sc); 606 LGE_UNLOCK(sc); 607 callout_drain(&sc->lge_stat_callout); 608 ether_ifdetach(ifp); 609 610 bus_generic_detach(dev); 611 device_delete_child(dev, sc->lge_miibus); 612 613 bus_teardown_intr(dev, sc->lge_irq, sc->lge_intrhand); 614 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->lge_irq); 615 bus_release_resource(dev, LGE_RES, LGE_RID, sc->lge_res); 616 617 contigfree(sc->lge_ldata, sizeof(struct lge_list_data), M_DEVBUF); 618 if_free(ifp); 619 lge_free_jumbo_mem(sc); 620 mtx_destroy(&sc->lge_mtx); 621 622 return(0); 623 } 624 625 /* 626 * Initialize the transmit descriptors. 627 */ 628 static int 629 lge_list_tx_init(sc) 630 struct lge_softc *sc; 631 { 632 struct lge_list_data *ld; 633 struct lge_ring_data *cd; 634 int i; 635 636 cd = &sc->lge_cdata; 637 ld = sc->lge_ldata; 638 for (i = 0; i < LGE_TX_LIST_CNT; i++) { 639 ld->lge_tx_list[i].lge_mbuf = NULL; 640 ld->lge_tx_list[i].lge_ctl = 0; 641 } 642 643 cd->lge_tx_prod = cd->lge_tx_cons = 0; 644 645 return(0); 646 } 647 648 649 /* 650 * Initialize the RX descriptors and allocate mbufs for them. Note that 651 * we arralge the descriptors in a closed ring, so that the last descriptor 652 * points back to the first. 653 */ 654 static int 655 lge_list_rx_init(sc) 656 struct lge_softc *sc; 657 { 658 struct lge_list_data *ld; 659 struct lge_ring_data *cd; 660 int i; 661 662 ld = sc->lge_ldata; 663 cd = &sc->lge_cdata; 664 665 cd->lge_rx_prod = cd->lge_rx_cons = 0; 666 667 CSR_WRITE_4(sc, LGE_RXDESC_ADDR_HI, 0); 668 669 for (i = 0; i < LGE_RX_LIST_CNT; i++) { 670 if (CSR_READ_1(sc, LGE_RXCMDFREE_8BIT) == 0) 671 break; 672 if (lge_newbuf(sc, &ld->lge_rx_list[i], NULL) == ENOBUFS) 673 return(ENOBUFS); 674 } 675 676 /* Clear possible 'rx command queue empty' interrupt. */ 677 CSR_READ_4(sc, LGE_ISR); 678 679 return(0); 680 } 681 682 /* 683 * Initialize an RX descriptor and attach an MBUF cluster. 684 */ 685 static int 686 lge_newbuf(sc, c, m) 687 struct lge_softc *sc; 688 struct lge_rx_desc *c; 689 struct mbuf *m; 690 { 691 struct mbuf *m_new = NULL; 692 char *buf = NULL; 693 694 if (m == NULL) { 695 MGETHDR(m_new, M_NOWAIT, MT_DATA); 696 if (m_new == NULL) { 697 device_printf(sc->lge_dev, "no memory for rx list " 698 "-- packet dropped!\n"); 699 return(ENOBUFS); 700 } 701 702 /* Allocate the jumbo buffer */ 703 buf = lge_jalloc(sc); 704 if (buf == NULL) { 705 #ifdef LGE_VERBOSE 706 device_printf(sc->lge_dev, "jumbo allocation failed " 707 "-- packet dropped!\n"); 708 #endif 709 m_freem(m_new); 710 return(ENOBUFS); 711 } 712 /* Attach the buffer to the mbuf */ 713 m_new->m_len = m_new->m_pkthdr.len = LGE_JUMBO_FRAMELEN; 714 m_extadd(m_new, buf, LGE_JUMBO_FRAMELEN, lge_jfree, sc, NULL, 715 0, EXT_NET_DRV); 716 } else { 717 m_new = m; 718 m_new->m_len = m_new->m_pkthdr.len = LGE_JUMBO_FRAMELEN; 719 m_new->m_data = m_new->m_ext.ext_buf; 720 } 721 722 /* 723 * Adjust alignment so packet payload begins on a 724 * longword boundary. Mandatory for Alpha, useful on 725 * x86 too. 726 */ 727 m_adj(m_new, ETHER_ALIGN); 728 729 c->lge_mbuf = m_new; 730 c->lge_fragptr_hi = 0; 731 c->lge_fragptr_lo = vtophys(mtod(m_new, caddr_t)); 732 c->lge_fraglen = m_new->m_len; 733 c->lge_ctl = m_new->m_len | LGE_RXCTL_WANTINTR | LGE_FRAGCNT(1); 734 c->lge_sts = 0; 735 736 /* 737 * Put this buffer in the RX command FIFO. To do this, 738 * we just write the physical address of the descriptor 739 * into the RX descriptor address registers. Note that 740 * there are two registers, one high DWORD and one low 741 * DWORD, which lets us specify a 64-bit address if 742 * desired. We only use a 32-bit address for now. 743 * Writing to the low DWORD register is what actually 744 * causes the command to be issued, so we do that 745 * last. 746 */ 747 CSR_WRITE_4(sc, LGE_RXDESC_ADDR_LO, vtophys(c)); 748 LGE_INC(sc->lge_cdata.lge_rx_prod, LGE_RX_LIST_CNT); 749 750 return(0); 751 } 752 753 static int 754 lge_alloc_jumbo_mem(sc) 755 struct lge_softc *sc; 756 { 757 caddr_t ptr; 758 int i; 759 struct lge_jpool_entry *entry; 760 761 /* Grab a big chunk o' storage. */ 762 sc->lge_cdata.lge_jumbo_buf = contigmalloc(LGE_JMEM, M_DEVBUF, 763 M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0); 764 765 if (sc->lge_cdata.lge_jumbo_buf == NULL) { 766 device_printf(sc->lge_dev, "no memory for jumbo buffers!\n"); 767 return(ENOBUFS); 768 } 769 770 SLIST_INIT(&sc->lge_jfree_listhead); 771 SLIST_INIT(&sc->lge_jinuse_listhead); 772 773 /* 774 * Now divide it up into 9K pieces and save the addresses 775 * in an array. 776 */ 777 ptr = sc->lge_cdata.lge_jumbo_buf; 778 for (i = 0; i < LGE_JSLOTS; i++) { 779 sc->lge_cdata.lge_jslots[i] = ptr; 780 ptr += LGE_JLEN; 781 entry = malloc(sizeof(struct lge_jpool_entry), 782 M_DEVBUF, M_NOWAIT); 783 if (entry == NULL) { 784 device_printf(sc->lge_dev, "no memory for jumbo " 785 "buffer queue!\n"); 786 return(ENOBUFS); 787 } 788 entry->slot = i; 789 SLIST_INSERT_HEAD(&sc->lge_jfree_listhead, 790 entry, jpool_entries); 791 } 792 793 return(0); 794 } 795 796 static void 797 lge_free_jumbo_mem(sc) 798 struct lge_softc *sc; 799 { 800 struct lge_jpool_entry *entry; 801 802 if (sc->lge_cdata.lge_jumbo_buf == NULL) 803 return; 804 805 while ((entry = SLIST_FIRST(&sc->lge_jinuse_listhead))) { 806 device_printf(sc->lge_dev, 807 "asked to free buffer that is in use!\n"); 808 SLIST_REMOVE_HEAD(&sc->lge_jinuse_listhead, jpool_entries); 809 SLIST_INSERT_HEAD(&sc->lge_jfree_listhead, entry, 810 jpool_entries); 811 } 812 while (!SLIST_EMPTY(&sc->lge_jfree_listhead)) { 813 entry = SLIST_FIRST(&sc->lge_jfree_listhead); 814 SLIST_REMOVE_HEAD(&sc->lge_jfree_listhead, jpool_entries); 815 free(entry, M_DEVBUF); 816 } 817 818 contigfree(sc->lge_cdata.lge_jumbo_buf, LGE_JMEM, M_DEVBUF); 819 820 return; 821 } 822 823 /* 824 * Allocate a jumbo buffer. 825 */ 826 static void * 827 lge_jalloc(sc) 828 struct lge_softc *sc; 829 { 830 struct lge_jpool_entry *entry; 831 832 entry = SLIST_FIRST(&sc->lge_jfree_listhead); 833 834 if (entry == NULL) { 835 #ifdef LGE_VERBOSE 836 device_printf(sc->lge_dev, "no free jumbo buffers\n"); 837 #endif 838 return(NULL); 839 } 840 841 SLIST_REMOVE_HEAD(&sc->lge_jfree_listhead, jpool_entries); 842 SLIST_INSERT_HEAD(&sc->lge_jinuse_listhead, entry, jpool_entries); 843 return(sc->lge_cdata.lge_jslots[entry->slot]); 844 } 845 846 /* 847 * Release a jumbo buffer. 848 */ 849 static void 850 lge_jfree(struct mbuf *m) 851 { 852 struct lge_softc *sc; 853 int i; 854 struct lge_jpool_entry *entry; 855 856 /* Extract the softc struct pointer. */ 857 sc = m->m_ext.ext_arg1; 858 859 if (sc == NULL) 860 panic("lge_jfree: can't find softc pointer!"); 861 862 /* calculate the slot this buffer belongs to */ 863 i = ((vm_offset_t)m->m_ext.ext_buf 864 - (vm_offset_t)sc->lge_cdata.lge_jumbo_buf) / LGE_JLEN; 865 866 if ((i < 0) || (i >= LGE_JSLOTS)) 867 panic("lge_jfree: asked to free buffer that we don't manage!"); 868 869 entry = SLIST_FIRST(&sc->lge_jinuse_listhead); 870 if (entry == NULL) 871 panic("lge_jfree: buffer not in use!"); 872 entry->slot = i; 873 SLIST_REMOVE_HEAD(&sc->lge_jinuse_listhead, jpool_entries); 874 SLIST_INSERT_HEAD(&sc->lge_jfree_listhead, entry, jpool_entries); 875 } 876 877 /* 878 * A frame has been uploaded: pass the resulting mbuf chain up to 879 * the higher level protocols. 880 */ 881 static void 882 lge_rxeof(sc, cnt) 883 struct lge_softc *sc; 884 int cnt; 885 { 886 struct mbuf *m; 887 struct ifnet *ifp; 888 struct lge_rx_desc *cur_rx; 889 int c, i, total_len = 0; 890 u_int32_t rxsts, rxctl; 891 892 ifp = sc->lge_ifp; 893 894 /* Find out how many frames were processed. */ 895 c = cnt; 896 i = sc->lge_cdata.lge_rx_cons; 897 898 /* Suck them in. */ 899 while(c) { 900 struct mbuf *m0 = NULL; 901 902 cur_rx = &sc->lge_ldata->lge_rx_list[i]; 903 rxctl = cur_rx->lge_ctl; 904 rxsts = cur_rx->lge_sts; 905 m = cur_rx->lge_mbuf; 906 cur_rx->lge_mbuf = NULL; 907 total_len = LGE_RXBYTES(cur_rx); 908 LGE_INC(i, LGE_RX_LIST_CNT); 909 c--; 910 911 /* 912 * If an error occurs, update stats, clear the 913 * status word and leave the mbuf cluster in place: 914 * it should simply get re-used next time this descriptor 915 * comes up in the ring. 916 */ 917 if (rxctl & LGE_RXCTL_ERRMASK) { 918 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 919 lge_newbuf(sc, &LGE_RXTAIL(sc), m); 920 continue; 921 } 922 923 if (lge_newbuf(sc, &LGE_RXTAIL(sc), NULL) == ENOBUFS) { 924 m0 = m_devget(mtod(m, char *), total_len, ETHER_ALIGN, 925 ifp, NULL); 926 lge_newbuf(sc, &LGE_RXTAIL(sc), m); 927 if (m0 == NULL) { 928 device_printf(sc->lge_dev, "no receive buffers " 929 "available -- packet dropped!\n"); 930 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 931 continue; 932 } 933 m = m0; 934 } else { 935 m->m_pkthdr.rcvif = ifp; 936 m->m_pkthdr.len = m->m_len = total_len; 937 } 938 939 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 940 941 /* Do IP checksum checking. */ 942 if (rxsts & LGE_RXSTS_ISIP) 943 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED; 944 if (!(rxsts & LGE_RXSTS_IPCSUMERR)) 945 m->m_pkthdr.csum_flags |= CSUM_IP_VALID; 946 if ((rxsts & LGE_RXSTS_ISTCP && 947 !(rxsts & LGE_RXSTS_TCPCSUMERR)) || 948 (rxsts & LGE_RXSTS_ISUDP && 949 !(rxsts & LGE_RXSTS_UDPCSUMERR))) { 950 m->m_pkthdr.csum_flags |= 951 CSUM_DATA_VALID|CSUM_PSEUDO_HDR; 952 m->m_pkthdr.csum_data = 0xffff; 953 } 954 955 LGE_UNLOCK(sc); 956 (*ifp->if_input)(ifp, m); 957 LGE_LOCK(sc); 958 } 959 960 sc->lge_cdata.lge_rx_cons = i; 961 962 return; 963 } 964 965 static void 966 lge_rxeoc(sc) 967 struct lge_softc *sc; 968 { 969 struct ifnet *ifp; 970 971 ifp = sc->lge_ifp; 972 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 973 lge_init_locked(sc); 974 return; 975 } 976 977 /* 978 * A frame was downloaded to the chip. It's safe for us to clean up 979 * the list buffers. 980 */ 981 982 static void 983 lge_txeof(sc) 984 struct lge_softc *sc; 985 { 986 struct lge_tx_desc *cur_tx = NULL; 987 struct ifnet *ifp; 988 u_int32_t idx, txdone; 989 990 ifp = sc->lge_ifp; 991 992 /* Clear the timeout timer. */ 993 sc->lge_timer = 0; 994 995 /* 996 * Go through our tx list and free mbufs for those 997 * frames that have been transmitted. 998 */ 999 idx = sc->lge_cdata.lge_tx_cons; 1000 txdone = CSR_READ_1(sc, LGE_TXDMADONE_8BIT); 1001 1002 while (idx != sc->lge_cdata.lge_tx_prod && txdone) { 1003 cur_tx = &sc->lge_ldata->lge_tx_list[idx]; 1004 1005 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 1006 if (cur_tx->lge_mbuf != NULL) { 1007 m_freem(cur_tx->lge_mbuf); 1008 cur_tx->lge_mbuf = NULL; 1009 } 1010 cur_tx->lge_ctl = 0; 1011 1012 txdone--; 1013 LGE_INC(idx, LGE_TX_LIST_CNT); 1014 sc->lge_timer = 0; 1015 } 1016 1017 sc->lge_cdata.lge_tx_cons = idx; 1018 1019 if (cur_tx != NULL) 1020 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1021 1022 return; 1023 } 1024 1025 static void 1026 lge_tick(xsc) 1027 void *xsc; 1028 { 1029 struct lge_softc *sc; 1030 struct mii_data *mii; 1031 struct ifnet *ifp; 1032 1033 sc = xsc; 1034 ifp = sc->lge_ifp; 1035 LGE_LOCK_ASSERT(sc); 1036 1037 CSR_WRITE_4(sc, LGE_STATSIDX, LGE_STATS_SINGLE_COLL_PKTS); 1038 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, CSR_READ_4(sc, LGE_STATSVAL)); 1039 CSR_WRITE_4(sc, LGE_STATSIDX, LGE_STATS_MULTI_COLL_PKTS); 1040 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, CSR_READ_4(sc, LGE_STATSVAL)); 1041 1042 if (!sc->lge_link) { 1043 mii = device_get_softc(sc->lge_miibus); 1044 mii_tick(mii); 1045 if (mii->mii_media_status & IFM_ACTIVE && 1046 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 1047 sc->lge_link++; 1048 if (bootverbose && 1049 (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX|| 1050 IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T)) 1051 device_printf(sc->lge_dev, "gigabit link up\n"); 1052 if (ifp->if_snd.ifq_head != NULL) 1053 lge_start_locked(ifp); 1054 } 1055 } 1056 1057 if (sc->lge_timer != 0 && --sc->lge_timer == 0) 1058 lge_watchdog(sc); 1059 callout_reset(&sc->lge_stat_callout, hz, lge_tick, sc); 1060 1061 return; 1062 } 1063 1064 static void 1065 lge_intr(arg) 1066 void *arg; 1067 { 1068 struct lge_softc *sc; 1069 struct ifnet *ifp; 1070 u_int32_t status; 1071 1072 sc = arg; 1073 ifp = sc->lge_ifp; 1074 LGE_LOCK(sc); 1075 1076 /* Suppress unwanted interrupts */ 1077 if (!(ifp->if_flags & IFF_UP)) { 1078 lge_stop(sc); 1079 LGE_UNLOCK(sc); 1080 return; 1081 } 1082 1083 for (;;) { 1084 /* 1085 * Reading the ISR register clears all interrupts, and 1086 * clears the 'interrupts enabled' bit in the IMR 1087 * register. 1088 */ 1089 status = CSR_READ_4(sc, LGE_ISR); 1090 1091 if ((status & LGE_INTRS) == 0) 1092 break; 1093 1094 if ((status & (LGE_ISR_TXCMDFIFO_EMPTY|LGE_ISR_TXDMA_DONE))) 1095 lge_txeof(sc); 1096 1097 if (status & LGE_ISR_RXDMA_DONE) 1098 lge_rxeof(sc, LGE_RX_DMACNT(status)); 1099 1100 if (status & LGE_ISR_RXCMDFIFO_EMPTY) 1101 lge_rxeoc(sc); 1102 1103 if (status & LGE_ISR_PHY_INTR) { 1104 sc->lge_link = 0; 1105 callout_stop(&sc->lge_stat_callout); 1106 lge_tick(sc); 1107 } 1108 } 1109 1110 /* Re-enable interrupts. */ 1111 CSR_WRITE_4(sc, LGE_IMR, LGE_IMR_SETRST_CTL0|LGE_IMR_INTR_ENB); 1112 1113 if (ifp->if_snd.ifq_head != NULL) 1114 lge_start_locked(ifp); 1115 1116 LGE_UNLOCK(sc); 1117 return; 1118 } 1119 1120 /* 1121 * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data 1122 * pointers to the fragment pointers. 1123 */ 1124 static int 1125 lge_encap(sc, m_head, txidx) 1126 struct lge_softc *sc; 1127 struct mbuf *m_head; 1128 u_int32_t *txidx; 1129 { 1130 struct lge_frag *f = NULL; 1131 struct lge_tx_desc *cur_tx; 1132 struct mbuf *m; 1133 int frag = 0, tot_len = 0; 1134 1135 /* 1136 * Start packing the mbufs in this chain into 1137 * the fragment pointers. Stop when we run out 1138 * of fragments or hit the end of the mbuf chain. 1139 */ 1140 m = m_head; 1141 cur_tx = &sc->lge_ldata->lge_tx_list[*txidx]; 1142 frag = 0; 1143 1144 for (m = m_head; m != NULL; m = m->m_next) { 1145 if (m->m_len != 0) { 1146 tot_len += m->m_len; 1147 f = &cur_tx->lge_frags[frag]; 1148 f->lge_fraglen = m->m_len; 1149 f->lge_fragptr_lo = vtophys(mtod(m, vm_offset_t)); 1150 f->lge_fragptr_hi = 0; 1151 frag++; 1152 } 1153 } 1154 1155 if (m != NULL) 1156 return(ENOBUFS); 1157 1158 cur_tx->lge_mbuf = m_head; 1159 cur_tx->lge_ctl = LGE_TXCTL_WANTINTR|LGE_FRAGCNT(frag)|tot_len; 1160 LGE_INC((*txidx), LGE_TX_LIST_CNT); 1161 1162 /* Queue for transmit */ 1163 CSR_WRITE_4(sc, LGE_TXDESC_ADDR_LO, vtophys(cur_tx)); 1164 1165 return(0); 1166 } 1167 1168 /* 1169 * Main transmit routine. To avoid having to do mbuf copies, we put pointers 1170 * to the mbuf data regions directly in the transmit lists. We also save a 1171 * copy of the pointers since the transmit list fragment pointers are 1172 * physical addresses. 1173 */ 1174 1175 static void 1176 lge_start(ifp) 1177 struct ifnet *ifp; 1178 { 1179 struct lge_softc *sc; 1180 1181 sc = ifp->if_softc; 1182 LGE_LOCK(sc); 1183 lge_start_locked(ifp); 1184 LGE_UNLOCK(sc); 1185 } 1186 1187 static void 1188 lge_start_locked(ifp) 1189 struct ifnet *ifp; 1190 { 1191 struct lge_softc *sc; 1192 struct mbuf *m_head = NULL; 1193 u_int32_t idx; 1194 1195 sc = ifp->if_softc; 1196 1197 if (!sc->lge_link) 1198 return; 1199 1200 idx = sc->lge_cdata.lge_tx_prod; 1201 1202 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) 1203 return; 1204 1205 while(sc->lge_ldata->lge_tx_list[idx].lge_mbuf == NULL) { 1206 if (CSR_READ_1(sc, LGE_TXCMDFREE_8BIT) == 0) 1207 break; 1208 1209 IF_DEQUEUE(&ifp->if_snd, m_head); 1210 if (m_head == NULL) 1211 break; 1212 1213 if (lge_encap(sc, m_head, &idx)) { 1214 IF_PREPEND(&ifp->if_snd, m_head); 1215 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1216 break; 1217 } 1218 1219 /* 1220 * If there's a BPF listener, bounce a copy of this frame 1221 * to him. 1222 */ 1223 BPF_MTAP(ifp, m_head); 1224 } 1225 1226 sc->lge_cdata.lge_tx_prod = idx; 1227 1228 /* 1229 * Set a timeout in case the chip goes out to lunch. 1230 */ 1231 sc->lge_timer = 5; 1232 1233 return; 1234 } 1235 1236 static void 1237 lge_init(xsc) 1238 void *xsc; 1239 { 1240 struct lge_softc *sc = xsc; 1241 1242 LGE_LOCK(sc); 1243 lge_init_locked(sc); 1244 LGE_UNLOCK(sc); 1245 } 1246 1247 static void 1248 lge_init_locked(sc) 1249 struct lge_softc *sc; 1250 { 1251 struct ifnet *ifp = sc->lge_ifp; 1252 1253 LGE_LOCK_ASSERT(sc); 1254 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1255 return; 1256 1257 /* 1258 * Cancel pending I/O and free all RX/TX buffers. 1259 */ 1260 lge_stop(sc); 1261 lge_reset(sc); 1262 1263 /* Set MAC address */ 1264 CSR_WRITE_4(sc, LGE_PAR0, *(u_int32_t *)(&IF_LLADDR(sc->lge_ifp)[0])); 1265 CSR_WRITE_4(sc, LGE_PAR1, *(u_int32_t *)(&IF_LLADDR(sc->lge_ifp)[4])); 1266 1267 /* Init circular RX list. */ 1268 if (lge_list_rx_init(sc) == ENOBUFS) { 1269 device_printf(sc->lge_dev, "initialization failed: no " 1270 "memory for rx buffers\n"); 1271 lge_stop(sc); 1272 return; 1273 } 1274 1275 /* 1276 * Init tx descriptors. 1277 */ 1278 lge_list_tx_init(sc); 1279 1280 /* Set initial value for MODE1 register. */ 1281 CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_UCAST| 1282 LGE_MODE1_TX_CRC|LGE_MODE1_TXPAD| 1283 LGE_MODE1_RX_FLOWCTL|LGE_MODE1_SETRST_CTL0| 1284 LGE_MODE1_SETRST_CTL1|LGE_MODE1_SETRST_CTL2); 1285 1286 /* If we want promiscuous mode, set the allframes bit. */ 1287 if (ifp->if_flags & IFF_PROMISC) { 1288 CSR_WRITE_4(sc, LGE_MODE1, 1289 LGE_MODE1_SETRST_CTL1|LGE_MODE1_RX_PROMISC); 1290 } else { 1291 CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_PROMISC); 1292 } 1293 1294 /* 1295 * Set the capture broadcast bit to capture broadcast frames. 1296 */ 1297 if (ifp->if_flags & IFF_BROADCAST) { 1298 CSR_WRITE_4(sc, LGE_MODE1, 1299 LGE_MODE1_SETRST_CTL1|LGE_MODE1_RX_BCAST); 1300 } else { 1301 CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_BCAST); 1302 } 1303 1304 /* Packet padding workaround? */ 1305 CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL1|LGE_MODE1_RMVPAD); 1306 1307 /* No error frames */ 1308 CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_ERRPKTS); 1309 1310 /* Receive large frames */ 1311 CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL1|LGE_MODE1_RX_GIANTS); 1312 1313 /* Workaround: disable RX/TX flow control */ 1314 CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_TX_FLOWCTL); 1315 CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_FLOWCTL); 1316 1317 /* Make sure to strip CRC from received frames */ 1318 CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_CRC); 1319 1320 /* Turn off magic packet mode */ 1321 CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_MPACK_ENB); 1322 1323 /* Turn off all VLAN stuff */ 1324 CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_VLAN_RX|LGE_MODE1_VLAN_TX| 1325 LGE_MODE1_VLAN_STRIP|LGE_MODE1_VLAN_INSERT); 1326 1327 /* Workarond: FIFO overflow */ 1328 CSR_WRITE_2(sc, LGE_RXFIFO_HIWAT, 0x3FFF); 1329 CSR_WRITE_4(sc, LGE_IMR, LGE_IMR_SETRST_CTL1|LGE_IMR_RXFIFO_WAT); 1330 1331 /* 1332 * Load the multicast filter. 1333 */ 1334 lge_setmulti(sc); 1335 1336 /* 1337 * Enable hardware checksum validation for all received IPv4 1338 * packets, do not reject packets with bad checksums. 1339 */ 1340 CSR_WRITE_4(sc, LGE_MODE2, LGE_MODE2_RX_IPCSUM| 1341 LGE_MODE2_RX_TCPCSUM|LGE_MODE2_RX_UDPCSUM| 1342 LGE_MODE2_RX_ERRCSUM); 1343 1344 /* 1345 * Enable the delivery of PHY interrupts based on 1346 * link/speed/duplex status chalges. 1347 */ 1348 CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL0|LGE_MODE1_GMIIPOLL); 1349 1350 /* Enable receiver and transmitter. */ 1351 CSR_WRITE_4(sc, LGE_RXDESC_ADDR_HI, 0); 1352 CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL1|LGE_MODE1_RX_ENB); 1353 1354 CSR_WRITE_4(sc, LGE_TXDESC_ADDR_HI, 0); 1355 CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_SETRST_CTL1|LGE_MODE1_TX_ENB); 1356 1357 /* 1358 * Enable interrupts. 1359 */ 1360 CSR_WRITE_4(sc, LGE_IMR, LGE_IMR_SETRST_CTL0| 1361 LGE_IMR_SETRST_CTL1|LGE_IMR_INTR_ENB|LGE_INTRS); 1362 1363 lge_ifmedia_upd_locked(ifp); 1364 1365 ifp->if_drv_flags |= IFF_DRV_RUNNING; 1366 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1367 1368 callout_reset(&sc->lge_stat_callout, hz, lge_tick, sc); 1369 1370 return; 1371 } 1372 1373 /* 1374 * Set media options. 1375 */ 1376 static int 1377 lge_ifmedia_upd(ifp) 1378 struct ifnet *ifp; 1379 { 1380 struct lge_softc *sc; 1381 1382 sc = ifp->if_softc; 1383 LGE_LOCK(sc); 1384 lge_ifmedia_upd_locked(ifp); 1385 LGE_UNLOCK(sc); 1386 1387 return(0); 1388 } 1389 1390 static void 1391 lge_ifmedia_upd_locked(ifp) 1392 struct ifnet *ifp; 1393 { 1394 struct lge_softc *sc; 1395 struct mii_data *mii; 1396 struct mii_softc *miisc; 1397 1398 sc = ifp->if_softc; 1399 1400 LGE_LOCK_ASSERT(sc); 1401 mii = device_get_softc(sc->lge_miibus); 1402 sc->lge_link = 0; 1403 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 1404 PHY_RESET(miisc); 1405 mii_mediachg(mii); 1406 } 1407 1408 /* 1409 * Report current media status. 1410 */ 1411 static void 1412 lge_ifmedia_sts(ifp, ifmr) 1413 struct ifnet *ifp; 1414 struct ifmediareq *ifmr; 1415 { 1416 struct lge_softc *sc; 1417 struct mii_data *mii; 1418 1419 sc = ifp->if_softc; 1420 1421 LGE_LOCK(sc); 1422 mii = device_get_softc(sc->lge_miibus); 1423 mii_pollstat(mii); 1424 ifmr->ifm_active = mii->mii_media_active; 1425 ifmr->ifm_status = mii->mii_media_status; 1426 LGE_UNLOCK(sc); 1427 1428 return; 1429 } 1430 1431 static int 1432 lge_ioctl(ifp, command, data) 1433 struct ifnet *ifp; 1434 u_long command; 1435 caddr_t data; 1436 { 1437 struct lge_softc *sc = ifp->if_softc; 1438 struct ifreq *ifr = (struct ifreq *) data; 1439 struct mii_data *mii; 1440 int error = 0; 1441 1442 switch(command) { 1443 case SIOCSIFMTU: 1444 LGE_LOCK(sc); 1445 if (ifr->ifr_mtu > LGE_JUMBO_MTU) 1446 error = EINVAL; 1447 else 1448 ifp->if_mtu = ifr->ifr_mtu; 1449 LGE_UNLOCK(sc); 1450 break; 1451 case SIOCSIFFLAGS: 1452 LGE_LOCK(sc); 1453 if (ifp->if_flags & IFF_UP) { 1454 if (ifp->if_drv_flags & IFF_DRV_RUNNING && 1455 ifp->if_flags & IFF_PROMISC && 1456 !(sc->lge_if_flags & IFF_PROMISC)) { 1457 CSR_WRITE_4(sc, LGE_MODE1, 1458 LGE_MODE1_SETRST_CTL1| 1459 LGE_MODE1_RX_PROMISC); 1460 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING && 1461 !(ifp->if_flags & IFF_PROMISC) && 1462 sc->lge_if_flags & IFF_PROMISC) { 1463 CSR_WRITE_4(sc, LGE_MODE1, 1464 LGE_MODE1_RX_PROMISC); 1465 } else { 1466 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1467 lge_init_locked(sc); 1468 } 1469 } else { 1470 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1471 lge_stop(sc); 1472 } 1473 sc->lge_if_flags = ifp->if_flags; 1474 LGE_UNLOCK(sc); 1475 error = 0; 1476 break; 1477 case SIOCADDMULTI: 1478 case SIOCDELMULTI: 1479 LGE_LOCK(sc); 1480 lge_setmulti(sc); 1481 LGE_UNLOCK(sc); 1482 error = 0; 1483 break; 1484 case SIOCGIFMEDIA: 1485 case SIOCSIFMEDIA: 1486 mii = device_get_softc(sc->lge_miibus); 1487 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 1488 break; 1489 default: 1490 error = ether_ioctl(ifp, command, data); 1491 break; 1492 } 1493 1494 return(error); 1495 } 1496 1497 static void 1498 lge_watchdog(sc) 1499 struct lge_softc *sc; 1500 { 1501 struct ifnet *ifp; 1502 1503 LGE_LOCK_ASSERT(sc); 1504 ifp = sc->lge_ifp; 1505 1506 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1507 if_printf(ifp, "watchdog timeout\n"); 1508 1509 lge_stop(sc); 1510 lge_reset(sc); 1511 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1512 lge_init_locked(sc); 1513 1514 if (ifp->if_snd.ifq_head != NULL) 1515 lge_start_locked(ifp); 1516 } 1517 1518 /* 1519 * Stop the adapter and free any mbufs allocated to the 1520 * RX and TX lists. 1521 */ 1522 static void 1523 lge_stop(sc) 1524 struct lge_softc *sc; 1525 { 1526 int i; 1527 struct ifnet *ifp; 1528 1529 LGE_LOCK_ASSERT(sc); 1530 ifp = sc->lge_ifp; 1531 sc->lge_timer = 0; 1532 callout_stop(&sc->lge_stat_callout); 1533 CSR_WRITE_4(sc, LGE_IMR, LGE_IMR_INTR_ENB); 1534 1535 /* Disable receiver and transmitter. */ 1536 CSR_WRITE_4(sc, LGE_MODE1, LGE_MODE1_RX_ENB|LGE_MODE1_TX_ENB); 1537 sc->lge_link = 0; 1538 1539 /* 1540 * Free data in the RX lists. 1541 */ 1542 for (i = 0; i < LGE_RX_LIST_CNT; i++) { 1543 if (sc->lge_ldata->lge_rx_list[i].lge_mbuf != NULL) { 1544 m_freem(sc->lge_ldata->lge_rx_list[i].lge_mbuf); 1545 sc->lge_ldata->lge_rx_list[i].lge_mbuf = NULL; 1546 } 1547 } 1548 bzero((char *)&sc->lge_ldata->lge_rx_list, 1549 sizeof(sc->lge_ldata->lge_rx_list)); 1550 1551 /* 1552 * Free the TX list buffers. 1553 */ 1554 for (i = 0; i < LGE_TX_LIST_CNT; i++) { 1555 if (sc->lge_ldata->lge_tx_list[i].lge_mbuf != NULL) { 1556 m_freem(sc->lge_ldata->lge_tx_list[i].lge_mbuf); 1557 sc->lge_ldata->lge_tx_list[i].lge_mbuf = NULL; 1558 } 1559 } 1560 1561 bzero((char *)&sc->lge_ldata->lge_tx_list, 1562 sizeof(sc->lge_ldata->lge_tx_list)); 1563 1564 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 1565 1566 return; 1567 } 1568 1569 /* 1570 * Stop all chip I/O so that the kernel's probe routines don't 1571 * get confused by errant DMAs when rebooting. 1572 */ 1573 static int 1574 lge_shutdown(dev) 1575 device_t dev; 1576 { 1577 struct lge_softc *sc; 1578 1579 sc = device_get_softc(dev); 1580 1581 LGE_LOCK(sc); 1582 lge_reset(sc); 1583 lge_stop(sc); 1584 LGE_UNLOCK(sc); 1585 1586 return (0); 1587 } 1588