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