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