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 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 /* 38 * Broadcom BCM570x family gigabit ethernet driver for FreeBSD. 39 * 40 * The Broadcom BCM5700 is based on technology originally developed by 41 * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet 42 * MAC chips. The BCM5700, sometimes refered to as the Tigon III, has 43 * two on-board MIPS R4000 CPUs and can have as much as 16MB of external 44 * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo 45 * frames, highly configurable RX filtering, and 16 RX and TX queues 46 * (which, along with RX filter rules, can be used for QOS applications). 47 * Other features, such as TCP segmentation, may be available as part 48 * of value-added firmware updates. Unlike the Tigon I and Tigon II, 49 * firmware images can be stored in hardware and need not be compiled 50 * into the driver. 51 * 52 * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will 53 * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus. 54 * 55 * The BCM5701 is a single-chip solution incorporating both the BCM5700 56 * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701 57 * does not support external SSRAM. 58 * 59 * Broadcom also produces a variation of the BCM5700 under the "Altima" 60 * brand name, which is functionally similar but lacks PCI-X support. 61 * 62 * Without external SSRAM, you can only have at most 4 TX rings, 63 * and the use of the mini RX ring is disabled. This seems to imply 64 * that these features are simply not available on the BCM5701. As a 65 * result, this driver does not implement any support for the mini RX 66 * ring. 67 */ 68 69 #include <sys/param.h> 70 #include <sys/endian.h> 71 #include <sys/systm.h> 72 #include <sys/sockio.h> 73 #include <sys/mbuf.h> 74 #include <sys/malloc.h> 75 #include <sys/kernel.h> 76 #include <sys/module.h> 77 #include <sys/socket.h> 78 #include <sys/queue.h> 79 80 #include <net/if.h> 81 #include <net/if_arp.h> 82 #include <net/ethernet.h> 83 #include <net/if_dl.h> 84 #include <net/if_media.h> 85 86 #include <net/bpf.h> 87 88 #include <net/if_types.h> 89 #include <net/if_vlan_var.h> 90 91 #include <netinet/in_systm.h> 92 #include <netinet/in.h> 93 #include <netinet/ip.h> 94 95 #include <machine/clock.h> /* for DELAY */ 96 #include <machine/bus.h> 97 #include <machine/resource.h> 98 #include <sys/bus.h> 99 #include <sys/rman.h> 100 101 #include <dev/mii/mii.h> 102 #include <dev/mii/miivar.h> 103 #include "miidevs.h" 104 #include <dev/mii/brgphyreg.h> 105 106 #include <dev/pci/pcireg.h> 107 #include <dev/pci/pcivar.h> 108 109 #include <dev/bge/if_bgereg.h> 110 111 #define BGE_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP) 112 113 MODULE_DEPEND(bge, pci, 1, 1, 1); 114 MODULE_DEPEND(bge, ether, 1, 1, 1); 115 MODULE_DEPEND(bge, miibus, 1, 1, 1); 116 117 /* "controller miibus0" required. See GENERIC if you get errors here. */ 118 #include "miibus_if.h" 119 120 /* 121 * Various supported device vendors/types and their names. Note: the 122 * spec seems to indicate that the hardware still has Alteon's vendor 123 * ID burned into it, though it will always be overriden by the vendor 124 * ID in the EEPROM. Just to be safe, we cover all possibilities. 125 */ 126 #define BGE_DEVDESC_MAX 64 /* Maximum device description length */ 127 128 static struct bge_type bge_devs[] = { 129 { ALT_VENDORID, ALT_DEVICEID_BCM5700, 130 "Broadcom BCM5700 Gigabit Ethernet" }, 131 { ALT_VENDORID, ALT_DEVICEID_BCM5701, 132 "Broadcom BCM5701 Gigabit Ethernet" }, 133 { BCOM_VENDORID, BCOM_DEVICEID_BCM5700, 134 "Broadcom BCM5700 Gigabit Ethernet" }, 135 { BCOM_VENDORID, BCOM_DEVICEID_BCM5701, 136 "Broadcom BCM5701 Gigabit Ethernet" }, 137 { BCOM_VENDORID, BCOM_DEVICEID_BCM5702, 138 "Broadcom BCM5702 Gigabit Ethernet" }, 139 { BCOM_VENDORID, BCOM_DEVICEID_BCM5702X, 140 "Broadcom BCM5702X Gigabit Ethernet" }, 141 { BCOM_VENDORID, BCOM_DEVICEID_BCM5703, 142 "Broadcom BCM5703 Gigabit Ethernet" }, 143 { BCOM_VENDORID, BCOM_DEVICEID_BCM5703X, 144 "Broadcom BCM5703X Gigabit Ethernet" }, 145 { BCOM_VENDORID, BCOM_DEVICEID_BCM5704C, 146 "Broadcom BCM5704C Dual Gigabit Ethernet" }, 147 { BCOM_VENDORID, BCOM_DEVICEID_BCM5704S, 148 "Broadcom BCM5704S Dual Gigabit Ethernet" }, 149 { BCOM_VENDORID, BCOM_DEVICEID_BCM5705, 150 "Broadcom BCM5705 Gigabit Ethernet" }, 151 { BCOM_VENDORID, BCOM_DEVICEID_BCM5705K, 152 "Broadcom BCM5705K Gigabit Ethernet" }, 153 { BCOM_VENDORID, BCOM_DEVICEID_BCM5705M, 154 "Broadcom BCM5705M Gigabit Ethernet" }, 155 { BCOM_VENDORID, BCOM_DEVICEID_BCM5705M_ALT, 156 "Broadcom BCM5705M Gigabit Ethernet" }, 157 { BCOM_VENDORID, BCOM_DEVICEID_BCM5714C, 158 "Broadcom BCM5714C Gigabit Ethernet" }, 159 { BCOM_VENDORID, BCOM_DEVICEID_BCM5721, 160 "Broadcom BCM5721 Gigabit Ethernet" }, 161 { BCOM_VENDORID, BCOM_DEVICEID_BCM5750, 162 "Broadcom BCM5750 Gigabit Ethernet" }, 163 { BCOM_VENDORID, BCOM_DEVICEID_BCM5750M, 164 "Broadcom BCM5750M Gigabit Ethernet" }, 165 { BCOM_VENDORID, BCOM_DEVICEID_BCM5751, 166 "Broadcom BCM5751 Gigabit Ethernet" }, 167 { BCOM_VENDORID, BCOM_DEVICEID_BCM5751M, 168 "Broadcom BCM5751M Gigabit Ethernet" }, 169 { BCOM_VENDORID, BCOM_DEVICEID_BCM5782, 170 "Broadcom BCM5782 Gigabit Ethernet" }, 171 { BCOM_VENDORID, BCOM_DEVICEID_BCM5788, 172 "Broadcom BCM5788 Gigabit Ethernet" }, 173 { BCOM_VENDORID, BCOM_DEVICEID_BCM5789, 174 "Broadcom BCM5789 Gigabit Ethernet" }, 175 { BCOM_VENDORID, BCOM_DEVICEID_BCM5901, 176 "Broadcom BCM5901 Fast Ethernet" }, 177 { BCOM_VENDORID, BCOM_DEVICEID_BCM5901A2, 178 "Broadcom BCM5901A2 Fast Ethernet" }, 179 { SK_VENDORID, SK_DEVICEID_ALTIMA, 180 "SysKonnect Gigabit Ethernet" }, 181 { ALTIMA_VENDORID, ALTIMA_DEVICE_AC1000, 182 "Altima AC1000 Gigabit Ethernet" }, 183 { ALTIMA_VENDORID, ALTIMA_DEVICE_AC1002, 184 "Altima AC1002 Gigabit Ethernet" }, 185 { ALTIMA_VENDORID, ALTIMA_DEVICE_AC9100, 186 "Altima AC9100 Gigabit Ethernet" }, 187 { 0, 0, NULL } 188 }; 189 190 static int bge_probe (device_t); 191 static int bge_attach (device_t); 192 static int bge_detach (device_t); 193 static void bge_release_resources 194 (struct bge_softc *); 195 static void bge_dma_map_addr (void *, bus_dma_segment_t *, int, int); 196 static void bge_dma_map_tx_desc (void *, bus_dma_segment_t *, int, 197 bus_size_t, int); 198 static int bge_dma_alloc (device_t); 199 static void bge_dma_free (struct bge_softc *); 200 201 static void bge_txeof (struct bge_softc *); 202 static void bge_rxeof (struct bge_softc *); 203 204 static void bge_tick_locked (struct bge_softc *); 205 static void bge_tick (void *); 206 static void bge_stats_update (struct bge_softc *); 207 static void bge_stats_update_regs 208 (struct bge_softc *); 209 static int bge_encap (struct bge_softc *, struct mbuf *, 210 u_int32_t *); 211 212 static void bge_intr (void *); 213 static void bge_start_locked (struct ifnet *); 214 static void bge_start (struct ifnet *); 215 static int bge_ioctl (struct ifnet *, u_long, caddr_t); 216 static void bge_init_locked (struct bge_softc *); 217 static void bge_init (void *); 218 static void bge_stop (struct bge_softc *); 219 static void bge_watchdog (struct ifnet *); 220 static void bge_shutdown (device_t); 221 static int bge_ifmedia_upd (struct ifnet *); 222 static void bge_ifmedia_sts (struct ifnet *, struct ifmediareq *); 223 224 static u_int8_t bge_eeprom_getbyte (struct bge_softc *, int, u_int8_t *); 225 static int bge_read_eeprom (struct bge_softc *, caddr_t, int, int); 226 227 static void bge_setmulti (struct bge_softc *); 228 229 static void bge_handle_events (struct bge_softc *); 230 static int bge_alloc_jumbo_mem (struct bge_softc *); 231 static void bge_free_jumbo_mem (struct bge_softc *); 232 static void *bge_jalloc (struct bge_softc *); 233 static void bge_jfree (void *, void *); 234 static int bge_newbuf_std (struct bge_softc *, int, struct mbuf *); 235 static int bge_newbuf_jumbo (struct bge_softc *, int, struct mbuf *); 236 static int bge_init_rx_ring_std (struct bge_softc *); 237 static void bge_free_rx_ring_std (struct bge_softc *); 238 static int bge_init_rx_ring_jumbo (struct bge_softc *); 239 static void bge_free_rx_ring_jumbo (struct bge_softc *); 240 static void bge_free_tx_ring (struct bge_softc *); 241 static int bge_init_tx_ring (struct bge_softc *); 242 243 static int bge_chipinit (struct bge_softc *); 244 static int bge_blockinit (struct bge_softc *); 245 246 #ifdef notdef 247 static u_int8_t bge_vpd_readbyte(struct bge_softc *, int); 248 static void bge_vpd_read_res (struct bge_softc *, struct vpd_res *, int); 249 static void bge_vpd_read (struct bge_softc *); 250 #endif 251 252 static u_int32_t bge_readmem_ind 253 (struct bge_softc *, int); 254 static void bge_writemem_ind (struct bge_softc *, int, int); 255 #ifdef notdef 256 static u_int32_t bge_readreg_ind 257 (struct bge_softc *, int); 258 #endif 259 static void bge_writereg_ind (struct bge_softc *, int, int); 260 261 static int bge_miibus_readreg (device_t, int, int); 262 static int bge_miibus_writereg (device_t, int, int, int); 263 static void bge_miibus_statchg (device_t); 264 265 static void bge_reset (struct bge_softc *); 266 267 static device_method_t bge_methods[] = { 268 /* Device interface */ 269 DEVMETHOD(device_probe, bge_probe), 270 DEVMETHOD(device_attach, bge_attach), 271 DEVMETHOD(device_detach, bge_detach), 272 DEVMETHOD(device_shutdown, bge_shutdown), 273 274 /* bus interface */ 275 DEVMETHOD(bus_print_child, bus_generic_print_child), 276 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 277 278 /* MII interface */ 279 DEVMETHOD(miibus_readreg, bge_miibus_readreg), 280 DEVMETHOD(miibus_writereg, bge_miibus_writereg), 281 DEVMETHOD(miibus_statchg, bge_miibus_statchg), 282 283 { 0, 0 } 284 }; 285 286 static driver_t bge_driver = { 287 "bge", 288 bge_methods, 289 sizeof(struct bge_softc) 290 }; 291 292 static devclass_t bge_devclass; 293 294 DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0); 295 DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0); 296 297 static u_int32_t 298 bge_readmem_ind(sc, off) 299 struct bge_softc *sc; 300 int off; 301 { 302 device_t dev; 303 304 dev = sc->bge_dev; 305 306 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4); 307 return(pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4)); 308 } 309 310 static void 311 bge_writemem_ind(sc, off, val) 312 struct bge_softc *sc; 313 int off, val; 314 { 315 device_t dev; 316 317 dev = sc->bge_dev; 318 319 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4); 320 pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4); 321 322 return; 323 } 324 325 #ifdef notdef 326 static u_int32_t 327 bge_readreg_ind(sc, off) 328 struct bge_softc *sc; 329 int off; 330 { 331 device_t dev; 332 333 dev = sc->bge_dev; 334 335 pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4); 336 return(pci_read_config(dev, BGE_PCI_REG_DATA, 4)); 337 } 338 #endif 339 340 static void 341 bge_writereg_ind(sc, off, val) 342 struct bge_softc *sc; 343 int off, val; 344 { 345 device_t dev; 346 347 dev = sc->bge_dev; 348 349 pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4); 350 pci_write_config(dev, BGE_PCI_REG_DATA, val, 4); 351 352 return; 353 } 354 355 /* 356 * Map a single buffer address. 357 */ 358 359 static void 360 bge_dma_map_addr(arg, segs, nseg, error) 361 void *arg; 362 bus_dma_segment_t *segs; 363 int nseg; 364 int error; 365 { 366 struct bge_dmamap_arg *ctx; 367 368 if (error) 369 return; 370 371 ctx = arg; 372 373 if (nseg > ctx->bge_maxsegs) { 374 ctx->bge_maxsegs = 0; 375 return; 376 } 377 378 ctx->bge_busaddr = segs->ds_addr; 379 380 return; 381 } 382 383 /* 384 * Map an mbuf chain into an TX ring. 385 */ 386 387 static void 388 bge_dma_map_tx_desc(arg, segs, nseg, mapsize, error) 389 void *arg; 390 bus_dma_segment_t *segs; 391 int nseg; 392 bus_size_t mapsize; 393 int error; 394 { 395 struct bge_dmamap_arg *ctx; 396 struct bge_tx_bd *d = NULL; 397 int i = 0, idx; 398 399 if (error) 400 return; 401 402 ctx = arg; 403 404 /* Signal error to caller if there's too many segments */ 405 if (nseg > ctx->bge_maxsegs) { 406 ctx->bge_maxsegs = 0; 407 return; 408 } 409 410 idx = ctx->bge_idx; 411 while(1) { 412 d = &ctx->bge_ring[idx]; 413 d->bge_addr.bge_addr_lo = 414 htole32(BGE_ADDR_LO(segs[i].ds_addr)); 415 d->bge_addr.bge_addr_hi = 416 htole32(BGE_ADDR_HI(segs[i].ds_addr)); 417 d->bge_len = htole16(segs[i].ds_len); 418 d->bge_flags = htole16(ctx->bge_flags); 419 i++; 420 if (i == nseg) 421 break; 422 BGE_INC(idx, BGE_TX_RING_CNT); 423 } 424 425 d->bge_flags |= htole16(BGE_TXBDFLAG_END); 426 ctx->bge_maxsegs = nseg; 427 ctx->bge_idx = idx; 428 429 return; 430 } 431 432 433 #ifdef notdef 434 static u_int8_t 435 bge_vpd_readbyte(sc, addr) 436 struct bge_softc *sc; 437 int addr; 438 { 439 int i; 440 device_t dev; 441 u_int32_t val; 442 443 dev = sc->bge_dev; 444 pci_write_config(dev, BGE_PCI_VPD_ADDR, addr, 2); 445 for (i = 0; i < BGE_TIMEOUT * 10; i++) { 446 DELAY(10); 447 if (pci_read_config(dev, BGE_PCI_VPD_ADDR, 2) & BGE_VPD_FLAG) 448 break; 449 } 450 451 if (i == BGE_TIMEOUT) { 452 printf("bge%d: VPD read timed out\n", sc->bge_unit); 453 return(0); 454 } 455 456 val = pci_read_config(dev, BGE_PCI_VPD_DATA, 4); 457 458 return((val >> ((addr % 4) * 8)) & 0xFF); 459 } 460 461 static void 462 bge_vpd_read_res(sc, res, addr) 463 struct bge_softc *sc; 464 struct vpd_res *res; 465 int addr; 466 { 467 int i; 468 u_int8_t *ptr; 469 470 ptr = (u_int8_t *)res; 471 for (i = 0; i < sizeof(struct vpd_res); i++) 472 ptr[i] = bge_vpd_readbyte(sc, i + addr); 473 474 return; 475 } 476 477 static void 478 bge_vpd_read(sc) 479 struct bge_softc *sc; 480 { 481 int pos = 0, i; 482 struct vpd_res res; 483 484 if (sc->bge_vpd_prodname != NULL) 485 free(sc->bge_vpd_prodname, M_DEVBUF); 486 if (sc->bge_vpd_readonly != NULL) 487 free(sc->bge_vpd_readonly, M_DEVBUF); 488 sc->bge_vpd_prodname = NULL; 489 sc->bge_vpd_readonly = NULL; 490 491 bge_vpd_read_res(sc, &res, pos); 492 493 if (res.vr_id != VPD_RES_ID) { 494 printf("bge%d: bad VPD resource id: expected %x got %x\n", 495 sc->bge_unit, VPD_RES_ID, res.vr_id); 496 return; 497 } 498 499 pos += sizeof(res); 500 sc->bge_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT); 501 for (i = 0; i < res.vr_len; i++) 502 sc->bge_vpd_prodname[i] = bge_vpd_readbyte(sc, i + pos); 503 sc->bge_vpd_prodname[i] = '\0'; 504 pos += i; 505 506 bge_vpd_read_res(sc, &res, pos); 507 508 if (res.vr_id != VPD_RES_READ) { 509 printf("bge%d: bad VPD resource id: expected %x got %x\n", 510 sc->bge_unit, VPD_RES_READ, res.vr_id); 511 return; 512 } 513 514 pos += sizeof(res); 515 sc->bge_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT); 516 for (i = 0; i < res.vr_len + 1; i++) 517 sc->bge_vpd_readonly[i] = bge_vpd_readbyte(sc, i + pos); 518 519 return; 520 } 521 #endif 522 523 /* 524 * Read a byte of data stored in the EEPROM at address 'addr.' The 525 * BCM570x supports both the traditional bitbang interface and an 526 * auto access interface for reading the EEPROM. We use the auto 527 * access method. 528 */ 529 static u_int8_t 530 bge_eeprom_getbyte(sc, addr, dest) 531 struct bge_softc *sc; 532 int addr; 533 u_int8_t *dest; 534 { 535 int i; 536 u_int32_t byte = 0; 537 538 /* 539 * Enable use of auto EEPROM access so we can avoid 540 * having to use the bitbang method. 541 */ 542 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM); 543 544 /* Reset the EEPROM, load the clock period. */ 545 CSR_WRITE_4(sc, BGE_EE_ADDR, 546 BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL)); 547 DELAY(20); 548 549 /* Issue the read EEPROM command. */ 550 CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr); 551 552 /* Wait for completion */ 553 for(i = 0; i < BGE_TIMEOUT * 10; i++) { 554 DELAY(10); 555 if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE) 556 break; 557 } 558 559 if (i == BGE_TIMEOUT) { 560 printf("bge%d: eeprom read timed out\n", sc->bge_unit); 561 return(0); 562 } 563 564 /* Get result. */ 565 byte = CSR_READ_4(sc, BGE_EE_DATA); 566 567 *dest = (byte >> ((addr % 4) * 8)) & 0xFF; 568 569 return(0); 570 } 571 572 /* 573 * Read a sequence of bytes from the EEPROM. 574 */ 575 static int 576 bge_read_eeprom(sc, dest, off, cnt) 577 struct bge_softc *sc; 578 caddr_t dest; 579 int off; 580 int cnt; 581 { 582 int err = 0, i; 583 u_int8_t byte = 0; 584 585 for (i = 0; i < cnt; i++) { 586 err = bge_eeprom_getbyte(sc, off + i, &byte); 587 if (err) 588 break; 589 *(dest + i) = byte; 590 } 591 592 return(err ? 1 : 0); 593 } 594 595 static int 596 bge_miibus_readreg(dev, phy, reg) 597 device_t dev; 598 int phy, reg; 599 { 600 struct bge_softc *sc; 601 u_int32_t val, autopoll; 602 int i; 603 604 sc = device_get_softc(dev); 605 606 /* 607 * Broadcom's own driver always assumes the internal 608 * PHY is at GMII address 1. On some chips, the PHY responds 609 * to accesses at all addresses, which could cause us to 610 * bogusly attach the PHY 32 times at probe type. Always 611 * restricting the lookup to address 1 is simpler than 612 * trying to figure out which chips revisions should be 613 * special-cased. 614 */ 615 if (phy != 1) 616 return(0); 617 618 /* Reading with autopolling on may trigger PCI errors */ 619 autopoll = CSR_READ_4(sc, BGE_MI_MODE); 620 if (autopoll & BGE_MIMODE_AUTOPOLL) { 621 BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL); 622 DELAY(40); 623 } 624 625 CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ|BGE_MICOMM_BUSY| 626 BGE_MIPHY(phy)|BGE_MIREG(reg)); 627 628 for (i = 0; i < BGE_TIMEOUT; i++) { 629 val = CSR_READ_4(sc, BGE_MI_COMM); 630 if (!(val & BGE_MICOMM_BUSY)) 631 break; 632 } 633 634 if (i == BGE_TIMEOUT) { 635 printf("bge%d: PHY read timed out\n", sc->bge_unit); 636 val = 0; 637 goto done; 638 } 639 640 val = CSR_READ_4(sc, BGE_MI_COMM); 641 642 done: 643 if (autopoll & BGE_MIMODE_AUTOPOLL) { 644 BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL); 645 DELAY(40); 646 } 647 648 if (val & BGE_MICOMM_READFAIL) 649 return(0); 650 651 return(val & 0xFFFF); 652 } 653 654 static int 655 bge_miibus_writereg(dev, phy, reg, val) 656 device_t dev; 657 int phy, reg, val; 658 { 659 struct bge_softc *sc; 660 u_int32_t autopoll; 661 int i; 662 663 sc = device_get_softc(dev); 664 665 /* Reading with autopolling on may trigger PCI errors */ 666 autopoll = CSR_READ_4(sc, BGE_MI_MODE); 667 if (autopoll & BGE_MIMODE_AUTOPOLL) { 668 BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL); 669 DELAY(40); 670 } 671 672 CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE|BGE_MICOMM_BUSY| 673 BGE_MIPHY(phy)|BGE_MIREG(reg)|val); 674 675 for (i = 0; i < BGE_TIMEOUT; i++) { 676 if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) 677 break; 678 } 679 680 if (autopoll & BGE_MIMODE_AUTOPOLL) { 681 BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL); 682 DELAY(40); 683 } 684 685 if (i == BGE_TIMEOUT) { 686 printf("bge%d: PHY read timed out\n", sc->bge_unit); 687 return(0); 688 } 689 690 return(0); 691 } 692 693 static void 694 bge_miibus_statchg(dev) 695 device_t dev; 696 { 697 struct bge_softc *sc; 698 struct mii_data *mii; 699 700 sc = device_get_softc(dev); 701 mii = device_get_softc(sc->bge_miibus); 702 703 BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE); 704 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) { 705 BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII); 706 } else { 707 BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII); 708 } 709 710 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) { 711 BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX); 712 } else { 713 BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX); 714 } 715 716 return; 717 } 718 719 /* 720 * Handle events that have triggered interrupts. 721 */ 722 static void 723 bge_handle_events(sc) 724 struct bge_softc *sc; 725 { 726 727 return; 728 } 729 730 /* 731 * Memory management for jumbo frames. 732 */ 733 734 static int 735 bge_alloc_jumbo_mem(sc) 736 struct bge_softc *sc; 737 { 738 caddr_t ptr; 739 register int i, error; 740 struct bge_jpool_entry *entry; 741 742 /* Create tag for jumbo buffer block */ 743 744 error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 745 PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, 746 NULL, BGE_JMEM, 1, BGE_JMEM, 0, NULL, NULL, 747 &sc->bge_cdata.bge_jumbo_tag); 748 749 if (error) { 750 printf("bge%d: could not allocate jumbo dma tag\n", 751 sc->bge_unit); 752 return (ENOMEM); 753 } 754 755 /* Allocate DMA'able memory for jumbo buffer block */ 756 757 error = bus_dmamem_alloc(sc->bge_cdata.bge_jumbo_tag, 758 (void **)&sc->bge_ldata.bge_jumbo_buf, BUS_DMA_NOWAIT, 759 &sc->bge_cdata.bge_jumbo_map); 760 761 if (error) 762 return (ENOMEM); 763 764 SLIST_INIT(&sc->bge_jfree_listhead); 765 SLIST_INIT(&sc->bge_jinuse_listhead); 766 767 /* 768 * Now divide it up into 9K pieces and save the addresses 769 * in an array. 770 */ 771 ptr = sc->bge_ldata.bge_jumbo_buf; 772 for (i = 0; i < BGE_JSLOTS; i++) { 773 sc->bge_cdata.bge_jslots[i] = ptr; 774 ptr += BGE_JLEN; 775 entry = malloc(sizeof(struct bge_jpool_entry), 776 M_DEVBUF, M_NOWAIT); 777 if (entry == NULL) { 778 bge_free_jumbo_mem(sc); 779 sc->bge_ldata.bge_jumbo_buf = NULL; 780 printf("bge%d: no memory for jumbo " 781 "buffer queue!\n", sc->bge_unit); 782 return(ENOBUFS); 783 } 784 entry->slot = i; 785 SLIST_INSERT_HEAD(&sc->bge_jfree_listhead, 786 entry, jpool_entries); 787 } 788 789 return(0); 790 } 791 792 static void 793 bge_free_jumbo_mem(sc) 794 struct bge_softc *sc; 795 { 796 int i; 797 struct bge_jpool_entry *entry; 798 799 for (i = 0; i < BGE_JSLOTS; i++) { 800 entry = SLIST_FIRST(&sc->bge_jfree_listhead); 801 SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries); 802 free(entry, M_DEVBUF); 803 } 804 805 /* Destroy jumbo buffer block */ 806 807 if (sc->bge_ldata.bge_rx_jumbo_ring) 808 bus_dmamem_free(sc->bge_cdata.bge_jumbo_tag, 809 sc->bge_ldata.bge_jumbo_buf, 810 sc->bge_cdata.bge_jumbo_map); 811 812 if (sc->bge_cdata.bge_rx_jumbo_ring_map) 813 bus_dmamap_destroy(sc->bge_cdata.bge_jumbo_tag, 814 sc->bge_cdata.bge_jumbo_map); 815 816 if (sc->bge_cdata.bge_jumbo_tag) 817 bus_dma_tag_destroy(sc->bge_cdata.bge_jumbo_tag); 818 819 return; 820 } 821 822 /* 823 * Allocate a jumbo buffer. 824 */ 825 static void * 826 bge_jalloc(sc) 827 struct bge_softc *sc; 828 { 829 struct bge_jpool_entry *entry; 830 831 entry = SLIST_FIRST(&sc->bge_jfree_listhead); 832 833 if (entry == NULL) { 834 printf("bge%d: no free jumbo buffers\n", sc->bge_unit); 835 return(NULL); 836 } 837 838 SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries); 839 SLIST_INSERT_HEAD(&sc->bge_jinuse_listhead, entry, jpool_entries); 840 return(sc->bge_cdata.bge_jslots[entry->slot]); 841 } 842 843 /* 844 * Release a jumbo buffer. 845 */ 846 static void 847 bge_jfree(buf, args) 848 void *buf; 849 void *args; 850 { 851 struct bge_jpool_entry *entry; 852 struct bge_softc *sc; 853 int i; 854 855 /* Extract the softc struct pointer. */ 856 sc = (struct bge_softc *)args; 857 858 if (sc == NULL) 859 panic("bge_jfree: can't find softc pointer!"); 860 861 /* calculate the slot this buffer belongs to */ 862 863 i = ((vm_offset_t)buf 864 - (vm_offset_t)sc->bge_ldata.bge_jumbo_buf) / BGE_JLEN; 865 866 if ((i < 0) || (i >= BGE_JSLOTS)) 867 panic("bge_jfree: asked to free buffer that we don't manage!"); 868 869 entry = SLIST_FIRST(&sc->bge_jinuse_listhead); 870 if (entry == NULL) 871 panic("bge_jfree: buffer not in use!"); 872 entry->slot = i; 873 SLIST_REMOVE_HEAD(&sc->bge_jinuse_listhead, jpool_entries); 874 SLIST_INSERT_HEAD(&sc->bge_jfree_listhead, entry, jpool_entries); 875 876 return; 877 } 878 879 880 /* 881 * Intialize a standard receive ring descriptor. 882 */ 883 static int 884 bge_newbuf_std(sc, i, m) 885 struct bge_softc *sc; 886 int i; 887 struct mbuf *m; 888 { 889 struct mbuf *m_new = NULL; 890 struct bge_rx_bd *r; 891 struct bge_dmamap_arg ctx; 892 int error; 893 894 if (m == NULL) { 895 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 896 if (m_new == NULL) { 897 return(ENOBUFS); 898 } 899 900 MCLGET(m_new, M_DONTWAIT); 901 if (!(m_new->m_flags & M_EXT)) { 902 m_freem(m_new); 903 return(ENOBUFS); 904 } 905 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 906 } else { 907 m_new = m; 908 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 909 m_new->m_data = m_new->m_ext.ext_buf; 910 } 911 912 if (!sc->bge_rx_alignment_bug) 913 m_adj(m_new, ETHER_ALIGN); 914 sc->bge_cdata.bge_rx_std_chain[i] = m_new; 915 r = &sc->bge_ldata.bge_rx_std_ring[i]; 916 ctx.bge_maxsegs = 1; 917 ctx.sc = sc; 918 error = bus_dmamap_load(sc->bge_cdata.bge_mtag, 919 sc->bge_cdata.bge_rx_std_dmamap[i], mtod(m_new, void *), 920 m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT); 921 if (error || ctx.bge_maxsegs == 0) { 922 if (m == NULL) 923 m_freem(m_new); 924 return(ENOMEM); 925 } 926 r->bge_addr.bge_addr_lo = htole32(BGE_ADDR_LO(ctx.bge_busaddr)); 927 r->bge_addr.bge_addr_hi = htole32(BGE_ADDR_HI(ctx.bge_busaddr)); 928 r->bge_flags = htole16(BGE_RXBDFLAG_END); 929 r->bge_len = htole16(m_new->m_len); 930 r->bge_idx = htole16(i); 931 932 bus_dmamap_sync(sc->bge_cdata.bge_mtag, 933 sc->bge_cdata.bge_rx_std_dmamap[i], 934 BUS_DMASYNC_PREREAD); 935 936 return(0); 937 } 938 939 /* 940 * Initialize a jumbo receive ring descriptor. This allocates 941 * a jumbo buffer from the pool managed internally by the driver. 942 */ 943 static int 944 bge_newbuf_jumbo(sc, i, m) 945 struct bge_softc *sc; 946 int i; 947 struct mbuf *m; 948 { 949 struct mbuf *m_new = NULL; 950 struct bge_rx_bd *r; 951 struct bge_dmamap_arg ctx; 952 int error; 953 954 if (m == NULL) { 955 caddr_t *buf = NULL; 956 957 /* Allocate the mbuf. */ 958 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 959 if (m_new == NULL) { 960 return(ENOBUFS); 961 } 962 963 /* Allocate the jumbo buffer */ 964 buf = bge_jalloc(sc); 965 if (buf == NULL) { 966 m_freem(m_new); 967 printf("bge%d: jumbo allocation failed " 968 "-- packet dropped!\n", sc->bge_unit); 969 return(ENOBUFS); 970 } 971 972 /* Attach the buffer to the mbuf. */ 973 m_new->m_data = (void *) buf; 974 m_new->m_len = m_new->m_pkthdr.len = BGE_JUMBO_FRAMELEN; 975 MEXTADD(m_new, buf, BGE_JUMBO_FRAMELEN, bge_jfree, 976 (struct bge_softc *)sc, 0, EXT_NET_DRV); 977 } else { 978 m_new = m; 979 m_new->m_data = m_new->m_ext.ext_buf; 980 m_new->m_ext.ext_size = BGE_JUMBO_FRAMELEN; 981 } 982 983 if (!sc->bge_rx_alignment_bug) 984 m_adj(m_new, ETHER_ALIGN); 985 /* Set up the descriptor. */ 986 sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new; 987 r = &sc->bge_ldata.bge_rx_jumbo_ring[i]; 988 ctx.bge_maxsegs = 1; 989 ctx.sc = sc; 990 error = bus_dmamap_load(sc->bge_cdata.bge_mtag_jumbo, 991 sc->bge_cdata.bge_rx_jumbo_dmamap[i], mtod(m_new, void *), 992 m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT); 993 if (error || ctx.bge_maxsegs == 0) { 994 if (m == NULL) 995 m_freem(m_new); 996 return(ENOMEM); 997 } 998 r->bge_addr.bge_addr_lo = htole32(BGE_ADDR_LO(ctx.bge_busaddr)); 999 r->bge_addr.bge_addr_hi = htole32(BGE_ADDR_HI(ctx.bge_busaddr)); 1000 r->bge_flags = htole16(BGE_RXBDFLAG_END|BGE_RXBDFLAG_JUMBO_RING); 1001 r->bge_len = htole16(m_new->m_len); 1002 r->bge_idx = htole16(i); 1003 1004 bus_dmamap_sync(sc->bge_cdata.bge_mtag, 1005 sc->bge_cdata.bge_rx_jumbo_dmamap[i], 1006 BUS_DMASYNC_PREREAD); 1007 1008 return(0); 1009 } 1010 1011 /* 1012 * The standard receive ring has 512 entries in it. At 2K per mbuf cluster, 1013 * that's 1MB or memory, which is a lot. For now, we fill only the first 1014 * 256 ring entries and hope that our CPU is fast enough to keep up with 1015 * the NIC. 1016 */ 1017 static int 1018 bge_init_rx_ring_std(sc) 1019 struct bge_softc *sc; 1020 { 1021 int i; 1022 1023 for (i = 0; i < BGE_SSLOTS; i++) { 1024 if (bge_newbuf_std(sc, i, NULL) == ENOBUFS) 1025 return(ENOBUFS); 1026 }; 1027 1028 bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag, 1029 sc->bge_cdata.bge_rx_std_ring_map, 1030 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 1031 1032 sc->bge_std = i - 1; 1033 CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std); 1034 1035 return(0); 1036 } 1037 1038 static void 1039 bge_free_rx_ring_std(sc) 1040 struct bge_softc *sc; 1041 { 1042 int i; 1043 1044 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) { 1045 if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) { 1046 m_freem(sc->bge_cdata.bge_rx_std_chain[i]); 1047 sc->bge_cdata.bge_rx_std_chain[i] = NULL; 1048 bus_dmamap_unload(sc->bge_cdata.bge_mtag, 1049 sc->bge_cdata.bge_rx_std_dmamap[i]); 1050 } 1051 bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i], 1052 sizeof(struct bge_rx_bd)); 1053 } 1054 1055 return; 1056 } 1057 1058 static int 1059 bge_init_rx_ring_jumbo(sc) 1060 struct bge_softc *sc; 1061 { 1062 int i; 1063 struct bge_rcb *rcb; 1064 1065 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) { 1066 if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS) 1067 return(ENOBUFS); 1068 }; 1069 1070 bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag, 1071 sc->bge_cdata.bge_rx_jumbo_ring_map, 1072 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 1073 1074 sc->bge_jumbo = i - 1; 1075 1076 rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb; 1077 rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0, 0); 1078 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags); 1079 1080 CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo); 1081 1082 return(0); 1083 } 1084 1085 static void 1086 bge_free_rx_ring_jumbo(sc) 1087 struct bge_softc *sc; 1088 { 1089 int i; 1090 1091 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) { 1092 if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) { 1093 m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]); 1094 sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL; 1095 bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo, 1096 sc->bge_cdata.bge_rx_jumbo_dmamap[i]); 1097 } 1098 bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i], 1099 sizeof(struct bge_rx_bd)); 1100 } 1101 1102 return; 1103 } 1104 1105 static void 1106 bge_free_tx_ring(sc) 1107 struct bge_softc *sc; 1108 { 1109 int i; 1110 1111 if (sc->bge_ldata.bge_tx_ring == NULL) 1112 return; 1113 1114 for (i = 0; i < BGE_TX_RING_CNT; i++) { 1115 if (sc->bge_cdata.bge_tx_chain[i] != NULL) { 1116 m_freem(sc->bge_cdata.bge_tx_chain[i]); 1117 sc->bge_cdata.bge_tx_chain[i] = NULL; 1118 bus_dmamap_unload(sc->bge_cdata.bge_mtag, 1119 sc->bge_cdata.bge_tx_dmamap[i]); 1120 } 1121 bzero((char *)&sc->bge_ldata.bge_tx_ring[i], 1122 sizeof(struct bge_tx_bd)); 1123 } 1124 1125 return; 1126 } 1127 1128 static int 1129 bge_init_tx_ring(sc) 1130 struct bge_softc *sc; 1131 { 1132 sc->bge_txcnt = 0; 1133 sc->bge_tx_saved_considx = 0; 1134 1135 CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, 0); 1136 /* 5700 b2 errata */ 1137 if (sc->bge_chiprev == BGE_CHIPREV_5700_BX) 1138 CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, 0); 1139 1140 CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0); 1141 /* 5700 b2 errata */ 1142 if (sc->bge_chiprev == BGE_CHIPREV_5700_BX) 1143 CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0); 1144 1145 return(0); 1146 } 1147 1148 static void 1149 bge_setmulti(sc) 1150 struct bge_softc *sc; 1151 { 1152 struct ifnet *ifp; 1153 struct ifmultiaddr *ifma; 1154 u_int32_t hashes[4] = { 0, 0, 0, 0 }; 1155 int h, i; 1156 1157 BGE_LOCK_ASSERT(sc); 1158 1159 ifp = sc->bge_ifp; 1160 1161 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 1162 for (i = 0; i < 4; i++) 1163 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF); 1164 return; 1165 } 1166 1167 /* First, zot all the existing filters. */ 1168 for (i = 0; i < 4; i++) 1169 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0); 1170 1171 /* Now program new ones. */ 1172 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1173 if (ifma->ifma_addr->sa_family != AF_LINK) 1174 continue; 1175 h = ether_crc32_le(LLADDR((struct sockaddr_dl *) 1176 ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F; 1177 hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F); 1178 } 1179 1180 for (i = 0; i < 4; i++) 1181 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]); 1182 1183 return; 1184 } 1185 1186 /* 1187 * Do endian, PCI and DMA initialization. Also check the on-board ROM 1188 * self-test results. 1189 */ 1190 static int 1191 bge_chipinit(sc) 1192 struct bge_softc *sc; 1193 { 1194 int i; 1195 u_int32_t dma_rw_ctl; 1196 1197 /* Set endianness before we access any non-PCI registers. */ 1198 #if BYTE_ORDER == BIG_ENDIAN 1199 pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, 1200 BGE_BIGENDIAN_INIT, 4); 1201 #else 1202 pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, 1203 BGE_LITTLEENDIAN_INIT, 4); 1204 #endif 1205 1206 /* 1207 * Check the 'ROM failed' bit on the RX CPU to see if 1208 * self-tests passed. 1209 */ 1210 if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) { 1211 printf("bge%d: RX CPU self-diagnostics failed!\n", 1212 sc->bge_unit); 1213 return(ENODEV); 1214 } 1215 1216 /* Clear the MAC control register */ 1217 CSR_WRITE_4(sc, BGE_MAC_MODE, 0); 1218 1219 /* 1220 * Clear the MAC statistics block in the NIC's 1221 * internal memory. 1222 */ 1223 for (i = BGE_STATS_BLOCK; 1224 i < BGE_STATS_BLOCK_END + 1; i += sizeof(u_int32_t)) 1225 BGE_MEMWIN_WRITE(sc, i, 0); 1226 1227 for (i = BGE_STATUS_BLOCK; 1228 i < BGE_STATUS_BLOCK_END + 1; i += sizeof(u_int32_t)) 1229 BGE_MEMWIN_WRITE(sc, i, 0); 1230 1231 /* Set up the PCI DMA control register. */ 1232 if (sc->bge_pcie) { 1233 dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD | 1234 (0xf << BGE_PCIDMARWCTL_RD_WAT_SHIFT) | 1235 (0x2 << BGE_PCIDMARWCTL_WR_WAT_SHIFT); 1236 } else if (pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) & 1237 BGE_PCISTATE_PCI_BUSMODE) { 1238 /* Conventional PCI bus */ 1239 dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD | 1240 (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) | 1241 (0x7 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) | 1242 (0x0F); 1243 } else { 1244 /* PCI-X bus */ 1245 /* 1246 * The 5704 uses a different encoding of read/write 1247 * watermarks. 1248 */ 1249 if (sc->bge_asicrev == BGE_ASICREV_BCM5704) 1250 dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD | 1251 (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) | 1252 (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT); 1253 else 1254 dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD | 1255 (0x3 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) | 1256 (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) | 1257 (0x0F); 1258 1259 /* 1260 * 5703 and 5704 need ONEDMA_AT_ONCE as a workaround 1261 * for hardware bugs. 1262 */ 1263 if (sc->bge_asicrev == BGE_ASICREV_BCM5703 || 1264 sc->bge_asicrev == BGE_ASICREV_BCM5704) { 1265 u_int32_t tmp; 1266 1267 tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1f; 1268 if (tmp == 0x6 || tmp == 0x7) 1269 dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE; 1270 } 1271 } 1272 1273 if (sc->bge_asicrev == BGE_ASICREV_BCM5703 || 1274 sc->bge_asicrev == BGE_ASICREV_BCM5704 || 1275 sc->bge_asicrev == BGE_ASICREV_BCM5705 || 1276 sc->bge_asicrev == BGE_ASICREV_BCM5750) 1277 dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA; 1278 pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4); 1279 1280 /* 1281 * Set up general mode register. 1282 */ 1283 CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_MODECTL_WORDSWAP_NONFRAME| 1284 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_DATA| 1285 BGE_MODECTL_MAC_ATTN_INTR|BGE_MODECTL_HOST_SEND_BDS| 1286 BGE_MODECTL_TX_NO_PHDR_CSUM|BGE_MODECTL_RX_NO_PHDR_CSUM); 1287 1288 /* 1289 * Disable memory write invalidate. Apparently it is not supported 1290 * properly by these devices. 1291 */ 1292 PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4); 1293 1294 #ifdef __brokenalpha__ 1295 /* 1296 * Must insure that we do not cross an 8K (bytes) boundary 1297 * for DMA reads. Our highest limit is 1K bytes. This is a 1298 * restriction on some ALPHA platforms with early revision 1299 * 21174 PCI chipsets, such as the AlphaPC 164lx 1300 */ 1301 PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL, 1302 BGE_PCI_READ_BNDRY_1024BYTES, 4); 1303 #endif 1304 1305 /* Set the timer prescaler (always 66Mhz) */ 1306 CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/); 1307 1308 return(0); 1309 } 1310 1311 static int 1312 bge_blockinit(sc) 1313 struct bge_softc *sc; 1314 { 1315 struct bge_rcb *rcb; 1316 volatile struct bge_rcb *vrcb; 1317 int i; 1318 1319 /* 1320 * Initialize the memory window pointer register so that 1321 * we can access the first 32K of internal NIC RAM. This will 1322 * allow us to set up the TX send ring RCBs and the RX return 1323 * ring RCBs, plus other things which live in NIC memory. 1324 */ 1325 CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0); 1326 1327 /* Note: the BCM5704 has a smaller mbuf space than other chips. */ 1328 1329 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 && 1330 sc->bge_asicrev != BGE_ASICREV_BCM5750) { 1331 /* Configure mbuf memory pool */ 1332 if (sc->bge_extram) { 1333 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, 1334 BGE_EXT_SSRAM); 1335 if (sc->bge_asicrev == BGE_ASICREV_BCM5704) 1336 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000); 1337 else 1338 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000); 1339 } else { 1340 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, 1341 BGE_BUFFPOOL_1); 1342 if (sc->bge_asicrev == BGE_ASICREV_BCM5704) 1343 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000); 1344 else 1345 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000); 1346 } 1347 1348 /* Configure DMA resource pool */ 1349 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR, 1350 BGE_DMA_DESCRIPTORS); 1351 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000); 1352 } 1353 1354 /* Configure mbuf pool watermarks */ 1355 if (sc->bge_asicrev == BGE_ASICREV_BCM5705 || 1356 sc->bge_asicrev == BGE_ASICREV_BCM5750) { 1357 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0); 1358 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10); 1359 } else { 1360 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50); 1361 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20); 1362 } 1363 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60); 1364 1365 /* Configure DMA resource watermarks */ 1366 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5); 1367 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10); 1368 1369 /* Enable buffer manager */ 1370 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 && 1371 sc->bge_asicrev != BGE_ASICREV_BCM5750) { 1372 CSR_WRITE_4(sc, BGE_BMAN_MODE, 1373 BGE_BMANMODE_ENABLE|BGE_BMANMODE_LOMBUF_ATTN); 1374 1375 /* Poll for buffer manager start indication */ 1376 for (i = 0; i < BGE_TIMEOUT; i++) { 1377 if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE) 1378 break; 1379 DELAY(10); 1380 } 1381 1382 if (i == BGE_TIMEOUT) { 1383 printf("bge%d: buffer manager failed to start\n", 1384 sc->bge_unit); 1385 return(ENXIO); 1386 } 1387 } 1388 1389 /* Enable flow-through queues */ 1390 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF); 1391 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0); 1392 1393 /* Wait until queue initialization is complete */ 1394 for (i = 0; i < BGE_TIMEOUT; i++) { 1395 if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0) 1396 break; 1397 DELAY(10); 1398 } 1399 1400 if (i == BGE_TIMEOUT) { 1401 printf("bge%d: flow-through queue init failed\n", 1402 sc->bge_unit); 1403 return(ENXIO); 1404 } 1405 1406 /* Initialize the standard RX ring control block */ 1407 rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb; 1408 rcb->bge_hostaddr.bge_addr_lo = 1409 BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr); 1410 rcb->bge_hostaddr.bge_addr_hi = 1411 BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr); 1412 bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag, 1413 sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD); 1414 if (sc->bge_asicrev == BGE_ASICREV_BCM5705 || 1415 sc->bge_asicrev == BGE_ASICREV_BCM5750) 1416 rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0); 1417 else 1418 rcb->bge_maxlen_flags = 1419 BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0); 1420 if (sc->bge_extram) 1421 rcb->bge_nicaddr = BGE_EXT_STD_RX_RINGS; 1422 else 1423 rcb->bge_nicaddr = BGE_STD_RX_RINGS; 1424 CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi); 1425 CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo); 1426 1427 CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags); 1428 CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr); 1429 1430 /* 1431 * Initialize the jumbo RX ring control block 1432 * We set the 'ring disabled' bit in the flags 1433 * field until we're actually ready to start 1434 * using this ring (i.e. once we set the MTU 1435 * high enough to require it). 1436 */ 1437 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 && 1438 sc->bge_asicrev != BGE_ASICREV_BCM5750) { 1439 rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb; 1440 1441 rcb->bge_hostaddr.bge_addr_lo = 1442 BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr); 1443 rcb->bge_hostaddr.bge_addr_hi = 1444 BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr); 1445 bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag, 1446 sc->bge_cdata.bge_rx_jumbo_ring_map, 1447 BUS_DMASYNC_PREREAD); 1448 rcb->bge_maxlen_flags = 1449 BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 1450 BGE_RCB_FLAG_RING_DISABLED); 1451 if (sc->bge_extram) 1452 rcb->bge_nicaddr = BGE_EXT_JUMBO_RX_RINGS; 1453 else 1454 rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS; 1455 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI, 1456 rcb->bge_hostaddr.bge_addr_hi); 1457 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO, 1458 rcb->bge_hostaddr.bge_addr_lo); 1459 1460 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, 1461 rcb->bge_maxlen_flags); 1462 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr); 1463 1464 /* Set up dummy disabled mini ring RCB */ 1465 rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb; 1466 rcb->bge_maxlen_flags = 1467 BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED); 1468 CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS, 1469 rcb->bge_maxlen_flags); 1470 } 1471 1472 /* 1473 * Set the BD ring replentish thresholds. The recommended 1474 * values are 1/8th the number of descriptors allocated to 1475 * each ring. 1476 */ 1477 CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, BGE_STD_RX_RING_CNT/8); 1478 CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8); 1479 1480 /* 1481 * Disable all unused send rings by setting the 'ring disabled' 1482 * bit in the flags field of all the TX send ring control blocks. 1483 * These are located in NIC memory. 1484 */ 1485 vrcb = (volatile struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START + 1486 BGE_SEND_RING_RCB); 1487 for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) { 1488 vrcb->bge_maxlen_flags = 1489 BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED); 1490 vrcb->bge_nicaddr = 0; 1491 vrcb++; 1492 } 1493 1494 /* Configure TX RCB 0 (we use only the first ring) */ 1495 vrcb = (volatile struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START + 1496 BGE_SEND_RING_RCB); 1497 vrcb->bge_hostaddr.bge_addr_lo = 1498 htole32(BGE_ADDR_LO(sc->bge_ldata.bge_tx_ring_paddr)); 1499 vrcb->bge_hostaddr.bge_addr_hi = 1500 htole32(BGE_ADDR_HI(sc->bge_ldata.bge_tx_ring_paddr)); 1501 vrcb->bge_nicaddr = BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT); 1502 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 && 1503 sc->bge_asicrev != BGE_ASICREV_BCM5750) 1504 vrcb->bge_maxlen_flags = 1505 BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0); 1506 1507 /* Disable all unused RX return rings */ 1508 vrcb = (volatile struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START + 1509 BGE_RX_RETURN_RING_RCB); 1510 for (i = 0; i < BGE_RX_RINGS_MAX; i++) { 1511 vrcb->bge_hostaddr.bge_addr_hi = 0; 1512 vrcb->bge_hostaddr.bge_addr_lo = 0; 1513 vrcb->bge_maxlen_flags = 1514 BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 1515 BGE_RCB_FLAG_RING_DISABLED); 1516 vrcb->bge_nicaddr = 0; 1517 CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO + 1518 (i * (sizeof(u_int64_t))), 0); 1519 vrcb++; 1520 } 1521 1522 /* Initialize RX ring indexes */ 1523 CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0); 1524 CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0); 1525 CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0); 1526 1527 /* 1528 * Set up RX return ring 0 1529 * Note that the NIC address for RX return rings is 0x00000000. 1530 * The return rings live entirely within the host, so the 1531 * nicaddr field in the RCB isn't used. 1532 */ 1533 vrcb = (volatile struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START + 1534 BGE_RX_RETURN_RING_RCB); 1535 vrcb->bge_hostaddr.bge_addr_lo = 1536 BGE_ADDR_LO(sc->bge_ldata.bge_rx_return_ring_paddr); 1537 vrcb->bge_hostaddr.bge_addr_hi = 1538 BGE_ADDR_HI(sc->bge_ldata.bge_rx_return_ring_paddr); 1539 bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag, 1540 sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREWRITE); 1541 vrcb->bge_nicaddr = 0x00000000; 1542 vrcb->bge_maxlen_flags = 1543 BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0); 1544 1545 /* Set random backoff seed for TX */ 1546 CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF, 1547 IFP2ENADDR(sc->bge_ifp)[0] + IFP2ENADDR(sc->bge_ifp)[1] + 1548 IFP2ENADDR(sc->bge_ifp)[2] + IFP2ENADDR(sc->bge_ifp)[3] + 1549 IFP2ENADDR(sc->bge_ifp)[4] + IFP2ENADDR(sc->bge_ifp)[5] + 1550 BGE_TX_BACKOFF_SEED_MASK); 1551 1552 /* Set inter-packet gap */ 1553 CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620); 1554 1555 /* 1556 * Specify which ring to use for packets that don't match 1557 * any RX rules. 1558 */ 1559 CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08); 1560 1561 /* 1562 * Configure number of RX lists. One interrupt distribution 1563 * list, sixteen active lists, one bad frames class. 1564 */ 1565 CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181); 1566 1567 /* Inialize RX list placement stats mask. */ 1568 CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF); 1569 CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1); 1570 1571 /* Disable host coalescing until we get it set up */ 1572 CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000); 1573 1574 /* Poll to make sure it's shut down. */ 1575 for (i = 0; i < BGE_TIMEOUT; i++) { 1576 if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE)) 1577 break; 1578 DELAY(10); 1579 } 1580 1581 if (i == BGE_TIMEOUT) { 1582 printf("bge%d: host coalescing engine failed to idle\n", 1583 sc->bge_unit); 1584 return(ENXIO); 1585 } 1586 1587 /* Set up host coalescing defaults */ 1588 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks); 1589 CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks); 1590 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds); 1591 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds); 1592 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 && 1593 sc->bge_asicrev != BGE_ASICREV_BCM5750) { 1594 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0); 1595 CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0); 1596 } 1597 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0); 1598 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0); 1599 1600 /* Set up address of statistics block */ 1601 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 && 1602 sc->bge_asicrev != BGE_ASICREV_BCM5750) { 1603 CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI, 1604 BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr)); 1605 CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO, 1606 BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr)); 1607 CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK); 1608 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK); 1609 CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks); 1610 } 1611 1612 /* Set up address of status block */ 1613 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI, 1614 BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr)); 1615 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO, 1616 BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr)); 1617 bus_dmamap_sync(sc->bge_cdata.bge_status_tag, 1618 sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREWRITE); 1619 sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx = 0; 1620 sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx = 0; 1621 1622 /* Turn on host coalescing state machine */ 1623 CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE); 1624 1625 /* Turn on RX BD completion state machine and enable attentions */ 1626 CSR_WRITE_4(sc, BGE_RBDC_MODE, 1627 BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN); 1628 1629 /* Turn on RX list placement state machine */ 1630 CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE); 1631 1632 /* Turn on RX list selector state machine. */ 1633 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 && 1634 sc->bge_asicrev != BGE_ASICREV_BCM5750) 1635 CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE); 1636 1637 /* Turn on DMA, clear stats */ 1638 CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB| 1639 BGE_MACMODE_RXDMA_ENB|BGE_MACMODE_RX_STATS_CLEAR| 1640 BGE_MACMODE_TX_STATS_CLEAR|BGE_MACMODE_RX_STATS_ENB| 1641 BGE_MACMODE_TX_STATS_ENB|BGE_MACMODE_FRMHDR_DMA_ENB| 1642 (sc->bge_tbi ? BGE_PORTMODE_TBI : BGE_PORTMODE_MII)); 1643 1644 /* Set misc. local control, enable interrupts on attentions */ 1645 CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN); 1646 1647 #ifdef notdef 1648 /* Assert GPIO pins for PHY reset */ 1649 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0| 1650 BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2); 1651 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0| 1652 BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2); 1653 #endif 1654 1655 /* Turn on DMA completion state machine */ 1656 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 && 1657 sc->bge_asicrev != BGE_ASICREV_BCM5750) 1658 CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE); 1659 1660 /* Turn on write DMA state machine */ 1661 CSR_WRITE_4(sc, BGE_WDMA_MODE, 1662 BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS); 1663 1664 /* Turn on read DMA state machine */ 1665 CSR_WRITE_4(sc, BGE_RDMA_MODE, 1666 BGE_RDMAMODE_ENABLE|BGE_RDMAMODE_ALL_ATTNS); 1667 1668 /* Turn on RX data completion state machine */ 1669 CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE); 1670 1671 /* Turn on RX BD initiator state machine */ 1672 CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE); 1673 1674 /* Turn on RX data and RX BD initiator state machine */ 1675 CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE); 1676 1677 /* Turn on Mbuf cluster free state machine */ 1678 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 && 1679 sc->bge_asicrev != BGE_ASICREV_BCM5750) 1680 CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE); 1681 1682 /* Turn on send BD completion state machine */ 1683 CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE); 1684 1685 /* Turn on send data completion state machine */ 1686 CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE); 1687 1688 /* Turn on send data initiator state machine */ 1689 CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE); 1690 1691 /* Turn on send BD initiator state machine */ 1692 CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE); 1693 1694 /* Turn on send BD selector state machine */ 1695 CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE); 1696 1697 CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF); 1698 CSR_WRITE_4(sc, BGE_SDI_STATS_CTL, 1699 BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER); 1700 1701 /* ack/clear link change events */ 1702 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED| 1703 BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE| 1704 BGE_MACSTAT_LINK_CHANGED); 1705 CSR_WRITE_4(sc, BGE_MI_STS, 0); 1706 1707 /* Enable PHY auto polling (for MII/GMII only) */ 1708 if (sc->bge_tbi) { 1709 CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK); 1710 } else { 1711 BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL|10<<16); 1712 if (sc->bge_asicrev == BGE_ASICREV_BCM5700) 1713 CSR_WRITE_4(sc, BGE_MAC_EVT_ENB, 1714 BGE_EVTENB_MI_INTERRUPT); 1715 } 1716 1717 /* Enable link state change attentions. */ 1718 BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED); 1719 1720 return(0); 1721 } 1722 1723 /* 1724 * Probe for a Broadcom chip. Check the PCI vendor and device IDs 1725 * against our list and return its name if we find a match. Note 1726 * that since the Broadcom controller contains VPD support, we 1727 * can get the device name string from the controller itself instead 1728 * of the compiled-in string. This is a little slow, but it guarantees 1729 * we'll always announce the right product name. 1730 */ 1731 static int 1732 bge_probe(dev) 1733 device_t dev; 1734 { 1735 struct bge_type *t; 1736 struct bge_softc *sc; 1737 char *descbuf; 1738 1739 t = bge_devs; 1740 1741 sc = device_get_softc(dev); 1742 bzero(sc, sizeof(struct bge_softc)); 1743 sc->bge_unit = device_get_unit(dev); 1744 sc->bge_dev = dev; 1745 1746 while(t->bge_name != NULL) { 1747 if ((pci_get_vendor(dev) == t->bge_vid) && 1748 (pci_get_device(dev) == t->bge_did)) { 1749 #ifdef notdef 1750 bge_vpd_read(sc); 1751 device_set_desc(dev, sc->bge_vpd_prodname); 1752 #endif 1753 descbuf = malloc(BGE_DEVDESC_MAX, M_TEMP, M_NOWAIT); 1754 if (descbuf == NULL) 1755 return(ENOMEM); 1756 snprintf(descbuf, BGE_DEVDESC_MAX, 1757 "%s, ASIC rev. %#04x", t->bge_name, 1758 pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >> 16); 1759 device_set_desc_copy(dev, descbuf); 1760 if (pci_get_subvendor(dev) == DELL_VENDORID) 1761 sc->bge_no_3_led = 1; 1762 free(descbuf, M_TEMP); 1763 return(0); 1764 } 1765 t++; 1766 } 1767 1768 return(ENXIO); 1769 } 1770 1771 static void 1772 bge_dma_free(sc) 1773 struct bge_softc *sc; 1774 { 1775 int i; 1776 1777 1778 /* Destroy DMA maps for RX buffers */ 1779 1780 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) { 1781 if (sc->bge_cdata.bge_rx_std_dmamap[i]) 1782 bus_dmamap_destroy(sc->bge_cdata.bge_mtag, 1783 sc->bge_cdata.bge_rx_std_dmamap[i]); 1784 } 1785 1786 /* Destroy DMA maps for jumbo RX buffers */ 1787 1788 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) { 1789 if (sc->bge_cdata.bge_rx_jumbo_dmamap[i]) 1790 bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo, 1791 sc->bge_cdata.bge_rx_jumbo_dmamap[i]); 1792 } 1793 1794 /* Destroy DMA maps for TX buffers */ 1795 1796 for (i = 0; i < BGE_TX_RING_CNT; i++) { 1797 if (sc->bge_cdata.bge_tx_dmamap[i]) 1798 bus_dmamap_destroy(sc->bge_cdata.bge_mtag, 1799 sc->bge_cdata.bge_tx_dmamap[i]); 1800 } 1801 1802 if (sc->bge_cdata.bge_mtag) 1803 bus_dma_tag_destroy(sc->bge_cdata.bge_mtag); 1804 1805 1806 /* Destroy standard RX ring */ 1807 1808 if (sc->bge_ldata.bge_rx_std_ring) 1809 bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag, 1810 sc->bge_ldata.bge_rx_std_ring, 1811 sc->bge_cdata.bge_rx_std_ring_map); 1812 1813 if (sc->bge_cdata.bge_rx_std_ring_map) { 1814 bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag, 1815 sc->bge_cdata.bge_rx_std_ring_map); 1816 bus_dmamap_destroy(sc->bge_cdata.bge_rx_std_ring_tag, 1817 sc->bge_cdata.bge_rx_std_ring_map); 1818 } 1819 1820 if (sc->bge_cdata.bge_rx_std_ring_tag) 1821 bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag); 1822 1823 /* Destroy jumbo RX ring */ 1824 1825 if (sc->bge_ldata.bge_rx_jumbo_ring) 1826 bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag, 1827 sc->bge_ldata.bge_rx_jumbo_ring, 1828 sc->bge_cdata.bge_rx_jumbo_ring_map); 1829 1830 if (sc->bge_cdata.bge_rx_jumbo_ring_map) { 1831 bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag, 1832 sc->bge_cdata.bge_rx_jumbo_ring_map); 1833 bus_dmamap_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag, 1834 sc->bge_cdata.bge_rx_jumbo_ring_map); 1835 } 1836 1837 if (sc->bge_cdata.bge_rx_jumbo_ring_tag) 1838 bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag); 1839 1840 /* Destroy RX return ring */ 1841 1842 if (sc->bge_ldata.bge_rx_return_ring) 1843 bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag, 1844 sc->bge_ldata.bge_rx_return_ring, 1845 sc->bge_cdata.bge_rx_return_ring_map); 1846 1847 if (sc->bge_cdata.bge_rx_return_ring_map) { 1848 bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag, 1849 sc->bge_cdata.bge_rx_return_ring_map); 1850 bus_dmamap_destroy(sc->bge_cdata.bge_rx_return_ring_tag, 1851 sc->bge_cdata.bge_rx_return_ring_map); 1852 } 1853 1854 if (sc->bge_cdata.bge_rx_return_ring_tag) 1855 bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag); 1856 1857 /* Destroy TX ring */ 1858 1859 if (sc->bge_ldata.bge_tx_ring) 1860 bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag, 1861 sc->bge_ldata.bge_tx_ring, 1862 sc->bge_cdata.bge_tx_ring_map); 1863 1864 if (sc->bge_cdata.bge_tx_ring_map) { 1865 bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag, 1866 sc->bge_cdata.bge_tx_ring_map); 1867 bus_dmamap_destroy(sc->bge_cdata.bge_tx_ring_tag, 1868 sc->bge_cdata.bge_tx_ring_map); 1869 } 1870 1871 if (sc->bge_cdata.bge_tx_ring_tag) 1872 bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag); 1873 1874 /* Destroy status block */ 1875 1876 if (sc->bge_ldata.bge_status_block) 1877 bus_dmamem_free(sc->bge_cdata.bge_status_tag, 1878 sc->bge_ldata.bge_status_block, 1879 sc->bge_cdata.bge_status_map); 1880 1881 if (sc->bge_cdata.bge_status_map) { 1882 bus_dmamap_unload(sc->bge_cdata.bge_status_tag, 1883 sc->bge_cdata.bge_status_map); 1884 bus_dmamap_destroy(sc->bge_cdata.bge_status_tag, 1885 sc->bge_cdata.bge_status_map); 1886 } 1887 1888 if (sc->bge_cdata.bge_status_tag) 1889 bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag); 1890 1891 /* Destroy statistics block */ 1892 1893 if (sc->bge_ldata.bge_stats) 1894 bus_dmamem_free(sc->bge_cdata.bge_stats_tag, 1895 sc->bge_ldata.bge_stats, 1896 sc->bge_cdata.bge_stats_map); 1897 1898 if (sc->bge_cdata.bge_stats_map) { 1899 bus_dmamap_unload(sc->bge_cdata.bge_stats_tag, 1900 sc->bge_cdata.bge_stats_map); 1901 bus_dmamap_destroy(sc->bge_cdata.bge_stats_tag, 1902 sc->bge_cdata.bge_stats_map); 1903 } 1904 1905 if (sc->bge_cdata.bge_stats_tag) 1906 bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag); 1907 1908 /* Destroy the parent tag */ 1909 1910 if (sc->bge_cdata.bge_parent_tag) 1911 bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag); 1912 1913 return; 1914 } 1915 1916 static int 1917 bge_dma_alloc(dev) 1918 device_t dev; 1919 { 1920 struct bge_softc *sc; 1921 int nseg, i, error; 1922 struct bge_dmamap_arg ctx; 1923 1924 sc = device_get_softc(dev); 1925 1926 /* 1927 * Allocate the parent bus DMA tag appropriate for PCI. 1928 */ 1929 #define BGE_NSEG_NEW 32 1930 error = bus_dma_tag_create(NULL, /* parent */ 1931 PAGE_SIZE, 0, /* alignment, boundary */ 1932 BUS_SPACE_MAXADDR, /* lowaddr */ 1933 BUS_SPACE_MAXADDR, /* highaddr */ 1934 NULL, NULL, /* filter, filterarg */ 1935 MAXBSIZE, BGE_NSEG_NEW, /* maxsize, nsegments */ 1936 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */ 1937 0, /* flags */ 1938 NULL, NULL, /* lockfunc, lockarg */ 1939 &sc->bge_cdata.bge_parent_tag); 1940 1941 /* 1942 * Create tag for RX mbufs. 1943 */ 1944 nseg = 32; 1945 error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1, 1946 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, 1947 NULL, MCLBYTES * nseg, nseg, MCLBYTES, BUS_DMA_ALLOCNOW, NULL, NULL, 1948 &sc->bge_cdata.bge_mtag); 1949 1950 if (error) { 1951 device_printf(dev, "could not allocate dma tag\n"); 1952 return (ENOMEM); 1953 } 1954 1955 /* Create DMA maps for RX buffers */ 1956 1957 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) { 1958 error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0, 1959 &sc->bge_cdata.bge_rx_std_dmamap[i]); 1960 if (error) { 1961 device_printf(dev, "can't create DMA map for RX\n"); 1962 return(ENOMEM); 1963 } 1964 } 1965 1966 /* Create DMA maps for TX buffers */ 1967 1968 for (i = 0; i < BGE_TX_RING_CNT; i++) { 1969 error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0, 1970 &sc->bge_cdata.bge_tx_dmamap[i]); 1971 if (error) { 1972 device_printf(dev, "can't create DMA map for RX\n"); 1973 return(ENOMEM); 1974 } 1975 } 1976 1977 /* Create tag for standard RX ring */ 1978 1979 error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1980 PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, 1981 NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0, 1982 NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag); 1983 1984 if (error) { 1985 device_printf(dev, "could not allocate dma tag\n"); 1986 return (ENOMEM); 1987 } 1988 1989 /* Allocate DMA'able memory for standard RX ring */ 1990 1991 error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag, 1992 (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT, 1993 &sc->bge_cdata.bge_rx_std_ring_map); 1994 if (error) 1995 return (ENOMEM); 1996 1997 bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ); 1998 1999 /* Load the address of the standard RX ring */ 2000 2001 ctx.bge_maxsegs = 1; 2002 ctx.sc = sc; 2003 2004 error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag, 2005 sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring, 2006 BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT); 2007 2008 if (error) 2009 return (ENOMEM); 2010 2011 sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr; 2012 2013 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 && 2014 sc->bge_asicrev != BGE_ASICREV_BCM5750) { 2015 2016 /* 2017 * Create tag for jumbo mbufs. 2018 * This is really a bit of a kludge. We allocate a special 2019 * jumbo buffer pool which (thanks to the way our DMA 2020 * memory allocation works) will consist of contiguous 2021 * pages. This means that even though a jumbo buffer might 2022 * be larger than a page size, we don't really need to 2023 * map it into more than one DMA segment. However, the 2024 * default mbuf tag will result in multi-segment mappings, 2025 * so we have to create a special jumbo mbuf tag that 2026 * lets us get away with mapping the jumbo buffers as 2027 * a single segment. I think eventually the driver should 2028 * be changed so that it uses ordinary mbufs and cluster 2029 * buffers, i.e. jumbo frames can span multiple DMA 2030 * descriptors. But that's a project for another day. 2031 */ 2032 2033 error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 2034 1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, 2035 NULL, MCLBYTES * nseg, nseg, BGE_JLEN, 0, NULL, NULL, 2036 &sc->bge_cdata.bge_mtag_jumbo); 2037 2038 if (error) { 2039 device_printf(dev, "could not allocate dma tag\n"); 2040 return (ENOMEM); 2041 } 2042 2043 /* Create tag for jumbo RX ring */ 2044 2045 error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 2046 PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, 2047 NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0, 2048 NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag); 2049 2050 if (error) { 2051 device_printf(dev, "could not allocate dma tag\n"); 2052 return (ENOMEM); 2053 } 2054 2055 /* Allocate DMA'able memory for jumbo RX ring */ 2056 2057 error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag, 2058 (void **)&sc->bge_ldata.bge_rx_jumbo_ring, BUS_DMA_NOWAIT, 2059 &sc->bge_cdata.bge_rx_jumbo_ring_map); 2060 if (error) 2061 return (ENOMEM); 2062 2063 bzero((char *)sc->bge_ldata.bge_rx_jumbo_ring, 2064 BGE_JUMBO_RX_RING_SZ); 2065 2066 /* Load the address of the jumbo RX ring */ 2067 2068 ctx.bge_maxsegs = 1; 2069 ctx.sc = sc; 2070 2071 error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag, 2072 sc->bge_cdata.bge_rx_jumbo_ring_map, 2073 sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ, 2074 bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT); 2075 2076 if (error) 2077 return (ENOMEM); 2078 2079 sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr; 2080 2081 /* Create DMA maps for jumbo RX buffers */ 2082 2083 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) { 2084 error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo, 2085 0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]); 2086 if (error) { 2087 device_printf(dev, 2088 "can't create DMA map for RX\n"); 2089 return(ENOMEM); 2090 } 2091 } 2092 2093 } 2094 2095 /* Create tag for RX return ring */ 2096 2097 error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 2098 PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, 2099 NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0, 2100 NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag); 2101 2102 if (error) { 2103 device_printf(dev, "could not allocate dma tag\n"); 2104 return (ENOMEM); 2105 } 2106 2107 /* Allocate DMA'able memory for RX return ring */ 2108 2109 error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag, 2110 (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT, 2111 &sc->bge_cdata.bge_rx_return_ring_map); 2112 if (error) 2113 return (ENOMEM); 2114 2115 bzero((char *)sc->bge_ldata.bge_rx_return_ring, 2116 BGE_RX_RTN_RING_SZ(sc)); 2117 2118 /* Load the address of the RX return ring */ 2119 2120 ctx.bge_maxsegs = 1; 2121 ctx.sc = sc; 2122 2123 error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag, 2124 sc->bge_cdata.bge_rx_return_ring_map, 2125 sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc), 2126 bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT); 2127 2128 if (error) 2129 return (ENOMEM); 2130 2131 sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr; 2132 2133 /* Create tag for TX ring */ 2134 2135 error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 2136 PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, 2137 NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL, 2138 &sc->bge_cdata.bge_tx_ring_tag); 2139 2140 if (error) { 2141 device_printf(dev, "could not allocate dma tag\n"); 2142 return (ENOMEM); 2143 } 2144 2145 /* Allocate DMA'able memory for TX ring */ 2146 2147 error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag, 2148 (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT, 2149 &sc->bge_cdata.bge_tx_ring_map); 2150 if (error) 2151 return (ENOMEM); 2152 2153 bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ); 2154 2155 /* Load the address of the TX ring */ 2156 2157 ctx.bge_maxsegs = 1; 2158 ctx.sc = sc; 2159 2160 error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag, 2161 sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring, 2162 BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT); 2163 2164 if (error) 2165 return (ENOMEM); 2166 2167 sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr; 2168 2169 /* Create tag for status block */ 2170 2171 error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 2172 PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, 2173 NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0, 2174 NULL, NULL, &sc->bge_cdata.bge_status_tag); 2175 2176 if (error) { 2177 device_printf(dev, "could not allocate dma tag\n"); 2178 return (ENOMEM); 2179 } 2180 2181 /* Allocate DMA'able memory for status block */ 2182 2183 error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag, 2184 (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT, 2185 &sc->bge_cdata.bge_status_map); 2186 if (error) 2187 return (ENOMEM); 2188 2189 bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ); 2190 2191 /* Load the address of the status block */ 2192 2193 ctx.sc = sc; 2194 ctx.bge_maxsegs = 1; 2195 2196 error = bus_dmamap_load(sc->bge_cdata.bge_status_tag, 2197 sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block, 2198 BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT); 2199 2200 if (error) 2201 return (ENOMEM); 2202 2203 sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr; 2204 2205 /* Create tag for statistics block */ 2206 2207 error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 2208 PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, 2209 NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL, 2210 &sc->bge_cdata.bge_stats_tag); 2211 2212 if (error) { 2213 device_printf(dev, "could not allocate dma tag\n"); 2214 return (ENOMEM); 2215 } 2216 2217 /* Allocate DMA'able memory for statistics block */ 2218 2219 error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag, 2220 (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT, 2221 &sc->bge_cdata.bge_stats_map); 2222 if (error) 2223 return (ENOMEM); 2224 2225 bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ); 2226 2227 /* Load the address of the statstics block */ 2228 2229 ctx.sc = sc; 2230 ctx.bge_maxsegs = 1; 2231 2232 error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag, 2233 sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats, 2234 BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT); 2235 2236 if (error) 2237 return (ENOMEM); 2238 2239 sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr; 2240 2241 return(0); 2242 } 2243 2244 static int 2245 bge_attach(dev) 2246 device_t dev; 2247 { 2248 struct ifnet *ifp; 2249 struct bge_softc *sc; 2250 u_int32_t hwcfg = 0; 2251 u_int32_t mac_tmp = 0; 2252 u_char eaddr[6]; 2253 int unit, error = 0, rid; 2254 2255 sc = device_get_softc(dev); 2256 unit = device_get_unit(dev); 2257 sc->bge_dev = dev; 2258 sc->bge_unit = unit; 2259 2260 /* 2261 * Map control/status registers. 2262 */ 2263 pci_enable_busmaster(dev); 2264 2265 rid = BGE_PCI_BAR0; 2266 sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 2267 RF_ACTIVE|PCI_RF_DENSE); 2268 2269 if (sc->bge_res == NULL) { 2270 printf ("bge%d: couldn't map memory\n", unit); 2271 error = ENXIO; 2272 goto fail; 2273 } 2274 2275 sc->bge_btag = rman_get_bustag(sc->bge_res); 2276 sc->bge_bhandle = rman_get_bushandle(sc->bge_res); 2277 sc->bge_vhandle = (vm_offset_t)rman_get_virtual(sc->bge_res); 2278 2279 /* Allocate interrupt */ 2280 rid = 0; 2281 2282 sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 2283 RF_SHAREABLE | RF_ACTIVE); 2284 2285 if (sc->bge_irq == NULL) { 2286 printf("bge%d: couldn't map interrupt\n", unit); 2287 error = ENXIO; 2288 goto fail; 2289 } 2290 2291 sc->bge_unit = unit; 2292 2293 BGE_LOCK_INIT(sc, device_get_nameunit(dev)); 2294 2295 /* Save ASIC rev. */ 2296 2297 sc->bge_chipid = 2298 pci_read_config(dev, BGE_PCI_MISC_CTL, 4) & 2299 BGE_PCIMISCCTL_ASICREV; 2300 sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid); 2301 sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid); 2302 2303 /* 2304 * Treat the 5714 like the 5750 until we have more info 2305 * on this chip. 2306 */ 2307 if (sc->bge_asicrev == BGE_ASICREV_BCM5714) 2308 sc->bge_asicrev = BGE_ASICREV_BCM5750; 2309 2310 /* 2311 * XXX: Broadcom Linux driver. Not in specs or eratta. 2312 * PCI-Express? 2313 */ 2314 if (sc->bge_asicrev == BGE_ASICREV_BCM5750) { 2315 u_int32_t v; 2316 2317 v = pci_read_config(dev, BGE_PCI_MSI_CAPID, 4); 2318 if (((v >> 8) & 0xff) == BGE_PCIE_CAPID_REG) { 2319 v = pci_read_config(dev, BGE_PCIE_CAPID_REG, 4); 2320 if ((v & 0xff) == BGE_PCIE_CAPID) 2321 sc->bge_pcie = 1; 2322 } 2323 } 2324 2325 /* Try to reset the chip. */ 2326 bge_reset(sc); 2327 2328 if (bge_chipinit(sc)) { 2329 printf("bge%d: chip initialization failed\n", sc->bge_unit); 2330 bge_release_resources(sc); 2331 error = ENXIO; 2332 goto fail; 2333 } 2334 2335 /* 2336 * Get station address from the EEPROM. 2337 */ 2338 mac_tmp = bge_readmem_ind(sc, 0x0c14); 2339 if ((mac_tmp >> 16) == 0x484b) { 2340 eaddr[0] = (u_char)(mac_tmp >> 8); 2341 eaddr[1] = (u_char)mac_tmp; 2342 mac_tmp = bge_readmem_ind(sc, 0x0c18); 2343 eaddr[2] = (u_char)(mac_tmp >> 24); 2344 eaddr[3] = (u_char)(mac_tmp >> 16); 2345 eaddr[4] = (u_char)(mac_tmp >> 8); 2346 eaddr[5] = (u_char)mac_tmp; 2347 } else if (bge_read_eeprom(sc, eaddr, 2348 BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) { 2349 printf("bge%d: failed to read station address\n", unit); 2350 bge_release_resources(sc); 2351 error = ENXIO; 2352 goto fail; 2353 } 2354 2355 /* 5705 limits RX return ring to 512 entries. */ 2356 if (sc->bge_asicrev == BGE_ASICREV_BCM5705 || 2357 sc->bge_asicrev == BGE_ASICREV_BCM5750) 2358 sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705; 2359 else 2360 sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT; 2361 2362 if (bge_dma_alloc(dev)) { 2363 printf ("bge%d: failed to allocate DMA resources\n", 2364 sc->bge_unit); 2365 bge_release_resources(sc); 2366 error = ENXIO; 2367 goto fail; 2368 } 2369 2370 /* 2371 * Try to allocate memory for jumbo buffers. 2372 * The 5705 does not appear to support jumbo frames. 2373 */ 2374 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 && 2375 sc->bge_asicrev != BGE_ASICREV_BCM5750) { 2376 if (bge_alloc_jumbo_mem(sc)) { 2377 printf("bge%d: jumbo buffer allocation " 2378 "failed\n", sc->bge_unit); 2379 bge_release_resources(sc); 2380 error = ENXIO; 2381 goto fail; 2382 } 2383 } 2384 2385 /* Set default tuneable values. */ 2386 sc->bge_stat_ticks = BGE_TICKS_PER_SEC; 2387 sc->bge_rx_coal_ticks = 150; 2388 sc->bge_tx_coal_ticks = 150; 2389 sc->bge_rx_max_coal_bds = 64; 2390 sc->bge_tx_max_coal_bds = 128; 2391 2392 /* Set up ifnet structure */ 2393 ifp = sc->bge_ifp = if_alloc(IFT_ETHER); 2394 if (ifp == NULL) { 2395 printf("bge%d: failed to if_alloc()\n", sc->bge_unit); 2396 bge_release_resources(sc); 2397 error = ENXIO; 2398 goto fail; 2399 } 2400 ifp->if_softc = sc; 2401 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 2402 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 2403 ifp->if_ioctl = bge_ioctl; 2404 ifp->if_start = bge_start; 2405 ifp->if_watchdog = bge_watchdog; 2406 ifp->if_init = bge_init; 2407 ifp->if_mtu = ETHERMTU; 2408 ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1; 2409 IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen); 2410 IFQ_SET_READY(&ifp->if_snd); 2411 ifp->if_hwassist = BGE_CSUM_FEATURES; 2412 /* NB: the code for RX csum offload is disabled for now */ 2413 ifp->if_capabilities = IFCAP_TXCSUM | IFCAP_VLAN_HWTAGGING | 2414 IFCAP_VLAN_MTU; 2415 ifp->if_capenable = ifp->if_capabilities; 2416 2417 /* 2418 * Figure out what sort of media we have by checking the 2419 * hardware config word in the first 32k of NIC internal memory, 2420 * or fall back to examining the EEPROM if necessary. 2421 * Note: on some BCM5700 cards, this value appears to be unset. 2422 * If that's the case, we have to rely on identifying the NIC 2423 * by its PCI subsystem ID, as we do below for the SysKonnect 2424 * SK-9D41. 2425 */ 2426 if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER) 2427 hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG); 2428 else { 2429 bge_read_eeprom(sc, (caddr_t)&hwcfg, 2430 BGE_EE_HWCFG_OFFSET, sizeof(hwcfg)); 2431 hwcfg = ntohl(hwcfg); 2432 } 2433 2434 if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) 2435 sc->bge_tbi = 1; 2436 2437 /* The SysKonnect SK-9D41 is a 1000baseSX card. */ 2438 if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41) 2439 sc->bge_tbi = 1; 2440 2441 if (sc->bge_tbi) { 2442 ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, 2443 bge_ifmedia_upd, bge_ifmedia_sts); 2444 ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL); 2445 ifmedia_add(&sc->bge_ifmedia, 2446 IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL); 2447 ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL); 2448 ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO); 2449 sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media; 2450 } else { 2451 /* 2452 * Do transceiver setup. 2453 */ 2454 if (mii_phy_probe(dev, &sc->bge_miibus, 2455 bge_ifmedia_upd, bge_ifmedia_sts)) { 2456 printf("bge%d: MII without any PHY!\n", sc->bge_unit); 2457 bge_release_resources(sc); 2458 bge_free_jumbo_mem(sc); 2459 if_free(ifp); 2460 error = ENXIO; 2461 goto fail; 2462 } 2463 } 2464 2465 /* 2466 * When using the BCM5701 in PCI-X mode, data corruption has 2467 * been observed in the first few bytes of some received packets. 2468 * Aligning the packet buffer in memory eliminates the corruption. 2469 * Unfortunately, this misaligns the packet payloads. On platforms 2470 * which do not support unaligned accesses, we will realign the 2471 * payloads by copying the received packets. 2472 */ 2473 switch (sc->bge_chipid) { 2474 case BGE_CHIPID_BCM5701_A0: 2475 case BGE_CHIPID_BCM5701_B0: 2476 case BGE_CHIPID_BCM5701_B2: 2477 case BGE_CHIPID_BCM5701_B5: 2478 /* If in PCI-X mode, work around the alignment bug. */ 2479 if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) & 2480 (BGE_PCISTATE_PCI_BUSMODE | BGE_PCISTATE_PCI_BUSSPEED)) == 2481 BGE_PCISTATE_PCI_BUSSPEED) 2482 sc->bge_rx_alignment_bug = 1; 2483 break; 2484 } 2485 2486 /* 2487 * Call MI attach routine. 2488 */ 2489 ether_ifattach(ifp, eaddr); 2490 callout_init(&sc->bge_stat_ch, CALLOUT_MPSAFE); 2491 2492 /* 2493 * Hookup IRQ last. 2494 */ 2495 error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE, 2496 bge_intr, sc, &sc->bge_intrhand); 2497 2498 if (error) { 2499 bge_detach(dev); 2500 printf("bge%d: couldn't set up irq\n", unit); 2501 } 2502 2503 fail: 2504 return(error); 2505 } 2506 2507 static int 2508 bge_detach(dev) 2509 device_t dev; 2510 { 2511 struct bge_softc *sc; 2512 struct ifnet *ifp; 2513 2514 sc = device_get_softc(dev); 2515 ifp = sc->bge_ifp; 2516 2517 BGE_LOCK(sc); 2518 bge_stop(sc); 2519 bge_reset(sc); 2520 BGE_UNLOCK(sc); 2521 2522 ether_ifdetach(ifp); 2523 if_free(ifp); 2524 2525 if (sc->bge_tbi) { 2526 ifmedia_removeall(&sc->bge_ifmedia); 2527 } else { 2528 bus_generic_detach(dev); 2529 device_delete_child(dev, sc->bge_miibus); 2530 } 2531 2532 bge_release_resources(sc); 2533 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 && 2534 sc->bge_asicrev != BGE_ASICREV_BCM5750) 2535 bge_free_jumbo_mem(sc); 2536 2537 return(0); 2538 } 2539 2540 static void 2541 bge_release_resources(sc) 2542 struct bge_softc *sc; 2543 { 2544 device_t dev; 2545 2546 dev = sc->bge_dev; 2547 2548 if (sc->bge_vpd_prodname != NULL) 2549 free(sc->bge_vpd_prodname, M_DEVBUF); 2550 2551 if (sc->bge_vpd_readonly != NULL) 2552 free(sc->bge_vpd_readonly, M_DEVBUF); 2553 2554 if (sc->bge_intrhand != NULL) 2555 bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand); 2556 2557 if (sc->bge_irq != NULL) 2558 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->bge_irq); 2559 2560 if (sc->bge_res != NULL) 2561 bus_release_resource(dev, SYS_RES_MEMORY, 2562 BGE_PCI_BAR0, sc->bge_res); 2563 2564 bge_dma_free(sc); 2565 2566 if (mtx_initialized(&sc->bge_mtx)) /* XXX */ 2567 BGE_LOCK_DESTROY(sc); 2568 2569 return; 2570 } 2571 2572 static void 2573 bge_reset(sc) 2574 struct bge_softc *sc; 2575 { 2576 device_t dev; 2577 u_int32_t cachesize, command, pcistate, reset; 2578 int i, val = 0; 2579 2580 dev = sc->bge_dev; 2581 2582 /* Save some important PCI state. */ 2583 cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4); 2584 command = pci_read_config(dev, BGE_PCI_CMD, 4); 2585 pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4); 2586 2587 pci_write_config(dev, BGE_PCI_MISC_CTL, 2588 BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR| 2589 BGE_PCIMISCCTL_ENDIAN_WORDSWAP|BGE_PCIMISCCTL_PCISTATE_RW, 4); 2590 2591 reset = BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1); 2592 2593 /* XXX: Broadcom Linux driver. */ 2594 if (sc->bge_pcie) { 2595 if (CSR_READ_4(sc, 0x7e2c) == 0x60) /* PCIE 1.0 */ 2596 CSR_WRITE_4(sc, 0x7e2c, 0x20); 2597 if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) { 2598 /* Prevent PCIE link training during global reset */ 2599 CSR_WRITE_4(sc, BGE_MISC_CFG, (1<<29)); 2600 reset |= (1<<29); 2601 } 2602 } 2603 2604 /* Issue global reset */ 2605 bge_writereg_ind(sc, BGE_MISC_CFG, reset); 2606 2607 DELAY(1000); 2608 2609 /* XXX: Broadcom Linux driver. */ 2610 if (sc->bge_pcie) { 2611 if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) { 2612 uint32_t v; 2613 2614 DELAY(500000); /* wait for link training to complete */ 2615 v = pci_read_config(dev, 0xc4, 4); 2616 pci_write_config(dev, 0xc4, v | (1<<15), 4); 2617 } 2618 /* Set PCIE max payload size and clear error status. */ 2619 pci_write_config(dev, 0xd8, 0xf5000, 4); 2620 } 2621 2622 /* Reset some of the PCI state that got zapped by reset */ 2623 pci_write_config(dev, BGE_PCI_MISC_CTL, 2624 BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR| 2625 BGE_PCIMISCCTL_ENDIAN_WORDSWAP|BGE_PCIMISCCTL_PCISTATE_RW, 4); 2626 pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4); 2627 pci_write_config(dev, BGE_PCI_CMD, command, 4); 2628 bge_writereg_ind(sc, BGE_MISC_CFG, (65 << 1)); 2629 2630 /* Enable memory arbiter. */ 2631 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 && 2632 sc->bge_asicrev != BGE_ASICREV_BCM5750) 2633 CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE); 2634 2635 /* 2636 * Prevent PXE restart: write a magic number to the 2637 * general communications memory at 0xB50. 2638 */ 2639 bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER); 2640 /* 2641 * Poll the value location we just wrote until 2642 * we see the 1's complement of the magic number. 2643 * This indicates that the firmware initialization 2644 * is complete. 2645 */ 2646 for (i = 0; i < BGE_TIMEOUT; i++) { 2647 val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM); 2648 if (val == ~BGE_MAGIC_NUMBER) 2649 break; 2650 DELAY(10); 2651 } 2652 2653 if (i == BGE_TIMEOUT) { 2654 printf("bge%d: firmware handshake timed out\n", sc->bge_unit); 2655 return; 2656 } 2657 2658 /* 2659 * XXX Wait for the value of the PCISTATE register to 2660 * return to its original pre-reset state. This is a 2661 * fairly good indicator of reset completion. If we don't 2662 * wait for the reset to fully complete, trying to read 2663 * from the device's non-PCI registers may yield garbage 2664 * results. 2665 */ 2666 for (i = 0; i < BGE_TIMEOUT; i++) { 2667 if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate) 2668 break; 2669 DELAY(10); 2670 } 2671 2672 /* Fix up byte swapping */ 2673 CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_MODECTL_BYTESWAP_NONFRAME| 2674 BGE_MODECTL_BYTESWAP_DATA); 2675 2676 CSR_WRITE_4(sc, BGE_MAC_MODE, 0); 2677 2678 /* 2679 * The 5704 in TBI mode apparently needs some special 2680 * adjustment to insure the SERDES drive level is set 2681 * to 1.2V. 2682 */ 2683 if (sc->bge_asicrev == BGE_ASICREV_BCM5704 && sc->bge_tbi) { 2684 uint32_t serdescfg; 2685 serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG); 2686 serdescfg = (serdescfg & ~0xFFF) | 0x880; 2687 CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg); 2688 } 2689 2690 /* XXX: Broadcom Linux driver. */ 2691 if (sc->bge_pcie && sc->bge_chipid != BGE_CHIPID_BCM5750_A0) { 2692 uint32_t v; 2693 2694 v = CSR_READ_4(sc, 0x7c00); 2695 CSR_WRITE_4(sc, 0x7c00, v | (1<<25)); 2696 } 2697 DELAY(10000); 2698 2699 return; 2700 } 2701 2702 /* 2703 * Frame reception handling. This is called if there's a frame 2704 * on the receive return list. 2705 * 2706 * Note: we have to be able to handle two possibilities here: 2707 * 1) the frame is from the jumbo recieve ring 2708 * 2) the frame is from the standard receive ring 2709 */ 2710 2711 static void 2712 bge_rxeof(sc) 2713 struct bge_softc *sc; 2714 { 2715 struct ifnet *ifp; 2716 int stdcnt = 0, jumbocnt = 0; 2717 2718 BGE_LOCK_ASSERT(sc); 2719 2720 ifp = sc->bge_ifp; 2721 2722 bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag, 2723 sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTWRITE); 2724 bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag, 2725 sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTREAD); 2726 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 && 2727 sc->bge_asicrev != BGE_ASICREV_BCM5750) { 2728 bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag, 2729 sc->bge_cdata.bge_rx_jumbo_ring_map, 2730 BUS_DMASYNC_POSTREAD); 2731 } 2732 2733 while(sc->bge_rx_saved_considx != 2734 sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx) { 2735 struct bge_rx_bd *cur_rx; 2736 u_int32_t rxidx; 2737 struct ether_header *eh; 2738 struct mbuf *m = NULL; 2739 u_int16_t vlan_tag = 0; 2740 int have_tag = 0; 2741 2742 cur_rx = 2743 &sc->bge_ldata.bge_rx_return_ring[sc->bge_rx_saved_considx]; 2744 2745 rxidx = cur_rx->bge_idx; 2746 BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt); 2747 2748 if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) { 2749 have_tag = 1; 2750 vlan_tag = cur_rx->bge_vlan_tag; 2751 } 2752 2753 if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) { 2754 BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT); 2755 bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo, 2756 sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx], 2757 BUS_DMASYNC_POSTREAD); 2758 bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo, 2759 sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx]); 2760 m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx]; 2761 sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL; 2762 jumbocnt++; 2763 if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) { 2764 ifp->if_ierrors++; 2765 bge_newbuf_jumbo(sc, sc->bge_jumbo, m); 2766 continue; 2767 } 2768 if (bge_newbuf_jumbo(sc, 2769 sc->bge_jumbo, NULL) == ENOBUFS) { 2770 ifp->if_ierrors++; 2771 bge_newbuf_jumbo(sc, sc->bge_jumbo, m); 2772 continue; 2773 } 2774 } else { 2775 BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT); 2776 bus_dmamap_sync(sc->bge_cdata.bge_mtag, 2777 sc->bge_cdata.bge_rx_std_dmamap[rxidx], 2778 BUS_DMASYNC_POSTREAD); 2779 bus_dmamap_unload(sc->bge_cdata.bge_mtag, 2780 sc->bge_cdata.bge_rx_std_dmamap[rxidx]); 2781 m = sc->bge_cdata.bge_rx_std_chain[rxidx]; 2782 sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL; 2783 stdcnt++; 2784 if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) { 2785 ifp->if_ierrors++; 2786 bge_newbuf_std(sc, sc->bge_std, m); 2787 continue; 2788 } 2789 if (bge_newbuf_std(sc, sc->bge_std, 2790 NULL) == ENOBUFS) { 2791 ifp->if_ierrors++; 2792 bge_newbuf_std(sc, sc->bge_std, m); 2793 continue; 2794 } 2795 } 2796 2797 ifp->if_ipackets++; 2798 #ifndef __i386__ 2799 /* 2800 * The i386 allows unaligned accesses, but for other 2801 * platforms we must make sure the payload is aligned. 2802 */ 2803 if (sc->bge_rx_alignment_bug) { 2804 bcopy(m->m_data, m->m_data + ETHER_ALIGN, 2805 cur_rx->bge_len); 2806 m->m_data += ETHER_ALIGN; 2807 } 2808 #endif 2809 eh = mtod(m, struct ether_header *); 2810 m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN; 2811 m->m_pkthdr.rcvif = ifp; 2812 2813 #if 0 /* currently broken for some packets, possibly related to TCP options */ 2814 if (ifp->if_capenable & IFCAP_RXCSUM) { 2815 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED; 2816 if ((cur_rx->bge_ip_csum ^ 0xffff) == 0) 2817 m->m_pkthdr.csum_flags |= CSUM_IP_VALID; 2818 if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) { 2819 m->m_pkthdr.csum_data = 2820 cur_rx->bge_tcp_udp_csum; 2821 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID; 2822 } 2823 } 2824 #endif 2825 2826 /* 2827 * If we received a packet with a vlan tag, 2828 * attach that information to the packet. 2829 */ 2830 if (have_tag) 2831 VLAN_INPUT_TAG(ifp, m, vlan_tag, continue); 2832 2833 BGE_UNLOCK(sc); 2834 (*ifp->if_input)(ifp, m); 2835 BGE_LOCK(sc); 2836 } 2837 2838 bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag, 2839 sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREWRITE); 2840 bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag, 2841 sc->bge_cdata.bge_rx_std_ring_map, 2842 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_PREWRITE); 2843 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 && 2844 sc->bge_asicrev != BGE_ASICREV_BCM5750) { 2845 bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag, 2846 sc->bge_cdata.bge_rx_jumbo_ring_map, 2847 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 2848 } 2849 2850 CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx); 2851 if (stdcnt) 2852 CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std); 2853 if (jumbocnt) 2854 CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo); 2855 2856 return; 2857 } 2858 2859 static void 2860 bge_txeof(sc) 2861 struct bge_softc *sc; 2862 { 2863 struct bge_tx_bd *cur_tx = NULL; 2864 struct ifnet *ifp; 2865 2866 BGE_LOCK_ASSERT(sc); 2867 2868 ifp = sc->bge_ifp; 2869 2870 /* 2871 * Go through our tx ring and free mbufs for those 2872 * frames that have been sent. 2873 */ 2874 while (sc->bge_tx_saved_considx != 2875 sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) { 2876 u_int32_t idx = 0; 2877 2878 idx = sc->bge_tx_saved_considx; 2879 cur_tx = &sc->bge_ldata.bge_tx_ring[idx]; 2880 if (cur_tx->bge_flags & BGE_TXBDFLAG_END) 2881 ifp->if_opackets++; 2882 if (sc->bge_cdata.bge_tx_chain[idx] != NULL) { 2883 m_freem(sc->bge_cdata.bge_tx_chain[idx]); 2884 sc->bge_cdata.bge_tx_chain[idx] = NULL; 2885 bus_dmamap_unload(sc->bge_cdata.bge_mtag, 2886 sc->bge_cdata.bge_tx_dmamap[idx]); 2887 } 2888 sc->bge_txcnt--; 2889 BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT); 2890 ifp->if_timer = 0; 2891 } 2892 2893 if (cur_tx != NULL) 2894 ifp->if_flags &= ~IFF_OACTIVE; 2895 2896 return; 2897 } 2898 2899 static void 2900 bge_intr(xsc) 2901 void *xsc; 2902 { 2903 struct bge_softc *sc; 2904 struct ifnet *ifp; 2905 u_int32_t statusword; 2906 u_int32_t status, mimode; 2907 2908 sc = xsc; 2909 ifp = sc->bge_ifp; 2910 2911 BGE_LOCK(sc); 2912 2913 bus_dmamap_sync(sc->bge_cdata.bge_status_tag, 2914 sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTWRITE); 2915 2916 statusword = 2917 atomic_readandclear_32(&sc->bge_ldata.bge_status_block->bge_status); 2918 2919 #ifdef notdef 2920 /* Avoid this for now -- checking this register is expensive. */ 2921 /* Make sure this is really our interrupt. */ 2922 if (!(CSR_READ_4(sc, BGE_MISC_LOCAL_CTL) & BGE_MLC_INTR_STATE)) 2923 return; 2924 #endif 2925 /* Ack interrupt and stop others from occuring. */ 2926 CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1); 2927 2928 /* 2929 * Process link state changes. 2930 * Grrr. The link status word in the status block does 2931 * not work correctly on the BCM5700 rev AX and BX chips, 2932 * according to all available information. Hence, we have 2933 * to enable MII interrupts in order to properly obtain 2934 * async link changes. Unfortunately, this also means that 2935 * we have to read the MAC status register to detect link 2936 * changes, thereby adding an additional register access to 2937 * the interrupt handler. 2938 */ 2939 2940 if (sc->bge_asicrev == BGE_ASICREV_BCM5700) { 2941 2942 status = CSR_READ_4(sc, BGE_MAC_STS); 2943 if (status & BGE_MACSTAT_MI_INTERRUPT) { 2944 sc->bge_link = 0; 2945 callout_stop(&sc->bge_stat_ch); 2946 bge_tick_locked(sc); 2947 /* Clear the interrupt */ 2948 CSR_WRITE_4(sc, BGE_MAC_EVT_ENB, 2949 BGE_EVTENB_MI_INTERRUPT); 2950 bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR); 2951 bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR, 2952 BRGPHY_INTRS); 2953 } 2954 } else { 2955 if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED) { 2956 /* 2957 * Sometimes PCS encoding errors are detected in 2958 * TBI mode (on fiber NICs), and for some reason 2959 * the chip will signal them as link changes. 2960 * If we get a link change event, but the 'PCS 2961 * encoding error' bit in the MAC status register 2962 * is set, don't bother doing a link check. 2963 * This avoids spurious "gigabit link up" messages 2964 * that sometimes appear on fiber NICs during 2965 * periods of heavy traffic. (There should be no 2966 * effect on copper NICs.) 2967 * 2968 * If we do have a copper NIC (bge_tbi == 0) then 2969 * check that the AUTOPOLL bit is set before 2970 * processing the event as a real link change. 2971 * Turning AUTOPOLL on and off in the MII read/write 2972 * functions will often trigger a link status 2973 * interrupt for no reason. 2974 */ 2975 status = CSR_READ_4(sc, BGE_MAC_STS); 2976 mimode = CSR_READ_4(sc, BGE_MI_MODE); 2977 if (!(status & (BGE_MACSTAT_PORT_DECODE_ERROR| 2978 BGE_MACSTAT_MI_COMPLETE)) && (!sc->bge_tbi && 2979 (mimode & BGE_MIMODE_AUTOPOLL))) { 2980 sc->bge_link = 0; 2981 callout_stop(&sc->bge_stat_ch); 2982 bge_tick_locked(sc); 2983 } 2984 /* Clear the interrupt */ 2985 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED| 2986 BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE| 2987 BGE_MACSTAT_LINK_CHANGED); 2988 2989 /* Force flush the status block cached by PCI bridge */ 2990 CSR_READ_4(sc, BGE_MBX_IRQ0_LO); 2991 } 2992 } 2993 2994 if (ifp->if_flags & IFF_RUNNING) { 2995 /* Check RX return ring producer/consumer */ 2996 bge_rxeof(sc); 2997 2998 /* Check TX ring producer/consumer */ 2999 bge_txeof(sc); 3000 } 3001 3002 bus_dmamap_sync(sc->bge_cdata.bge_status_tag, 3003 sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREWRITE); 3004 3005 bge_handle_events(sc); 3006 3007 /* Re-enable interrupts. */ 3008 CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0); 3009 3010 if (ifp->if_flags & IFF_RUNNING && !IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 3011 bge_start_locked(ifp); 3012 3013 BGE_UNLOCK(sc); 3014 3015 return; 3016 } 3017 3018 static void 3019 bge_tick_locked(sc) 3020 struct bge_softc *sc; 3021 { 3022 struct mii_data *mii = NULL; 3023 struct ifmedia *ifm = NULL; 3024 struct ifnet *ifp; 3025 3026 ifp = sc->bge_ifp; 3027 3028 BGE_LOCK_ASSERT(sc); 3029 3030 if (sc->bge_asicrev == BGE_ASICREV_BCM5705 || 3031 sc->bge_asicrev == BGE_ASICREV_BCM5750) 3032 bge_stats_update_regs(sc); 3033 else 3034 bge_stats_update(sc); 3035 callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc); 3036 if (sc->bge_link) 3037 return; 3038 3039 if (sc->bge_tbi) { 3040 ifm = &sc->bge_ifmedia; 3041 if (CSR_READ_4(sc, BGE_MAC_STS) & 3042 BGE_MACSTAT_TBI_PCS_SYNCHED) { 3043 sc->bge_link++; 3044 if (sc->bge_asicrev == BGE_ASICREV_BCM5704) 3045 BGE_CLRBIT(sc, BGE_MAC_MODE, 3046 BGE_MACMODE_TBI_SEND_CFGS); 3047 CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF); 3048 if (bootverbose) 3049 printf("bge%d: gigabit link up\n", 3050 sc->bge_unit); 3051 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 3052 bge_start_locked(ifp); 3053 } 3054 return; 3055 } 3056 3057 mii = device_get_softc(sc->bge_miibus); 3058 mii_tick(mii); 3059 3060 if (!sc->bge_link && mii->mii_media_status & IFM_ACTIVE && 3061 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 3062 sc->bge_link++; 3063 if ((IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T || 3064 IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX) && 3065 bootverbose) 3066 printf("bge%d: gigabit link up\n", sc->bge_unit); 3067 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 3068 bge_start_locked(ifp); 3069 } 3070 3071 return; 3072 } 3073 3074 static void 3075 bge_tick(xsc) 3076 void *xsc; 3077 { 3078 struct bge_softc *sc; 3079 3080 sc = xsc; 3081 3082 BGE_LOCK(sc); 3083 bge_tick_locked(sc); 3084 BGE_UNLOCK(sc); 3085 } 3086 3087 static void 3088 bge_stats_update_regs(sc) 3089 struct bge_softc *sc; 3090 { 3091 struct ifnet *ifp; 3092 struct bge_mac_stats_regs stats; 3093 u_int32_t *s; 3094 int i; 3095 3096 ifp = sc->bge_ifp; 3097 3098 s = (u_int32_t *)&stats; 3099 for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) { 3100 *s = CSR_READ_4(sc, BGE_RX_STATS + i); 3101 s++; 3102 } 3103 3104 ifp->if_collisions += 3105 (stats.dot3StatsSingleCollisionFrames + 3106 stats.dot3StatsMultipleCollisionFrames + 3107 stats.dot3StatsExcessiveCollisions + 3108 stats.dot3StatsLateCollisions) - 3109 ifp->if_collisions; 3110 3111 return; 3112 } 3113 3114 static void 3115 bge_stats_update(sc) 3116 struct bge_softc *sc; 3117 { 3118 struct ifnet *ifp; 3119 struct bge_stats *stats; 3120 3121 ifp = sc->bge_ifp; 3122 3123 stats = (struct bge_stats *)(sc->bge_vhandle + 3124 BGE_MEMWIN_START + BGE_STATS_BLOCK); 3125 3126 ifp->if_collisions += 3127 (stats->txstats.dot3StatsSingleCollisionFrames.bge_addr_lo + 3128 stats->txstats.dot3StatsMultipleCollisionFrames.bge_addr_lo + 3129 stats->txstats.dot3StatsExcessiveCollisions.bge_addr_lo + 3130 stats->txstats.dot3StatsLateCollisions.bge_addr_lo) - 3131 ifp->if_collisions; 3132 3133 #ifdef notdef 3134 ifp->if_collisions += 3135 (sc->bge_rdata->bge_info.bge_stats.dot3StatsSingleCollisionFrames + 3136 sc->bge_rdata->bge_info.bge_stats.dot3StatsMultipleCollisionFrames + 3137 sc->bge_rdata->bge_info.bge_stats.dot3StatsExcessiveCollisions + 3138 sc->bge_rdata->bge_info.bge_stats.dot3StatsLateCollisions) - 3139 ifp->if_collisions; 3140 #endif 3141 3142 return; 3143 } 3144 3145 /* 3146 * Encapsulate an mbuf chain in the tx ring by coupling the mbuf data 3147 * pointers to descriptors. 3148 */ 3149 static int 3150 bge_encap(sc, m_head, txidx) 3151 struct bge_softc *sc; 3152 struct mbuf *m_head; 3153 u_int32_t *txidx; 3154 { 3155 struct bge_tx_bd *f = NULL; 3156 u_int16_t csum_flags = 0; 3157 struct m_tag *mtag; 3158 struct bge_dmamap_arg ctx; 3159 bus_dmamap_t map; 3160 int error; 3161 3162 3163 if (m_head->m_pkthdr.csum_flags) { 3164 if (m_head->m_pkthdr.csum_flags & CSUM_IP) 3165 csum_flags |= BGE_TXBDFLAG_IP_CSUM; 3166 if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) 3167 csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM; 3168 if (m_head->m_flags & M_LASTFRAG) 3169 csum_flags |= BGE_TXBDFLAG_IP_FRAG_END; 3170 else if (m_head->m_flags & M_FRAG) 3171 csum_flags |= BGE_TXBDFLAG_IP_FRAG; 3172 } 3173 3174 mtag = VLAN_OUTPUT_TAG(sc->bge_ifp, m_head); 3175 3176 ctx.sc = sc; 3177 ctx.bge_idx = *txidx; 3178 ctx.bge_ring = sc->bge_ldata.bge_tx_ring; 3179 ctx.bge_flags = csum_flags; 3180 /* 3181 * Sanity check: avoid coming within 16 descriptors 3182 * of the end of the ring. 3183 */ 3184 ctx.bge_maxsegs = (BGE_TX_RING_CNT - sc->bge_txcnt) - 16; 3185 3186 map = sc->bge_cdata.bge_tx_dmamap[*txidx]; 3187 error = bus_dmamap_load_mbuf(sc->bge_cdata.bge_mtag, map, 3188 m_head, bge_dma_map_tx_desc, &ctx, BUS_DMA_NOWAIT); 3189 3190 if (error || ctx.bge_maxsegs == 0 /*|| 3191 ctx.bge_idx == sc->bge_tx_saved_considx*/) 3192 return (ENOBUFS); 3193 3194 /* 3195 * Insure that the map for this transmission 3196 * is placed at the array index of the last descriptor 3197 * in this chain. 3198 */ 3199 sc->bge_cdata.bge_tx_dmamap[*txidx] = 3200 sc->bge_cdata.bge_tx_dmamap[ctx.bge_idx]; 3201 sc->bge_cdata.bge_tx_dmamap[ctx.bge_idx] = map; 3202 sc->bge_cdata.bge_tx_chain[ctx.bge_idx] = m_head; 3203 sc->bge_txcnt += ctx.bge_maxsegs; 3204 f = &sc->bge_ldata.bge_tx_ring[*txidx]; 3205 if (mtag != NULL) { 3206 f->bge_flags |= htole16(BGE_TXBDFLAG_VLAN_TAG); 3207 f->bge_vlan_tag = htole16(VLAN_TAG_VALUE(mtag)); 3208 } else { 3209 f->bge_vlan_tag = 0; 3210 } 3211 3212 BGE_INC(ctx.bge_idx, BGE_TX_RING_CNT); 3213 *txidx = ctx.bge_idx; 3214 3215 return(0); 3216 } 3217 3218 /* 3219 * Main transmit routine. To avoid having to do mbuf copies, we put pointers 3220 * to the mbuf data regions directly in the transmit descriptors. 3221 */ 3222 static void 3223 bge_start_locked(ifp) 3224 struct ifnet *ifp; 3225 { 3226 struct bge_softc *sc; 3227 struct mbuf *m_head = NULL; 3228 u_int32_t prodidx = 0; 3229 int count = 0; 3230 3231 sc = ifp->if_softc; 3232 3233 if (!sc->bge_link && IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 3234 return; 3235 3236 prodidx = CSR_READ_4(sc, BGE_MBX_TX_HOST_PROD0_LO); 3237 3238 while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) { 3239 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head); 3240 if (m_head == NULL) 3241 break; 3242 3243 /* 3244 * XXX 3245 * The code inside the if() block is never reached since we 3246 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting 3247 * requests to checksum TCP/UDP in a fragmented packet. 3248 * 3249 * XXX 3250 * safety overkill. If this is a fragmented packet chain 3251 * with delayed TCP/UDP checksums, then only encapsulate 3252 * it if we have enough descriptors to handle the entire 3253 * chain at once. 3254 * (paranoia -- may not actually be needed) 3255 */ 3256 if (m_head->m_flags & M_FIRSTFRAG && 3257 m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) { 3258 if ((BGE_TX_RING_CNT - sc->bge_txcnt) < 3259 m_head->m_pkthdr.csum_data + 16) { 3260 IFQ_DRV_PREPEND(&ifp->if_snd, m_head); 3261 ifp->if_flags |= IFF_OACTIVE; 3262 break; 3263 } 3264 } 3265 3266 /* 3267 * Pack the data into the transmit ring. If we 3268 * don't have room, set the OACTIVE flag and wait 3269 * for the NIC to drain the ring. 3270 */ 3271 if (bge_encap(sc, m_head, &prodidx)) { 3272 IFQ_DRV_PREPEND(&ifp->if_snd, m_head); 3273 ifp->if_flags |= IFF_OACTIVE; 3274 break; 3275 } 3276 ++count; 3277 3278 /* 3279 * If there's a BPF listener, bounce a copy of this frame 3280 * to him. 3281 */ 3282 BPF_MTAP(ifp, m_head); 3283 } 3284 3285 if (count == 0) { 3286 /* no packets were dequeued */ 3287 return; 3288 } 3289 3290 /* Transmit */ 3291 CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx); 3292 /* 5700 b2 errata */ 3293 if (sc->bge_chiprev == BGE_CHIPREV_5700_BX) 3294 CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx); 3295 3296 /* 3297 * Set a timeout in case the chip goes out to lunch. 3298 */ 3299 ifp->if_timer = 5; 3300 3301 return; 3302 } 3303 3304 /* 3305 * Main transmit routine. To avoid having to do mbuf copies, we put pointers 3306 * to the mbuf data regions directly in the transmit descriptors. 3307 */ 3308 static void 3309 bge_start(ifp) 3310 struct ifnet *ifp; 3311 { 3312 struct bge_softc *sc; 3313 3314 sc = ifp->if_softc; 3315 BGE_LOCK(sc); 3316 bge_start_locked(ifp); 3317 BGE_UNLOCK(sc); 3318 } 3319 3320 static void 3321 bge_init_locked(sc) 3322 struct bge_softc *sc; 3323 { 3324 struct ifnet *ifp; 3325 u_int16_t *m; 3326 3327 BGE_LOCK_ASSERT(sc); 3328 3329 ifp = sc->bge_ifp; 3330 3331 if (ifp->if_flags & IFF_RUNNING) 3332 return; 3333 3334 /* Cancel pending I/O and flush buffers. */ 3335 bge_stop(sc); 3336 bge_reset(sc); 3337 bge_chipinit(sc); 3338 3339 /* 3340 * Init the various state machines, ring 3341 * control blocks and firmware. 3342 */ 3343 if (bge_blockinit(sc)) { 3344 printf("bge%d: initialization failure\n", sc->bge_unit); 3345 return; 3346 } 3347 3348 ifp = sc->bge_ifp; 3349 3350 /* Specify MTU. */ 3351 CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu + 3352 ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN); 3353 3354 /* Load our MAC address. */ 3355 m = (u_int16_t *)&IFP2ENADDR(sc->bge_ifp)[0]; 3356 CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0])); 3357 CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2])); 3358 3359 /* Enable or disable promiscuous mode as needed. */ 3360 if (ifp->if_flags & IFF_PROMISC) { 3361 BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC); 3362 } else { 3363 BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC); 3364 } 3365 3366 /* Program multicast filter. */ 3367 bge_setmulti(sc); 3368 3369 /* Init RX ring. */ 3370 bge_init_rx_ring_std(sc); 3371 3372 /* 3373 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's 3374 * memory to insure that the chip has in fact read the first 3375 * entry of the ring. 3376 */ 3377 if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) { 3378 u_int32_t v, i; 3379 for (i = 0; i < 10; i++) { 3380 DELAY(20); 3381 v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8); 3382 if (v == (MCLBYTES - ETHER_ALIGN)) 3383 break; 3384 } 3385 if (i == 10) 3386 printf ("bge%d: 5705 A0 chip failed to load RX ring\n", 3387 sc->bge_unit); 3388 } 3389 3390 /* Init jumbo RX ring. */ 3391 if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN)) 3392 bge_init_rx_ring_jumbo(sc); 3393 3394 /* Init our RX return ring index */ 3395 sc->bge_rx_saved_considx = 0; 3396 3397 /* Init TX ring. */ 3398 bge_init_tx_ring(sc); 3399 3400 /* Turn on transmitter */ 3401 BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE); 3402 3403 /* Turn on receiver */ 3404 BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE); 3405 3406 /* Tell firmware we're alive. */ 3407 BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP); 3408 3409 /* Enable host interrupts. */ 3410 BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA); 3411 BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR); 3412 CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0); 3413 3414 bge_ifmedia_upd(ifp); 3415 3416 ifp->if_flags |= IFF_RUNNING; 3417 ifp->if_flags &= ~IFF_OACTIVE; 3418 3419 callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc); 3420 3421 return; 3422 } 3423 3424 static void 3425 bge_init(xsc) 3426 void *xsc; 3427 { 3428 struct bge_softc *sc = xsc; 3429 3430 BGE_LOCK(sc); 3431 bge_init_locked(sc); 3432 BGE_UNLOCK(sc); 3433 3434 return; 3435 } 3436 3437 /* 3438 * Set media options. 3439 */ 3440 static int 3441 bge_ifmedia_upd(ifp) 3442 struct ifnet *ifp; 3443 { 3444 struct bge_softc *sc; 3445 struct mii_data *mii; 3446 struct ifmedia *ifm; 3447 3448 sc = ifp->if_softc; 3449 ifm = &sc->bge_ifmedia; 3450 3451 /* If this is a 1000baseX NIC, enable the TBI port. */ 3452 if (sc->bge_tbi) { 3453 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) 3454 return(EINVAL); 3455 switch(IFM_SUBTYPE(ifm->ifm_media)) { 3456 case IFM_AUTO: 3457 break; 3458 case IFM_1000_SX: 3459 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) { 3460 BGE_CLRBIT(sc, BGE_MAC_MODE, 3461 BGE_MACMODE_HALF_DUPLEX); 3462 } else { 3463 BGE_SETBIT(sc, BGE_MAC_MODE, 3464 BGE_MACMODE_HALF_DUPLEX); 3465 } 3466 break; 3467 default: 3468 return(EINVAL); 3469 } 3470 return(0); 3471 } 3472 3473 mii = device_get_softc(sc->bge_miibus); 3474 sc->bge_link = 0; 3475 if (mii->mii_instance) { 3476 struct mii_softc *miisc; 3477 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL; 3478 miisc = LIST_NEXT(miisc, mii_list)) 3479 mii_phy_reset(miisc); 3480 } 3481 mii_mediachg(mii); 3482 3483 return(0); 3484 } 3485 3486 /* 3487 * Report current media status. 3488 */ 3489 static void 3490 bge_ifmedia_sts(ifp, ifmr) 3491 struct ifnet *ifp; 3492 struct ifmediareq *ifmr; 3493 { 3494 struct bge_softc *sc; 3495 struct mii_data *mii; 3496 3497 sc = ifp->if_softc; 3498 3499 if (sc->bge_tbi) { 3500 ifmr->ifm_status = IFM_AVALID; 3501 ifmr->ifm_active = IFM_ETHER; 3502 if (CSR_READ_4(sc, BGE_MAC_STS) & 3503 BGE_MACSTAT_TBI_PCS_SYNCHED) 3504 ifmr->ifm_status |= IFM_ACTIVE; 3505 ifmr->ifm_active |= IFM_1000_SX; 3506 if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX) 3507 ifmr->ifm_active |= IFM_HDX; 3508 else 3509 ifmr->ifm_active |= IFM_FDX; 3510 return; 3511 } 3512 3513 mii = device_get_softc(sc->bge_miibus); 3514 mii_pollstat(mii); 3515 ifmr->ifm_active = mii->mii_media_active; 3516 ifmr->ifm_status = mii->mii_media_status; 3517 3518 return; 3519 } 3520 3521 static int 3522 bge_ioctl(ifp, command, data) 3523 struct ifnet *ifp; 3524 u_long command; 3525 caddr_t data; 3526 { 3527 struct bge_softc *sc = ifp->if_softc; 3528 struct ifreq *ifr = (struct ifreq *) data; 3529 int mask, error = 0; 3530 struct mii_data *mii; 3531 3532 switch(command) { 3533 case SIOCSIFMTU: 3534 /* Disallow jumbo frames on 5705. */ 3535 if (((sc->bge_asicrev == BGE_ASICREV_BCM5705 || 3536 sc->bge_asicrev == BGE_ASICREV_BCM5750) && 3537 ifr->ifr_mtu > ETHERMTU) || ifr->ifr_mtu > BGE_JUMBO_MTU) 3538 error = EINVAL; 3539 else { 3540 ifp->if_mtu = ifr->ifr_mtu; 3541 ifp->if_flags &= ~IFF_RUNNING; 3542 bge_init(sc); 3543 } 3544 break; 3545 case SIOCSIFFLAGS: 3546 BGE_LOCK(sc); 3547 if (ifp->if_flags & IFF_UP) { 3548 /* 3549 * If only the state of the PROMISC flag changed, 3550 * then just use the 'set promisc mode' command 3551 * instead of reinitializing the entire NIC. Doing 3552 * a full re-init means reloading the firmware and 3553 * waiting for it to start up, which may take a 3554 * second or two. 3555 */ 3556 if (ifp->if_flags & IFF_RUNNING && 3557 ifp->if_flags & IFF_PROMISC && 3558 !(sc->bge_if_flags & IFF_PROMISC)) { 3559 BGE_SETBIT(sc, BGE_RX_MODE, 3560 BGE_RXMODE_RX_PROMISC); 3561 } else if (ifp->if_flags & IFF_RUNNING && 3562 !(ifp->if_flags & IFF_PROMISC) && 3563 sc->bge_if_flags & IFF_PROMISC) { 3564 BGE_CLRBIT(sc, BGE_RX_MODE, 3565 BGE_RXMODE_RX_PROMISC); 3566 } else 3567 bge_init_locked(sc); 3568 } else { 3569 if (ifp->if_flags & IFF_RUNNING) { 3570 bge_stop(sc); 3571 } 3572 } 3573 sc->bge_if_flags = ifp->if_flags; 3574 BGE_UNLOCK(sc); 3575 error = 0; 3576 break; 3577 case SIOCADDMULTI: 3578 case SIOCDELMULTI: 3579 if (ifp->if_flags & IFF_RUNNING) { 3580 BGE_LOCK(sc); 3581 bge_setmulti(sc); 3582 BGE_UNLOCK(sc); 3583 error = 0; 3584 } 3585 break; 3586 case SIOCSIFMEDIA: 3587 case SIOCGIFMEDIA: 3588 if (sc->bge_tbi) { 3589 error = ifmedia_ioctl(ifp, ifr, 3590 &sc->bge_ifmedia, command); 3591 } else { 3592 mii = device_get_softc(sc->bge_miibus); 3593 error = ifmedia_ioctl(ifp, ifr, 3594 &mii->mii_media, command); 3595 } 3596 break; 3597 case SIOCSIFCAP: 3598 mask = ifr->ifr_reqcap ^ ifp->if_capenable; 3599 /* NB: the code for RX csum offload is disabled for now */ 3600 if (mask & IFCAP_TXCSUM) { 3601 ifp->if_capenable ^= IFCAP_TXCSUM; 3602 if (IFCAP_TXCSUM & ifp->if_capenable) 3603 ifp->if_hwassist = BGE_CSUM_FEATURES; 3604 else 3605 ifp->if_hwassist = 0; 3606 } 3607 error = 0; 3608 break; 3609 default: 3610 error = ether_ioctl(ifp, command, data); 3611 break; 3612 } 3613 3614 return(error); 3615 } 3616 3617 static void 3618 bge_watchdog(ifp) 3619 struct ifnet *ifp; 3620 { 3621 struct bge_softc *sc; 3622 3623 sc = ifp->if_softc; 3624 3625 printf("bge%d: watchdog timeout -- resetting\n", sc->bge_unit); 3626 3627 ifp->if_flags &= ~IFF_RUNNING; 3628 bge_init(sc); 3629 3630 ifp->if_oerrors++; 3631 3632 return; 3633 } 3634 3635 /* 3636 * Stop the adapter and free any mbufs allocated to the 3637 * RX and TX lists. 3638 */ 3639 static void 3640 bge_stop(sc) 3641 struct bge_softc *sc; 3642 { 3643 struct ifnet *ifp; 3644 struct ifmedia_entry *ifm; 3645 struct mii_data *mii = NULL; 3646 int mtmp, itmp; 3647 3648 BGE_LOCK_ASSERT(sc); 3649 3650 ifp = sc->bge_ifp; 3651 3652 if (!sc->bge_tbi) 3653 mii = device_get_softc(sc->bge_miibus); 3654 3655 callout_stop(&sc->bge_stat_ch); 3656 3657 /* 3658 * Disable all of the receiver blocks 3659 */ 3660 BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE); 3661 BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE); 3662 BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE); 3663 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 && 3664 sc->bge_asicrev != BGE_ASICREV_BCM5750) 3665 BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE); 3666 BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE); 3667 BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE); 3668 BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE); 3669 3670 /* 3671 * Disable all of the transmit blocks 3672 */ 3673 BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE); 3674 BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE); 3675 BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE); 3676 BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE); 3677 BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE); 3678 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 && 3679 sc->bge_asicrev != BGE_ASICREV_BCM5750) 3680 BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE); 3681 BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE); 3682 3683 /* 3684 * Shut down all of the memory managers and related 3685 * state machines. 3686 */ 3687 BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE); 3688 BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE); 3689 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 && 3690 sc->bge_asicrev != BGE_ASICREV_BCM5750) 3691 BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE); 3692 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF); 3693 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0); 3694 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 && 3695 sc->bge_asicrev != BGE_ASICREV_BCM5750) { 3696 BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE); 3697 BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE); 3698 } 3699 3700 /* Disable host interrupts. */ 3701 BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR); 3702 CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1); 3703 3704 /* 3705 * Tell firmware we're shutting down. 3706 */ 3707 BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP); 3708 3709 /* Free the RX lists. */ 3710 bge_free_rx_ring_std(sc); 3711 3712 /* Free jumbo RX list. */ 3713 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 && 3714 sc->bge_asicrev != BGE_ASICREV_BCM5750) 3715 bge_free_rx_ring_jumbo(sc); 3716 3717 /* Free TX buffers. */ 3718 bge_free_tx_ring(sc); 3719 3720 /* 3721 * Isolate/power down the PHY, but leave the media selection 3722 * unchanged so that things will be put back to normal when 3723 * we bring the interface back up. 3724 */ 3725 if (!sc->bge_tbi) { 3726 itmp = ifp->if_flags; 3727 ifp->if_flags |= IFF_UP; 3728 ifm = mii->mii_media.ifm_cur; 3729 mtmp = ifm->ifm_media; 3730 ifm->ifm_media = IFM_ETHER|IFM_NONE; 3731 mii_mediachg(mii); 3732 ifm->ifm_media = mtmp; 3733 ifp->if_flags = itmp; 3734 } 3735 3736 sc->bge_link = 0; 3737 3738 sc->bge_tx_saved_considx = BGE_TXCONS_UNSET; 3739 3740 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 3741 3742 return; 3743 } 3744 3745 /* 3746 * Stop all chip I/O so that the kernel's probe routines don't 3747 * get confused by errant DMAs when rebooting. 3748 */ 3749 static void 3750 bge_shutdown(dev) 3751 device_t dev; 3752 { 3753 struct bge_softc *sc; 3754 3755 sc = device_get_softc(dev); 3756 3757 BGE_LOCK(sc); 3758 bge_stop(sc); 3759 bge_reset(sc); 3760 BGE_UNLOCK(sc); 3761 3762 return; 3763 } 3764