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