1 /*- 2 * Copyright (c) 2001 Wind River Systems 3 * Copyright (c) 1997, 1998, 1999, 2000, 2001 4 * Bill Paul <wpaul@bsdi.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 * National Semiconductor DP83820/DP83821 gigabit ethernet driver 39 * for FreeBSD. Datasheets are available from: 40 * 41 * http://www.national.com/ds/DP/DP83820.pdf 42 * http://www.national.com/ds/DP/DP83821.pdf 43 * 44 * These chips are used on several low cost gigabit ethernet NICs 45 * sold by D-Link, Addtron, SMC and Asante. Both parts are 46 * virtually the same, except the 83820 is a 64-bit/32-bit part, 47 * while the 83821 is 32-bit only. 48 * 49 * Many cards also use National gigE transceivers, such as the 50 * DP83891, DP83861 and DP83862 gigPHYTER parts. The DP83861 datasheet 51 * contains a full register description that applies to all of these 52 * components: 53 * 54 * http://www.national.com/ds/DP/DP83861.pdf 55 * 56 * Written by Bill Paul <wpaul@bsdi.com> 57 * BSDi Open Source Solutions 58 */ 59 60 /* 61 * The NatSemi DP83820 and 83821 controllers are enhanced versions 62 * of the NatSemi MacPHYTER 10/100 devices. They support 10, 100 63 * and 1000Mbps speeds with 1000baseX (ten bit interface), MII and GMII 64 * ports. Other features include 8K TX FIFO and 32K RX FIFO, TCP/IP 65 * hardware checksum offload (IPv4 only), VLAN tagging and filtering, 66 * priority TX and RX queues, a 2048 bit multicast hash filter, 4 RX pattern 67 * matching buffers, one perfect address filter buffer and interrupt 68 * moderation. The 83820 supports both 64-bit and 32-bit addressing 69 * and data transfers: the 64-bit support can be toggled on or off 70 * via software. This affects the size of certain fields in the DMA 71 * descriptors. 72 * 73 * There are two bugs/misfeatures in the 83820/83821 that I have 74 * discovered so far: 75 * 76 * - Receive buffers must be aligned on 64-bit boundaries, which means 77 * you must resort to copying data in order to fix up the payload 78 * alignment. 79 * 80 * - In order to transmit jumbo frames larger than 8170 bytes, you have 81 * to turn off transmit checksum offloading, because the chip can't 82 * compute the checksum on an outgoing frame unless it fits entirely 83 * within the TX FIFO, which is only 8192 bytes in size. If you have 84 * TX checksum offload enabled and you transmit attempt to transmit a 85 * frame larger than 8170 bytes, the transmitter will wedge. 86 * 87 * To work around the latter problem, TX checksum offload is disabled 88 * if the user selects an MTU larger than 8152 (8170 - 18). 89 */ 90 91 #include <sys/param.h> 92 #include <sys/systm.h> 93 #include <sys/sockio.h> 94 #include <sys/mbuf.h> 95 #include <sys/malloc.h> 96 #include <sys/module.h> 97 #include <sys/kernel.h> 98 #include <sys/socket.h> 99 100 #include <net/if.h> 101 #include <net/if_arp.h> 102 #include <net/ethernet.h> 103 #include <net/if_dl.h> 104 #include <net/if_media.h> 105 #include <net/if_types.h> 106 #include <net/if_vlan_var.h> 107 108 #include <net/bpf.h> 109 110 #include <vm/vm.h> /* for vtophys */ 111 #include <vm/pmap.h> /* for vtophys */ 112 #include <machine/clock.h> /* for DELAY */ 113 #include <machine/bus_pio.h> 114 #include <machine/bus_memio.h> 115 #include <machine/bus.h> 116 #include <machine/resource.h> 117 #include <sys/bus.h> 118 #include <sys/rman.h> 119 120 #include <dev/mii/mii.h> 121 #include <dev/mii/miivar.h> 122 123 #include <dev/pci/pcireg.h> 124 #include <dev/pci/pcivar.h> 125 126 #define NGE_USEIOSPACE 127 128 #include <dev/nge/if_ngereg.h> 129 130 MODULE_DEPEND(nge, pci, 1, 1, 1); 131 MODULE_DEPEND(nge, ether, 1, 1, 1); 132 MODULE_DEPEND(nge, miibus, 1, 1, 1); 133 134 /* "controller miibus0" required. See GENERIC if you get errors here. */ 135 #include "miibus_if.h" 136 137 #define NGE_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP) 138 139 /* 140 * Various supported device vendors/types and their names. 141 */ 142 static struct nge_type nge_devs[] = { 143 { NGE_VENDORID, NGE_DEVICEID, 144 "National Semiconductor Gigabit Ethernet" }, 145 { 0, 0, NULL } 146 }; 147 148 static int nge_probe(device_t); 149 static int nge_attach(device_t); 150 static int nge_detach(device_t); 151 152 static int nge_newbuf(struct nge_softc *, struct nge_desc *, struct mbuf *); 153 static int nge_encap(struct nge_softc *, struct mbuf *, u_int32_t *); 154 #ifdef NGE_FIXUP_RX 155 static __inline void nge_fixup_rx (struct mbuf *); 156 #endif 157 static void nge_rxeof(struct nge_softc *); 158 static void nge_txeof(struct nge_softc *); 159 static void nge_intr(void *); 160 static void nge_tick(void *); 161 static void nge_tick_locked(struct nge_softc *); 162 static void nge_start(struct ifnet *); 163 static void nge_start_locked(struct ifnet *); 164 static int nge_ioctl(struct ifnet *, u_long, caddr_t); 165 static void nge_init(void *); 166 static void nge_init_locked(struct nge_softc *); 167 static void nge_stop(struct nge_softc *); 168 static void nge_watchdog(struct ifnet *); 169 static void nge_shutdown(device_t); 170 static int nge_ifmedia_upd(struct ifnet *); 171 static void nge_ifmedia_sts(struct ifnet *, struct ifmediareq *); 172 173 static void nge_delay(struct nge_softc *); 174 static void nge_eeprom_idle(struct nge_softc *); 175 static void nge_eeprom_putbyte(struct nge_softc *, int); 176 static void nge_eeprom_getword(struct nge_softc *, int, u_int16_t *); 177 static void nge_read_eeprom(struct nge_softc *, caddr_t, int, int, int); 178 179 static void nge_mii_sync(struct nge_softc *); 180 static void nge_mii_send(struct nge_softc *, u_int32_t, int); 181 static int nge_mii_readreg(struct nge_softc *, struct nge_mii_frame *); 182 static int nge_mii_writereg(struct nge_softc *, struct nge_mii_frame *); 183 184 static int nge_miibus_readreg(device_t, int, int); 185 static int nge_miibus_writereg(device_t, int, int, int); 186 static void nge_miibus_statchg(device_t); 187 188 static void nge_setmulti(struct nge_softc *); 189 static void nge_reset(struct nge_softc *); 190 static int nge_list_rx_init(struct nge_softc *); 191 static int nge_list_tx_init(struct nge_softc *); 192 193 #ifdef NGE_USEIOSPACE 194 #define NGE_RES SYS_RES_IOPORT 195 #define NGE_RID NGE_PCI_LOIO 196 #else 197 #define NGE_RES SYS_RES_MEMORY 198 #define NGE_RID NGE_PCI_LOMEM 199 #endif 200 201 static device_method_t nge_methods[] = { 202 /* Device interface */ 203 DEVMETHOD(device_probe, nge_probe), 204 DEVMETHOD(device_attach, nge_attach), 205 DEVMETHOD(device_detach, nge_detach), 206 DEVMETHOD(device_shutdown, nge_shutdown), 207 208 /* bus interface */ 209 DEVMETHOD(bus_print_child, bus_generic_print_child), 210 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 211 212 /* MII interface */ 213 DEVMETHOD(miibus_readreg, nge_miibus_readreg), 214 DEVMETHOD(miibus_writereg, nge_miibus_writereg), 215 DEVMETHOD(miibus_statchg, nge_miibus_statchg), 216 217 { 0, 0 } 218 }; 219 220 static driver_t nge_driver = { 221 "nge", 222 nge_methods, 223 sizeof(struct nge_softc) 224 }; 225 226 static devclass_t nge_devclass; 227 228 DRIVER_MODULE(nge, pci, nge_driver, nge_devclass, 0, 0); 229 DRIVER_MODULE(miibus, nge, miibus_driver, miibus_devclass, 0, 0); 230 231 #define NGE_SETBIT(sc, reg, x) \ 232 CSR_WRITE_4(sc, reg, \ 233 CSR_READ_4(sc, reg) | (x)) 234 235 #define NGE_CLRBIT(sc, reg, x) \ 236 CSR_WRITE_4(sc, reg, \ 237 CSR_READ_4(sc, reg) & ~(x)) 238 239 #define SIO_SET(x) \ 240 CSR_WRITE_4(sc, NGE_MEAR, CSR_READ_4(sc, NGE_MEAR) | (x)) 241 242 #define SIO_CLR(x) \ 243 CSR_WRITE_4(sc, NGE_MEAR, CSR_READ_4(sc, NGE_MEAR) & ~(x)) 244 245 static void 246 nge_delay(sc) 247 struct nge_softc *sc; 248 { 249 int idx; 250 251 for (idx = (300 / 33) + 1; idx > 0; idx--) 252 CSR_READ_4(sc, NGE_CSR); 253 254 return; 255 } 256 257 static void 258 nge_eeprom_idle(sc) 259 struct nge_softc *sc; 260 { 261 register int i; 262 263 SIO_SET(NGE_MEAR_EE_CSEL); 264 nge_delay(sc); 265 SIO_SET(NGE_MEAR_EE_CLK); 266 nge_delay(sc); 267 268 for (i = 0; i < 25; i++) { 269 SIO_CLR(NGE_MEAR_EE_CLK); 270 nge_delay(sc); 271 SIO_SET(NGE_MEAR_EE_CLK); 272 nge_delay(sc); 273 } 274 275 SIO_CLR(NGE_MEAR_EE_CLK); 276 nge_delay(sc); 277 SIO_CLR(NGE_MEAR_EE_CSEL); 278 nge_delay(sc); 279 CSR_WRITE_4(sc, NGE_MEAR, 0x00000000); 280 281 return; 282 } 283 284 /* 285 * Send a read command and address to the EEPROM, check for ACK. 286 */ 287 static void 288 nge_eeprom_putbyte(sc, addr) 289 struct nge_softc *sc; 290 int addr; 291 { 292 register int d, i; 293 294 d = addr | NGE_EECMD_READ; 295 296 /* 297 * Feed in each bit and stobe the clock. 298 */ 299 for (i = 0x400; i; i >>= 1) { 300 if (d & i) { 301 SIO_SET(NGE_MEAR_EE_DIN); 302 } else { 303 SIO_CLR(NGE_MEAR_EE_DIN); 304 } 305 nge_delay(sc); 306 SIO_SET(NGE_MEAR_EE_CLK); 307 nge_delay(sc); 308 SIO_CLR(NGE_MEAR_EE_CLK); 309 nge_delay(sc); 310 } 311 312 return; 313 } 314 315 /* 316 * Read a word of data stored in the EEPROM at address 'addr.' 317 */ 318 static void 319 nge_eeprom_getword(sc, addr, dest) 320 struct nge_softc *sc; 321 int addr; 322 u_int16_t *dest; 323 { 324 register int i; 325 u_int16_t word = 0; 326 327 /* Force EEPROM to idle state. */ 328 nge_eeprom_idle(sc); 329 330 /* Enter EEPROM access mode. */ 331 nge_delay(sc); 332 SIO_CLR(NGE_MEAR_EE_CLK); 333 nge_delay(sc); 334 SIO_SET(NGE_MEAR_EE_CSEL); 335 nge_delay(sc); 336 337 /* 338 * Send address of word we want to read. 339 */ 340 nge_eeprom_putbyte(sc, addr); 341 342 /* 343 * Start reading bits from EEPROM. 344 */ 345 for (i = 0x8000; i; i >>= 1) { 346 SIO_SET(NGE_MEAR_EE_CLK); 347 nge_delay(sc); 348 if (CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_EE_DOUT) 349 word |= i; 350 nge_delay(sc); 351 SIO_CLR(NGE_MEAR_EE_CLK); 352 nge_delay(sc); 353 } 354 355 /* Turn off EEPROM access mode. */ 356 nge_eeprom_idle(sc); 357 358 *dest = word; 359 360 return; 361 } 362 363 /* 364 * Read a sequence of words from the EEPROM. 365 */ 366 static void 367 nge_read_eeprom(sc, dest, off, cnt, swap) 368 struct nge_softc *sc; 369 caddr_t dest; 370 int off; 371 int cnt; 372 int swap; 373 { 374 int i; 375 u_int16_t word = 0, *ptr; 376 377 for (i = 0; i < cnt; i++) { 378 nge_eeprom_getword(sc, off + i, &word); 379 ptr = (u_int16_t *)(dest + (i * 2)); 380 if (swap) 381 *ptr = ntohs(word); 382 else 383 *ptr = word; 384 } 385 386 return; 387 } 388 389 /* 390 * Sync the PHYs by setting data bit and strobing the clock 32 times. 391 */ 392 static void 393 nge_mii_sync(sc) 394 struct nge_softc *sc; 395 { 396 register int i; 397 398 SIO_SET(NGE_MEAR_MII_DIR|NGE_MEAR_MII_DATA); 399 400 for (i = 0; i < 32; i++) { 401 SIO_SET(NGE_MEAR_MII_CLK); 402 DELAY(1); 403 SIO_CLR(NGE_MEAR_MII_CLK); 404 DELAY(1); 405 } 406 407 return; 408 } 409 410 /* 411 * Clock a series of bits through the MII. 412 */ 413 static void 414 nge_mii_send(sc, bits, cnt) 415 struct nge_softc *sc; 416 u_int32_t bits; 417 int cnt; 418 { 419 int i; 420 421 SIO_CLR(NGE_MEAR_MII_CLK); 422 423 for (i = (0x1 << (cnt - 1)); i; i >>= 1) { 424 if (bits & i) { 425 SIO_SET(NGE_MEAR_MII_DATA); 426 } else { 427 SIO_CLR(NGE_MEAR_MII_DATA); 428 } 429 DELAY(1); 430 SIO_CLR(NGE_MEAR_MII_CLK); 431 DELAY(1); 432 SIO_SET(NGE_MEAR_MII_CLK); 433 } 434 } 435 436 /* 437 * Read an PHY register through the MII. 438 */ 439 static int 440 nge_mii_readreg(sc, frame) 441 struct nge_softc *sc; 442 struct nge_mii_frame *frame; 443 444 { 445 int i, ack; 446 447 /* 448 * Set up frame for RX. 449 */ 450 frame->mii_stdelim = NGE_MII_STARTDELIM; 451 frame->mii_opcode = NGE_MII_READOP; 452 frame->mii_turnaround = 0; 453 frame->mii_data = 0; 454 455 CSR_WRITE_4(sc, NGE_MEAR, 0); 456 457 /* 458 * Turn on data xmit. 459 */ 460 SIO_SET(NGE_MEAR_MII_DIR); 461 462 nge_mii_sync(sc); 463 464 /* 465 * Send command/address info. 466 */ 467 nge_mii_send(sc, frame->mii_stdelim, 2); 468 nge_mii_send(sc, frame->mii_opcode, 2); 469 nge_mii_send(sc, frame->mii_phyaddr, 5); 470 nge_mii_send(sc, frame->mii_regaddr, 5); 471 472 /* Idle bit */ 473 SIO_CLR((NGE_MEAR_MII_CLK|NGE_MEAR_MII_DATA)); 474 DELAY(1); 475 SIO_SET(NGE_MEAR_MII_CLK); 476 DELAY(1); 477 478 /* Turn off xmit. */ 479 SIO_CLR(NGE_MEAR_MII_DIR); 480 /* Check for ack */ 481 SIO_CLR(NGE_MEAR_MII_CLK); 482 DELAY(1); 483 ack = CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_MII_DATA; 484 SIO_SET(NGE_MEAR_MII_CLK); 485 DELAY(1); 486 487 /* 488 * Now try reading data bits. If the ack failed, we still 489 * need to clock through 16 cycles to keep the PHY(s) in sync. 490 */ 491 if (ack) { 492 for(i = 0; i < 16; i++) { 493 SIO_CLR(NGE_MEAR_MII_CLK); 494 DELAY(1); 495 SIO_SET(NGE_MEAR_MII_CLK); 496 DELAY(1); 497 } 498 goto fail; 499 } 500 501 for (i = 0x8000; i; i >>= 1) { 502 SIO_CLR(NGE_MEAR_MII_CLK); 503 DELAY(1); 504 if (!ack) { 505 if (CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_MII_DATA) 506 frame->mii_data |= i; 507 DELAY(1); 508 } 509 SIO_SET(NGE_MEAR_MII_CLK); 510 DELAY(1); 511 } 512 513 fail: 514 515 SIO_CLR(NGE_MEAR_MII_CLK); 516 DELAY(1); 517 SIO_SET(NGE_MEAR_MII_CLK); 518 DELAY(1); 519 520 if (ack) 521 return(1); 522 return(0); 523 } 524 525 /* 526 * Write to a PHY register through the MII. 527 */ 528 static int 529 nge_mii_writereg(sc, frame) 530 struct nge_softc *sc; 531 struct nge_mii_frame *frame; 532 533 { 534 535 /* 536 * Set up frame for TX. 537 */ 538 539 frame->mii_stdelim = NGE_MII_STARTDELIM; 540 frame->mii_opcode = NGE_MII_WRITEOP; 541 frame->mii_turnaround = NGE_MII_TURNAROUND; 542 543 /* 544 * Turn on data output. 545 */ 546 SIO_SET(NGE_MEAR_MII_DIR); 547 548 nge_mii_sync(sc); 549 550 nge_mii_send(sc, frame->mii_stdelim, 2); 551 nge_mii_send(sc, frame->mii_opcode, 2); 552 nge_mii_send(sc, frame->mii_phyaddr, 5); 553 nge_mii_send(sc, frame->mii_regaddr, 5); 554 nge_mii_send(sc, frame->mii_turnaround, 2); 555 nge_mii_send(sc, frame->mii_data, 16); 556 557 /* Idle bit. */ 558 SIO_SET(NGE_MEAR_MII_CLK); 559 DELAY(1); 560 SIO_CLR(NGE_MEAR_MII_CLK); 561 DELAY(1); 562 563 /* 564 * Turn off xmit. 565 */ 566 SIO_CLR(NGE_MEAR_MII_DIR); 567 568 return(0); 569 } 570 571 static int 572 nge_miibus_readreg(dev, phy, reg) 573 device_t dev; 574 int phy, reg; 575 { 576 struct nge_softc *sc; 577 struct nge_mii_frame frame; 578 579 sc = device_get_softc(dev); 580 581 bzero((char *)&frame, sizeof(frame)); 582 583 frame.mii_phyaddr = phy; 584 frame.mii_regaddr = reg; 585 nge_mii_readreg(sc, &frame); 586 587 return(frame.mii_data); 588 } 589 590 static int 591 nge_miibus_writereg(dev, phy, reg, data) 592 device_t dev; 593 int phy, reg, data; 594 { 595 struct nge_softc *sc; 596 struct nge_mii_frame frame; 597 598 sc = device_get_softc(dev); 599 600 bzero((char *)&frame, sizeof(frame)); 601 602 frame.mii_phyaddr = phy; 603 frame.mii_regaddr = reg; 604 frame.mii_data = data; 605 nge_mii_writereg(sc, &frame); 606 607 return(0); 608 } 609 610 static void 611 nge_miibus_statchg(dev) 612 device_t dev; 613 { 614 int status; 615 struct nge_softc *sc; 616 struct mii_data *mii; 617 618 sc = device_get_softc(dev); 619 if (sc->nge_tbi) { 620 if (IFM_SUBTYPE(sc->nge_ifmedia.ifm_cur->ifm_media) 621 == IFM_AUTO) { 622 status = CSR_READ_4(sc, NGE_TBI_ANLPAR); 623 if (status == 0 || status & NGE_TBIANAR_FDX) { 624 NGE_SETBIT(sc, NGE_TX_CFG, 625 (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR)); 626 NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX); 627 } else { 628 NGE_CLRBIT(sc, NGE_TX_CFG, 629 (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR)); 630 NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX); 631 } 632 633 } else if ((sc->nge_ifmedia.ifm_cur->ifm_media & IFM_GMASK) 634 != IFM_FDX) { 635 NGE_CLRBIT(sc, NGE_TX_CFG, 636 (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR)); 637 NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX); 638 } else { 639 NGE_SETBIT(sc, NGE_TX_CFG, 640 (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR)); 641 NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX); 642 } 643 } else { 644 mii = device_get_softc(sc->nge_miibus); 645 646 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) { 647 NGE_SETBIT(sc, NGE_TX_CFG, 648 (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR)); 649 NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX); 650 } else { 651 NGE_CLRBIT(sc, NGE_TX_CFG, 652 (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR)); 653 NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX); 654 } 655 656 /* If we have a 1000Mbps link, set the mode_1000 bit. */ 657 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T || 658 IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX) { 659 NGE_SETBIT(sc, NGE_CFG, NGE_CFG_MODE_1000); 660 } else { 661 NGE_CLRBIT(sc, NGE_CFG, NGE_CFG_MODE_1000); 662 } 663 } 664 return; 665 } 666 667 static void 668 nge_setmulti(sc) 669 struct nge_softc *sc; 670 { 671 struct ifnet *ifp; 672 struct ifmultiaddr *ifma; 673 u_int32_t h = 0, i, filtsave; 674 int bit, index; 675 676 NGE_LOCK_ASSERT(sc); 677 ifp = &sc->arpcom.ac_if; 678 679 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 680 NGE_CLRBIT(sc, NGE_RXFILT_CTL, 681 NGE_RXFILTCTL_MCHASH|NGE_RXFILTCTL_UCHASH); 682 NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ALLMULTI); 683 return; 684 } 685 686 /* 687 * We have to explicitly enable the multicast hash table 688 * on the NatSemi chip if we want to use it, which we do. 689 * We also have to tell it that we don't want to use the 690 * hash table for matching unicast addresses. 691 */ 692 NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_MCHASH); 693 NGE_CLRBIT(sc, NGE_RXFILT_CTL, 694 NGE_RXFILTCTL_ALLMULTI|NGE_RXFILTCTL_UCHASH); 695 696 filtsave = CSR_READ_4(sc, NGE_RXFILT_CTL); 697 698 /* first, zot all the existing hash bits */ 699 for (i = 0; i < NGE_MCAST_FILTER_LEN; i += 2) { 700 CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_MCAST_LO + i); 701 CSR_WRITE_4(sc, NGE_RXFILT_DATA, 0); 702 } 703 704 /* 705 * From the 11 bits returned by the crc routine, the top 7 706 * bits represent the 16-bit word in the mcast hash table 707 * that needs to be updated, and the lower 4 bits represent 708 * which bit within that byte needs to be set. 709 */ 710 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 711 if (ifma->ifma_addr->sa_family != AF_LINK) 712 continue; 713 h = ether_crc32_be(LLADDR((struct sockaddr_dl *) 714 ifma->ifma_addr), ETHER_ADDR_LEN) >> 21; 715 index = (h >> 4) & 0x7F; 716 bit = h & 0xF; 717 CSR_WRITE_4(sc, NGE_RXFILT_CTL, 718 NGE_FILTADDR_MCAST_LO + (index * 2)); 719 NGE_SETBIT(sc, NGE_RXFILT_DATA, (1 << bit)); 720 } 721 722 CSR_WRITE_4(sc, NGE_RXFILT_CTL, filtsave); 723 724 return; 725 } 726 727 static void 728 nge_reset(sc) 729 struct nge_softc *sc; 730 { 731 register int i; 732 733 NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RESET); 734 735 for (i = 0; i < NGE_TIMEOUT; i++) { 736 if (!(CSR_READ_4(sc, NGE_CSR) & NGE_CSR_RESET)) 737 break; 738 } 739 740 if (i == NGE_TIMEOUT) 741 printf("nge%d: reset never completed\n", sc->nge_unit); 742 743 /* Wait a little while for the chip to get its brains in order. */ 744 DELAY(1000); 745 746 /* 747 * If this is a NetSemi chip, make sure to clear 748 * PME mode. 749 */ 750 CSR_WRITE_4(sc, NGE_CLKRUN, NGE_CLKRUN_PMESTS); 751 CSR_WRITE_4(sc, NGE_CLKRUN, 0); 752 753 return; 754 } 755 756 /* 757 * Probe for a NatSemi chip. Check the PCI vendor and device 758 * IDs against our list and return a device name if we find a match. 759 */ 760 static int 761 nge_probe(dev) 762 device_t dev; 763 { 764 struct nge_type *t; 765 766 t = nge_devs; 767 768 while(t->nge_name != NULL) { 769 if ((pci_get_vendor(dev) == t->nge_vid) && 770 (pci_get_device(dev) == t->nge_did)) { 771 device_set_desc(dev, t->nge_name); 772 return(BUS_PROBE_DEFAULT); 773 } 774 t++; 775 } 776 777 return(ENXIO); 778 } 779 780 /* 781 * Attach the interface. Allocate softc structures, do ifmedia 782 * setup and ethernet/BPF attach. 783 */ 784 static int 785 nge_attach(dev) 786 device_t dev; 787 { 788 u_char eaddr[ETHER_ADDR_LEN]; 789 struct nge_softc *sc; 790 struct ifnet *ifp; 791 int unit, error = 0, rid; 792 const char *sep = ""; 793 794 sc = device_get_softc(dev); 795 unit = device_get_unit(dev); 796 bzero(sc, sizeof(struct nge_softc)); 797 798 NGE_LOCK_INIT(sc, device_get_nameunit(dev)); 799 /* 800 * Map control/status registers. 801 */ 802 pci_enable_busmaster(dev); 803 804 rid = NGE_RID; 805 sc->nge_res = bus_alloc_resource_any(dev, NGE_RES, &rid, RF_ACTIVE); 806 807 if (sc->nge_res == NULL) { 808 printf("nge%d: couldn't map ports/memory\n", unit); 809 error = ENXIO; 810 goto fail; 811 } 812 813 sc->nge_btag = rman_get_bustag(sc->nge_res); 814 sc->nge_bhandle = rman_get_bushandle(sc->nge_res); 815 816 /* Allocate interrupt */ 817 rid = 0; 818 sc->nge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 819 RF_SHAREABLE | RF_ACTIVE); 820 821 if (sc->nge_irq == NULL) { 822 printf("nge%d: couldn't map interrupt\n", unit); 823 bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res); 824 error = ENXIO; 825 goto fail; 826 } 827 828 /* Reset the adapter. */ 829 nge_reset(sc); 830 831 /* 832 * Get station address from the EEPROM. 833 */ 834 nge_read_eeprom(sc, (caddr_t)&eaddr[4], NGE_EE_NODEADDR, 1, 0); 835 nge_read_eeprom(sc, (caddr_t)&eaddr[2], NGE_EE_NODEADDR + 1, 1, 0); 836 nge_read_eeprom(sc, (caddr_t)&eaddr[0], NGE_EE_NODEADDR + 2, 1, 0); 837 838 sc->nge_unit = unit; 839 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); 840 841 sc->nge_ldata = contigmalloc(sizeof(struct nge_list_data), M_DEVBUF, 842 M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0); 843 844 if (sc->nge_ldata == NULL) { 845 printf("nge%d: no memory for list buffers!\n", unit); 846 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq); 847 bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res); 848 error = ENXIO; 849 goto fail; 850 } 851 bzero(sc->nge_ldata, sizeof(struct nge_list_data)); 852 853 ifp = &sc->arpcom.ac_if; 854 ifp->if_softc = sc; 855 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 856 ifp->if_mtu = ETHERMTU; 857 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 858 ifp->if_ioctl = nge_ioctl; 859 ifp->if_start = nge_start; 860 ifp->if_watchdog = nge_watchdog; 861 ifp->if_init = nge_init; 862 ifp->if_baudrate = 1000000000; 863 ifp->if_snd.ifq_maxlen = NGE_TX_LIST_CNT - 1; 864 ifp->if_hwassist = NGE_CSUM_FEATURES; 865 ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING; 866 #ifdef DEVICE_POLLING 867 ifp->if_capabilities |= IFCAP_POLLING; 868 #endif 869 ifp->if_capenable = ifp->if_capabilities; 870 871 /* 872 * Do MII setup. 873 */ 874 if (mii_phy_probe(dev, &sc->nge_miibus, 875 nge_ifmedia_upd, nge_ifmedia_sts)) { 876 if (CSR_READ_4(sc, NGE_CFG) & NGE_CFG_TBI_EN) { 877 sc->nge_tbi = 1; 878 device_printf(dev, "Using TBI\n"); 879 880 sc->nge_miibus = dev; 881 882 ifmedia_init(&sc->nge_ifmedia, 0, nge_ifmedia_upd, 883 nge_ifmedia_sts); 884 #define ADD(m, c) ifmedia_add(&sc->nge_ifmedia, (m), (c), NULL) 885 #define PRINT(s) printf("%s%s", sep, s); sep = ", " 886 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, 0), 0); 887 device_printf(dev, " "); 888 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0, 0), 0); 889 PRINT("1000baseSX"); 890 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX, 0),0); 891 PRINT("1000baseSX-FDX"); 892 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0), 0); 893 PRINT("auto"); 894 895 printf("\n"); 896 #undef ADD 897 #undef PRINT 898 ifmedia_set(&sc->nge_ifmedia, 899 IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0)); 900 901 CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO) 902 | NGE_GPIO_GP4_OUT 903 | NGE_GPIO_GP1_OUTENB | NGE_GPIO_GP2_OUTENB 904 | NGE_GPIO_GP3_OUTENB 905 | NGE_GPIO_GP3_IN | NGE_GPIO_GP4_IN); 906 907 } else { 908 printf("nge%d: MII without any PHY!\n", sc->nge_unit); 909 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq); 910 bus_release_resource(dev, NGE_RES, NGE_RID, 911 sc->nge_res); 912 error = ENXIO; 913 goto fail; 914 } 915 } 916 917 /* 918 * Call MI attach routine. 919 */ 920 ether_ifattach(ifp, eaddr); 921 callout_init(&sc->nge_stat_ch, CALLOUT_MPSAFE); 922 923 /* 924 * Hookup IRQ last. 925 */ 926 error = bus_setup_intr(dev, sc->nge_irq, INTR_TYPE_NET | INTR_MPSAFE, 927 nge_intr, sc, &sc->nge_intrhand); 928 if (error) { 929 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq); 930 bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res); 931 printf("nge%d: couldn't set up irq\n", unit); 932 } 933 934 fail: 935 936 if (error) 937 NGE_LOCK_DESTROY(sc); 938 return(error); 939 } 940 941 static int 942 nge_detach(dev) 943 device_t dev; 944 { 945 struct nge_softc *sc; 946 struct ifnet *ifp; 947 948 sc = device_get_softc(dev); 949 ifp = &sc->arpcom.ac_if; 950 951 NGE_LOCK(sc); 952 nge_reset(sc); 953 nge_stop(sc); 954 NGE_UNLOCK(sc); 955 ether_ifdetach(ifp); 956 957 bus_generic_detach(dev); 958 if (!sc->nge_tbi) { 959 device_delete_child(dev, sc->nge_miibus); 960 } 961 bus_teardown_intr(dev, sc->nge_irq, sc->nge_intrhand); 962 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq); 963 bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res); 964 965 contigfree(sc->nge_ldata, sizeof(struct nge_list_data), M_DEVBUF); 966 967 NGE_LOCK_DESTROY(sc); 968 969 return(0); 970 } 971 972 /* 973 * Initialize the transmit descriptors. 974 */ 975 static int 976 nge_list_tx_init(sc) 977 struct nge_softc *sc; 978 { 979 struct nge_list_data *ld; 980 struct nge_ring_data *cd; 981 int i; 982 983 cd = &sc->nge_cdata; 984 ld = sc->nge_ldata; 985 986 for (i = 0; i < NGE_TX_LIST_CNT; i++) { 987 if (i == (NGE_TX_LIST_CNT - 1)) { 988 ld->nge_tx_list[i].nge_nextdesc = 989 &ld->nge_tx_list[0]; 990 ld->nge_tx_list[i].nge_next = 991 vtophys(&ld->nge_tx_list[0]); 992 } else { 993 ld->nge_tx_list[i].nge_nextdesc = 994 &ld->nge_tx_list[i + 1]; 995 ld->nge_tx_list[i].nge_next = 996 vtophys(&ld->nge_tx_list[i + 1]); 997 } 998 ld->nge_tx_list[i].nge_mbuf = NULL; 999 ld->nge_tx_list[i].nge_ptr = 0; 1000 ld->nge_tx_list[i].nge_ctl = 0; 1001 } 1002 1003 cd->nge_tx_prod = cd->nge_tx_cons = cd->nge_tx_cnt = 0; 1004 1005 return(0); 1006 } 1007 1008 1009 /* 1010 * Initialize the RX descriptors and allocate mbufs for them. Note that 1011 * we arrange the descriptors in a closed ring, so that the last descriptor 1012 * points back to the first. 1013 */ 1014 static int 1015 nge_list_rx_init(sc) 1016 struct nge_softc *sc; 1017 { 1018 struct nge_list_data *ld; 1019 struct nge_ring_data *cd; 1020 int i; 1021 1022 ld = sc->nge_ldata; 1023 cd = &sc->nge_cdata; 1024 1025 for (i = 0; i < NGE_RX_LIST_CNT; i++) { 1026 if (nge_newbuf(sc, &ld->nge_rx_list[i], NULL) == ENOBUFS) 1027 return(ENOBUFS); 1028 if (i == (NGE_RX_LIST_CNT - 1)) { 1029 ld->nge_rx_list[i].nge_nextdesc = 1030 &ld->nge_rx_list[0]; 1031 ld->nge_rx_list[i].nge_next = 1032 vtophys(&ld->nge_rx_list[0]); 1033 } else { 1034 ld->nge_rx_list[i].nge_nextdesc = 1035 &ld->nge_rx_list[i + 1]; 1036 ld->nge_rx_list[i].nge_next = 1037 vtophys(&ld->nge_rx_list[i + 1]); 1038 } 1039 } 1040 1041 cd->nge_rx_prod = 0; 1042 sc->nge_head = sc->nge_tail = NULL; 1043 1044 return(0); 1045 } 1046 1047 /* 1048 * Initialize an RX descriptor and attach an MBUF cluster. 1049 */ 1050 static int 1051 nge_newbuf(sc, c, m) 1052 struct nge_softc *sc; 1053 struct nge_desc *c; 1054 struct mbuf *m; 1055 { 1056 1057 if (m == NULL) { 1058 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1059 if (m == NULL) 1060 return (ENOBUFS); 1061 } else 1062 m->m_data = m->m_ext.ext_buf; 1063 1064 m->m_len = m->m_pkthdr.len = MCLBYTES; 1065 1066 m_adj(m, sizeof(u_int64_t)); 1067 1068 c->nge_mbuf = m; 1069 c->nge_ptr = vtophys(mtod(m, caddr_t)); 1070 c->nge_ctl = m->m_len; 1071 c->nge_extsts = 0; 1072 1073 return(0); 1074 } 1075 1076 #ifdef NGE_FIXUP_RX 1077 static __inline void 1078 nge_fixup_rx(m) 1079 struct mbuf *m; 1080 { 1081 int i; 1082 uint16_t *src, *dst; 1083 1084 src = mtod(m, uint16_t *); 1085 dst = src - 1; 1086 1087 for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++) 1088 *dst++ = *src++; 1089 1090 m->m_data -= ETHER_ALIGN; 1091 1092 return; 1093 } 1094 #endif 1095 1096 /* 1097 * A frame has been uploaded: pass the resulting mbuf chain up to 1098 * the higher level protocols. 1099 */ 1100 static void 1101 nge_rxeof(sc) 1102 struct nge_softc *sc; 1103 { 1104 struct mbuf *m; 1105 struct ifnet *ifp; 1106 struct nge_desc *cur_rx; 1107 int i, total_len = 0; 1108 u_int32_t rxstat; 1109 1110 NGE_LOCK_ASSERT(sc); 1111 ifp = &sc->arpcom.ac_if; 1112 i = sc->nge_cdata.nge_rx_prod; 1113 1114 while(NGE_OWNDESC(&sc->nge_ldata->nge_rx_list[i])) { 1115 u_int32_t extsts; 1116 1117 #ifdef DEVICE_POLLING 1118 if (ifp->if_flags & IFF_POLLING) { 1119 if (sc->rxcycles <= 0) 1120 break; 1121 sc->rxcycles--; 1122 } 1123 #endif /* DEVICE_POLLING */ 1124 1125 cur_rx = &sc->nge_ldata->nge_rx_list[i]; 1126 rxstat = cur_rx->nge_rxstat; 1127 extsts = cur_rx->nge_extsts; 1128 m = cur_rx->nge_mbuf; 1129 cur_rx->nge_mbuf = NULL; 1130 total_len = NGE_RXBYTES(cur_rx); 1131 NGE_INC(i, NGE_RX_LIST_CNT); 1132 1133 if (rxstat & NGE_CMDSTS_MORE) { 1134 m->m_len = total_len; 1135 if (sc->nge_head == NULL) { 1136 m->m_pkthdr.len = total_len; 1137 sc->nge_head = sc->nge_tail = m; 1138 } else { 1139 m->m_flags &= ~M_PKTHDR; 1140 sc->nge_head->m_pkthdr.len += total_len; 1141 sc->nge_tail->m_next = m; 1142 sc->nge_tail = m; 1143 } 1144 nge_newbuf(sc, cur_rx, NULL); 1145 continue; 1146 } 1147 1148 /* 1149 * If an error occurs, update stats, clear the 1150 * status word and leave the mbuf cluster in place: 1151 * it should simply get re-used next time this descriptor 1152 * comes up in the ring. 1153 */ 1154 if (!(rxstat & NGE_CMDSTS_PKT_OK)) { 1155 ifp->if_ierrors++; 1156 if (sc->nge_head != NULL) { 1157 m_freem(sc->nge_head); 1158 sc->nge_head = sc->nge_tail = NULL; 1159 } 1160 nge_newbuf(sc, cur_rx, m); 1161 continue; 1162 } 1163 1164 /* Try conjure up a replacement mbuf. */ 1165 1166 if (nge_newbuf(sc, cur_rx, NULL)) { 1167 ifp->if_ierrors++; 1168 if (sc->nge_head != NULL) { 1169 m_freem(sc->nge_head); 1170 sc->nge_head = sc->nge_tail = NULL; 1171 } 1172 nge_newbuf(sc, cur_rx, m); 1173 continue; 1174 } 1175 1176 if (sc->nge_head != NULL) { 1177 m->m_len = total_len; 1178 m->m_flags &= ~M_PKTHDR; 1179 sc->nge_tail->m_next = m; 1180 m = sc->nge_head; 1181 m->m_pkthdr.len += total_len; 1182 sc->nge_head = sc->nge_tail = NULL; 1183 } else 1184 m->m_pkthdr.len = m->m_len = total_len; 1185 1186 /* 1187 * Ok. NatSemi really screwed up here. This is the 1188 * only gigE chip I know of with alignment constraints 1189 * on receive buffers. RX buffers must be 64-bit aligned. 1190 */ 1191 /* 1192 * By popular demand, ignore the alignment problems 1193 * on the Intel x86 platform. The performance hit 1194 * incurred due to unaligned accesses is much smaller 1195 * than the hit produced by forcing buffer copies all 1196 * the time, especially with jumbo frames. We still 1197 * need to fix up the alignment everywhere else though. 1198 */ 1199 #ifdef NGE_FIXUP_RX 1200 nge_fixup_rx(m); 1201 #endif 1202 1203 ifp->if_ipackets++; 1204 m->m_pkthdr.rcvif = ifp; 1205 1206 /* Do IP checksum checking. */ 1207 if (extsts & NGE_RXEXTSTS_IPPKT) 1208 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED; 1209 if (!(extsts & NGE_RXEXTSTS_IPCSUMERR)) 1210 m->m_pkthdr.csum_flags |= CSUM_IP_VALID; 1211 if ((extsts & NGE_RXEXTSTS_TCPPKT && 1212 !(extsts & NGE_RXEXTSTS_TCPCSUMERR)) || 1213 (extsts & NGE_RXEXTSTS_UDPPKT && 1214 !(extsts & NGE_RXEXTSTS_UDPCSUMERR))) { 1215 m->m_pkthdr.csum_flags |= 1216 CSUM_DATA_VALID|CSUM_PSEUDO_HDR; 1217 m->m_pkthdr.csum_data = 0xffff; 1218 } 1219 1220 /* 1221 * If we received a packet with a vlan tag, pass it 1222 * to vlan_input() instead of ether_input(). 1223 */ 1224 if (extsts & NGE_RXEXTSTS_VLANPKT) { 1225 VLAN_INPUT_TAG(ifp, m, 1226 ntohs(extsts & NGE_RXEXTSTS_VTCI), continue); 1227 } 1228 NGE_UNLOCK(sc); 1229 (*ifp->if_input)(ifp, m); 1230 NGE_LOCK(sc); 1231 } 1232 1233 sc->nge_cdata.nge_rx_prod = i; 1234 1235 return; 1236 } 1237 1238 /* 1239 * A frame was downloaded to the chip. It's safe for us to clean up 1240 * the list buffers. 1241 */ 1242 1243 static void 1244 nge_txeof(sc) 1245 struct nge_softc *sc; 1246 { 1247 struct nge_desc *cur_tx; 1248 struct ifnet *ifp; 1249 u_int32_t idx; 1250 1251 NGE_LOCK_ASSERT(sc); 1252 ifp = &sc->arpcom.ac_if; 1253 1254 /* 1255 * Go through our tx list and free mbufs for those 1256 * frames that have been transmitted. 1257 */ 1258 idx = sc->nge_cdata.nge_tx_cons; 1259 while (idx != sc->nge_cdata.nge_tx_prod) { 1260 cur_tx = &sc->nge_ldata->nge_tx_list[idx]; 1261 1262 if (NGE_OWNDESC(cur_tx)) 1263 break; 1264 1265 if (cur_tx->nge_ctl & NGE_CMDSTS_MORE) { 1266 sc->nge_cdata.nge_tx_cnt--; 1267 NGE_INC(idx, NGE_TX_LIST_CNT); 1268 continue; 1269 } 1270 1271 if (!(cur_tx->nge_ctl & NGE_CMDSTS_PKT_OK)) { 1272 ifp->if_oerrors++; 1273 if (cur_tx->nge_txstat & NGE_TXSTAT_EXCESSCOLLS) 1274 ifp->if_collisions++; 1275 if (cur_tx->nge_txstat & NGE_TXSTAT_OUTOFWINCOLL) 1276 ifp->if_collisions++; 1277 } 1278 1279 ifp->if_collisions += 1280 (cur_tx->nge_txstat & NGE_TXSTAT_COLLCNT) >> 16; 1281 1282 ifp->if_opackets++; 1283 if (cur_tx->nge_mbuf != NULL) { 1284 m_freem(cur_tx->nge_mbuf); 1285 cur_tx->nge_mbuf = NULL; 1286 ifp->if_flags &= ~IFF_OACTIVE; 1287 } 1288 1289 sc->nge_cdata.nge_tx_cnt--; 1290 NGE_INC(idx, NGE_TX_LIST_CNT); 1291 } 1292 1293 sc->nge_cdata.nge_tx_cons = idx; 1294 1295 if (idx == sc->nge_cdata.nge_tx_prod) 1296 ifp->if_timer = 0; 1297 1298 return; 1299 } 1300 1301 static void 1302 nge_tick(xsc) 1303 void *xsc; 1304 { 1305 struct nge_softc *sc; 1306 1307 sc = xsc; 1308 1309 NGE_LOCK(sc); 1310 nge_tick_locked(sc); 1311 NGE_UNLOCK(sc); 1312 } 1313 1314 static void 1315 nge_tick_locked(sc) 1316 struct nge_softc *sc; 1317 { 1318 struct mii_data *mii; 1319 struct ifnet *ifp; 1320 1321 NGE_LOCK_ASSERT(sc); 1322 ifp = &sc->arpcom.ac_if; 1323 1324 if (sc->nge_tbi) { 1325 if (!sc->nge_link) { 1326 if (CSR_READ_4(sc, NGE_TBI_BMSR) 1327 & NGE_TBIBMSR_ANEG_DONE) { 1328 if (bootverbose) 1329 printf("nge%d: gigabit link up\n", 1330 sc->nge_unit); 1331 nge_miibus_statchg(sc->nge_miibus); 1332 sc->nge_link++; 1333 if (ifp->if_snd.ifq_head != NULL) 1334 nge_start_locked(ifp); 1335 } 1336 } 1337 } else { 1338 mii = device_get_softc(sc->nge_miibus); 1339 mii_tick(mii); 1340 1341 if (!sc->nge_link) { 1342 if (mii->mii_media_status & IFM_ACTIVE && 1343 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 1344 sc->nge_link++; 1345 if (IFM_SUBTYPE(mii->mii_media_active) 1346 == IFM_1000_T && bootverbose) 1347 printf("nge%d: gigabit link up\n", 1348 sc->nge_unit); 1349 if (ifp->if_snd.ifq_head != NULL) 1350 nge_start_locked(ifp); 1351 } 1352 } 1353 } 1354 callout_reset(&sc->nge_stat_ch, hz, nge_tick, sc); 1355 1356 return; 1357 } 1358 1359 #ifdef DEVICE_POLLING 1360 static poll_handler_t nge_poll; 1361 1362 static void 1363 nge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 1364 { 1365 struct nge_softc *sc = ifp->if_softc; 1366 1367 NGE_LOCK(sc); 1368 if (!(ifp->if_capenable & IFCAP_POLLING)) { 1369 ether_poll_deregister(ifp); 1370 cmd = POLL_DEREGISTER; 1371 } 1372 if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */ 1373 CSR_WRITE_4(sc, NGE_IER, 1); 1374 NGE_UNLOCK(sc); 1375 return; 1376 } 1377 1378 /* 1379 * On the nge, reading the status register also clears it. 1380 * So before returning to intr mode we must make sure that all 1381 * possible pending sources of interrupts have been served. 1382 * In practice this means run to completion the *eof routines, 1383 * and then call the interrupt routine 1384 */ 1385 sc->rxcycles = count; 1386 nge_rxeof(sc); 1387 nge_txeof(sc); 1388 if (ifp->if_snd.ifq_head != NULL) 1389 nge_start_locked(ifp); 1390 1391 if (sc->rxcycles > 0 || cmd == POLL_AND_CHECK_STATUS) { 1392 u_int32_t status; 1393 1394 /* Reading the ISR register clears all interrupts. */ 1395 status = CSR_READ_4(sc, NGE_ISR); 1396 1397 if (status & (NGE_ISR_RX_ERR|NGE_ISR_RX_OFLOW)) 1398 nge_rxeof(sc); 1399 1400 if (status & (NGE_ISR_RX_IDLE)) 1401 NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE); 1402 1403 if (status & NGE_ISR_SYSERR) { 1404 nge_reset(sc); 1405 nge_init_locked(sc); 1406 } 1407 } 1408 NGE_UNLOCK(sc); 1409 } 1410 #endif /* DEVICE_POLLING */ 1411 1412 static void 1413 nge_intr(arg) 1414 void *arg; 1415 { 1416 struct nge_softc *sc; 1417 struct ifnet *ifp; 1418 u_int32_t status; 1419 1420 sc = arg; 1421 ifp = &sc->arpcom.ac_if; 1422 1423 NGE_LOCK(sc); 1424 #ifdef DEVICE_POLLING 1425 if (ifp->if_flags & IFF_POLLING) { 1426 NGE_UNLOCK(sc); 1427 return; 1428 } 1429 if ((ifp->if_capenable & IFCAP_POLLING) && 1430 ether_poll_register(nge_poll, ifp)) { /* ok, disable interrupts */ 1431 CSR_WRITE_4(sc, NGE_IER, 0); 1432 NGE_UNLOCK(sc); 1433 nge_poll(ifp, 0, 1); 1434 return; 1435 } 1436 #endif /* DEVICE_POLLING */ 1437 1438 /* Supress unwanted interrupts */ 1439 if (!(ifp->if_flags & IFF_UP)) { 1440 nge_stop(sc); 1441 NGE_UNLOCK(sc); 1442 return; 1443 } 1444 1445 /* Disable interrupts. */ 1446 CSR_WRITE_4(sc, NGE_IER, 0); 1447 1448 /* Data LED on for TBI mode */ 1449 if(sc->nge_tbi) 1450 CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO) 1451 | NGE_GPIO_GP3_OUT); 1452 1453 for (;;) { 1454 /* Reading the ISR register clears all interrupts. */ 1455 status = CSR_READ_4(sc, NGE_ISR); 1456 1457 if ((status & NGE_INTRS) == 0) 1458 break; 1459 1460 if ((status & NGE_ISR_TX_DESC_OK) || 1461 (status & NGE_ISR_TX_ERR) || 1462 (status & NGE_ISR_TX_OK) || 1463 (status & NGE_ISR_TX_IDLE)) 1464 nge_txeof(sc); 1465 1466 if ((status & NGE_ISR_RX_DESC_OK) || 1467 (status & NGE_ISR_RX_ERR) || 1468 (status & NGE_ISR_RX_OFLOW) || 1469 (status & NGE_ISR_RX_FIFO_OFLOW) || 1470 (status & NGE_ISR_RX_IDLE) || 1471 (status & NGE_ISR_RX_OK)) 1472 nge_rxeof(sc); 1473 1474 if ((status & NGE_ISR_RX_IDLE)) 1475 NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE); 1476 1477 if (status & NGE_ISR_SYSERR) { 1478 nge_reset(sc); 1479 ifp->if_flags &= ~IFF_RUNNING; 1480 nge_init_locked(sc); 1481 } 1482 1483 #if 0 1484 /* 1485 * XXX: nge_tick() is not ready to be called this way 1486 * it screws up the aneg timeout because mii_tick() is 1487 * only to be called once per second. 1488 */ 1489 if (status & NGE_IMR_PHY_INTR) { 1490 sc->nge_link = 0; 1491 nge_tick_locked(sc); 1492 } 1493 #endif 1494 } 1495 1496 /* Re-enable interrupts. */ 1497 CSR_WRITE_4(sc, NGE_IER, 1); 1498 1499 if (ifp->if_snd.ifq_head != NULL) 1500 nge_start_locked(ifp); 1501 1502 /* Data LED off for TBI mode */ 1503 1504 if(sc->nge_tbi) 1505 CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO) 1506 & ~NGE_GPIO_GP3_OUT); 1507 1508 NGE_UNLOCK(sc); 1509 1510 return; 1511 } 1512 1513 /* 1514 * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data 1515 * pointers to the fragment pointers. 1516 */ 1517 static int 1518 nge_encap(sc, m_head, txidx) 1519 struct nge_softc *sc; 1520 struct mbuf *m_head; 1521 u_int32_t *txidx; 1522 { 1523 struct nge_desc *f = NULL; 1524 struct mbuf *m; 1525 int frag, cur, cnt = 0; 1526 struct m_tag *mtag; 1527 1528 /* 1529 * Start packing the mbufs in this chain into 1530 * the fragment pointers. Stop when we run out 1531 * of fragments or hit the end of the mbuf chain. 1532 */ 1533 m = m_head; 1534 cur = frag = *txidx; 1535 1536 for (m = m_head; m != NULL; m = m->m_next) { 1537 if (m->m_len != 0) { 1538 if ((NGE_TX_LIST_CNT - 1539 (sc->nge_cdata.nge_tx_cnt + cnt)) < 2) 1540 return(ENOBUFS); 1541 f = &sc->nge_ldata->nge_tx_list[frag]; 1542 f->nge_ctl = NGE_CMDSTS_MORE | m->m_len; 1543 f->nge_ptr = vtophys(mtod(m, vm_offset_t)); 1544 if (cnt != 0) 1545 f->nge_ctl |= NGE_CMDSTS_OWN; 1546 cur = frag; 1547 NGE_INC(frag, NGE_TX_LIST_CNT); 1548 cnt++; 1549 } 1550 } 1551 1552 if (m != NULL) 1553 return(ENOBUFS); 1554 1555 sc->nge_ldata->nge_tx_list[*txidx].nge_extsts = 0; 1556 if (m_head->m_pkthdr.csum_flags) { 1557 if (m_head->m_pkthdr.csum_flags & CSUM_IP) 1558 sc->nge_ldata->nge_tx_list[*txidx].nge_extsts |= 1559 NGE_TXEXTSTS_IPCSUM; 1560 if (m_head->m_pkthdr.csum_flags & CSUM_TCP) 1561 sc->nge_ldata->nge_tx_list[*txidx].nge_extsts |= 1562 NGE_TXEXTSTS_TCPCSUM; 1563 if (m_head->m_pkthdr.csum_flags & CSUM_UDP) 1564 sc->nge_ldata->nge_tx_list[*txidx].nge_extsts |= 1565 NGE_TXEXTSTS_UDPCSUM; 1566 } 1567 1568 mtag = VLAN_OUTPUT_TAG(&sc->arpcom.ac_if, m_head); 1569 if (mtag != NULL) { 1570 sc->nge_ldata->nge_tx_list[cur].nge_extsts |= 1571 (NGE_TXEXTSTS_VLANPKT|htons(VLAN_TAG_VALUE(mtag))); 1572 } 1573 1574 sc->nge_ldata->nge_tx_list[cur].nge_mbuf = m_head; 1575 sc->nge_ldata->nge_tx_list[cur].nge_ctl &= ~NGE_CMDSTS_MORE; 1576 sc->nge_ldata->nge_tx_list[*txidx].nge_ctl |= NGE_CMDSTS_OWN; 1577 sc->nge_cdata.nge_tx_cnt += cnt; 1578 *txidx = frag; 1579 1580 return(0); 1581 } 1582 1583 /* 1584 * Main transmit routine. To avoid having to do mbuf copies, we put pointers 1585 * to the mbuf data regions directly in the transmit lists. We also save a 1586 * copy of the pointers since the transmit list fragment pointers are 1587 * physical addresses. 1588 */ 1589 1590 static void 1591 nge_start(ifp) 1592 struct ifnet *ifp; 1593 { 1594 struct nge_softc *sc; 1595 1596 sc = ifp->if_softc; 1597 NGE_LOCK(sc); 1598 nge_start_locked(ifp); 1599 NGE_UNLOCK(sc); 1600 } 1601 1602 static void 1603 nge_start_locked(ifp) 1604 struct ifnet *ifp; 1605 { 1606 struct nge_softc *sc; 1607 struct mbuf *m_head = NULL; 1608 u_int32_t idx; 1609 1610 sc = ifp->if_softc; 1611 1612 if (!sc->nge_link) 1613 return; 1614 1615 idx = sc->nge_cdata.nge_tx_prod; 1616 1617 if (ifp->if_flags & IFF_OACTIVE) 1618 return; 1619 1620 while(sc->nge_ldata->nge_tx_list[idx].nge_mbuf == NULL) { 1621 IF_DEQUEUE(&ifp->if_snd, m_head); 1622 if (m_head == NULL) 1623 break; 1624 1625 if (nge_encap(sc, m_head, &idx)) { 1626 IF_PREPEND(&ifp->if_snd, m_head); 1627 ifp->if_flags |= IFF_OACTIVE; 1628 break; 1629 } 1630 1631 /* 1632 * If there's a BPF listener, bounce a copy of this frame 1633 * to him. 1634 */ 1635 BPF_MTAP(ifp, m_head); 1636 1637 } 1638 1639 /* Transmit */ 1640 sc->nge_cdata.nge_tx_prod = idx; 1641 NGE_SETBIT(sc, NGE_CSR, NGE_CSR_TX_ENABLE); 1642 1643 /* 1644 * Set a timeout in case the chip goes out to lunch. 1645 */ 1646 ifp->if_timer = 5; 1647 1648 return; 1649 } 1650 1651 static void 1652 nge_init(xsc) 1653 void *xsc; 1654 { 1655 struct nge_softc *sc = xsc; 1656 1657 NGE_LOCK(sc); 1658 nge_init_locked(sc); 1659 NGE_UNLOCK(sc); 1660 } 1661 1662 static void 1663 nge_init_locked(sc) 1664 struct nge_softc *sc; 1665 { 1666 struct ifnet *ifp = &sc->arpcom.ac_if; 1667 struct mii_data *mii; 1668 1669 NGE_LOCK_ASSERT(sc); 1670 1671 if (ifp->if_flags & IFF_RUNNING) 1672 return; 1673 1674 /* 1675 * Cancel pending I/O and free all RX/TX buffers. 1676 */ 1677 nge_stop(sc); 1678 1679 if (sc->nge_tbi) { 1680 mii = NULL; 1681 } else { 1682 mii = device_get_softc(sc->nge_miibus); 1683 } 1684 1685 /* Set MAC address */ 1686 CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR0); 1687 CSR_WRITE_4(sc, NGE_RXFILT_DATA, 1688 ((u_int16_t *)sc->arpcom.ac_enaddr)[0]); 1689 CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR1); 1690 CSR_WRITE_4(sc, NGE_RXFILT_DATA, 1691 ((u_int16_t *)sc->arpcom.ac_enaddr)[1]); 1692 CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR2); 1693 CSR_WRITE_4(sc, NGE_RXFILT_DATA, 1694 ((u_int16_t *)sc->arpcom.ac_enaddr)[2]); 1695 1696 /* Init circular RX list. */ 1697 if (nge_list_rx_init(sc) == ENOBUFS) { 1698 printf("nge%d: initialization failed: no " 1699 "memory for rx buffers\n", sc->nge_unit); 1700 nge_stop(sc); 1701 return; 1702 } 1703 1704 /* 1705 * Init tx descriptors. 1706 */ 1707 nge_list_tx_init(sc); 1708 1709 /* 1710 * For the NatSemi chip, we have to explicitly enable the 1711 * reception of ARP frames, as well as turn on the 'perfect 1712 * match' filter where we store the station address, otherwise 1713 * we won't receive unicasts meant for this host. 1714 */ 1715 NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ARP); 1716 NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_PERFECT); 1717 1718 /* If we want promiscuous mode, set the allframes bit. */ 1719 if (ifp->if_flags & IFF_PROMISC) { 1720 NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ALLPHYS); 1721 } else { 1722 NGE_CLRBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ALLPHYS); 1723 } 1724 1725 /* 1726 * Set the capture broadcast bit to capture broadcast frames. 1727 */ 1728 if (ifp->if_flags & IFF_BROADCAST) { 1729 NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_BROAD); 1730 } else { 1731 NGE_CLRBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_BROAD); 1732 } 1733 1734 /* 1735 * Load the multicast filter. 1736 */ 1737 nge_setmulti(sc); 1738 1739 /* Turn the receive filter on */ 1740 NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ENABLE); 1741 1742 /* 1743 * Load the address of the RX and TX lists. 1744 */ 1745 CSR_WRITE_4(sc, NGE_RX_LISTPTR, 1746 vtophys(&sc->nge_ldata->nge_rx_list[0])); 1747 CSR_WRITE_4(sc, NGE_TX_LISTPTR, 1748 vtophys(&sc->nge_ldata->nge_tx_list[0])); 1749 1750 /* Set RX configuration */ 1751 CSR_WRITE_4(sc, NGE_RX_CFG, NGE_RXCFG); 1752 /* 1753 * Enable hardware checksum validation for all IPv4 1754 * packets, do not reject packets with bad checksums. 1755 */ 1756 CSR_WRITE_4(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_IPCSUM_ENB); 1757 1758 /* 1759 * Tell the chip to detect and strip VLAN tag info from 1760 * received frames. The tag will be provided in the extsts 1761 * field in the RX descriptors. 1762 */ 1763 NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL, 1764 NGE_VIPRXCTL_TAG_DETECT_ENB|NGE_VIPRXCTL_TAG_STRIP_ENB); 1765 1766 /* Set TX configuration */ 1767 CSR_WRITE_4(sc, NGE_TX_CFG, NGE_TXCFG); 1768 1769 /* 1770 * Enable TX IPv4 checksumming on a per-packet basis. 1771 */ 1772 CSR_WRITE_4(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_CSUM_PER_PKT); 1773 1774 /* 1775 * Tell the chip to insert VLAN tags on a per-packet basis as 1776 * dictated by the code in the frame encapsulation routine. 1777 */ 1778 NGE_SETBIT(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_TAG_PER_PKT); 1779 1780 /* Set full/half duplex mode. */ 1781 if (sc->nge_tbi) { 1782 if ((sc->nge_ifmedia.ifm_cur->ifm_media & IFM_GMASK) 1783 == IFM_FDX) { 1784 NGE_SETBIT(sc, NGE_TX_CFG, 1785 (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR)); 1786 NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX); 1787 } else { 1788 NGE_CLRBIT(sc, NGE_TX_CFG, 1789 (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR)); 1790 NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX); 1791 } 1792 } else { 1793 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) { 1794 NGE_SETBIT(sc, NGE_TX_CFG, 1795 (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR)); 1796 NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX); 1797 } else { 1798 NGE_CLRBIT(sc, NGE_TX_CFG, 1799 (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR)); 1800 NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX); 1801 } 1802 } 1803 1804 nge_tick_locked(sc); 1805 1806 /* 1807 * Enable the delivery of PHY interrupts based on 1808 * link/speed/duplex status changes. Also enable the 1809 * extsts field in the DMA descriptors (needed for 1810 * TCP/IP checksum offload on transmit). 1811 */ 1812 NGE_SETBIT(sc, NGE_CFG, NGE_CFG_PHYINTR_SPD| 1813 NGE_CFG_PHYINTR_LNK|NGE_CFG_PHYINTR_DUP|NGE_CFG_EXTSTS_ENB); 1814 1815 /* 1816 * Configure interrupt holdoff (moderation). We can 1817 * have the chip delay interrupt delivery for a certain 1818 * period. Units are in 100us, and the max setting 1819 * is 25500us (0xFF x 100us). Default is a 100us holdoff. 1820 */ 1821 CSR_WRITE_4(sc, NGE_IHR, 0x01); 1822 1823 /* 1824 * Enable interrupts. 1825 */ 1826 CSR_WRITE_4(sc, NGE_IMR, NGE_INTRS); 1827 #ifdef DEVICE_POLLING 1828 /* 1829 * ... only enable interrupts if we are not polling, make sure 1830 * they are off otherwise. 1831 */ 1832 if (ifp->if_flags & IFF_POLLING) 1833 CSR_WRITE_4(sc, NGE_IER, 0); 1834 else 1835 #endif /* DEVICE_POLLING */ 1836 CSR_WRITE_4(sc, NGE_IER, 1); 1837 1838 /* Enable receiver and transmitter. */ 1839 NGE_CLRBIT(sc, NGE_CSR, NGE_CSR_TX_DISABLE|NGE_CSR_RX_DISABLE); 1840 NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE); 1841 1842 nge_ifmedia_upd(ifp); 1843 1844 ifp->if_flags |= IFF_RUNNING; 1845 ifp->if_flags &= ~IFF_OACTIVE; 1846 1847 return; 1848 } 1849 1850 /* 1851 * Set media options. 1852 */ 1853 static int 1854 nge_ifmedia_upd(ifp) 1855 struct ifnet *ifp; 1856 { 1857 struct nge_softc *sc; 1858 struct mii_data *mii; 1859 1860 sc = ifp->if_softc; 1861 1862 if (sc->nge_tbi) { 1863 if (IFM_SUBTYPE(sc->nge_ifmedia.ifm_cur->ifm_media) 1864 == IFM_AUTO) { 1865 CSR_WRITE_4(sc, NGE_TBI_ANAR, 1866 CSR_READ_4(sc, NGE_TBI_ANAR) 1867 | NGE_TBIANAR_HDX | NGE_TBIANAR_FDX 1868 | NGE_TBIANAR_PS1 | NGE_TBIANAR_PS2); 1869 CSR_WRITE_4(sc, NGE_TBI_BMCR, NGE_TBIBMCR_ENABLE_ANEG 1870 | NGE_TBIBMCR_RESTART_ANEG); 1871 CSR_WRITE_4(sc, NGE_TBI_BMCR, NGE_TBIBMCR_ENABLE_ANEG); 1872 } else if ((sc->nge_ifmedia.ifm_cur->ifm_media 1873 & IFM_GMASK) == IFM_FDX) { 1874 NGE_SETBIT(sc, NGE_TX_CFG, 1875 (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR)); 1876 NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX); 1877 1878 CSR_WRITE_4(sc, NGE_TBI_ANAR, 0); 1879 CSR_WRITE_4(sc, NGE_TBI_BMCR, 0); 1880 } else { 1881 NGE_CLRBIT(sc, NGE_TX_CFG, 1882 (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR)); 1883 NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX); 1884 1885 CSR_WRITE_4(sc, NGE_TBI_ANAR, 0); 1886 CSR_WRITE_4(sc, NGE_TBI_BMCR, 0); 1887 } 1888 1889 CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO) 1890 & ~NGE_GPIO_GP3_OUT); 1891 } else { 1892 mii = device_get_softc(sc->nge_miibus); 1893 sc->nge_link = 0; 1894 if (mii->mii_instance) { 1895 struct mii_softc *miisc; 1896 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL; 1897 miisc = LIST_NEXT(miisc, mii_list)) 1898 mii_phy_reset(miisc); 1899 } 1900 mii_mediachg(mii); 1901 } 1902 1903 return(0); 1904 } 1905 1906 /* 1907 * Report current media status. 1908 */ 1909 static void 1910 nge_ifmedia_sts(ifp, ifmr) 1911 struct ifnet *ifp; 1912 struct ifmediareq *ifmr; 1913 { 1914 struct nge_softc *sc; 1915 struct mii_data *mii; 1916 1917 sc = ifp->if_softc; 1918 1919 if (sc->nge_tbi) { 1920 ifmr->ifm_status = IFM_AVALID; 1921 ifmr->ifm_active = IFM_ETHER; 1922 1923 if (CSR_READ_4(sc, NGE_TBI_BMSR) & NGE_TBIBMSR_ANEG_DONE) { 1924 ifmr->ifm_status |= IFM_ACTIVE; 1925 } 1926 if (CSR_READ_4(sc, NGE_TBI_BMCR) & NGE_TBIBMCR_LOOPBACK) 1927 ifmr->ifm_active |= IFM_LOOP; 1928 if (!CSR_READ_4(sc, NGE_TBI_BMSR) & NGE_TBIBMSR_ANEG_DONE) { 1929 ifmr->ifm_active |= IFM_NONE; 1930 ifmr->ifm_status = 0; 1931 return; 1932 } 1933 ifmr->ifm_active |= IFM_1000_SX; 1934 if (IFM_SUBTYPE(sc->nge_ifmedia.ifm_cur->ifm_media) 1935 == IFM_AUTO) { 1936 ifmr->ifm_active |= IFM_AUTO; 1937 if (CSR_READ_4(sc, NGE_TBI_ANLPAR) 1938 & NGE_TBIANAR_FDX) { 1939 ifmr->ifm_active |= IFM_FDX; 1940 }else if (CSR_READ_4(sc, NGE_TBI_ANLPAR) 1941 & NGE_TBIANAR_HDX) { 1942 ifmr->ifm_active |= IFM_HDX; 1943 } 1944 } else if ((sc->nge_ifmedia.ifm_cur->ifm_media & IFM_GMASK) 1945 == IFM_FDX) 1946 ifmr->ifm_active |= IFM_FDX; 1947 else 1948 ifmr->ifm_active |= IFM_HDX; 1949 1950 } else { 1951 mii = device_get_softc(sc->nge_miibus); 1952 mii_pollstat(mii); 1953 ifmr->ifm_active = mii->mii_media_active; 1954 ifmr->ifm_status = mii->mii_media_status; 1955 } 1956 1957 return; 1958 } 1959 1960 static int 1961 nge_ioctl(ifp, command, data) 1962 struct ifnet *ifp; 1963 u_long command; 1964 caddr_t data; 1965 { 1966 struct nge_softc *sc = ifp->if_softc; 1967 struct ifreq *ifr = (struct ifreq *) data; 1968 struct mii_data *mii; 1969 int error = 0; 1970 1971 switch(command) { 1972 case SIOCSIFMTU: 1973 if (ifr->ifr_mtu > NGE_JUMBO_MTU) 1974 error = EINVAL; 1975 else { 1976 ifp->if_mtu = ifr->ifr_mtu; 1977 /* 1978 * Workaround: if the MTU is larger than 1979 * 8152 (TX FIFO size minus 64 minus 18), turn off 1980 * TX checksum offloading. 1981 */ 1982 if (ifr->ifr_mtu >= 8152) { 1983 ifp->if_capenable &= ~IFCAP_TXCSUM; 1984 ifp->if_hwassist = 0; 1985 } else { 1986 ifp->if_capenable |= IFCAP_TXCSUM; 1987 ifp->if_hwassist = NGE_CSUM_FEATURES; 1988 } 1989 } 1990 break; 1991 case SIOCSIFFLAGS: 1992 NGE_LOCK(sc); 1993 if (ifp->if_flags & IFF_UP) { 1994 if (ifp->if_flags & IFF_RUNNING && 1995 ifp->if_flags & IFF_PROMISC && 1996 !(sc->nge_if_flags & IFF_PROMISC)) { 1997 NGE_SETBIT(sc, NGE_RXFILT_CTL, 1998 NGE_RXFILTCTL_ALLPHYS| 1999 NGE_RXFILTCTL_ALLMULTI); 2000 } else if (ifp->if_flags & IFF_RUNNING && 2001 !(ifp->if_flags & IFF_PROMISC) && 2002 sc->nge_if_flags & IFF_PROMISC) { 2003 NGE_CLRBIT(sc, NGE_RXFILT_CTL, 2004 NGE_RXFILTCTL_ALLPHYS); 2005 if (!(ifp->if_flags & IFF_ALLMULTI)) 2006 NGE_CLRBIT(sc, NGE_RXFILT_CTL, 2007 NGE_RXFILTCTL_ALLMULTI); 2008 } else { 2009 ifp->if_flags &= ~IFF_RUNNING; 2010 nge_init_locked(sc); 2011 } 2012 } else { 2013 if (ifp->if_flags & IFF_RUNNING) 2014 nge_stop(sc); 2015 } 2016 sc->nge_if_flags = ifp->if_flags; 2017 NGE_UNLOCK(sc); 2018 error = 0; 2019 break; 2020 case SIOCADDMULTI: 2021 case SIOCDELMULTI: 2022 NGE_LOCK(sc); 2023 nge_setmulti(sc); 2024 NGE_UNLOCK(sc); 2025 error = 0; 2026 break; 2027 case SIOCGIFMEDIA: 2028 case SIOCSIFMEDIA: 2029 if (sc->nge_tbi) { 2030 error = ifmedia_ioctl(ifp, ifr, &sc->nge_ifmedia, 2031 command); 2032 } else { 2033 mii = device_get_softc(sc->nge_miibus); 2034 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, 2035 command); 2036 } 2037 break; 2038 case SIOCSIFCAP: 2039 ifp->if_capenable &= ~IFCAP_POLLING; 2040 ifp->if_capenable |= ifr->ifr_reqcap & IFCAP_POLLING; 2041 break; 2042 default: 2043 error = ether_ioctl(ifp, command, data); 2044 break; 2045 } 2046 2047 return(error); 2048 } 2049 2050 static void 2051 nge_watchdog(ifp) 2052 struct ifnet *ifp; 2053 { 2054 struct nge_softc *sc; 2055 2056 sc = ifp->if_softc; 2057 2058 ifp->if_oerrors++; 2059 printf("nge%d: watchdog timeout\n", sc->nge_unit); 2060 2061 NGE_LOCK(sc); 2062 nge_stop(sc); 2063 nge_reset(sc); 2064 ifp->if_flags &= ~IFF_RUNNING; 2065 nge_init_locked(sc); 2066 2067 if (ifp->if_snd.ifq_head != NULL) 2068 nge_start_locked(ifp); 2069 2070 NGE_UNLOCK(sc); 2071 2072 return; 2073 } 2074 2075 /* 2076 * Stop the adapter and free any mbufs allocated to the 2077 * RX and TX lists. 2078 */ 2079 static void 2080 nge_stop(sc) 2081 struct nge_softc *sc; 2082 { 2083 register int i; 2084 struct ifnet *ifp; 2085 struct mii_data *mii; 2086 2087 NGE_LOCK_ASSERT(sc); 2088 ifp = &sc->arpcom.ac_if; 2089 ifp->if_timer = 0; 2090 if (sc->nge_tbi) { 2091 mii = NULL; 2092 } else { 2093 mii = device_get_softc(sc->nge_miibus); 2094 } 2095 2096 callout_stop(&sc->nge_stat_ch); 2097 #ifdef DEVICE_POLLING 2098 ether_poll_deregister(ifp); 2099 #endif 2100 CSR_WRITE_4(sc, NGE_IER, 0); 2101 CSR_WRITE_4(sc, NGE_IMR, 0); 2102 NGE_SETBIT(sc, NGE_CSR, NGE_CSR_TX_DISABLE|NGE_CSR_RX_DISABLE); 2103 DELAY(1000); 2104 CSR_WRITE_4(sc, NGE_TX_LISTPTR, 0); 2105 CSR_WRITE_4(sc, NGE_RX_LISTPTR, 0); 2106 2107 if (!sc->nge_tbi) 2108 mii_down(mii); 2109 2110 sc->nge_link = 0; 2111 2112 /* 2113 * Free data in the RX lists. 2114 */ 2115 for (i = 0; i < NGE_RX_LIST_CNT; i++) { 2116 if (sc->nge_ldata->nge_rx_list[i].nge_mbuf != NULL) { 2117 m_freem(sc->nge_ldata->nge_rx_list[i].nge_mbuf); 2118 sc->nge_ldata->nge_rx_list[i].nge_mbuf = NULL; 2119 } 2120 } 2121 bzero((char *)&sc->nge_ldata->nge_rx_list, 2122 sizeof(sc->nge_ldata->nge_rx_list)); 2123 2124 /* 2125 * Free the TX list buffers. 2126 */ 2127 for (i = 0; i < NGE_TX_LIST_CNT; i++) { 2128 if (sc->nge_ldata->nge_tx_list[i].nge_mbuf != NULL) { 2129 m_freem(sc->nge_ldata->nge_tx_list[i].nge_mbuf); 2130 sc->nge_ldata->nge_tx_list[i].nge_mbuf = NULL; 2131 } 2132 } 2133 2134 bzero((char *)&sc->nge_ldata->nge_tx_list, 2135 sizeof(sc->nge_ldata->nge_tx_list)); 2136 2137 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 2138 2139 return; 2140 } 2141 2142 /* 2143 * Stop all chip I/O so that the kernel's probe routines don't 2144 * get confused by errant DMAs when rebooting. 2145 */ 2146 static void 2147 nge_shutdown(dev) 2148 device_t dev; 2149 { 2150 struct nge_softc *sc; 2151 2152 sc = device_get_softc(dev); 2153 2154 NGE_LOCK(sc); 2155 nge_reset(sc); 2156 nge_stop(sc); 2157 NGE_UNLOCK(sc); 2158 2159 return; 2160 } 2161