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