1 /* 2 * Copyright (c) 2001 Wind River Systems 3 * Copyright (c) 1997, 1998, 1999, 2001 4 * Bill Paul <wpaul@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 * $FreeBSD$ 34 */ 35 36 /* 37 * Broadcom BCM570x family gigabit ethernet driver for FreeBSD. 38 * 39 * Written by Bill Paul <wpaul@windriver.com> 40 * Senior Engineer, Wind River Systems 41 */ 42 43 /* 44 * The Broadcom BCM5700 is based on technology originally developed by 45 * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet 46 * MAC chips. The BCM5700, sometimes refered to as the Tigon III, has 47 * two on-board MIPS R4000 CPUs and can have as much as 16MB of external 48 * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo 49 * frames, highly configurable RX filtering, and 16 RX and TX queues 50 * (which, along with RX filter rules, can be used for QOS applications). 51 * Other features, such as TCP segmentation, may be available as part 52 * of value-added firmware updates. Unlike the Tigon I and Tigon II, 53 * firmware images can be stored in hardware and need not be compiled 54 * into the driver. 55 * 56 * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will 57 * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus. 58 * 59 * The BCM5701 is a single-chip solution incorporating both the BCM5700 60 * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701 61 * does not support external SSRAM. 62 * 63 * Broadcom also produces a variation of the BCM5700 under the "Altima" 64 * brand name, which is functionally similar but lacks PCI-X support. 65 * 66 * Without external SSRAM, you can only have at most 4 TX rings, 67 * and the use of the mini RX ring is disabled. This seems to imply 68 * that these features are simply not available on the BCM5701. As a 69 * result, this driver does not implement any support for the mini RX 70 * ring. 71 */ 72 73 #include <sys/param.h> 74 #include <sys/systm.h> 75 #include <sys/sockio.h> 76 #include <sys/mbuf.h> 77 #include <sys/malloc.h> 78 #include <sys/kernel.h> 79 #include <sys/socket.h> 80 #include <sys/queue.h> 81 82 #include <net/if.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 88 #include <net/bpf.h> 89 90 #include <net/if_types.h> 91 #include <net/if_vlan_var.h> 92 93 #include <netinet/in_systm.h> 94 #include <netinet/in.h> 95 #include <netinet/ip.h> 96 97 #include <vm/vm.h> /* for vtophys */ 98 #include <vm/pmap.h> /* for vtophys */ 99 #include <machine/clock.h> /* for DELAY */ 100 #include <machine/bus_memio.h> 101 #include <machine/bus.h> 102 #include <machine/resource.h> 103 #include <sys/bus.h> 104 #include <sys/rman.h> 105 106 #include <dev/mii/mii.h> 107 #include <dev/mii/miivar.h> 108 #include <dev/mii/miidevs.h> 109 #include <dev/mii/brgphyreg.h> 110 111 #include <pci/pcireg.h> 112 #include <pci/pcivar.h> 113 114 #include <dev/bge/if_bgereg.h> 115 116 #define BGE_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_IP_FRAGS) 117 118 MODULE_DEPEND(bge, miibus, 1, 1, 1); 119 120 /* "controller miibus0" required. See GENERIC if you get errors here. */ 121 #include "miibus_if.h" 122 123 #if !defined(lint) 124 static const char rcsid[] = 125 "$FreeBSD$"; 126 #endif 127 128 /* 129 * Various supported device vendors/types and their names. Note: the 130 * spec seems to indicate that the hardware still has Alteon's vendor 131 * ID burned into it, though it will always be overriden by the vendor 132 * ID in the EEPROM. Just to be safe, we cover all possibilities. 133 */ 134 135 static struct bge_type bge_devs[] = { 136 { ALT_VENDORID, ALT_DEVICEID_BCM5700, 137 "Broadcom BCM5700 Gigabit Ethernet" }, 138 { ALT_VENDORID, ALT_DEVICEID_BCM5701, 139 "Broadcom BCM5701 Gigabit Ethernet" }, 140 { BCOM_VENDORID, BCOM_DEVICEID_BCM5700, 141 "Broadcom BCM5700 Gigabit Ethernet" }, 142 { BCOM_VENDORID, BCOM_DEVICEID_BCM5701, 143 "Broadcom BCM5701 Gigabit Ethernet" }, 144 { SK_VENDORID, SK_DEVICEID_ALTIMA, 145 "SysKonnect Gigabit Ethernet" }, 146 { ALTIMA_VENDORID, ALTIMA_DEVICE_AC1000, 147 "Altima AC1000 Gigabit Ethernet" }, 148 { 0, 0, NULL } 149 }; 150 151 static int bge_probe (device_t); 152 static int bge_attach (device_t); 153 static int bge_detach (device_t); 154 static void bge_release_resources 155 (struct bge_softc *); 156 static void bge_txeof (struct bge_softc *); 157 static void bge_rxeof (struct bge_softc *); 158 159 static void bge_tick (void *); 160 static void bge_stats_update (struct bge_softc *); 161 static int bge_encap (struct bge_softc *, struct mbuf *, 162 u_int32_t *); 163 164 static void bge_intr (void *); 165 static void bge_start (struct ifnet *); 166 static int bge_ioctl (struct ifnet *, u_long, caddr_t); 167 static void bge_init (void *); 168 static void bge_stop (struct bge_softc *); 169 static void bge_watchdog (struct ifnet *); 170 static void bge_shutdown (device_t); 171 static int bge_ifmedia_upd (struct ifnet *); 172 static void bge_ifmedia_sts (struct ifnet *, struct ifmediareq *); 173 174 static u_int8_t bge_eeprom_getbyte (struct bge_softc *, int, u_int8_t *); 175 static int bge_read_eeprom (struct bge_softc *, caddr_t, int, int); 176 177 static u_int32_t bge_crc (caddr_t); 178 static void bge_setmulti (struct bge_softc *); 179 180 static void bge_handle_events (struct bge_softc *); 181 static int bge_alloc_jumbo_mem (struct bge_softc *); 182 static void bge_free_jumbo_mem (struct bge_softc *); 183 static void *bge_jalloc (struct bge_softc *); 184 static void bge_jfree (caddr_t, void *); 185 static int bge_newbuf_std (struct bge_softc *, int, struct mbuf *); 186 static int bge_newbuf_jumbo (struct bge_softc *, int, struct mbuf *); 187 static int bge_init_rx_ring_std (struct bge_softc *); 188 static void bge_free_rx_ring_std (struct bge_softc *); 189 static int bge_init_rx_ring_jumbo (struct bge_softc *); 190 static void bge_free_rx_ring_jumbo (struct bge_softc *); 191 static void bge_free_tx_ring (struct bge_softc *); 192 static int bge_init_tx_ring (struct bge_softc *); 193 194 static int bge_chipinit (struct bge_softc *); 195 static int bge_blockinit (struct bge_softc *); 196 197 #ifdef notdef 198 static u_int8_t bge_vpd_readbyte(struct bge_softc *, int); 199 static void bge_vpd_read_res (struct bge_softc *, struct vpd_res *, int); 200 static void bge_vpd_read (struct bge_softc *); 201 #endif 202 203 static u_int32_t bge_readmem_ind 204 (struct bge_softc *, int); 205 static void bge_writemem_ind (struct bge_softc *, int, int); 206 #ifdef notdef 207 static u_int32_t bge_readreg_ind 208 (struct bge_softc *, int); 209 #endif 210 static void bge_writereg_ind (struct bge_softc *, int, int); 211 212 static int bge_miibus_readreg (device_t, int, int); 213 static int bge_miibus_writereg (device_t, int, int, int); 214 static void bge_miibus_statchg (device_t); 215 216 static void bge_reset (struct bge_softc *); 217 static void bge_phy_hack (struct bge_softc *); 218 219 static device_method_t bge_methods[] = { 220 /* Device interface */ 221 DEVMETHOD(device_probe, bge_probe), 222 DEVMETHOD(device_attach, bge_attach), 223 DEVMETHOD(device_detach, bge_detach), 224 DEVMETHOD(device_shutdown, bge_shutdown), 225 226 /* bus interface */ 227 DEVMETHOD(bus_print_child, bus_generic_print_child), 228 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 229 230 /* MII interface */ 231 DEVMETHOD(miibus_readreg, bge_miibus_readreg), 232 DEVMETHOD(miibus_writereg, bge_miibus_writereg), 233 DEVMETHOD(miibus_statchg, bge_miibus_statchg), 234 235 { 0, 0 } 236 }; 237 238 static driver_t bge_driver = { 239 "bge", 240 bge_methods, 241 sizeof(struct bge_softc) 242 }; 243 244 static devclass_t bge_devclass; 245 246 DRIVER_MODULE(if_bge, pci, bge_driver, bge_devclass, 0, 0); 247 DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0); 248 249 static u_int32_t 250 bge_readmem_ind(sc, off) 251 struct bge_softc *sc; 252 int off; 253 { 254 device_t dev; 255 256 dev = sc->bge_dev; 257 258 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4); 259 return(pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4)); 260 } 261 262 static void 263 bge_writemem_ind(sc, off, val) 264 struct bge_softc *sc; 265 int off, val; 266 { 267 device_t dev; 268 269 dev = sc->bge_dev; 270 271 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4); 272 pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4); 273 274 return; 275 } 276 277 #ifdef notdef 278 static u_int32_t 279 bge_readreg_ind(sc, off) 280 struct bge_softc *sc; 281 int off; 282 { 283 device_t dev; 284 285 dev = sc->bge_dev; 286 287 pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4); 288 return(pci_read_config(dev, BGE_PCI_REG_DATA, 4)); 289 } 290 #endif 291 292 static void 293 bge_writereg_ind(sc, off, val) 294 struct bge_softc *sc; 295 int off, val; 296 { 297 device_t dev; 298 299 dev = sc->bge_dev; 300 301 pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4); 302 pci_write_config(dev, BGE_PCI_REG_DATA, val, 4); 303 304 return; 305 } 306 307 #ifdef notdef 308 static u_int8_t 309 bge_vpd_readbyte(sc, addr) 310 struct bge_softc *sc; 311 int addr; 312 { 313 int i; 314 device_t dev; 315 u_int32_t val; 316 317 dev = sc->bge_dev; 318 pci_write_config(dev, BGE_PCI_VPD_ADDR, addr, 2); 319 for (i = 0; i < BGE_TIMEOUT * 10; i++) { 320 DELAY(10); 321 if (pci_read_config(dev, BGE_PCI_VPD_ADDR, 2) & BGE_VPD_FLAG) 322 break; 323 } 324 325 if (i == BGE_TIMEOUT) { 326 printf("bge%d: VPD read timed out\n", sc->bge_unit); 327 return(0); 328 } 329 330 val = pci_read_config(dev, BGE_PCI_VPD_DATA, 4); 331 332 return((val >> ((addr % 4) * 8)) & 0xFF); 333 } 334 335 static void 336 bge_vpd_read_res(sc, res, addr) 337 struct bge_softc *sc; 338 struct vpd_res *res; 339 int addr; 340 { 341 int i; 342 u_int8_t *ptr; 343 344 ptr = (u_int8_t *)res; 345 for (i = 0; i < sizeof(struct vpd_res); i++) 346 ptr[i] = bge_vpd_readbyte(sc, i + addr); 347 348 return; 349 } 350 351 static void 352 bge_vpd_read(sc) 353 struct bge_softc *sc; 354 { 355 int pos = 0, i; 356 struct vpd_res res; 357 358 if (sc->bge_vpd_prodname != NULL) 359 free(sc->bge_vpd_prodname, M_DEVBUF); 360 if (sc->bge_vpd_readonly != NULL) 361 free(sc->bge_vpd_readonly, M_DEVBUF); 362 sc->bge_vpd_prodname = NULL; 363 sc->bge_vpd_readonly = NULL; 364 365 bge_vpd_read_res(sc, &res, pos); 366 367 if (res.vr_id != VPD_RES_ID) { 368 printf("bge%d: bad VPD resource id: expected %x got %x\n", 369 sc->bge_unit, VPD_RES_ID, res.vr_id); 370 return; 371 } 372 373 pos += sizeof(res); 374 sc->bge_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT); 375 for (i = 0; i < res.vr_len; i++) 376 sc->bge_vpd_prodname[i] = bge_vpd_readbyte(sc, i + pos); 377 sc->bge_vpd_prodname[i] = '\0'; 378 pos += i; 379 380 bge_vpd_read_res(sc, &res, pos); 381 382 if (res.vr_id != VPD_RES_READ) { 383 printf("bge%d: bad VPD resource id: expected %x got %x\n", 384 sc->bge_unit, VPD_RES_READ, res.vr_id); 385 return; 386 } 387 388 pos += sizeof(res); 389 sc->bge_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT); 390 for (i = 0; i < res.vr_len + 1; i++) 391 sc->bge_vpd_readonly[i] = bge_vpd_readbyte(sc, i + pos); 392 393 return; 394 } 395 #endif 396 397 /* 398 * Read a byte of data stored in the EEPROM at address 'addr.' The 399 * BCM570x supports both the traditional bitbang interface and an 400 * auto access interface for reading the EEPROM. We use the auto 401 * access method. 402 */ 403 static u_int8_t 404 bge_eeprom_getbyte(sc, addr, dest) 405 struct bge_softc *sc; 406 int addr; 407 u_int8_t *dest; 408 { 409 int i; 410 u_int32_t byte = 0; 411 412 /* 413 * Enable use of auto EEPROM access so we can avoid 414 * having to use the bitbang method. 415 */ 416 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM); 417 418 /* Reset the EEPROM, load the clock period. */ 419 CSR_WRITE_4(sc, BGE_EE_ADDR, 420 BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL)); 421 DELAY(20); 422 423 /* Issue the read EEPROM command. */ 424 CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr); 425 426 /* Wait for completion */ 427 for(i = 0; i < BGE_TIMEOUT * 10; i++) { 428 DELAY(10); 429 if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE) 430 break; 431 } 432 433 if (i == BGE_TIMEOUT) { 434 printf("bge%d: eeprom read timed out\n", sc->bge_unit); 435 return(0); 436 } 437 438 /* Get result. */ 439 byte = CSR_READ_4(sc, BGE_EE_DATA); 440 441 *dest = (byte >> ((addr % 4) * 8)) & 0xFF; 442 443 return(0); 444 } 445 446 /* 447 * Read a sequence of bytes from the EEPROM. 448 */ 449 static int 450 bge_read_eeprom(sc, dest, off, cnt) 451 struct bge_softc *sc; 452 caddr_t dest; 453 int off; 454 int cnt; 455 { 456 int err = 0, i; 457 u_int8_t byte = 0; 458 459 for (i = 0; i < cnt; i++) { 460 err = bge_eeprom_getbyte(sc, off + i, &byte); 461 if (err) 462 break; 463 *(dest + i) = byte; 464 } 465 466 return(err ? 1 : 0); 467 } 468 469 static int 470 bge_miibus_readreg(dev, phy, reg) 471 device_t dev; 472 int phy, reg; 473 { 474 struct bge_softc *sc; 475 struct ifnet *ifp; 476 u_int32_t val; 477 int i; 478 479 sc = device_get_softc(dev); 480 ifp = &sc->arpcom.ac_if; 481 482 if (sc->bge_asicrev == BGE_ASICREV_BCM5701_B5 && phy != 1) 483 return(0); 484 485 if (ifp->if_flags & IFF_RUNNING) 486 BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL); 487 488 CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ|BGE_MICOMM_BUSY| 489 BGE_MIPHY(phy)|BGE_MIREG(reg)); 490 491 for (i = 0; i < BGE_TIMEOUT; i++) { 492 val = CSR_READ_4(sc, BGE_MI_COMM); 493 if (!(val & BGE_MICOMM_BUSY)) 494 break; 495 } 496 497 if (i == BGE_TIMEOUT) { 498 printf("bge%d: PHY read timed out\n", sc->bge_unit); 499 return(0); 500 } 501 502 val = CSR_READ_4(sc, BGE_MI_COMM); 503 504 if (ifp->if_flags & IFF_RUNNING) 505 BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL); 506 507 if (val & BGE_MICOMM_READFAIL) 508 return(0); 509 510 return(val & 0xFFFF); 511 } 512 513 static int 514 bge_miibus_writereg(dev, phy, reg, val) 515 device_t dev; 516 int phy, reg, val; 517 { 518 struct bge_softc *sc; 519 int i; 520 521 sc = device_get_softc(dev); 522 523 CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE|BGE_MICOMM_BUSY| 524 BGE_MIPHY(phy)|BGE_MIREG(reg)|val); 525 526 for (i = 0; i < BGE_TIMEOUT; i++) { 527 if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) 528 break; 529 } 530 531 if (i == BGE_TIMEOUT) { 532 printf("bge%d: PHY read timed out\n", sc->bge_unit); 533 return(0); 534 } 535 536 return(0); 537 } 538 539 static void 540 bge_miibus_statchg(dev) 541 device_t dev; 542 { 543 struct bge_softc *sc; 544 struct mii_data *mii; 545 546 sc = device_get_softc(dev); 547 mii = device_get_softc(sc->bge_miibus); 548 549 BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE); 550 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_TX) { 551 BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII); 552 } else { 553 BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII); 554 } 555 556 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) { 557 BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX); 558 } else { 559 BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX); 560 } 561 562 bge_phy_hack(sc); 563 564 return; 565 } 566 567 /* 568 * Handle events that have triggered interrupts. 569 */ 570 static void 571 bge_handle_events(sc) 572 struct bge_softc *sc; 573 { 574 575 return; 576 } 577 578 /* 579 * Memory management for jumbo frames. 580 */ 581 582 static int 583 bge_alloc_jumbo_mem(sc) 584 struct bge_softc *sc; 585 { 586 caddr_t ptr; 587 register int i; 588 struct bge_jpool_entry *entry; 589 590 /* Grab a big chunk o' storage. */ 591 sc->bge_cdata.bge_jumbo_buf = contigmalloc(BGE_JMEM, M_DEVBUF, 592 M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0); 593 594 if (sc->bge_cdata.bge_jumbo_buf == NULL) { 595 printf("bge%d: no memory for jumbo buffers!\n", sc->bge_unit); 596 return(ENOBUFS); 597 } 598 599 SLIST_INIT(&sc->bge_jfree_listhead); 600 SLIST_INIT(&sc->bge_jinuse_listhead); 601 602 /* 603 * Now divide it up into 9K pieces and save the addresses 604 * in an array. 605 */ 606 ptr = sc->bge_cdata.bge_jumbo_buf; 607 for (i = 0; i < BGE_JSLOTS; i++) { 608 sc->bge_cdata.bge_jslots[i] = ptr; 609 ptr += BGE_JLEN; 610 entry = malloc(sizeof(struct bge_jpool_entry), 611 M_DEVBUF, M_NOWAIT); 612 if (entry == NULL) { 613 contigfree(sc->bge_cdata.bge_jumbo_buf, 614 BGE_JMEM, M_DEVBUF); 615 sc->bge_cdata.bge_jumbo_buf = NULL; 616 printf("bge%d: no memory for jumbo " 617 "buffer queue!\n", sc->bge_unit); 618 return(ENOBUFS); 619 } 620 entry->slot = i; 621 SLIST_INSERT_HEAD(&sc->bge_jfree_listhead, 622 entry, jpool_entries); 623 } 624 625 return(0); 626 } 627 628 static void 629 bge_free_jumbo_mem(sc) 630 struct bge_softc *sc; 631 { 632 int i; 633 struct bge_jpool_entry *entry; 634 635 for (i = 0; i < BGE_JSLOTS; i++) { 636 entry = SLIST_FIRST(&sc->bge_jfree_listhead); 637 SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries); 638 free(entry, M_DEVBUF); 639 } 640 641 contigfree(sc->bge_cdata.bge_jumbo_buf, BGE_JMEM, M_DEVBUF); 642 643 return; 644 } 645 646 /* 647 * Allocate a jumbo buffer. 648 */ 649 static void * 650 bge_jalloc(sc) 651 struct bge_softc *sc; 652 { 653 struct bge_jpool_entry *entry; 654 655 entry = SLIST_FIRST(&sc->bge_jfree_listhead); 656 657 if (entry == NULL) { 658 printf("bge%d: no free jumbo buffers\n", sc->bge_unit); 659 return(NULL); 660 } 661 662 SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries); 663 SLIST_INSERT_HEAD(&sc->bge_jinuse_listhead, entry, jpool_entries); 664 return(sc->bge_cdata.bge_jslots[entry->slot]); 665 } 666 667 /* 668 * Release a jumbo buffer. 669 */ 670 static void 671 bge_jfree(buf, args) 672 caddr_t buf; 673 void *args; 674 { 675 struct bge_jpool_entry *entry; 676 struct bge_softc *sc; 677 int i; 678 679 /* Extract the softc struct pointer. */ 680 sc = (struct bge_softc *)args; 681 682 if (sc == NULL) 683 panic("bge_jfree: can't find softc pointer!"); 684 685 /* calculate the slot this buffer belongs to */ 686 687 i = ((vm_offset_t)buf 688 - (vm_offset_t)sc->bge_cdata.bge_jumbo_buf) / BGE_JLEN; 689 690 if ((i < 0) || (i >= BGE_JSLOTS)) 691 panic("bge_jfree: asked to free buffer that we don't manage!"); 692 693 entry = SLIST_FIRST(&sc->bge_jinuse_listhead); 694 if (entry == NULL) 695 panic("bge_jfree: buffer not in use!"); 696 entry->slot = i; 697 SLIST_REMOVE_HEAD(&sc->bge_jinuse_listhead, jpool_entries); 698 SLIST_INSERT_HEAD(&sc->bge_jfree_listhead, entry, jpool_entries); 699 700 return; 701 } 702 703 704 /* 705 * Intialize a standard receive ring descriptor. 706 */ 707 static int 708 bge_newbuf_std(sc, i, m) 709 struct bge_softc *sc; 710 int i; 711 struct mbuf *m; 712 { 713 struct mbuf *m_new = NULL; 714 struct bge_rx_bd *r; 715 716 if (m == NULL) { 717 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 718 if (m_new == NULL) { 719 return(ENOBUFS); 720 } 721 722 MCLGET(m_new, M_DONTWAIT); 723 if (!(m_new->m_flags & M_EXT)) { 724 m_freem(m_new); 725 return(ENOBUFS); 726 } 727 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 728 } else { 729 m_new = m; 730 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 731 m_new->m_data = m_new->m_ext.ext_buf; 732 } 733 734 m_adj(m_new, ETHER_ALIGN); 735 sc->bge_cdata.bge_rx_std_chain[i] = m_new; 736 r = &sc->bge_rdata->bge_rx_std_ring[i]; 737 BGE_HOSTADDR(r->bge_addr) = vtophys(mtod(m_new, caddr_t)); 738 r->bge_flags = BGE_RXBDFLAG_END; 739 r->bge_len = m_new->m_len; 740 r->bge_idx = i; 741 742 return(0); 743 } 744 745 /* 746 * Initialize a jumbo receive ring descriptor. This allocates 747 * a jumbo buffer from the pool managed internally by the driver. 748 */ 749 static int 750 bge_newbuf_jumbo(sc, i, m) 751 struct bge_softc *sc; 752 int i; 753 struct mbuf *m; 754 { 755 struct mbuf *m_new = NULL; 756 struct bge_rx_bd *r; 757 758 if (m == NULL) { 759 caddr_t *buf = NULL; 760 761 /* Allocate the mbuf. */ 762 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 763 if (m_new == NULL) { 764 return(ENOBUFS); 765 } 766 767 /* Allocate the jumbo buffer */ 768 buf = bge_jalloc(sc); 769 if (buf == NULL) { 770 m_freem(m_new); 771 printf("bge%d: jumbo allocation failed " 772 "-- packet dropped!\n", sc->bge_unit); 773 return(ENOBUFS); 774 } 775 776 /* Attach the buffer to the mbuf. */ 777 m_new->m_data = (void *) buf; 778 m_new->m_len = m_new->m_pkthdr.len = BGE_JUMBO_FRAMELEN; 779 MEXTADD(m_new, buf, BGE_JUMBO_FRAMELEN, bge_jfree, 780 (struct bge_softc *)sc, 0, EXT_NET_DRV); 781 } else { 782 m_new = m; 783 m_new->m_data = m_new->m_ext.ext_buf; 784 m_new->m_ext.ext_size = BGE_JUMBO_FRAMELEN; 785 } 786 787 m_adj(m_new, ETHER_ALIGN); 788 /* Set up the descriptor. */ 789 r = &sc->bge_rdata->bge_rx_jumbo_ring[i]; 790 sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new; 791 BGE_HOSTADDR(r->bge_addr) = vtophys(mtod(m_new, caddr_t)); 792 r->bge_flags = BGE_RXBDFLAG_END|BGE_RXBDFLAG_JUMBO_RING; 793 r->bge_len = m_new->m_len; 794 r->bge_idx = i; 795 796 return(0); 797 } 798 799 /* 800 * The standard receive ring has 512 entries in it. At 2K per mbuf cluster, 801 * that's 1MB or memory, which is a lot. For now, we fill only the first 802 * 256 ring entries and hope that our CPU is fast enough to keep up with 803 * the NIC. 804 */ 805 static int 806 bge_init_rx_ring_std(sc) 807 struct bge_softc *sc; 808 { 809 int i; 810 811 for (i = 0; i < BGE_SSLOTS; i++) { 812 if (bge_newbuf_std(sc, i, NULL) == ENOBUFS) 813 return(ENOBUFS); 814 }; 815 816 sc->bge_std = i - 1; 817 CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std); 818 819 return(0); 820 } 821 822 static void 823 bge_free_rx_ring_std(sc) 824 struct bge_softc *sc; 825 { 826 int i; 827 828 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) { 829 if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) { 830 m_freem(sc->bge_cdata.bge_rx_std_chain[i]); 831 sc->bge_cdata.bge_rx_std_chain[i] = NULL; 832 } 833 bzero((char *)&sc->bge_rdata->bge_rx_std_ring[i], 834 sizeof(struct bge_rx_bd)); 835 } 836 837 return; 838 } 839 840 static int 841 bge_init_rx_ring_jumbo(sc) 842 struct bge_softc *sc; 843 { 844 int i; 845 struct bge_rcb *rcb; 846 struct bge_rcb_opaque *rcbo; 847 848 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) { 849 if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS) 850 return(ENOBUFS); 851 }; 852 853 sc->bge_jumbo = i - 1; 854 855 rcb = &sc->bge_rdata->bge_info.bge_jumbo_rx_rcb; 856 rcbo = (struct bge_rcb_opaque *)rcb; 857 rcb->bge_flags = 0; 858 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcbo->bge_reg2); 859 860 CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo); 861 862 return(0); 863 } 864 865 static void 866 bge_free_rx_ring_jumbo(sc) 867 struct bge_softc *sc; 868 { 869 int i; 870 871 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) { 872 if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) { 873 m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]); 874 sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL; 875 } 876 bzero((char *)&sc->bge_rdata->bge_rx_jumbo_ring[i], 877 sizeof(struct bge_rx_bd)); 878 } 879 880 return; 881 } 882 883 static void 884 bge_free_tx_ring(sc) 885 struct bge_softc *sc; 886 { 887 int i; 888 889 if (sc->bge_rdata->bge_tx_ring == NULL) 890 return; 891 892 for (i = 0; i < BGE_TX_RING_CNT; i++) { 893 if (sc->bge_cdata.bge_tx_chain[i] != NULL) { 894 m_freem(sc->bge_cdata.bge_tx_chain[i]); 895 sc->bge_cdata.bge_tx_chain[i] = NULL; 896 } 897 bzero((char *)&sc->bge_rdata->bge_tx_ring[i], 898 sizeof(struct bge_tx_bd)); 899 } 900 901 return; 902 } 903 904 static int 905 bge_init_tx_ring(sc) 906 struct bge_softc *sc; 907 { 908 sc->bge_txcnt = 0; 909 sc->bge_tx_saved_considx = 0; 910 CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, 0); 911 CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0); 912 913 return(0); 914 } 915 916 #define BGE_POLY 0xEDB88320 917 918 static u_int32_t 919 bge_crc(addr) 920 caddr_t addr; 921 { 922 u_int32_t idx, bit, data, crc; 923 924 /* Compute CRC for the address value. */ 925 crc = 0xFFFFFFFF; /* initial value */ 926 927 for (idx = 0; idx < 6; idx++) { 928 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1) 929 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? BGE_POLY : 0); 930 } 931 932 return(crc & 0x7F); 933 } 934 935 static void 936 bge_setmulti(sc) 937 struct bge_softc *sc; 938 { 939 struct ifnet *ifp; 940 struct ifmultiaddr *ifma; 941 u_int32_t hashes[4] = { 0, 0, 0, 0 }; 942 int h, i; 943 944 ifp = &sc->arpcom.ac_if; 945 946 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 947 for (i = 0; i < 4; i++) 948 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF); 949 return; 950 } 951 952 /* First, zot all the existing filters. */ 953 for (i = 0; i < 4; i++) 954 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0); 955 956 /* Now program new ones. */ 957 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 958 if (ifma->ifma_addr->sa_family != AF_LINK) 959 continue; 960 h = bge_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr)); 961 hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F); 962 } 963 964 for (i = 0; i < 4; i++) 965 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]); 966 967 return; 968 } 969 970 /* 971 * Do endian, PCI and DMA initialization. Also check the on-board ROM 972 * self-test results. 973 */ 974 static int 975 bge_chipinit(sc) 976 struct bge_softc *sc; 977 { 978 u_int32_t cachesize; 979 int i; 980 981 /* Set endianness before we access any non-PCI registers. */ 982 #if BYTE_ORDER == BIG_ENDIAN 983 pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, 984 BGE_BIGENDIAN_INIT, 4); 985 #else 986 pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, 987 BGE_LITTLEENDIAN_INIT, 4); 988 #endif 989 990 /* 991 * Check the 'ROM failed' bit on the RX CPU to see if 992 * self-tests passed. 993 */ 994 if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) { 995 printf("bge%d: RX CPU self-diagnostics failed!\n", 996 sc->bge_unit); 997 return(ENODEV); 998 } 999 1000 /* Clear the MAC control register */ 1001 CSR_WRITE_4(sc, BGE_MAC_MODE, 0); 1002 1003 /* 1004 * Clear the MAC statistics block in the NIC's 1005 * internal memory. 1006 */ 1007 for (i = BGE_STATS_BLOCK; 1008 i < BGE_STATS_BLOCK_END + 1; i += sizeof(u_int32_t)) 1009 BGE_MEMWIN_WRITE(sc, i, 0); 1010 1011 for (i = BGE_STATUS_BLOCK; 1012 i < BGE_STATUS_BLOCK_END + 1; i += sizeof(u_int32_t)) 1013 BGE_MEMWIN_WRITE(sc, i, 0); 1014 1015 /* Set up the PCI DMA control register. */ 1016 pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, 1017 BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD|0x0F, 4); 1018 1019 /* 1020 * Set up general mode register. 1021 */ 1022 CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_MODECTL_WORDSWAP_NONFRAME| 1023 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_DATA| 1024 BGE_MODECTL_MAC_ATTN_INTR|BGE_MODECTL_HOST_SEND_BDS| 1025 BGE_MODECTL_NO_RX_CRC|BGE_MODECTL_TX_NO_PHDR_CSUM| 1026 BGE_MODECTL_RX_NO_PHDR_CSUM); 1027 1028 /* Get cache line size. */ 1029 cachesize = pci_read_config(sc->bge_dev, BGE_PCI_CACHESZ, 1); 1030 1031 /* 1032 * Avoid violating PCI spec on certain chip revs. 1033 */ 1034 if (pci_read_config(sc->bge_dev, BGE_PCI_CMD, 4) & PCIM_CMD_MWIEN) { 1035 switch(cachesize) { 1036 case 1: 1037 PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL, 1038 BGE_PCI_WRITE_BNDRY_16BYTES, 4); 1039 break; 1040 case 2: 1041 PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL, 1042 BGE_PCI_WRITE_BNDRY_32BYTES, 4); 1043 break; 1044 case 4: 1045 PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL, 1046 BGE_PCI_WRITE_BNDRY_64BYTES, 4); 1047 break; 1048 case 8: 1049 PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL, 1050 BGE_PCI_WRITE_BNDRY_128BYTES, 4); 1051 break; 1052 case 16: 1053 PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL, 1054 BGE_PCI_WRITE_BNDRY_256BYTES, 4); 1055 break; 1056 case 32: 1057 PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL, 1058 BGE_PCI_WRITE_BNDRY_512BYTES, 4); 1059 break; 1060 case 64: 1061 PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL, 1062 BGE_PCI_WRITE_BNDRY_1024BYTES, 4); 1063 break; 1064 default: 1065 /* Disable PCI memory write and invalidate. */ 1066 if (bootverbose) 1067 printf("bge%d: cache line size %d not " 1068 "supported; disabling PCI MWI\n", 1069 sc->bge_unit, cachesize); 1070 PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, 1071 PCIM_CMD_MWIEN, 4); 1072 break; 1073 } 1074 } 1075 1076 #ifdef __brokenalpha__ 1077 /* 1078 * Must insure that we do not cross an 8K (bytes) boundary 1079 * for DMA reads. Our highest limit is 1K bytes. This is a 1080 * restriction on some ALPHA platforms with early revision 1081 * 21174 PCI chipsets, such as the AlphaPC 164lx 1082 */ 1083 PCI_SETBIT(sc, BGE_PCI_DMA_RW_CTL, BGE_PCI_READ_BNDRY_1024, 4); 1084 #endif 1085 1086 /* Set the timer prescaler (always 66Mhz) */ 1087 CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/); 1088 1089 return(0); 1090 } 1091 1092 static int 1093 bge_blockinit(sc) 1094 struct bge_softc *sc; 1095 { 1096 struct bge_rcb *rcb; 1097 struct bge_rcb_opaque *rcbo; 1098 int i; 1099 1100 /* 1101 * Initialize the memory window pointer register so that 1102 * we can access the first 32K of internal NIC RAM. This will 1103 * allow us to set up the TX send ring RCBs and the RX return 1104 * ring RCBs, plus other things which live in NIC memory. 1105 */ 1106 CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0); 1107 1108 /* Configure mbuf memory pool */ 1109 if (sc->bge_extram) { 1110 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_EXT_SSRAM); 1111 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000); 1112 } else { 1113 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1); 1114 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000); 1115 } 1116 1117 /* Configure DMA resource pool */ 1118 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR, BGE_DMA_DESCRIPTORS); 1119 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000); 1120 1121 /* Configure mbuf pool watermarks */ 1122 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 24); 1123 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 24); 1124 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 48); 1125 1126 /* Configure DMA resource watermarks */ 1127 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5); 1128 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10); 1129 1130 /* Enable buffer manager */ 1131 CSR_WRITE_4(sc, BGE_BMAN_MODE, 1132 BGE_BMANMODE_ENABLE|BGE_BMANMODE_LOMBUF_ATTN); 1133 1134 /* Poll for buffer manager start indication */ 1135 for (i = 0; i < BGE_TIMEOUT; i++) { 1136 if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE) 1137 break; 1138 DELAY(10); 1139 } 1140 1141 if (i == BGE_TIMEOUT) { 1142 printf("bge%d: buffer manager failed to start\n", 1143 sc->bge_unit); 1144 return(ENXIO); 1145 } 1146 1147 /* Enable flow-through queues */ 1148 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF); 1149 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0); 1150 1151 /* Wait until queue initialization is complete */ 1152 for (i = 0; i < BGE_TIMEOUT; i++) { 1153 if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0) 1154 break; 1155 DELAY(10); 1156 } 1157 1158 if (i == BGE_TIMEOUT) { 1159 printf("bge%d: flow-through queue init failed\n", 1160 sc->bge_unit); 1161 return(ENXIO); 1162 } 1163 1164 /* Initialize the standard RX ring control block */ 1165 rcb = &sc->bge_rdata->bge_info.bge_std_rx_rcb; 1166 BGE_HOSTADDR(rcb->bge_hostaddr) = 1167 vtophys(&sc->bge_rdata->bge_rx_std_ring); 1168 rcb->bge_max_len = BGE_MAX_FRAMELEN; 1169 if (sc->bge_extram) 1170 rcb->bge_nicaddr = BGE_EXT_STD_RX_RINGS; 1171 else 1172 rcb->bge_nicaddr = BGE_STD_RX_RINGS; 1173 rcb->bge_flags = 0; 1174 rcbo = (struct bge_rcb_opaque *)rcb; 1175 CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcbo->bge_reg0); 1176 CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcbo->bge_reg1); 1177 CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcbo->bge_reg2); 1178 CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcbo->bge_reg3); 1179 1180 /* 1181 * Initialize the jumbo RX ring control block 1182 * We set the 'ring disabled' bit in the flags 1183 * field until we're actually ready to start 1184 * using this ring (i.e. once we set the MTU 1185 * high enough to require it). 1186 */ 1187 rcb = &sc->bge_rdata->bge_info.bge_jumbo_rx_rcb; 1188 BGE_HOSTADDR(rcb->bge_hostaddr) = 1189 vtophys(&sc->bge_rdata->bge_rx_jumbo_ring); 1190 rcb->bge_max_len = BGE_MAX_FRAMELEN; 1191 if (sc->bge_extram) 1192 rcb->bge_nicaddr = BGE_EXT_JUMBO_RX_RINGS; 1193 else 1194 rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS; 1195 rcb->bge_flags = BGE_RCB_FLAG_RING_DISABLED; 1196 1197 rcbo = (struct bge_rcb_opaque *)rcb; 1198 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI, rcbo->bge_reg0); 1199 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO, rcbo->bge_reg1); 1200 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcbo->bge_reg2); 1201 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcbo->bge_reg3); 1202 1203 /* Set up dummy disabled mini ring RCB */ 1204 rcb = &sc->bge_rdata->bge_info.bge_mini_rx_rcb; 1205 rcb->bge_flags = BGE_RCB_FLAG_RING_DISABLED; 1206 rcbo = (struct bge_rcb_opaque *)rcb; 1207 CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS, rcbo->bge_reg2); 1208 1209 /* 1210 * Set the BD ring replentish thresholds. The recommended 1211 * values are 1/8th the number of descriptors allocated to 1212 * each ring. 1213 */ 1214 CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, BGE_STD_RX_RING_CNT/8); 1215 CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8); 1216 1217 /* 1218 * Disable all unused send rings by setting the 'ring disabled' 1219 * bit in the flags field of all the TX send ring control blocks. 1220 * These are located in NIC memory. 1221 */ 1222 rcb = (struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START + 1223 BGE_SEND_RING_RCB); 1224 for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) { 1225 rcb->bge_flags = BGE_RCB_FLAG_RING_DISABLED; 1226 rcb->bge_max_len = 0; 1227 rcb->bge_nicaddr = 0; 1228 rcb++; 1229 } 1230 1231 /* Configure TX RCB 0 (we use only the first ring) */ 1232 rcb = (struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START + 1233 BGE_SEND_RING_RCB); 1234 rcb->bge_hostaddr.bge_addr_hi = 0; 1235 BGE_HOSTADDR(rcb->bge_hostaddr) = 1236 vtophys(&sc->bge_rdata->bge_tx_ring); 1237 rcb->bge_nicaddr = BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT); 1238 rcb->bge_max_len = BGE_TX_RING_CNT; 1239 rcb->bge_flags = 0; 1240 1241 /* Disable all unused RX return rings */ 1242 rcb = (struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START + 1243 BGE_RX_RETURN_RING_RCB); 1244 for (i = 0; i < BGE_RX_RINGS_MAX; i++) { 1245 rcb->bge_hostaddr.bge_addr_hi = 0; 1246 rcb->bge_hostaddr.bge_addr_lo = 0; 1247 rcb->bge_flags = BGE_RCB_FLAG_RING_DISABLED; 1248 rcb->bge_max_len = BGE_RETURN_RING_CNT; 1249 rcb->bge_nicaddr = 0; 1250 CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO + 1251 (i * (sizeof(u_int64_t))), 0); 1252 rcb++; 1253 } 1254 1255 /* Initialize RX ring indexes */ 1256 CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0); 1257 CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0); 1258 CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0); 1259 1260 /* 1261 * Set up RX return ring 0 1262 * Note that the NIC address for RX return rings is 0x00000000. 1263 * The return rings live entirely within the host, so the 1264 * nicaddr field in the RCB isn't used. 1265 */ 1266 rcb = (struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START + 1267 BGE_RX_RETURN_RING_RCB); 1268 rcb->bge_hostaddr.bge_addr_hi = 0; 1269 BGE_HOSTADDR(rcb->bge_hostaddr) = 1270 vtophys(&sc->bge_rdata->bge_rx_return_ring); 1271 rcb->bge_nicaddr = 0x00000000; 1272 rcb->bge_max_len = BGE_RETURN_RING_CNT; 1273 rcb->bge_flags = 0; 1274 1275 /* Set random backoff seed for TX */ 1276 CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF, 1277 sc->arpcom.ac_enaddr[0] + sc->arpcom.ac_enaddr[1] + 1278 sc->arpcom.ac_enaddr[2] + sc->arpcom.ac_enaddr[3] + 1279 sc->arpcom.ac_enaddr[4] + sc->arpcom.ac_enaddr[5] + 1280 BGE_TX_BACKOFF_SEED_MASK); 1281 1282 /* Set inter-packet gap */ 1283 CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620); 1284 1285 /* 1286 * Specify which ring to use for packets that don't match 1287 * any RX rules. 1288 */ 1289 CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08); 1290 1291 /* 1292 * Configure number of RX lists. One interrupt distribution 1293 * list, sixteen active lists, one bad frames class. 1294 */ 1295 CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181); 1296 1297 /* Inialize RX list placement stats mask. */ 1298 CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF); 1299 CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1); 1300 1301 /* Disable host coalescing until we get it set up */ 1302 CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000); 1303 1304 /* Poll to make sure it's shut down. */ 1305 for (i = 0; i < BGE_TIMEOUT; i++) { 1306 if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE)) 1307 break; 1308 DELAY(10); 1309 } 1310 1311 if (i == BGE_TIMEOUT) { 1312 printf("bge%d: host coalescing engine failed to idle\n", 1313 sc->bge_unit); 1314 return(ENXIO); 1315 } 1316 1317 /* Set up host coalescing defaults */ 1318 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks); 1319 CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks); 1320 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds); 1321 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds); 1322 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0); 1323 CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0); 1324 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0); 1325 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0); 1326 CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks); 1327 1328 /* Set up address of statistics block */ 1329 CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK); 1330 CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI, 0); 1331 CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO, 1332 vtophys(&sc->bge_rdata->bge_info.bge_stats)); 1333 1334 /* Set up address of status block */ 1335 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK); 1336 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI, 0); 1337 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO, 1338 vtophys(&sc->bge_rdata->bge_status_block)); 1339 sc->bge_rdata->bge_status_block.bge_idx[0].bge_rx_prod_idx = 0; 1340 sc->bge_rdata->bge_status_block.bge_idx[0].bge_tx_cons_idx = 0; 1341 1342 /* Turn on host coalescing state machine */ 1343 CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE); 1344 1345 /* Turn on RX BD completion state machine and enable attentions */ 1346 CSR_WRITE_4(sc, BGE_RBDC_MODE, 1347 BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN); 1348 1349 /* Turn on RX list placement state machine */ 1350 CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE); 1351 1352 /* Turn on RX list selector state machine. */ 1353 CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE); 1354 1355 /* Turn on DMA, clear stats */ 1356 CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB| 1357 BGE_MACMODE_RXDMA_ENB|BGE_MACMODE_RX_STATS_CLEAR| 1358 BGE_MACMODE_TX_STATS_CLEAR|BGE_MACMODE_RX_STATS_ENB| 1359 BGE_MACMODE_TX_STATS_ENB|BGE_MACMODE_FRMHDR_DMA_ENB| 1360 (sc->bge_tbi ? BGE_PORTMODE_TBI : BGE_PORTMODE_MII)); 1361 1362 /* Set misc. local control, enable interrupts on attentions */ 1363 CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN); 1364 1365 #ifdef notdef 1366 /* Assert GPIO pins for PHY reset */ 1367 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0| 1368 BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2); 1369 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0| 1370 BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2); 1371 #endif 1372 1373 /* Turn on DMA completion state machine */ 1374 CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE); 1375 1376 /* Turn on write DMA state machine */ 1377 CSR_WRITE_4(sc, BGE_WDMA_MODE, 1378 BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS); 1379 1380 /* Turn on read DMA state machine */ 1381 CSR_WRITE_4(sc, BGE_RDMA_MODE, 1382 BGE_RDMAMODE_ENABLE|BGE_RDMAMODE_ALL_ATTNS); 1383 1384 /* Turn on RX data completion state machine */ 1385 CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE); 1386 1387 /* Turn on RX BD initiator state machine */ 1388 CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE); 1389 1390 /* Turn on RX data and RX BD initiator state machine */ 1391 CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE); 1392 1393 /* Turn on Mbuf cluster free state machine */ 1394 CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE); 1395 1396 /* Turn on send BD completion state machine */ 1397 CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE); 1398 1399 /* Turn on send data completion state machine */ 1400 CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE); 1401 1402 /* Turn on send data initiator state machine */ 1403 CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE); 1404 1405 /* Turn on send BD initiator state machine */ 1406 CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE); 1407 1408 /* Turn on send BD selector state machine */ 1409 CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE); 1410 1411 CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF); 1412 CSR_WRITE_4(sc, BGE_SDI_STATS_CTL, 1413 BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER); 1414 1415 /* init LED register */ 1416 CSR_WRITE_4(sc, BGE_MAC_LED_CTL, 0x00000000); 1417 1418 /* ack/clear link change events */ 1419 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED| 1420 BGE_MACSTAT_CFG_CHANGED); 1421 CSR_WRITE_4(sc, BGE_MI_STS, 0); 1422 1423 /* Enable PHY auto polling (for MII/GMII only) */ 1424 if (sc->bge_tbi) { 1425 CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK); 1426 } else 1427 BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL|10<<16); 1428 1429 /* Enable link state change attentions. */ 1430 BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED); 1431 1432 return(0); 1433 } 1434 1435 /* 1436 * Probe for a Broadcom chip. Check the PCI vendor and device IDs 1437 * against our list and return its name if we find a match. Note 1438 * that since the Broadcom controller contains VPD support, we 1439 * can get the device name string from the controller itself instead 1440 * of the compiled-in string. This is a little slow, but it guarantees 1441 * we'll always announce the right product name. 1442 */ 1443 static int 1444 bge_probe(dev) 1445 device_t dev; 1446 { 1447 struct bge_type *t; 1448 struct bge_softc *sc; 1449 1450 t = bge_devs; 1451 1452 sc = device_get_softc(dev); 1453 bzero(sc, sizeof(struct bge_softc)); 1454 sc->bge_unit = device_get_unit(dev); 1455 sc->bge_dev = dev; 1456 1457 while(t->bge_name != NULL) { 1458 if ((pci_get_vendor(dev) == t->bge_vid) && 1459 (pci_get_device(dev) == t->bge_did)) { 1460 #ifdef notdef 1461 bge_vpd_read(sc); 1462 device_set_desc(dev, sc->bge_vpd_prodname); 1463 #endif 1464 device_set_desc(dev, t->bge_name); 1465 return(0); 1466 } 1467 t++; 1468 } 1469 1470 return(ENXIO); 1471 } 1472 1473 static int 1474 bge_attach(dev) 1475 device_t dev; 1476 { 1477 int s; 1478 u_int32_t command; 1479 struct ifnet *ifp; 1480 struct bge_softc *sc; 1481 int unit, error = 0, rid; 1482 1483 s = splimp(); 1484 1485 sc = device_get_softc(dev); 1486 unit = device_get_unit(dev); 1487 sc->bge_dev = dev; 1488 sc->bge_unit = unit; 1489 1490 /* 1491 * Map control/status registers. 1492 */ 1493 pci_enable_busmaster(dev); 1494 pci_enable_io(dev, SYS_RES_MEMORY); 1495 command = pci_read_config(dev, PCIR_COMMAND, 4); 1496 1497 if (!(command & PCIM_CMD_MEMEN)) { 1498 printf("bge%d: failed to enable memory mapping!\n", unit); 1499 error = ENXIO; 1500 goto fail; 1501 } 1502 1503 rid = BGE_PCI_BAR0; 1504 sc->bge_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 1505 0, ~0, 1, RF_ACTIVE); 1506 1507 if (sc->bge_res == NULL) { 1508 printf ("bge%d: couldn't map memory\n", unit); 1509 error = ENXIO; 1510 goto fail; 1511 } 1512 1513 sc->bge_btag = rman_get_bustag(sc->bge_res); 1514 sc->bge_bhandle = rman_get_bushandle(sc->bge_res); 1515 sc->bge_vhandle = (vm_offset_t)rman_get_virtual(sc->bge_res); 1516 1517 /* 1518 * XXX FIXME: rman_get_virtual() on the alpha is currently 1519 * broken and returns a physical address instead of a kernel 1520 * virtual address. Consequently, we need to do a little 1521 * extra mangling of the vhandle on the alpha. This should 1522 * eventually be fixed! The whole idea here is to get rid 1523 * of platform dependencies. 1524 */ 1525 #ifdef __alpha__ 1526 if (pci_cvt_to_bwx(sc->bge_vhandle)) 1527 sc->bge_vhandle = pci_cvt_to_bwx(sc->bge_vhandle); 1528 else 1529 sc->bge_vhandle = pci_cvt_to_dense(sc->bge_vhandle); 1530 sc->bge_vhandle = ALPHA_PHYS_TO_K0SEG(sc->bge_vhandle); 1531 #endif 1532 1533 /* Allocate interrupt */ 1534 rid = 0; 1535 1536 sc->bge_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 1537 RF_SHAREABLE | RF_ACTIVE); 1538 1539 if (sc->bge_irq == NULL) { 1540 printf("bge%d: couldn't map interrupt\n", unit); 1541 error = ENXIO; 1542 goto fail; 1543 } 1544 1545 error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET, 1546 bge_intr, sc, &sc->bge_intrhand); 1547 1548 if (error) { 1549 bge_release_resources(sc); 1550 printf("bge%d: couldn't set up irq\n", unit); 1551 goto fail; 1552 } 1553 1554 sc->bge_unit = unit; 1555 1556 /* Try to reset the chip. */ 1557 bge_reset(sc); 1558 1559 if (bge_chipinit(sc)) { 1560 printf("bge%d: chip initialization failed\n", sc->bge_unit); 1561 bge_release_resources(sc); 1562 error = ENXIO; 1563 goto fail; 1564 } 1565 1566 /* 1567 * Get station address from the EEPROM. 1568 */ 1569 if (bge_read_eeprom(sc, (caddr_t)&sc->arpcom.ac_enaddr, 1570 BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) { 1571 printf("bge%d: failed to read station address\n", unit); 1572 bge_release_resources(sc); 1573 error = ENXIO; 1574 goto fail; 1575 } 1576 1577 /* 1578 * A Broadcom chip was detected. Inform the world. 1579 */ 1580 printf("bge%d: Ethernet address: %6D\n", unit, 1581 sc->arpcom.ac_enaddr, ":"); 1582 1583 /* Allocate the general information block and ring buffers. */ 1584 sc->bge_rdata = contigmalloc(sizeof(struct bge_ring_data), M_DEVBUF, 1585 M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0); 1586 1587 if (sc->bge_rdata == NULL) { 1588 bge_release_resources(sc); 1589 error = ENXIO; 1590 printf("bge%d: no memory for list buffers!\n", sc->bge_unit); 1591 goto fail; 1592 } 1593 1594 bzero(sc->bge_rdata, sizeof(struct bge_ring_data)); 1595 1596 /* Try to allocate memory for jumbo buffers. */ 1597 if (bge_alloc_jumbo_mem(sc)) { 1598 printf("bge%d: jumbo buffer allocation " 1599 "failed\n", sc->bge_unit); 1600 bge_release_resources(sc); 1601 error = ENXIO; 1602 goto fail; 1603 } 1604 1605 /* Set default tuneable values. */ 1606 sc->bge_stat_ticks = BGE_TICKS_PER_SEC; 1607 sc->bge_rx_coal_ticks = 150; 1608 sc->bge_tx_coal_ticks = 150; 1609 sc->bge_rx_max_coal_bds = 64; 1610 sc->bge_tx_max_coal_bds = 128; 1611 1612 /* Set up ifnet structure */ 1613 ifp = &sc->arpcom.ac_if; 1614 ifp->if_softc = sc; 1615 ifp->if_unit = sc->bge_unit; 1616 ifp->if_name = "bge"; 1617 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1618 ifp->if_ioctl = bge_ioctl; 1619 ifp->if_output = ether_output; 1620 ifp->if_start = bge_start; 1621 ifp->if_watchdog = bge_watchdog; 1622 ifp->if_init = bge_init; 1623 ifp->if_mtu = ETHERMTU; 1624 ifp->if_snd.ifq_maxlen = BGE_TX_RING_CNT - 1; 1625 ifp->if_hwassist = BGE_CSUM_FEATURES; 1626 ifp->if_capabilities = IFCAP_HWCSUM; 1627 ifp->if_capenable = ifp->if_capabilities; 1628 1629 /* Save ASIC rev. */ 1630 1631 sc->bge_asicrev = 1632 pci_read_config(dev, BGE_PCI_MISC_CTL, 4) & 1633 BGE_PCIMISCCTL_ASICREV; 1634 1635 /* The SysKonnect SK-9D41 is a 1000baseSX card. */ 1636 if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41) 1637 sc->bge_tbi = 1; 1638 1639 if (sc->bge_tbi) { 1640 ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, 1641 bge_ifmedia_upd, bge_ifmedia_sts); 1642 ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL); 1643 ifmedia_add(&sc->bge_ifmedia, 1644 IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL); 1645 ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL); 1646 ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO); 1647 } else { 1648 /* 1649 * Do transceiver setup. 1650 */ 1651 if (mii_phy_probe(dev, &sc->bge_miibus, 1652 bge_ifmedia_upd, bge_ifmedia_sts)) { 1653 printf("bge%d: MII without any PHY!\n", sc->bge_unit); 1654 bge_release_resources(sc); 1655 bge_free_jumbo_mem(sc); 1656 error = ENXIO; 1657 goto fail; 1658 } 1659 } 1660 1661 /* 1662 * Call MI attach routine. 1663 */ 1664 ether_ifattach(ifp, ETHER_BPF_SUPPORTED); 1665 callout_handle_init(&sc->bge_stat_ch); 1666 1667 fail: 1668 splx(s); 1669 1670 return(error); 1671 } 1672 1673 static int 1674 bge_detach(dev) 1675 device_t dev; 1676 { 1677 struct bge_softc *sc; 1678 struct ifnet *ifp; 1679 int s; 1680 1681 s = splimp(); 1682 1683 sc = device_get_softc(dev); 1684 ifp = &sc->arpcom.ac_if; 1685 1686 ether_ifdetach(ifp, ETHER_BPF_SUPPORTED); 1687 bge_stop(sc); 1688 bge_reset(sc); 1689 1690 if (sc->bge_tbi) { 1691 ifmedia_removeall(&sc->bge_ifmedia); 1692 } else { 1693 bus_generic_detach(dev); 1694 device_delete_child(dev, sc->bge_miibus); 1695 } 1696 1697 bge_release_resources(sc); 1698 bge_free_jumbo_mem(sc); 1699 1700 splx(s); 1701 1702 return(0); 1703 } 1704 1705 static void 1706 bge_release_resources(sc) 1707 struct bge_softc *sc; 1708 { 1709 device_t dev; 1710 1711 dev = sc->bge_dev; 1712 1713 if (sc->bge_vpd_prodname != NULL) 1714 free(sc->bge_vpd_prodname, M_DEVBUF); 1715 1716 if (sc->bge_vpd_readonly != NULL) 1717 free(sc->bge_vpd_readonly, M_DEVBUF); 1718 1719 if (sc->bge_intrhand != NULL) 1720 bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand); 1721 1722 if (sc->bge_irq != NULL) 1723 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->bge_irq); 1724 1725 if (sc->bge_res != NULL) 1726 bus_release_resource(dev, SYS_RES_MEMORY, 1727 BGE_PCI_BAR0, sc->bge_res); 1728 1729 if (sc->bge_rdata != NULL) 1730 contigfree(sc->bge_rdata, 1731 sizeof(struct bge_ring_data), M_DEVBUF); 1732 1733 return; 1734 } 1735 1736 static void 1737 bge_reset(sc) 1738 struct bge_softc *sc; 1739 { 1740 device_t dev; 1741 u_int32_t cachesize, command, pcistate; 1742 int i, val = 0; 1743 1744 dev = sc->bge_dev; 1745 1746 /* Save some important PCI state. */ 1747 cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4); 1748 command = pci_read_config(dev, BGE_PCI_CMD, 4); 1749 pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4); 1750 1751 pci_write_config(dev, BGE_PCI_MISC_CTL, 1752 BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR| 1753 BGE_PCIMISCCTL_ENDIAN_WORDSWAP|BGE_PCIMISCCTL_PCISTATE_RW, 4); 1754 1755 /* Issue global reset */ 1756 bge_writereg_ind(sc, BGE_MISC_CFG, 1757 BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1)); 1758 1759 DELAY(1000); 1760 1761 /* Reset some of the PCI state that got zapped by reset */ 1762 pci_write_config(dev, BGE_PCI_MISC_CTL, 1763 BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR| 1764 BGE_PCIMISCCTL_ENDIAN_WORDSWAP|BGE_PCIMISCCTL_PCISTATE_RW, 4); 1765 pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4); 1766 pci_write_config(dev, BGE_PCI_CMD, command, 4); 1767 bge_writereg_ind(sc, BGE_MISC_CFG, (65 << 1)); 1768 1769 /* 1770 * Prevent PXE restart: write a magic number to the 1771 * general communications memory at 0xB50. 1772 */ 1773 bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER); 1774 /* 1775 * Poll the value location we just wrote until 1776 * we see the 1's complement of the magic number. 1777 * This indicates that the firmware initialization 1778 * is complete. 1779 */ 1780 for (i = 0; i < BGE_TIMEOUT; i++) { 1781 val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM); 1782 if (val == ~BGE_MAGIC_NUMBER) 1783 break; 1784 DELAY(10); 1785 } 1786 1787 if (i == BGE_TIMEOUT) { 1788 printf("bge%d: firmware handshake timed out\n", sc->bge_unit); 1789 return; 1790 } 1791 1792 /* 1793 * XXX Wait for the value of the PCISTATE register to 1794 * return to its original pre-reset state. This is a 1795 * fairly good indicator of reset completion. If we don't 1796 * wait for the reset to fully complete, trying to read 1797 * from the device's non-PCI registers may yield garbage 1798 * results. 1799 */ 1800 for (i = 0; i < BGE_TIMEOUT; i++) { 1801 if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate) 1802 break; 1803 DELAY(10); 1804 } 1805 1806 /* Enable memory arbiter. */ 1807 CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE); 1808 1809 /* Fix up byte swapping */ 1810 CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_MODECTL_BYTESWAP_NONFRAME| 1811 BGE_MODECTL_BYTESWAP_DATA); 1812 1813 CSR_WRITE_4(sc, BGE_MAC_MODE, 0); 1814 1815 DELAY(10000); 1816 1817 return; 1818 } 1819 1820 /* 1821 * Frame reception handling. This is called if there's a frame 1822 * on the receive return list. 1823 * 1824 * Note: we have to be able to handle two possibilities here: 1825 * 1) the frame is from the jumbo recieve ring 1826 * 2) the frame is from the standard receive ring 1827 */ 1828 1829 static void 1830 bge_rxeof(sc) 1831 struct bge_softc *sc; 1832 { 1833 struct ifnet *ifp; 1834 int stdcnt = 0, jumbocnt = 0; 1835 1836 ifp = &sc->arpcom.ac_if; 1837 1838 while(sc->bge_rx_saved_considx != 1839 sc->bge_rdata->bge_status_block.bge_idx[0].bge_rx_prod_idx) { 1840 struct bge_rx_bd *cur_rx; 1841 u_int32_t rxidx; 1842 struct ether_header *eh; 1843 struct mbuf *m = NULL; 1844 u_int16_t vlan_tag = 0; 1845 int have_tag = 0; 1846 1847 cur_rx = 1848 &sc->bge_rdata->bge_rx_return_ring[sc->bge_rx_saved_considx]; 1849 1850 rxidx = cur_rx->bge_idx; 1851 BGE_INC(sc->bge_rx_saved_considx, BGE_RETURN_RING_CNT); 1852 1853 if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) { 1854 have_tag = 1; 1855 vlan_tag = cur_rx->bge_vlan_tag; 1856 } 1857 1858 if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) { 1859 BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT); 1860 m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx]; 1861 sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL; 1862 jumbocnt++; 1863 if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) { 1864 ifp->if_ierrors++; 1865 bge_newbuf_jumbo(sc, sc->bge_jumbo, m); 1866 continue; 1867 } 1868 if (bge_newbuf_jumbo(sc, 1869 sc->bge_jumbo, NULL) == ENOBUFS) { 1870 ifp->if_ierrors++; 1871 bge_newbuf_jumbo(sc, sc->bge_jumbo, m); 1872 continue; 1873 } 1874 } else { 1875 BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT); 1876 m = sc->bge_cdata.bge_rx_std_chain[rxidx]; 1877 sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL; 1878 stdcnt++; 1879 if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) { 1880 ifp->if_ierrors++; 1881 bge_newbuf_std(sc, sc->bge_std, m); 1882 continue; 1883 } 1884 if (bge_newbuf_std(sc, sc->bge_std, 1885 NULL) == ENOBUFS) { 1886 ifp->if_ierrors++; 1887 bge_newbuf_std(sc, sc->bge_std, m); 1888 continue; 1889 } 1890 } 1891 1892 ifp->if_ipackets++; 1893 eh = mtod(m, struct ether_header *); 1894 m->m_pkthdr.len = m->m_len = cur_rx->bge_len; 1895 m->m_pkthdr.rcvif = ifp; 1896 1897 /* Remove header from mbuf and pass it on. */ 1898 m_adj(m, sizeof(struct ether_header)); 1899 1900 #if 0 /* currently broken for some packets, possibly related to TCP options */ 1901 if (ifp->if_hwassist) { 1902 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED; 1903 if ((cur_rx->bge_ip_csum ^ 0xffff) == 0) 1904 m->m_pkthdr.csum_flags |= CSUM_IP_VALID; 1905 if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) { 1906 m->m_pkthdr.csum_data = 1907 cur_rx->bge_tcp_udp_csum; 1908 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID; 1909 } 1910 } 1911 #endif 1912 1913 /* 1914 * If we received a packet with a vlan tag, pass it 1915 * to vlan_input() instead of ether_input(). 1916 */ 1917 if (have_tag) { 1918 VLAN_INPUT_TAG(eh, m, vlan_tag); 1919 have_tag = vlan_tag = 0; 1920 continue; 1921 } 1922 1923 ether_input(ifp, eh, m); 1924 } 1925 1926 CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx); 1927 if (stdcnt) 1928 CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std); 1929 if (jumbocnt) 1930 CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo); 1931 1932 return; 1933 } 1934 1935 static void 1936 bge_txeof(sc) 1937 struct bge_softc *sc; 1938 { 1939 struct bge_tx_bd *cur_tx = NULL; 1940 struct ifnet *ifp; 1941 1942 ifp = &sc->arpcom.ac_if; 1943 1944 /* 1945 * Go through our tx ring and free mbufs for those 1946 * frames that have been sent. 1947 */ 1948 while (sc->bge_tx_saved_considx != 1949 sc->bge_rdata->bge_status_block.bge_idx[0].bge_tx_cons_idx) { 1950 u_int32_t idx = 0; 1951 1952 idx = sc->bge_tx_saved_considx; 1953 cur_tx = &sc->bge_rdata->bge_tx_ring[idx]; 1954 if (cur_tx->bge_flags & BGE_TXBDFLAG_END) 1955 ifp->if_opackets++; 1956 if (sc->bge_cdata.bge_tx_chain[idx] != NULL) { 1957 m_freem(sc->bge_cdata.bge_tx_chain[idx]); 1958 sc->bge_cdata.bge_tx_chain[idx] = NULL; 1959 } 1960 sc->bge_txcnt--; 1961 BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT); 1962 ifp->if_timer = 0; 1963 } 1964 1965 if (cur_tx != NULL) 1966 ifp->if_flags &= ~IFF_OACTIVE; 1967 1968 return; 1969 } 1970 1971 static void 1972 bge_intr(xsc) 1973 void *xsc; 1974 { 1975 struct bge_softc *sc; 1976 struct ifnet *ifp; 1977 1978 sc = xsc; 1979 ifp = &sc->arpcom.ac_if; 1980 1981 #ifdef notdef 1982 /* Avoid this for now -- checking this register is expensive. */ 1983 /* Make sure this is really our interrupt. */ 1984 if (!(CSR_READ_4(sc, BGE_MISC_LOCAL_CTL) & BGE_MLC_INTR_STATE)) 1985 return; 1986 #endif 1987 /* Ack interrupt and stop others from occuring. */ 1988 CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1); 1989 1990 /* Process link state changes. */ 1991 if (sc->bge_rdata->bge_status_block.bge_status & 1992 BGE_STATFLAG_LINKSTATE_CHANGED) { 1993 if (sc->bge_asicrev != BGE_ASICREV_BCM5701_B5) { 1994 sc->bge_link = 0; 1995 untimeout(bge_tick, sc, sc->bge_stat_ch); 1996 bge_tick(sc); 1997 } 1998 /* ack the event to clear/reset it */ 1999 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED| 2000 BGE_MACSTAT_CFG_CHANGED); 2001 CSR_WRITE_4(sc, BGE_MI_STS, 0); 2002 } 2003 2004 if (ifp->if_flags & IFF_RUNNING) { 2005 /* Check RX return ring producer/consumer */ 2006 bge_rxeof(sc); 2007 2008 /* Check TX ring producer/consumer */ 2009 bge_txeof(sc); 2010 } 2011 2012 bge_handle_events(sc); 2013 2014 /* Re-enable interrupts. */ 2015 CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0); 2016 2017 if (ifp->if_flags & IFF_RUNNING && ifp->if_snd.ifq_head != NULL) 2018 bge_start(ifp); 2019 2020 return; 2021 } 2022 2023 static void 2024 bge_tick(xsc) 2025 void *xsc; 2026 { 2027 struct bge_softc *sc; 2028 struct mii_data *mii = NULL; 2029 struct ifmedia *ifm = NULL; 2030 struct ifnet *ifp; 2031 int s; 2032 2033 sc = xsc; 2034 ifp = &sc->arpcom.ac_if; 2035 2036 s = splimp(); 2037 2038 bge_stats_update(sc); 2039 sc->bge_stat_ch = timeout(bge_tick, sc, hz); 2040 if (sc->bge_link) 2041 return; 2042 2043 if (sc->bge_tbi) { 2044 ifm = &sc->bge_ifmedia; 2045 if (CSR_READ_4(sc, BGE_MAC_STS) & 2046 BGE_MACSTAT_TBI_PCS_SYNCHED) { 2047 sc->bge_link++; 2048 CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF); 2049 printf("bge%d: gigabit link up\n", sc->bge_unit); 2050 if (ifp->if_snd.ifq_head != NULL) 2051 bge_start(ifp); 2052 } 2053 return; 2054 } 2055 2056 mii = device_get_softc(sc->bge_miibus); 2057 mii_tick(mii); 2058 2059 if (!sc->bge_link && mii->mii_media_status & IFM_ACTIVE && 2060 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 2061 sc->bge_link++; 2062 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_TX || 2063 IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX) 2064 printf("bge%d: gigabit link up\n", 2065 sc->bge_unit); 2066 if (ifp->if_snd.ifq_head != NULL) 2067 bge_start(ifp); 2068 } 2069 2070 splx(s); 2071 2072 return; 2073 } 2074 2075 static void 2076 bge_stats_update(sc) 2077 struct bge_softc *sc; 2078 { 2079 struct ifnet *ifp; 2080 struct bge_stats *stats; 2081 2082 ifp = &sc->arpcom.ac_if; 2083 2084 stats = (struct bge_stats *)(sc->bge_vhandle + 2085 BGE_MEMWIN_START + BGE_STATS_BLOCK); 2086 2087 ifp->if_collisions += 2088 (stats->dot3StatsSingleCollisionFrames.bge_addr_lo + 2089 stats->dot3StatsMultipleCollisionFrames.bge_addr_lo + 2090 stats->dot3StatsExcessiveCollisions.bge_addr_lo + 2091 stats->dot3StatsLateCollisions.bge_addr_lo) - 2092 ifp->if_collisions; 2093 2094 #ifdef notdef 2095 ifp->if_collisions += 2096 (sc->bge_rdata->bge_info.bge_stats.dot3StatsSingleCollisionFrames + 2097 sc->bge_rdata->bge_info.bge_stats.dot3StatsMultipleCollisionFrames + 2098 sc->bge_rdata->bge_info.bge_stats.dot3StatsExcessiveCollisions + 2099 sc->bge_rdata->bge_info.bge_stats.dot3StatsLateCollisions) - 2100 ifp->if_collisions; 2101 #endif 2102 2103 return; 2104 } 2105 2106 /* 2107 * Encapsulate an mbuf chain in the tx ring by coupling the mbuf data 2108 * pointers to descriptors. 2109 */ 2110 static int 2111 bge_encap(sc, m_head, txidx) 2112 struct bge_softc *sc; 2113 struct mbuf *m_head; 2114 u_int32_t *txidx; 2115 { 2116 struct bge_tx_bd *f = NULL; 2117 struct mbuf *m; 2118 u_int32_t frag, cur, cnt = 0; 2119 u_int16_t csum_flags = 0; 2120 struct ifvlan *ifv = NULL; 2121 2122 if ((m_head->m_flags & (M_PROTO1|M_PKTHDR)) == (M_PROTO1|M_PKTHDR) && 2123 m_head->m_pkthdr.rcvif != NULL && 2124 m_head->m_pkthdr.rcvif->if_type == IFT_L2VLAN) 2125 ifv = m_head->m_pkthdr.rcvif->if_softc; 2126 2127 m = m_head; 2128 cur = frag = *txidx; 2129 2130 if (m_head->m_pkthdr.csum_flags) { 2131 if (m_head->m_pkthdr.csum_flags & CSUM_IP) 2132 csum_flags |= BGE_TXBDFLAG_IP_CSUM; 2133 if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) 2134 csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM; 2135 if (m_head->m_flags & M_LASTFRAG) 2136 csum_flags |= BGE_TXBDFLAG_IP_FRAG_END; 2137 else if (m_head->m_flags & M_FRAG) 2138 csum_flags |= BGE_TXBDFLAG_IP_FRAG; 2139 } 2140 2141 /* 2142 * Start packing the mbufs in this chain into 2143 * the fragment pointers. Stop when we run out 2144 * of fragments or hit the end of the mbuf chain. 2145 */ 2146 for (m = m_head; m != NULL; m = m->m_next) { 2147 if (m->m_len != 0) { 2148 f = &sc->bge_rdata->bge_tx_ring[frag]; 2149 if (sc->bge_cdata.bge_tx_chain[frag] != NULL) 2150 break; 2151 BGE_HOSTADDR(f->bge_addr) = 2152 vtophys(mtod(m, vm_offset_t)); 2153 f->bge_len = m->m_len; 2154 f->bge_flags = csum_flags; 2155 if (ifv != NULL) { 2156 f->bge_flags |= BGE_TXBDFLAG_VLAN_TAG; 2157 f->bge_vlan_tag = ifv->ifv_tag; 2158 } else { 2159 f->bge_vlan_tag = 0; 2160 } 2161 /* 2162 * Sanity check: avoid coming within 16 descriptors 2163 * of the end of the ring. 2164 */ 2165 if ((BGE_TX_RING_CNT - (sc->bge_txcnt + cnt)) < 16) 2166 return(ENOBUFS); 2167 cur = frag; 2168 BGE_INC(frag, BGE_TX_RING_CNT); 2169 cnt++; 2170 } 2171 } 2172 2173 if (m != NULL) 2174 return(ENOBUFS); 2175 2176 if (frag == sc->bge_tx_saved_considx) 2177 return(ENOBUFS); 2178 2179 sc->bge_rdata->bge_tx_ring[cur].bge_flags |= BGE_TXBDFLAG_END; 2180 sc->bge_cdata.bge_tx_chain[cur] = m_head; 2181 sc->bge_txcnt += cnt; 2182 2183 *txidx = frag; 2184 2185 return(0); 2186 } 2187 2188 /* 2189 * Main transmit routine. To avoid having to do mbuf copies, we put pointers 2190 * to the mbuf data regions directly in the transmit descriptors. 2191 */ 2192 static void 2193 bge_start(ifp) 2194 struct ifnet *ifp; 2195 { 2196 struct bge_softc *sc; 2197 struct mbuf *m_head = NULL; 2198 u_int32_t prodidx = 0; 2199 2200 sc = ifp->if_softc; 2201 2202 if (!sc->bge_link && ifp->if_snd.ifq_len < 10) 2203 return; 2204 2205 prodidx = CSR_READ_4(sc, BGE_MBX_TX_HOST_PROD0_LO); 2206 2207 while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) { 2208 IF_DEQUEUE(&ifp->if_snd, m_head); 2209 if (m_head == NULL) 2210 break; 2211 2212 /* 2213 * XXX 2214 * safety overkill. If this is a fragmented packet chain 2215 * with delayed TCP/UDP checksums, then only encapsulate 2216 * it if we have enough descriptors to handle the entire 2217 * chain at once. 2218 * (paranoia -- may not actually be needed) 2219 */ 2220 if (m_head->m_flags & M_FIRSTFRAG && 2221 m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) { 2222 if ((BGE_TX_RING_CNT - sc->bge_txcnt) < 2223 m_head->m_pkthdr.csum_data + 16) { 2224 IF_PREPEND(&ifp->if_snd, m_head); 2225 ifp->if_flags |= IFF_OACTIVE; 2226 break; 2227 } 2228 } 2229 2230 /* 2231 * Pack the data into the transmit ring. If we 2232 * don't have room, set the OACTIVE flag and wait 2233 * for the NIC to drain the ring. 2234 */ 2235 if (bge_encap(sc, m_head, &prodidx)) { 2236 IF_PREPEND(&ifp->if_snd, m_head); 2237 ifp->if_flags |= IFF_OACTIVE; 2238 break; 2239 } 2240 2241 /* 2242 * If there's a BPF listener, bounce a copy of this frame 2243 * to him. 2244 */ 2245 if (ifp->if_bpf) 2246 bpf_mtap(ifp, m_head); 2247 } 2248 2249 /* Transmit */ 2250 CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx); 2251 2252 /* 2253 * Set a timeout in case the chip goes out to lunch. 2254 */ 2255 ifp->if_timer = 5; 2256 2257 return; 2258 } 2259 2260 /* 2261 * If we have a BCM5400 or BCM5401 PHY, we need to properly 2262 * program its internal DSP. Failing to do this can result in 2263 * massive packet loss at 1Gb speeds. 2264 */ 2265 static void 2266 bge_phy_hack(sc) 2267 struct bge_softc *sc; 2268 { 2269 struct bge_bcom_hack bhack[] = { 2270 { BRGPHY_MII_AUXCTL, 0x4C20 }, 2271 { BRGPHY_MII_DSP_ADDR_REG, 0x0012 }, 2272 { BRGPHY_MII_DSP_RW_PORT, 0x1804 }, 2273 { BRGPHY_MII_DSP_ADDR_REG, 0x0013 }, 2274 { BRGPHY_MII_DSP_RW_PORT, 0x1204 }, 2275 { BRGPHY_MII_DSP_ADDR_REG, 0x8006 }, 2276 { BRGPHY_MII_DSP_RW_PORT, 0x0132 }, 2277 { BRGPHY_MII_DSP_ADDR_REG, 0x8006 }, 2278 { BRGPHY_MII_DSP_RW_PORT, 0x0232 }, 2279 { BRGPHY_MII_DSP_ADDR_REG, 0x201F }, 2280 { BRGPHY_MII_DSP_RW_PORT, 0x0A20 }, 2281 { 0, 0 } }; 2282 u_int16_t vid, did; 2283 int i; 2284 2285 vid = bge_miibus_readreg(sc->bge_dev, 1, MII_PHYIDR1); 2286 did = bge_miibus_readreg(sc->bge_dev, 1, MII_PHYIDR2); 2287 2288 if (MII_OUI(vid, did) == MII_OUI_xxBROADCOM && 2289 (MII_MODEL(did) == MII_MODEL_xxBROADCOM_BCM5400 || 2290 MII_MODEL(did) == MII_MODEL_xxBROADCOM_BCM5401)) { 2291 i = 0; 2292 while(bhack[i].reg) { 2293 bge_miibus_writereg(sc->bge_dev, 1, bhack[i].reg, 2294 bhack[i].val); 2295 i++; 2296 } 2297 } 2298 2299 return; 2300 } 2301 2302 static void 2303 bge_init(xsc) 2304 void *xsc; 2305 { 2306 struct bge_softc *sc = xsc; 2307 struct ifnet *ifp; 2308 u_int16_t *m; 2309 int s; 2310 2311 s = splimp(); 2312 2313 ifp = &sc->arpcom.ac_if; 2314 2315 if (ifp->if_flags & IFF_RUNNING) 2316 return; 2317 2318 /* Cancel pending I/O and flush buffers. */ 2319 bge_stop(sc); 2320 bge_reset(sc); 2321 bge_chipinit(sc); 2322 2323 /* 2324 * Init the various state machines, ring 2325 * control blocks and firmware. 2326 */ 2327 if (bge_blockinit(sc)) { 2328 printf("bge%d: initialization failure\n", sc->bge_unit); 2329 splx(s); 2330 return; 2331 } 2332 2333 ifp = &sc->arpcom.ac_if; 2334 2335 /* Specify MTU. */ 2336 CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu + 2337 ETHER_HDR_LEN + ETHER_CRC_LEN); 2338 2339 /* Load our MAC address. */ 2340 m = (u_int16_t *)&sc->arpcom.ac_enaddr[0]; 2341 CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0])); 2342 CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2])); 2343 2344 /* Enable or disable promiscuous mode as needed. */ 2345 if (ifp->if_flags & IFF_PROMISC) { 2346 BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC); 2347 } else { 2348 BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC); 2349 } 2350 2351 /* Program multicast filter. */ 2352 bge_setmulti(sc); 2353 2354 /* Init RX ring. */ 2355 bge_init_rx_ring_std(sc); 2356 2357 /* Init jumbo RX ring. */ 2358 if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN)) 2359 bge_init_rx_ring_jumbo(sc); 2360 2361 /* Init our RX return ring index */ 2362 sc->bge_rx_saved_considx = 0; 2363 2364 /* Init TX ring. */ 2365 bge_init_tx_ring(sc); 2366 2367 /* Turn on transmitter */ 2368 BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE); 2369 2370 /* Turn on receiver */ 2371 BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE); 2372 2373 /* Tell firmware we're alive. */ 2374 BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP); 2375 2376 /* Enable host interrupts. */ 2377 BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA); 2378 BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR); 2379 CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0); 2380 2381 bge_ifmedia_upd(ifp); 2382 2383 ifp->if_flags |= IFF_RUNNING; 2384 ifp->if_flags &= ~IFF_OACTIVE; 2385 2386 splx(s); 2387 2388 sc->bge_stat_ch = timeout(bge_tick, sc, hz); 2389 2390 return; 2391 } 2392 2393 /* 2394 * Set media options. 2395 */ 2396 static int 2397 bge_ifmedia_upd(ifp) 2398 struct ifnet *ifp; 2399 { 2400 struct bge_softc *sc; 2401 struct mii_data *mii; 2402 struct ifmedia *ifm; 2403 2404 sc = ifp->if_softc; 2405 ifm = &sc->bge_ifmedia; 2406 2407 /* If this is a 1000baseX NIC, enable the TBI port. */ 2408 if (sc->bge_tbi) { 2409 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) 2410 return(EINVAL); 2411 switch(IFM_SUBTYPE(ifm->ifm_media)) { 2412 case IFM_AUTO: 2413 break; 2414 case IFM_1000_SX: 2415 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) { 2416 BGE_CLRBIT(sc, BGE_MAC_MODE, 2417 BGE_MACMODE_HALF_DUPLEX); 2418 } else { 2419 BGE_SETBIT(sc, BGE_MAC_MODE, 2420 BGE_MACMODE_HALF_DUPLEX); 2421 } 2422 break; 2423 default: 2424 return(EINVAL); 2425 } 2426 return(0); 2427 } 2428 2429 mii = device_get_softc(sc->bge_miibus); 2430 sc->bge_link = 0; 2431 if (mii->mii_instance) { 2432 struct mii_softc *miisc; 2433 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL; 2434 miisc = LIST_NEXT(miisc, mii_list)) 2435 mii_phy_reset(miisc); 2436 } 2437 bge_phy_hack(sc); 2438 mii_mediachg(mii); 2439 2440 return(0); 2441 } 2442 2443 /* 2444 * Report current media status. 2445 */ 2446 static void 2447 bge_ifmedia_sts(ifp, ifmr) 2448 struct ifnet *ifp; 2449 struct ifmediareq *ifmr; 2450 { 2451 struct bge_softc *sc; 2452 struct mii_data *mii; 2453 2454 sc = ifp->if_softc; 2455 2456 if (sc->bge_tbi) { 2457 ifmr->ifm_status = IFM_AVALID; 2458 ifmr->ifm_active = IFM_ETHER; 2459 if (CSR_READ_4(sc, BGE_MAC_STS) & 2460 BGE_MACSTAT_TBI_PCS_SYNCHED) 2461 ifmr->ifm_status |= IFM_ACTIVE; 2462 ifmr->ifm_active |= IFM_1000_SX; 2463 if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX) 2464 ifmr->ifm_active |= IFM_HDX; 2465 else 2466 ifmr->ifm_active |= IFM_FDX; 2467 return; 2468 } 2469 2470 mii = device_get_softc(sc->bge_miibus); 2471 mii_pollstat(mii); 2472 ifmr->ifm_active = mii->mii_media_active; 2473 ifmr->ifm_status = mii->mii_media_status; 2474 2475 return; 2476 } 2477 2478 static int 2479 bge_ioctl(ifp, command, data) 2480 struct ifnet *ifp; 2481 u_long command; 2482 caddr_t data; 2483 { 2484 struct bge_softc *sc = ifp->if_softc; 2485 struct ifreq *ifr = (struct ifreq *) data; 2486 int s, mask, error = 0; 2487 struct mii_data *mii; 2488 2489 s = splimp(); 2490 2491 switch(command) { 2492 case SIOCSIFADDR: 2493 case SIOCGIFADDR: 2494 error = ether_ioctl(ifp, command, data); 2495 break; 2496 case SIOCSIFMTU: 2497 if (ifr->ifr_mtu > BGE_JUMBO_MTU) 2498 error = EINVAL; 2499 else { 2500 ifp->if_mtu = ifr->ifr_mtu; 2501 ifp->if_flags &= ~IFF_RUNNING; 2502 bge_init(sc); 2503 } 2504 break; 2505 case SIOCSIFFLAGS: 2506 if (ifp->if_flags & IFF_UP) { 2507 /* 2508 * If only the state of the PROMISC flag changed, 2509 * then just use the 'set promisc mode' command 2510 * instead of reinitializing the entire NIC. Doing 2511 * a full re-init means reloading the firmware and 2512 * waiting for it to start up, which may take a 2513 * second or two. 2514 */ 2515 if (ifp->if_flags & IFF_RUNNING && 2516 ifp->if_flags & IFF_PROMISC && 2517 !(sc->bge_if_flags & IFF_PROMISC)) { 2518 BGE_SETBIT(sc, BGE_RX_MODE, 2519 BGE_RXMODE_RX_PROMISC); 2520 } else if (ifp->if_flags & IFF_RUNNING && 2521 !(ifp->if_flags & IFF_PROMISC) && 2522 sc->bge_if_flags & IFF_PROMISC) { 2523 BGE_CLRBIT(sc, BGE_RX_MODE, 2524 BGE_RXMODE_RX_PROMISC); 2525 } else 2526 bge_init(sc); 2527 } else { 2528 if (ifp->if_flags & IFF_RUNNING) { 2529 bge_stop(sc); 2530 } 2531 } 2532 sc->bge_if_flags = ifp->if_flags; 2533 error = 0; 2534 break; 2535 case SIOCADDMULTI: 2536 case SIOCDELMULTI: 2537 if (ifp->if_flags & IFF_RUNNING) { 2538 bge_setmulti(sc); 2539 error = 0; 2540 } 2541 break; 2542 case SIOCSIFMEDIA: 2543 case SIOCGIFMEDIA: 2544 if (sc->bge_tbi) { 2545 error = ifmedia_ioctl(ifp, ifr, 2546 &sc->bge_ifmedia, command); 2547 } else { 2548 mii = device_get_softc(sc->bge_miibus); 2549 error = ifmedia_ioctl(ifp, ifr, 2550 &mii->mii_media, command); 2551 } 2552 break; 2553 case SIOCSIFCAP: 2554 mask = ifr->ifr_reqcap ^ ifp->if_capenable; 2555 if (mask & IFCAP_HWCSUM) { 2556 if (IFCAP_HWCSUM & ifp->if_capenable) 2557 ifp->if_capenable &= ~IFCAP_HWCSUM; 2558 else 2559 ifp->if_capenable |= IFCAP_HWCSUM; 2560 } 2561 error = 0; 2562 break; 2563 default: 2564 error = EINVAL; 2565 break; 2566 } 2567 2568 (void)splx(s); 2569 2570 return(error); 2571 } 2572 2573 static void 2574 bge_watchdog(ifp) 2575 struct ifnet *ifp; 2576 { 2577 struct bge_softc *sc; 2578 2579 sc = ifp->if_softc; 2580 2581 printf("bge%d: watchdog timeout -- resetting\n", sc->bge_unit); 2582 2583 ifp->if_flags &= ~IFF_RUNNING; 2584 bge_init(sc); 2585 2586 ifp->if_oerrors++; 2587 2588 return; 2589 } 2590 2591 /* 2592 * Stop the adapter and free any mbufs allocated to the 2593 * RX and TX lists. 2594 */ 2595 static void 2596 bge_stop(sc) 2597 struct bge_softc *sc; 2598 { 2599 struct ifnet *ifp; 2600 struct ifmedia_entry *ifm; 2601 struct mii_data *mii = NULL; 2602 int mtmp, itmp; 2603 2604 ifp = &sc->arpcom.ac_if; 2605 2606 if (!sc->bge_tbi) 2607 mii = device_get_softc(sc->bge_miibus); 2608 2609 untimeout(bge_tick, sc, sc->bge_stat_ch); 2610 2611 /* 2612 * Disable all of the receiver blocks 2613 */ 2614 BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE); 2615 BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE); 2616 BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE); 2617 BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE); 2618 BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE); 2619 BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE); 2620 BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE); 2621 2622 /* 2623 * Disable all of the transmit blocks 2624 */ 2625 BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE); 2626 BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE); 2627 BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE); 2628 BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE); 2629 BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE); 2630 BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE); 2631 BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE); 2632 2633 /* 2634 * Shut down all of the memory managers and related 2635 * state machines. 2636 */ 2637 BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE); 2638 BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE); 2639 BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE); 2640 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF); 2641 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0); 2642 BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE); 2643 BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE); 2644 2645 /* Disable host interrupts. */ 2646 BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR); 2647 CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1); 2648 2649 /* 2650 * Tell firmware we're shutting down. 2651 */ 2652 BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP); 2653 2654 /* Free the RX lists. */ 2655 bge_free_rx_ring_std(sc); 2656 2657 /* Free jumbo RX list. */ 2658 bge_free_rx_ring_jumbo(sc); 2659 2660 /* Free TX buffers. */ 2661 bge_free_tx_ring(sc); 2662 2663 /* 2664 * Isolate/power down the PHY, but leave the media selection 2665 * unchanged so that things will be put back to normal when 2666 * we bring the interface back up. 2667 */ 2668 if (!sc->bge_tbi) { 2669 itmp = ifp->if_flags; 2670 ifp->if_flags |= IFF_UP; 2671 ifm = mii->mii_media.ifm_cur; 2672 mtmp = ifm->ifm_media; 2673 ifm->ifm_media = IFM_ETHER|IFM_NONE; 2674 mii_mediachg(mii); 2675 ifm->ifm_media = mtmp; 2676 ifp->if_flags = itmp; 2677 } 2678 2679 sc->bge_link = 0; 2680 2681 sc->bge_tx_saved_considx = BGE_TXCONS_UNSET; 2682 2683 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 2684 2685 return; 2686 } 2687 2688 /* 2689 * Stop all chip I/O so that the kernel's probe routines don't 2690 * get confused by errant DMAs when rebooting. 2691 */ 2692 static void 2693 bge_shutdown(dev) 2694 device_t dev; 2695 { 2696 struct bge_softc *sc; 2697 2698 sc = device_get_softc(dev); 2699 2700 bge_stop(sc); 2701 bge_reset(sc); 2702 2703 return; 2704 } 2705