if_wb.c revision 151545
1/*- 2 * Copyright (c) 1997, 1998 3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Bill Paul. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33#include <sys/cdefs.h> 34__FBSDID("$FreeBSD: head/sys/pci/if_wb.c 151545 2005-10-22 05:06:55Z imp $"); 35 36/* 37 * Winbond fast ethernet PCI NIC driver 38 * 39 * Supports various cheap network adapters based on the Winbond W89C840F 40 * fast ethernet controller chip. This includes adapters manufactured by 41 * Winbond itself and some made by Linksys. 42 * 43 * Written by Bill Paul <wpaul@ctr.columbia.edu> 44 * Electrical Engineering Department 45 * Columbia University, New York City 46 */ 47/* 48 * The Winbond W89C840F chip is a bus master; in some ways it resembles 49 * a DEC 'tulip' chip, only not as complicated. Unfortunately, it has 50 * one major difference which is that while the registers do many of 51 * the same things as a tulip adapter, the offsets are different: where 52 * tulip registers are typically spaced 8 bytes apart, the Winbond 53 * registers are spaced 4 bytes apart. The receiver filter is also 54 * programmed differently. 55 * 56 * Like the tulip, the Winbond chip uses small descriptors containing 57 * a status word, a control word and 32-bit areas that can either be used 58 * to point to two external data blocks, or to point to a single block 59 * and another descriptor in a linked list. Descriptors can be grouped 60 * together in blocks to form fixed length rings or can be chained 61 * together in linked lists. A single packet may be spread out over 62 * several descriptors if necessary. 63 * 64 * For the receive ring, this driver uses a linked list of descriptors, 65 * each pointing to a single mbuf cluster buffer, which us large enough 66 * to hold an entire packet. The link list is looped back to created a 67 * closed ring. 68 * 69 * For transmission, the driver creates a linked list of 'super descriptors' 70 * which each contain several individual descriptors linked toghether. 71 * Each 'super descriptor' contains WB_MAXFRAGS descriptors, which we 72 * abuse as fragment pointers. This allows us to use a buffer managment 73 * scheme very similar to that used in the ThunderLAN and Etherlink XL 74 * drivers. 75 * 76 * Autonegotiation is performed using the external PHY via the MII bus. 77 * The sample boards I have all use a Davicom PHY. 78 * 79 * Note: the author of the Linux driver for the Winbond chip alludes 80 * to some sort of flaw in the chip's design that seems to mandate some 81 * drastic workaround which signigicantly impairs transmit performance. 82 * I have no idea what he's on about: transmit performance with all 83 * three of my test boards seems fine. 84 */ 85 86#include <sys/param.h> 87#include <sys/systm.h> 88#include <sys/sockio.h> 89#include <sys/mbuf.h> 90#include <sys/malloc.h> 91#include <sys/module.h> 92#include <sys/kernel.h> 93#include <sys/socket.h> 94#include <sys/queue.h> 95 96#include <net/if.h> 97#include <net/if_arp.h> 98#include <net/ethernet.h> 99#include <net/if_dl.h> 100#include <net/if_media.h> 101#include <net/if_types.h> 102 103#include <net/bpf.h> 104 105#include <vm/vm.h> /* for vtophys */ 106#include <vm/pmap.h> /* for vtophys */ 107#include <machine/bus.h> 108#include <machine/resource.h> 109#include <sys/bus.h> 110#include <sys/rman.h> 111 112#include <dev/pci/pcireg.h> 113#include <dev/pci/pcivar.h> 114 115#include <dev/mii/mii.h> 116#include <dev/mii/miivar.h> 117 118/* "device miibus" required. See GENERIC if you get errors here. */ 119#include "miibus_if.h" 120 121#define WB_USEIOSPACE 122 123#include <pci/if_wbreg.h> 124 125MODULE_DEPEND(wb, pci, 1, 1, 1); 126MODULE_DEPEND(wb, ether, 1, 1, 1); 127MODULE_DEPEND(wb, miibus, 1, 1, 1); 128 129/* 130 * Various supported device vendors/types and their names. 131 */ 132static struct wb_type wb_devs[] = { 133 { WB_VENDORID, WB_DEVICEID_840F, 134 "Winbond W89C840F 10/100BaseTX" }, 135 { CP_VENDORID, CP_DEVICEID_RL100, 136 "Compex RL100-ATX 10/100baseTX" }, 137 { 0, 0, NULL } 138}; 139 140static int wb_probe(device_t); 141static int wb_attach(device_t); 142static int wb_detach(device_t); 143 144static void wb_bfree(void *addr, void *args); 145static int wb_newbuf(struct wb_softc *, struct wb_chain_onefrag *, 146 struct mbuf *); 147static int wb_encap(struct wb_softc *, struct wb_chain *, struct mbuf *); 148 149static void wb_rxeof(struct wb_softc *); 150static void wb_rxeoc(struct wb_softc *); 151static void wb_txeof(struct wb_softc *); 152static void wb_txeoc(struct wb_softc *); 153static void wb_intr(void *); 154static void wb_tick(void *); 155static void wb_start(struct ifnet *); 156static int wb_ioctl(struct ifnet *, u_long, caddr_t); 157static void wb_init(void *); 158static void wb_stop(struct wb_softc *); 159static void wb_watchdog(struct ifnet *); 160static void wb_shutdown(device_t); 161static int wb_ifmedia_upd(struct ifnet *); 162static void wb_ifmedia_sts(struct ifnet *, struct ifmediareq *); 163 164static void wb_eeprom_putbyte(struct wb_softc *, int); 165static void wb_eeprom_getword(struct wb_softc *, int, u_int16_t *); 166static void wb_read_eeprom(struct wb_softc *, caddr_t, int, int, int); 167static void wb_mii_sync(struct wb_softc *); 168static void wb_mii_send(struct wb_softc *, u_int32_t, int); 169static int wb_mii_readreg(struct wb_softc *, struct wb_mii_frame *); 170static int wb_mii_writereg(struct wb_softc *, struct wb_mii_frame *); 171 172static void wb_setcfg(struct wb_softc *, u_int32_t); 173static void wb_setmulti(struct wb_softc *); 174static void wb_reset(struct wb_softc *); 175static void wb_fixmedia(struct wb_softc *); 176static int wb_list_rx_init(struct wb_softc *); 177static int wb_list_tx_init(struct wb_softc *); 178 179static int wb_miibus_readreg(device_t, int, int); 180static int wb_miibus_writereg(device_t, int, int, int); 181static void wb_miibus_statchg(device_t); 182 183#ifdef WB_USEIOSPACE 184#define WB_RES SYS_RES_IOPORT 185#define WB_RID WB_PCI_LOIO 186#else 187#define WB_RES SYS_RES_MEMORY 188#define WB_RID WB_PCI_LOMEM 189#endif 190 191static device_method_t wb_methods[] = { 192 /* Device interface */ 193 DEVMETHOD(device_probe, wb_probe), 194 DEVMETHOD(device_attach, wb_attach), 195 DEVMETHOD(device_detach, wb_detach), 196 DEVMETHOD(device_shutdown, wb_shutdown), 197 198 /* bus interface, for miibus */ 199 DEVMETHOD(bus_print_child, bus_generic_print_child), 200 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 201 202 /* MII interface */ 203 DEVMETHOD(miibus_readreg, wb_miibus_readreg), 204 DEVMETHOD(miibus_writereg, wb_miibus_writereg), 205 DEVMETHOD(miibus_statchg, wb_miibus_statchg), 206 { 0, 0 } 207}; 208 209static driver_t wb_driver = { 210 "wb", 211 wb_methods, 212 sizeof(struct wb_softc) 213}; 214 215static devclass_t wb_devclass; 216 217DRIVER_MODULE(wb, pci, wb_driver, wb_devclass, 0, 0); 218DRIVER_MODULE(miibus, wb, miibus_driver, miibus_devclass, 0, 0); 219 220#define WB_SETBIT(sc, reg, x) \ 221 CSR_WRITE_4(sc, reg, \ 222 CSR_READ_4(sc, reg) | (x)) 223 224#define WB_CLRBIT(sc, reg, x) \ 225 CSR_WRITE_4(sc, reg, \ 226 CSR_READ_4(sc, reg) & ~(x)) 227 228#define SIO_SET(x) \ 229 CSR_WRITE_4(sc, WB_SIO, \ 230 CSR_READ_4(sc, WB_SIO) | (x)) 231 232#define SIO_CLR(x) \ 233 CSR_WRITE_4(sc, WB_SIO, \ 234 CSR_READ_4(sc, WB_SIO) & ~(x)) 235 236/* 237 * Send a read command and address to the EEPROM, check for ACK. 238 */ 239static void 240wb_eeprom_putbyte(sc, addr) 241 struct wb_softc *sc; 242 int addr; 243{ 244 register int d, i; 245 246 d = addr | WB_EECMD_READ; 247 248 /* 249 * Feed in each bit and stobe the clock. 250 */ 251 for (i = 0x400; i; i >>= 1) { 252 if (d & i) { 253 SIO_SET(WB_SIO_EE_DATAIN); 254 } else { 255 SIO_CLR(WB_SIO_EE_DATAIN); 256 } 257 DELAY(100); 258 SIO_SET(WB_SIO_EE_CLK); 259 DELAY(150); 260 SIO_CLR(WB_SIO_EE_CLK); 261 DELAY(100); 262 } 263 264 return; 265} 266 267/* 268 * Read a word of data stored in the EEPROM at address 'addr.' 269 */ 270static void 271wb_eeprom_getword(sc, addr, dest) 272 struct wb_softc *sc; 273 int addr; 274 u_int16_t *dest; 275{ 276 register int i; 277 u_int16_t word = 0; 278 279 /* Enter EEPROM access mode. */ 280 CSR_WRITE_4(sc, WB_SIO, WB_SIO_EESEL|WB_SIO_EE_CS); 281 282 /* 283 * Send address of word we want to read. 284 */ 285 wb_eeprom_putbyte(sc, addr); 286 287 CSR_WRITE_4(sc, WB_SIO, WB_SIO_EESEL|WB_SIO_EE_CS); 288 289 /* 290 * Start reading bits from EEPROM. 291 */ 292 for (i = 0x8000; i; i >>= 1) { 293 SIO_SET(WB_SIO_EE_CLK); 294 DELAY(100); 295 if (CSR_READ_4(sc, WB_SIO) & WB_SIO_EE_DATAOUT) 296 word |= i; 297 SIO_CLR(WB_SIO_EE_CLK); 298 DELAY(100); 299 } 300 301 /* Turn off EEPROM access mode. */ 302 CSR_WRITE_4(sc, WB_SIO, 0); 303 304 *dest = word; 305 306 return; 307} 308 309/* 310 * Read a sequence of words from the EEPROM. 311 */ 312static void 313wb_read_eeprom(sc, dest, off, cnt, swap) 314 struct wb_softc *sc; 315 caddr_t dest; 316 int off; 317 int cnt; 318 int swap; 319{ 320 int i; 321 u_int16_t word = 0, *ptr; 322 323 for (i = 0; i < cnt; i++) { 324 wb_eeprom_getword(sc, off + i, &word); 325 ptr = (u_int16_t *)(dest + (i * 2)); 326 if (swap) 327 *ptr = ntohs(word); 328 else 329 *ptr = word; 330 } 331 332 return; 333} 334 335/* 336 * Sync the PHYs by setting data bit and strobing the clock 32 times. 337 */ 338static void 339wb_mii_sync(sc) 340 struct wb_softc *sc; 341{ 342 register int i; 343 344 SIO_SET(WB_SIO_MII_DIR|WB_SIO_MII_DATAIN); 345 346 for (i = 0; i < 32; i++) { 347 SIO_SET(WB_SIO_MII_CLK); 348 DELAY(1); 349 SIO_CLR(WB_SIO_MII_CLK); 350 DELAY(1); 351 } 352 353 return; 354} 355 356/* 357 * Clock a series of bits through the MII. 358 */ 359static void 360wb_mii_send(sc, bits, cnt) 361 struct wb_softc *sc; 362 u_int32_t bits; 363 int cnt; 364{ 365 int i; 366 367 SIO_CLR(WB_SIO_MII_CLK); 368 369 for (i = (0x1 << (cnt - 1)); i; i >>= 1) { 370 if (bits & i) { 371 SIO_SET(WB_SIO_MII_DATAIN); 372 } else { 373 SIO_CLR(WB_SIO_MII_DATAIN); 374 } 375 DELAY(1); 376 SIO_CLR(WB_SIO_MII_CLK); 377 DELAY(1); 378 SIO_SET(WB_SIO_MII_CLK); 379 } 380} 381 382/* 383 * Read an PHY register through the MII. 384 */ 385static int 386wb_mii_readreg(sc, frame) 387 struct wb_softc *sc; 388 struct wb_mii_frame *frame; 389 390{ 391 int i, ack; 392 393 WB_LOCK(sc); 394 395 /* 396 * Set up frame for RX. 397 */ 398 frame->mii_stdelim = WB_MII_STARTDELIM; 399 frame->mii_opcode = WB_MII_READOP; 400 frame->mii_turnaround = 0; 401 frame->mii_data = 0; 402 403 CSR_WRITE_4(sc, WB_SIO, 0); 404 405 /* 406 * Turn on data xmit. 407 */ 408 SIO_SET(WB_SIO_MII_DIR); 409 410 wb_mii_sync(sc); 411 412 /* 413 * Send command/address info. 414 */ 415 wb_mii_send(sc, frame->mii_stdelim, 2); 416 wb_mii_send(sc, frame->mii_opcode, 2); 417 wb_mii_send(sc, frame->mii_phyaddr, 5); 418 wb_mii_send(sc, frame->mii_regaddr, 5); 419 420 /* Idle bit */ 421 SIO_CLR((WB_SIO_MII_CLK|WB_SIO_MII_DATAIN)); 422 DELAY(1); 423 SIO_SET(WB_SIO_MII_CLK); 424 DELAY(1); 425 426 /* Turn off xmit. */ 427 SIO_CLR(WB_SIO_MII_DIR); 428 /* Check for ack */ 429 SIO_CLR(WB_SIO_MII_CLK); 430 DELAY(1); 431 ack = CSR_READ_4(sc, WB_SIO) & WB_SIO_MII_DATAOUT; 432 SIO_SET(WB_SIO_MII_CLK); 433 DELAY(1); 434 SIO_CLR(WB_SIO_MII_CLK); 435 DELAY(1); 436 SIO_SET(WB_SIO_MII_CLK); 437 DELAY(1); 438 439 /* 440 * Now try reading data bits. If the ack failed, we still 441 * need to clock through 16 cycles to keep the PHY(s) in sync. 442 */ 443 if (ack) { 444 for(i = 0; i < 16; i++) { 445 SIO_CLR(WB_SIO_MII_CLK); 446 DELAY(1); 447 SIO_SET(WB_SIO_MII_CLK); 448 DELAY(1); 449 } 450 goto fail; 451 } 452 453 for (i = 0x8000; i; i >>= 1) { 454 SIO_CLR(WB_SIO_MII_CLK); 455 DELAY(1); 456 if (!ack) { 457 if (CSR_READ_4(sc, WB_SIO) & WB_SIO_MII_DATAOUT) 458 frame->mii_data |= i; 459 DELAY(1); 460 } 461 SIO_SET(WB_SIO_MII_CLK); 462 DELAY(1); 463 } 464 465fail: 466 467 SIO_CLR(WB_SIO_MII_CLK); 468 DELAY(1); 469 SIO_SET(WB_SIO_MII_CLK); 470 DELAY(1); 471 472 WB_UNLOCK(sc); 473 474 if (ack) 475 return(1); 476 return(0); 477} 478 479/* 480 * Write to a PHY register through the MII. 481 */ 482static int 483wb_mii_writereg(sc, frame) 484 struct wb_softc *sc; 485 struct wb_mii_frame *frame; 486 487{ 488 WB_LOCK(sc); 489 490 /* 491 * Set up frame for TX. 492 */ 493 494 frame->mii_stdelim = WB_MII_STARTDELIM; 495 frame->mii_opcode = WB_MII_WRITEOP; 496 frame->mii_turnaround = WB_MII_TURNAROUND; 497 498 /* 499 * Turn on data output. 500 */ 501 SIO_SET(WB_SIO_MII_DIR); 502 503 wb_mii_sync(sc); 504 505 wb_mii_send(sc, frame->mii_stdelim, 2); 506 wb_mii_send(sc, frame->mii_opcode, 2); 507 wb_mii_send(sc, frame->mii_phyaddr, 5); 508 wb_mii_send(sc, frame->mii_regaddr, 5); 509 wb_mii_send(sc, frame->mii_turnaround, 2); 510 wb_mii_send(sc, frame->mii_data, 16); 511 512 /* Idle bit. */ 513 SIO_SET(WB_SIO_MII_CLK); 514 DELAY(1); 515 SIO_CLR(WB_SIO_MII_CLK); 516 DELAY(1); 517 518 /* 519 * Turn off xmit. 520 */ 521 SIO_CLR(WB_SIO_MII_DIR); 522 523 WB_UNLOCK(sc); 524 525 return(0); 526} 527 528static int 529wb_miibus_readreg(dev, phy, reg) 530 device_t dev; 531 int phy, reg; 532{ 533 struct wb_softc *sc; 534 struct wb_mii_frame frame; 535 536 sc = device_get_softc(dev); 537 538 bzero((char *)&frame, sizeof(frame)); 539 540 frame.mii_phyaddr = phy; 541 frame.mii_regaddr = reg; 542 wb_mii_readreg(sc, &frame); 543 544 return(frame.mii_data); 545} 546 547static int 548wb_miibus_writereg(dev, phy, reg, data) 549 device_t dev; 550 int phy, reg, data; 551{ 552 struct wb_softc *sc; 553 struct wb_mii_frame frame; 554 555 sc = device_get_softc(dev); 556 557 bzero((char *)&frame, sizeof(frame)); 558 559 frame.mii_phyaddr = phy; 560 frame.mii_regaddr = reg; 561 frame.mii_data = data; 562 563 wb_mii_writereg(sc, &frame); 564 565 return(0); 566} 567 568static void 569wb_miibus_statchg(dev) 570 device_t dev; 571{ 572 struct wb_softc *sc; 573 struct mii_data *mii; 574 575 sc = device_get_softc(dev); 576 WB_LOCK(sc); 577 mii = device_get_softc(sc->wb_miibus); 578 wb_setcfg(sc, mii->mii_media_active); 579 WB_UNLOCK(sc); 580 581 return; 582} 583 584/* 585 * Program the 64-bit multicast hash filter. 586 */ 587static void 588wb_setmulti(sc) 589 struct wb_softc *sc; 590{ 591 struct ifnet *ifp; 592 int h = 0; 593 u_int32_t hashes[2] = { 0, 0 }; 594 struct ifmultiaddr *ifma; 595 u_int32_t rxfilt; 596 int mcnt = 0; 597 598 ifp = sc->wb_ifp; 599 600 rxfilt = CSR_READ_4(sc, WB_NETCFG); 601 602 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 603 rxfilt |= WB_NETCFG_RX_MULTI; 604 CSR_WRITE_4(sc, WB_NETCFG, rxfilt); 605 CSR_WRITE_4(sc, WB_MAR0, 0xFFFFFFFF); 606 CSR_WRITE_4(sc, WB_MAR1, 0xFFFFFFFF); 607 return; 608 } 609 610 /* first, zot all the existing hash bits */ 611 CSR_WRITE_4(sc, WB_MAR0, 0); 612 CSR_WRITE_4(sc, WB_MAR1, 0); 613 614 /* now program new ones */ 615 IF_ADDR_LOCK(ifp); 616 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 617 if (ifma->ifma_addr->sa_family != AF_LINK) 618 continue; 619 h = ~ether_crc32_be(LLADDR((struct sockaddr_dl *) 620 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 621 if (h < 32) 622 hashes[0] |= (1 << h); 623 else 624 hashes[1] |= (1 << (h - 32)); 625 mcnt++; 626 } 627 IF_ADDR_UNLOCK(ifp); 628 629 if (mcnt) 630 rxfilt |= WB_NETCFG_RX_MULTI; 631 else 632 rxfilt &= ~WB_NETCFG_RX_MULTI; 633 634 CSR_WRITE_4(sc, WB_MAR0, hashes[0]); 635 CSR_WRITE_4(sc, WB_MAR1, hashes[1]); 636 CSR_WRITE_4(sc, WB_NETCFG, rxfilt); 637 638 return; 639} 640 641/* 642 * The Winbond manual states that in order to fiddle with the 643 * 'full-duplex' and '100Mbps' bits in the netconfig register, we 644 * first have to put the transmit and/or receive logic in the idle state. 645 */ 646static void 647wb_setcfg(sc, media) 648 struct wb_softc *sc; 649 u_int32_t media; 650{ 651 int i, restart = 0; 652 653 if (CSR_READ_4(sc, WB_NETCFG) & (WB_NETCFG_TX_ON|WB_NETCFG_RX_ON)) { 654 restart = 1; 655 WB_CLRBIT(sc, WB_NETCFG, (WB_NETCFG_TX_ON|WB_NETCFG_RX_ON)); 656 657 for (i = 0; i < WB_TIMEOUT; i++) { 658 DELAY(10); 659 if ((CSR_READ_4(sc, WB_ISR) & WB_ISR_TX_IDLE) && 660 (CSR_READ_4(sc, WB_ISR) & WB_ISR_RX_IDLE)) 661 break; 662 } 663 664 if (i == WB_TIMEOUT) 665 if_printf(sc->wb_ifp, 666 "failed to force tx and rx to idle state\n"); 667 } 668 669 if (IFM_SUBTYPE(media) == IFM_10_T) 670 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_100MBPS); 671 else 672 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_100MBPS); 673 674 if ((media & IFM_GMASK) == IFM_FDX) 675 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_FULLDUPLEX); 676 else 677 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_FULLDUPLEX); 678 679 if (restart) 680 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON|WB_NETCFG_RX_ON); 681 682 return; 683} 684 685static void 686wb_reset(sc) 687 struct wb_softc *sc; 688{ 689 register int i; 690 struct mii_data *mii; 691 692 CSR_WRITE_4(sc, WB_NETCFG, 0); 693 CSR_WRITE_4(sc, WB_BUSCTL, 0); 694 CSR_WRITE_4(sc, WB_TXADDR, 0); 695 CSR_WRITE_4(sc, WB_RXADDR, 0); 696 697 WB_SETBIT(sc, WB_BUSCTL, WB_BUSCTL_RESET); 698 WB_SETBIT(sc, WB_BUSCTL, WB_BUSCTL_RESET); 699 700 for (i = 0; i < WB_TIMEOUT; i++) { 701 DELAY(10); 702 if (!(CSR_READ_4(sc, WB_BUSCTL) & WB_BUSCTL_RESET)) 703 break; 704 } 705 if (i == WB_TIMEOUT) 706 if_printf(sc->wb_ifp, "reset never completed!\n"); 707 708 /* Wait a little while for the chip to get its brains in order. */ 709 DELAY(1000); 710 711 if (sc->wb_miibus == NULL) 712 return; 713 714 mii = device_get_softc(sc->wb_miibus); 715 if (mii == NULL) 716 return; 717 718 if (mii->mii_instance) { 719 struct mii_softc *miisc; 720 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 721 mii_phy_reset(miisc); 722 } 723 724 return; 725} 726 727static void 728wb_fixmedia(sc) 729 struct wb_softc *sc; 730{ 731 struct mii_data *mii = NULL; 732 struct ifnet *ifp; 733 u_int32_t media; 734 735 if (sc->wb_miibus == NULL) 736 return; 737 738 mii = device_get_softc(sc->wb_miibus); 739 ifp = sc->wb_ifp; 740 741 mii_pollstat(mii); 742 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) { 743 media = mii->mii_media_active & ~IFM_10_T; 744 media |= IFM_100_TX; 745 } else if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) { 746 media = mii->mii_media_active & ~IFM_100_TX; 747 media |= IFM_10_T; 748 } else 749 return; 750 751 ifmedia_set(&mii->mii_media, media); 752 753 return; 754} 755 756/* 757 * Probe for a Winbond chip. Check the PCI vendor and device 758 * IDs against our list and return a device name if we find a match. 759 */ 760static int 761wb_probe(dev) 762 device_t dev; 763{ 764 struct wb_type *t; 765 766 t = wb_devs; 767 768 while(t->wb_name != NULL) { 769 if ((pci_get_vendor(dev) == t->wb_vid) && 770 (pci_get_device(dev) == t->wb_did)) { 771 device_set_desc(dev, t->wb_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 */ 784static int 785wb_attach(dev) 786 device_t dev; 787{ 788 u_char eaddr[ETHER_ADDR_LEN]; 789 struct wb_softc *sc; 790 struct ifnet *ifp; 791 int error = 0, rid; 792 793 sc = device_get_softc(dev); 794 795 mtx_init(&sc->wb_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 796 MTX_DEF | MTX_RECURSE); 797 /* 798 * Map control/status registers. 799 */ 800 pci_enable_busmaster(dev); 801 802 rid = WB_RID; 803 sc->wb_res = bus_alloc_resource_any(dev, WB_RES, &rid, RF_ACTIVE); 804 805 if (sc->wb_res == NULL) { 806 device_printf(dev, "couldn't map ports/memory\n"); 807 error = ENXIO; 808 goto fail; 809 } 810 811 sc->wb_btag = rman_get_bustag(sc->wb_res); 812 sc->wb_bhandle = rman_get_bushandle(sc->wb_res); 813 814 /* Allocate interrupt */ 815 rid = 0; 816 sc->wb_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 817 RF_SHAREABLE | RF_ACTIVE); 818 819 if (sc->wb_irq == NULL) { 820 device_printf(dev, "couldn't map interrupt\n"); 821 error = ENXIO; 822 goto fail; 823 } 824 825 /* Save the cache line size. */ 826 sc->wb_cachesize = pci_read_config(dev, WB_PCI_CACHELEN, 4) & 0xFF; 827 828 /* Reset the adapter. */ 829 wb_reset(sc); 830 831 /* 832 * Get station address from the EEPROM. 833 */ 834 wb_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 0); 835 836 sc->wb_ldata = contigmalloc(sizeof(struct wb_list_data) + 8, M_DEVBUF, 837 M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0); 838 839 if (sc->wb_ldata == NULL) { 840 device_printf(dev, "no memory for list buffers!\n"); 841 error = ENXIO; 842 goto fail; 843 } 844 845 bzero(sc->wb_ldata, sizeof(struct wb_list_data)); 846 847 ifp = sc->wb_ifp = if_alloc(IFT_ETHER); 848 if (ifp == NULL) { 849 device_printf(dev, "can not if_alloc()\n"); 850 error = ENOSPC; 851 goto fail; 852 } 853 ifp->if_softc = sc; 854 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 855 ifp->if_mtu = ETHERMTU; 856 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST | 857 IFF_NEEDSGIANT; 858 ifp->if_ioctl = wb_ioctl; 859 ifp->if_start = wb_start; 860 ifp->if_watchdog = wb_watchdog; 861 ifp->if_init = wb_init; 862 ifp->if_baudrate = 10000000; 863 ifp->if_snd.ifq_maxlen = WB_TX_LIST_CNT - 1; 864 865 /* 866 * Do MII setup. 867 */ 868 if (mii_phy_probe(dev, &sc->wb_miibus, 869 wb_ifmedia_upd, wb_ifmedia_sts)) { 870 error = ENXIO; 871 goto fail; 872 } 873 874 /* 875 * Call MI attach routine. 876 */ 877 ether_ifattach(ifp, eaddr); 878 879 /* Hook interrupt last to avoid having to lock softc */ 880 error = bus_setup_intr(dev, sc->wb_irq, INTR_TYPE_NET, 881 wb_intr, sc, &sc->wb_intrhand); 882 883 if (error) { 884 device_printf(dev, "couldn't set up irq\n"); 885 ether_ifdetach(ifp); 886 goto fail; 887 } 888 889fail: 890 if (error) 891 wb_detach(dev); 892 893 return(error); 894} 895 896/* 897 * Shutdown hardware and free up resources. This can be called any 898 * time after the mutex has been initialized. It is called in both 899 * the error case in attach and the normal detach case so it needs 900 * to be careful about only freeing resources that have actually been 901 * allocated. 902 */ 903static int 904wb_detach(dev) 905 device_t dev; 906{ 907 struct wb_softc *sc; 908 struct ifnet *ifp; 909 910 sc = device_get_softc(dev); 911 KASSERT(mtx_initialized(&sc->wb_mtx), ("wb mutex not initialized")); 912 WB_LOCK(sc); 913 ifp = sc->wb_ifp; 914 915 /* 916 * Delete any miibus and phy devices attached to this interface. 917 * This should only be done if attach succeeded. 918 */ 919 if (device_is_attached(dev)) { 920 wb_stop(sc); 921 ether_ifdetach(ifp); 922 } 923 if (sc->wb_miibus) 924 device_delete_child(dev, sc->wb_miibus); 925 bus_generic_detach(dev); 926 927 if (sc->wb_intrhand) 928 bus_teardown_intr(dev, sc->wb_irq, sc->wb_intrhand); 929 if (sc->wb_irq) 930 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->wb_irq); 931 if (sc->wb_res) 932 bus_release_resource(dev, WB_RES, WB_RID, sc->wb_res); 933 934 if (ifp) 935 if_free(ifp); 936 937 if (sc->wb_ldata) { 938 contigfree(sc->wb_ldata, sizeof(struct wb_list_data) + 8, 939 M_DEVBUF); 940 } 941 942 WB_UNLOCK(sc); 943 mtx_destroy(&sc->wb_mtx); 944 945 return(0); 946} 947 948/* 949 * Initialize the transmit descriptors. 950 */ 951static int 952wb_list_tx_init(sc) 953 struct wb_softc *sc; 954{ 955 struct wb_chain_data *cd; 956 struct wb_list_data *ld; 957 int i; 958 959 cd = &sc->wb_cdata; 960 ld = sc->wb_ldata; 961 962 for (i = 0; i < WB_TX_LIST_CNT; i++) { 963 cd->wb_tx_chain[i].wb_ptr = &ld->wb_tx_list[i]; 964 if (i == (WB_TX_LIST_CNT - 1)) { 965 cd->wb_tx_chain[i].wb_nextdesc = 966 &cd->wb_tx_chain[0]; 967 } else { 968 cd->wb_tx_chain[i].wb_nextdesc = 969 &cd->wb_tx_chain[i + 1]; 970 } 971 } 972 973 cd->wb_tx_free = &cd->wb_tx_chain[0]; 974 cd->wb_tx_tail = cd->wb_tx_head = NULL; 975 976 return(0); 977} 978 979 980/* 981 * Initialize the RX descriptors and allocate mbufs for them. Note that 982 * we arrange the descriptors in a closed ring, so that the last descriptor 983 * points back to the first. 984 */ 985static int 986wb_list_rx_init(sc) 987 struct wb_softc *sc; 988{ 989 struct wb_chain_data *cd; 990 struct wb_list_data *ld; 991 int i; 992 993 cd = &sc->wb_cdata; 994 ld = sc->wb_ldata; 995 996 for (i = 0; i < WB_RX_LIST_CNT; i++) { 997 cd->wb_rx_chain[i].wb_ptr = 998 (struct wb_desc *)&ld->wb_rx_list[i]; 999 cd->wb_rx_chain[i].wb_buf = (void *)&ld->wb_rxbufs[i]; 1000 if (wb_newbuf(sc, &cd->wb_rx_chain[i], NULL) == ENOBUFS) 1001 return(ENOBUFS); 1002 if (i == (WB_RX_LIST_CNT - 1)) { 1003 cd->wb_rx_chain[i].wb_nextdesc = &cd->wb_rx_chain[0]; 1004 ld->wb_rx_list[i].wb_next = 1005 vtophys(&ld->wb_rx_list[0]); 1006 } else { 1007 cd->wb_rx_chain[i].wb_nextdesc = 1008 &cd->wb_rx_chain[i + 1]; 1009 ld->wb_rx_list[i].wb_next = 1010 vtophys(&ld->wb_rx_list[i + 1]); 1011 } 1012 } 1013 1014 cd->wb_rx_head = &cd->wb_rx_chain[0]; 1015 1016 return(0); 1017} 1018 1019static void 1020wb_bfree(buf, args) 1021 void *buf; 1022 void *args; 1023{ 1024 return; 1025} 1026 1027/* 1028 * Initialize an RX descriptor and attach an MBUF cluster. 1029 */ 1030static int 1031wb_newbuf(sc, c, m) 1032 struct wb_softc *sc; 1033 struct wb_chain_onefrag *c; 1034 struct mbuf *m; 1035{ 1036 struct mbuf *m_new = NULL; 1037 1038 if (m == NULL) { 1039 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 1040 if (m_new == NULL) 1041 return(ENOBUFS); 1042 m_new->m_data = c->wb_buf; 1043 m_new->m_pkthdr.len = m_new->m_len = WB_BUFBYTES; 1044 MEXTADD(m_new, c->wb_buf, WB_BUFBYTES, wb_bfree, NULL, 0, 1045 EXT_NET_DRV); 1046 } else { 1047 m_new = m; 1048 m_new->m_len = m_new->m_pkthdr.len = WB_BUFBYTES; 1049 m_new->m_data = m_new->m_ext.ext_buf; 1050 } 1051 1052 m_adj(m_new, sizeof(u_int64_t)); 1053 1054 c->wb_mbuf = m_new; 1055 c->wb_ptr->wb_data = vtophys(mtod(m_new, caddr_t)); 1056 c->wb_ptr->wb_ctl = WB_RXCTL_RLINK | 1536; 1057 c->wb_ptr->wb_status = WB_RXSTAT; 1058 1059 return(0); 1060} 1061 1062/* 1063 * A frame has been uploaded: pass the resulting mbuf chain up to 1064 * the higher level protocols. 1065 */ 1066static void 1067wb_rxeof(sc) 1068 struct wb_softc *sc; 1069{ 1070 struct mbuf *m = NULL; 1071 struct ifnet *ifp; 1072 struct wb_chain_onefrag *cur_rx; 1073 int total_len = 0; 1074 u_int32_t rxstat; 1075 1076 WB_LOCK_ASSERT(sc); 1077 1078 ifp = sc->wb_ifp; 1079 1080 while(!((rxstat = sc->wb_cdata.wb_rx_head->wb_ptr->wb_status) & 1081 WB_RXSTAT_OWN)) { 1082 struct mbuf *m0 = NULL; 1083 1084 cur_rx = sc->wb_cdata.wb_rx_head; 1085 sc->wb_cdata.wb_rx_head = cur_rx->wb_nextdesc; 1086 1087 m = cur_rx->wb_mbuf; 1088 1089 if ((rxstat & WB_RXSTAT_MIIERR) || 1090 (WB_RXBYTES(cur_rx->wb_ptr->wb_status) < WB_MIN_FRAMELEN) || 1091 (WB_RXBYTES(cur_rx->wb_ptr->wb_status) > 1536) || 1092 !(rxstat & WB_RXSTAT_LASTFRAG) || 1093 !(rxstat & WB_RXSTAT_RXCMP)) { 1094 ifp->if_ierrors++; 1095 wb_newbuf(sc, cur_rx, m); 1096 if_printf(ifp, "receiver babbling: possible chip " 1097 "bug, forcing reset\n"); 1098 wb_fixmedia(sc); 1099 wb_reset(sc); 1100 wb_init(sc); 1101 return; 1102 } 1103 1104 if (rxstat & WB_RXSTAT_RXERR) { 1105 ifp->if_ierrors++; 1106 wb_newbuf(sc, cur_rx, m); 1107 break; 1108 } 1109 1110 /* No errors; receive the packet. */ 1111 total_len = WB_RXBYTES(cur_rx->wb_ptr->wb_status); 1112 1113 /* 1114 * XXX The Winbond chip includes the CRC with every 1115 * received frame, and there's no way to turn this 1116 * behavior off (at least, I can't find anything in 1117 * the manual that explains how to do it) so we have 1118 * to trim off the CRC manually. 1119 */ 1120 total_len -= ETHER_CRC_LEN; 1121 1122 m0 = m_devget(mtod(m, char *), total_len, ETHER_ALIGN, ifp, 1123 NULL); 1124 wb_newbuf(sc, cur_rx, m); 1125 if (m0 == NULL) { 1126 ifp->if_ierrors++; 1127 break; 1128 } 1129 m = m0; 1130 1131 ifp->if_ipackets++; 1132 WB_UNLOCK(sc); 1133 (*ifp->if_input)(ifp, m); 1134 WB_LOCK(sc); 1135 } 1136} 1137 1138static void 1139wb_rxeoc(sc) 1140 struct wb_softc *sc; 1141{ 1142 wb_rxeof(sc); 1143 1144 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON); 1145 CSR_WRITE_4(sc, WB_RXADDR, vtophys(&sc->wb_ldata->wb_rx_list[0])); 1146 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON); 1147 if (CSR_READ_4(sc, WB_ISR) & WB_RXSTATE_SUSPEND) 1148 CSR_WRITE_4(sc, WB_RXSTART, 0xFFFFFFFF); 1149 1150 return; 1151} 1152 1153/* 1154 * A frame was downloaded to the chip. It's safe for us to clean up 1155 * the list buffers. 1156 */ 1157static void 1158wb_txeof(sc) 1159 struct wb_softc *sc; 1160{ 1161 struct wb_chain *cur_tx; 1162 struct ifnet *ifp; 1163 1164 ifp = sc->wb_ifp; 1165 1166 /* Clear the timeout timer. */ 1167 ifp->if_timer = 0; 1168 1169 if (sc->wb_cdata.wb_tx_head == NULL) 1170 return; 1171 1172 /* 1173 * Go through our tx list and free mbufs for those 1174 * frames that have been transmitted. 1175 */ 1176 while(sc->wb_cdata.wb_tx_head->wb_mbuf != NULL) { 1177 u_int32_t txstat; 1178 1179 cur_tx = sc->wb_cdata.wb_tx_head; 1180 txstat = WB_TXSTATUS(cur_tx); 1181 1182 if ((txstat & WB_TXSTAT_OWN) || txstat == WB_UNSENT) 1183 break; 1184 1185 if (txstat & WB_TXSTAT_TXERR) { 1186 ifp->if_oerrors++; 1187 if (txstat & WB_TXSTAT_ABORT) 1188 ifp->if_collisions++; 1189 if (txstat & WB_TXSTAT_LATECOLL) 1190 ifp->if_collisions++; 1191 } 1192 1193 ifp->if_collisions += (txstat & WB_TXSTAT_COLLCNT) >> 3; 1194 1195 ifp->if_opackets++; 1196 m_freem(cur_tx->wb_mbuf); 1197 cur_tx->wb_mbuf = NULL; 1198 1199 if (sc->wb_cdata.wb_tx_head == sc->wb_cdata.wb_tx_tail) { 1200 sc->wb_cdata.wb_tx_head = NULL; 1201 sc->wb_cdata.wb_tx_tail = NULL; 1202 break; 1203 } 1204 1205 sc->wb_cdata.wb_tx_head = cur_tx->wb_nextdesc; 1206 } 1207 1208 return; 1209} 1210 1211/* 1212 * TX 'end of channel' interrupt handler. 1213 */ 1214static void 1215wb_txeoc(sc) 1216 struct wb_softc *sc; 1217{ 1218 struct ifnet *ifp; 1219 1220 ifp = sc->wb_ifp; 1221 1222 ifp->if_timer = 0; 1223 1224 if (sc->wb_cdata.wb_tx_head == NULL) { 1225 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1226 sc->wb_cdata.wb_tx_tail = NULL; 1227 } else { 1228 if (WB_TXOWN(sc->wb_cdata.wb_tx_head) == WB_UNSENT) { 1229 WB_TXOWN(sc->wb_cdata.wb_tx_head) = WB_TXSTAT_OWN; 1230 ifp->if_timer = 5; 1231 CSR_WRITE_4(sc, WB_TXSTART, 0xFFFFFFFF); 1232 } 1233 } 1234 1235 return; 1236} 1237 1238static void 1239wb_intr(arg) 1240 void *arg; 1241{ 1242 struct wb_softc *sc; 1243 struct ifnet *ifp; 1244 u_int32_t status; 1245 1246 sc = arg; 1247 WB_LOCK(sc); 1248 ifp = sc->wb_ifp; 1249 1250 if (!(ifp->if_flags & IFF_UP)) { 1251 WB_UNLOCK(sc); 1252 return; 1253 } 1254 1255 /* Disable interrupts. */ 1256 CSR_WRITE_4(sc, WB_IMR, 0x00000000); 1257 1258 for (;;) { 1259 1260 status = CSR_READ_4(sc, WB_ISR); 1261 if (status) 1262 CSR_WRITE_4(sc, WB_ISR, status); 1263 1264 if ((status & WB_INTRS) == 0) 1265 break; 1266 1267 if ((status & WB_ISR_RX_NOBUF) || (status & WB_ISR_RX_ERR)) { 1268 ifp->if_ierrors++; 1269 wb_reset(sc); 1270 if (status & WB_ISR_RX_ERR) 1271 wb_fixmedia(sc); 1272 wb_init(sc); 1273 continue; 1274 } 1275 1276 if (status & WB_ISR_RX_OK) 1277 wb_rxeof(sc); 1278 1279 if (status & WB_ISR_RX_IDLE) 1280 wb_rxeoc(sc); 1281 1282 if (status & WB_ISR_TX_OK) 1283 wb_txeof(sc); 1284 1285 if (status & WB_ISR_TX_NOBUF) 1286 wb_txeoc(sc); 1287 1288 if (status & WB_ISR_TX_IDLE) { 1289 wb_txeof(sc); 1290 if (sc->wb_cdata.wb_tx_head != NULL) { 1291 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON); 1292 CSR_WRITE_4(sc, WB_TXSTART, 0xFFFFFFFF); 1293 } 1294 } 1295 1296 if (status & WB_ISR_TX_UNDERRUN) { 1297 ifp->if_oerrors++; 1298 wb_txeof(sc); 1299 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON); 1300 /* Jack up TX threshold */ 1301 sc->wb_txthresh += WB_TXTHRESH_CHUNK; 1302 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_THRESH); 1303 WB_SETBIT(sc, WB_NETCFG, WB_TXTHRESH(sc->wb_txthresh)); 1304 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON); 1305 } 1306 1307 if (status & WB_ISR_BUS_ERR) { 1308 wb_reset(sc); 1309 wb_init(sc); 1310 } 1311 1312 } 1313 1314 /* Re-enable interrupts. */ 1315 CSR_WRITE_4(sc, WB_IMR, WB_INTRS); 1316 1317 if (ifp->if_snd.ifq_head != NULL) { 1318 wb_start(ifp); 1319 } 1320 1321 WB_UNLOCK(sc); 1322 1323 return; 1324} 1325 1326static void 1327wb_tick(xsc) 1328 void *xsc; 1329{ 1330 struct wb_softc *sc; 1331 struct mii_data *mii; 1332 1333 sc = xsc; 1334 WB_LOCK(sc); 1335 mii = device_get_softc(sc->wb_miibus); 1336 1337 mii_tick(mii); 1338 1339 sc->wb_stat_ch = timeout(wb_tick, sc, hz); 1340 1341 WB_UNLOCK(sc); 1342 1343 return; 1344} 1345 1346/* 1347 * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data 1348 * pointers to the fragment pointers. 1349 */ 1350static int 1351wb_encap(sc, c, m_head) 1352 struct wb_softc *sc; 1353 struct wb_chain *c; 1354 struct mbuf *m_head; 1355{ 1356 int frag = 0; 1357 struct wb_desc *f = NULL; 1358 int total_len; 1359 struct mbuf *m; 1360 1361 /* 1362 * Start packing the mbufs in this chain into 1363 * the fragment pointers. Stop when we run out 1364 * of fragments or hit the end of the mbuf chain. 1365 */ 1366 m = m_head; 1367 total_len = 0; 1368 1369 for (m = m_head, frag = 0; m != NULL; m = m->m_next) { 1370 if (m->m_len != 0) { 1371 if (frag == WB_MAXFRAGS) 1372 break; 1373 total_len += m->m_len; 1374 f = &c->wb_ptr->wb_frag[frag]; 1375 f->wb_ctl = WB_TXCTL_TLINK | m->m_len; 1376 if (frag == 0) { 1377 f->wb_ctl |= WB_TXCTL_FIRSTFRAG; 1378 f->wb_status = 0; 1379 } else 1380 f->wb_status = WB_TXSTAT_OWN; 1381 f->wb_next = vtophys(&c->wb_ptr->wb_frag[frag + 1]); 1382 f->wb_data = vtophys(mtod(m, vm_offset_t)); 1383 frag++; 1384 } 1385 } 1386 1387 /* 1388 * Handle special case: we used up all 16 fragments, 1389 * but we have more mbufs left in the chain. Copy the 1390 * data into an mbuf cluster. Note that we don't 1391 * bother clearing the values in the other fragment 1392 * pointers/counters; it wouldn't gain us anything, 1393 * and would waste cycles. 1394 */ 1395 if (m != NULL) { 1396 struct mbuf *m_new = NULL; 1397 1398 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 1399 if (m_new == NULL) 1400 return(1); 1401 if (m_head->m_pkthdr.len > MHLEN) { 1402 MCLGET(m_new, M_DONTWAIT); 1403 if (!(m_new->m_flags & M_EXT)) { 1404 m_freem(m_new); 1405 return(1); 1406 } 1407 } 1408 m_copydata(m_head, 0, m_head->m_pkthdr.len, 1409 mtod(m_new, caddr_t)); 1410 m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len; 1411 m_freem(m_head); 1412 m_head = m_new; 1413 f = &c->wb_ptr->wb_frag[0]; 1414 f->wb_status = 0; 1415 f->wb_data = vtophys(mtod(m_new, caddr_t)); 1416 f->wb_ctl = total_len = m_new->m_len; 1417 f->wb_ctl |= WB_TXCTL_TLINK|WB_TXCTL_FIRSTFRAG; 1418 frag = 1; 1419 } 1420 1421 if (total_len < WB_MIN_FRAMELEN) { 1422 f = &c->wb_ptr->wb_frag[frag]; 1423 f->wb_ctl = WB_MIN_FRAMELEN - total_len; 1424 f->wb_data = vtophys(&sc->wb_cdata.wb_pad); 1425 f->wb_ctl |= WB_TXCTL_TLINK; 1426 f->wb_status = WB_TXSTAT_OWN; 1427 frag++; 1428 } 1429 1430 c->wb_mbuf = m_head; 1431 c->wb_lastdesc = frag - 1; 1432 WB_TXCTL(c) |= WB_TXCTL_LASTFRAG; 1433 WB_TXNEXT(c) = vtophys(&c->wb_nextdesc->wb_ptr->wb_frag[0]); 1434 1435 return(0); 1436} 1437 1438/* 1439 * Main transmit routine. To avoid having to do mbuf copies, we put pointers 1440 * to the mbuf data regions directly in the transmit lists. We also save a 1441 * copy of the pointers since the transmit list fragment pointers are 1442 * physical addresses. 1443 */ 1444 1445static void 1446wb_start(ifp) 1447 struct ifnet *ifp; 1448{ 1449 struct wb_softc *sc; 1450 struct mbuf *m_head = NULL; 1451 struct wb_chain *cur_tx = NULL, *start_tx; 1452 1453 sc = ifp->if_softc; 1454 WB_LOCK(sc); 1455 1456 /* 1457 * Check for an available queue slot. If there are none, 1458 * punt. 1459 */ 1460 if (sc->wb_cdata.wb_tx_free->wb_mbuf != NULL) { 1461 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1462 WB_UNLOCK(sc); 1463 return; 1464 } 1465 1466 start_tx = sc->wb_cdata.wb_tx_free; 1467 1468 while(sc->wb_cdata.wb_tx_free->wb_mbuf == NULL) { 1469 IF_DEQUEUE(&ifp->if_snd, m_head); 1470 if (m_head == NULL) 1471 break; 1472 1473 /* Pick a descriptor off the free list. */ 1474 cur_tx = sc->wb_cdata.wb_tx_free; 1475 sc->wb_cdata.wb_tx_free = cur_tx->wb_nextdesc; 1476 1477 /* Pack the data into the descriptor. */ 1478 wb_encap(sc, cur_tx, m_head); 1479 1480 if (cur_tx != start_tx) 1481 WB_TXOWN(cur_tx) = WB_TXSTAT_OWN; 1482 1483 /* 1484 * If there's a BPF listener, bounce a copy of this frame 1485 * to him. 1486 */ 1487 BPF_MTAP(ifp, cur_tx->wb_mbuf); 1488 } 1489 1490 /* 1491 * If there are no packets queued, bail. 1492 */ 1493 if (cur_tx == NULL) { 1494 WB_UNLOCK(sc); 1495 return; 1496 } 1497 1498 /* 1499 * Place the request for the upload interrupt 1500 * in the last descriptor in the chain. This way, if 1501 * we're chaining several packets at once, we'll only 1502 * get an interupt once for the whole chain rather than 1503 * once for each packet. 1504 */ 1505 WB_TXCTL(cur_tx) |= WB_TXCTL_FINT; 1506 cur_tx->wb_ptr->wb_frag[0].wb_ctl |= WB_TXCTL_FINT; 1507 sc->wb_cdata.wb_tx_tail = cur_tx; 1508 1509 if (sc->wb_cdata.wb_tx_head == NULL) { 1510 sc->wb_cdata.wb_tx_head = start_tx; 1511 WB_TXOWN(start_tx) = WB_TXSTAT_OWN; 1512 CSR_WRITE_4(sc, WB_TXSTART, 0xFFFFFFFF); 1513 } else { 1514 /* 1515 * We need to distinguish between the case where 1516 * the own bit is clear because the chip cleared it 1517 * and where the own bit is clear because we haven't 1518 * set it yet. The magic value WB_UNSET is just some 1519 * ramdomly chosen number which doesn't have the own 1520 * bit set. When we actually transmit the frame, the 1521 * status word will have _only_ the own bit set, so 1522 * the txeoc handler will be able to tell if it needs 1523 * to initiate another transmission to flush out pending 1524 * frames. 1525 */ 1526 WB_TXOWN(start_tx) = WB_UNSENT; 1527 } 1528 1529 /* 1530 * Set a timeout in case the chip goes out to lunch. 1531 */ 1532 ifp->if_timer = 5; 1533 WB_UNLOCK(sc); 1534 1535 return; 1536} 1537 1538static void 1539wb_init(xsc) 1540 void *xsc; 1541{ 1542 struct wb_softc *sc = xsc; 1543 struct ifnet *ifp = sc->wb_ifp; 1544 int i; 1545 struct mii_data *mii; 1546 1547 WB_LOCK(sc); 1548 mii = device_get_softc(sc->wb_miibus); 1549 1550 /* 1551 * Cancel pending I/O and free all RX/TX buffers. 1552 */ 1553 wb_stop(sc); 1554 wb_reset(sc); 1555 1556 sc->wb_txthresh = WB_TXTHRESH_INIT; 1557 1558 /* 1559 * Set cache alignment and burst length. 1560 */ 1561#ifdef foo 1562 CSR_WRITE_4(sc, WB_BUSCTL, WB_BUSCTL_CONFIG); 1563 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_THRESH); 1564 WB_SETBIT(sc, WB_NETCFG, WB_TXTHRESH(sc->wb_txthresh)); 1565#endif 1566 1567 CSR_WRITE_4(sc, WB_BUSCTL, WB_BUSCTL_MUSTBEONE|WB_BUSCTL_ARBITRATION); 1568 WB_SETBIT(sc, WB_BUSCTL, WB_BURSTLEN_16LONG); 1569 switch(sc->wb_cachesize) { 1570 case 32: 1571 WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_32LONG); 1572 break; 1573 case 16: 1574 WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_16LONG); 1575 break; 1576 case 8: 1577 WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_8LONG); 1578 break; 1579 case 0: 1580 default: 1581 WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_NONE); 1582 break; 1583 } 1584 1585 /* This doesn't tend to work too well at 100Mbps. */ 1586 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_EARLY_ON); 1587 1588 /* Init our MAC address */ 1589 for (i = 0; i < ETHER_ADDR_LEN; i++) { 1590 CSR_WRITE_1(sc, WB_NODE0 + i, IFP2ENADDR(sc->wb_ifp)[i]); 1591 } 1592 1593 /* Init circular RX list. */ 1594 if (wb_list_rx_init(sc) == ENOBUFS) { 1595 if_printf(ifp, 1596 "initialization failed: no memory for rx buffers\n"); 1597 wb_stop(sc); 1598 WB_UNLOCK(sc); 1599 return; 1600 } 1601 1602 /* Init TX descriptors. */ 1603 wb_list_tx_init(sc); 1604 1605 /* If we want promiscuous mode, set the allframes bit. */ 1606 if (ifp->if_flags & IFF_PROMISC) { 1607 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_ALLPHYS); 1608 } else { 1609 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_ALLPHYS); 1610 } 1611 1612 /* 1613 * Set capture broadcast bit to capture broadcast frames. 1614 */ 1615 if (ifp->if_flags & IFF_BROADCAST) { 1616 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_BROAD); 1617 } else { 1618 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_BROAD); 1619 } 1620 1621 /* 1622 * Program the multicast filter, if necessary. 1623 */ 1624 wb_setmulti(sc); 1625 1626 /* 1627 * Load the address of the RX list. 1628 */ 1629 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON); 1630 CSR_WRITE_4(sc, WB_RXADDR, vtophys(&sc->wb_ldata->wb_rx_list[0])); 1631 1632 /* 1633 * Enable interrupts. 1634 */ 1635 CSR_WRITE_4(sc, WB_IMR, WB_INTRS); 1636 CSR_WRITE_4(sc, WB_ISR, 0xFFFFFFFF); 1637 1638 /* Enable receiver and transmitter. */ 1639 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON); 1640 CSR_WRITE_4(sc, WB_RXSTART, 0xFFFFFFFF); 1641 1642 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON); 1643 CSR_WRITE_4(sc, WB_TXADDR, vtophys(&sc->wb_ldata->wb_tx_list[0])); 1644 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON); 1645 1646 mii_mediachg(mii); 1647 1648 ifp->if_drv_flags |= IFF_DRV_RUNNING; 1649 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1650 1651 sc->wb_stat_ch = timeout(wb_tick, sc, hz); 1652 WB_UNLOCK(sc); 1653 1654 return; 1655} 1656 1657/* 1658 * Set media options. 1659 */ 1660static int 1661wb_ifmedia_upd(ifp) 1662 struct ifnet *ifp; 1663{ 1664 struct wb_softc *sc; 1665 1666 sc = ifp->if_softc; 1667 1668 if (ifp->if_flags & IFF_UP) 1669 wb_init(sc); 1670 1671 return(0); 1672} 1673 1674/* 1675 * Report current media status. 1676 */ 1677static void 1678wb_ifmedia_sts(ifp, ifmr) 1679 struct ifnet *ifp; 1680 struct ifmediareq *ifmr; 1681{ 1682 struct wb_softc *sc; 1683 struct mii_data *mii; 1684 1685 sc = ifp->if_softc; 1686 1687 mii = device_get_softc(sc->wb_miibus); 1688 1689 mii_pollstat(mii); 1690 ifmr->ifm_active = mii->mii_media_active; 1691 ifmr->ifm_status = mii->mii_media_status; 1692 1693 return; 1694} 1695 1696static int 1697wb_ioctl(ifp, command, data) 1698 struct ifnet *ifp; 1699 u_long command; 1700 caddr_t data; 1701{ 1702 struct wb_softc *sc = ifp->if_softc; 1703 struct mii_data *mii; 1704 struct ifreq *ifr = (struct ifreq *) data; 1705 int error = 0; 1706 1707 WB_LOCK(sc); 1708 1709 switch(command) { 1710 case SIOCSIFFLAGS: 1711 if (ifp->if_flags & IFF_UP) { 1712 wb_init(sc); 1713 } else { 1714 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1715 wb_stop(sc); 1716 } 1717 error = 0; 1718 break; 1719 case SIOCADDMULTI: 1720 case SIOCDELMULTI: 1721 wb_setmulti(sc); 1722 error = 0; 1723 break; 1724 case SIOCGIFMEDIA: 1725 case SIOCSIFMEDIA: 1726 mii = device_get_softc(sc->wb_miibus); 1727 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 1728 break; 1729 default: 1730 error = ether_ioctl(ifp, command, data); 1731 break; 1732 } 1733 1734 WB_UNLOCK(sc); 1735 1736 return(error); 1737} 1738 1739static void 1740wb_watchdog(ifp) 1741 struct ifnet *ifp; 1742{ 1743 struct wb_softc *sc; 1744 1745 sc = ifp->if_softc; 1746 1747 WB_LOCK(sc); 1748 ifp->if_oerrors++; 1749 if_printf(ifp, "watchdog timeout\n"); 1750#ifdef foo 1751 if (!(wb_phy_readreg(sc, PHY_BMSR) & PHY_BMSR_LINKSTAT)) 1752 if_printf(ifp, "no carrier - transceiver cable problem?\n"); 1753#endif 1754 wb_stop(sc); 1755 wb_reset(sc); 1756 wb_init(sc); 1757 1758 if (ifp->if_snd.ifq_head != NULL) 1759 wb_start(ifp); 1760 WB_UNLOCK(sc); 1761 1762 return; 1763} 1764 1765/* 1766 * Stop the adapter and free any mbufs allocated to the 1767 * RX and TX lists. 1768 */ 1769static void 1770wb_stop(sc) 1771 struct wb_softc *sc; 1772{ 1773 register int i; 1774 struct ifnet *ifp; 1775 1776 WB_LOCK(sc); 1777 ifp = sc->wb_ifp; 1778 ifp->if_timer = 0; 1779 1780 untimeout(wb_tick, sc, sc->wb_stat_ch); 1781 1782 WB_CLRBIT(sc, WB_NETCFG, (WB_NETCFG_RX_ON|WB_NETCFG_TX_ON)); 1783 CSR_WRITE_4(sc, WB_IMR, 0x00000000); 1784 CSR_WRITE_4(sc, WB_TXADDR, 0x00000000); 1785 CSR_WRITE_4(sc, WB_RXADDR, 0x00000000); 1786 1787 /* 1788 * Free data in the RX lists. 1789 */ 1790 for (i = 0; i < WB_RX_LIST_CNT; i++) { 1791 if (sc->wb_cdata.wb_rx_chain[i].wb_mbuf != NULL) { 1792 m_freem(sc->wb_cdata.wb_rx_chain[i].wb_mbuf); 1793 sc->wb_cdata.wb_rx_chain[i].wb_mbuf = NULL; 1794 } 1795 } 1796 bzero((char *)&sc->wb_ldata->wb_rx_list, 1797 sizeof(sc->wb_ldata->wb_rx_list)); 1798 1799 /* 1800 * Free the TX list buffers. 1801 */ 1802 for (i = 0; i < WB_TX_LIST_CNT; i++) { 1803 if (sc->wb_cdata.wb_tx_chain[i].wb_mbuf != NULL) { 1804 m_freem(sc->wb_cdata.wb_tx_chain[i].wb_mbuf); 1805 sc->wb_cdata.wb_tx_chain[i].wb_mbuf = NULL; 1806 } 1807 } 1808 1809 bzero((char *)&sc->wb_ldata->wb_tx_list, 1810 sizeof(sc->wb_ldata->wb_tx_list)); 1811 1812 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 1813 WB_UNLOCK(sc); 1814 1815 return; 1816} 1817 1818/* 1819 * Stop all chip I/O so that the kernel's probe routines don't 1820 * get confused by errant DMAs when rebooting. 1821 */ 1822static void 1823wb_shutdown(dev) 1824 device_t dev; 1825{ 1826 struct wb_softc *sc; 1827 1828 sc = device_get_softc(dev); 1829 wb_stop(sc); 1830 1831 return; 1832} 1833