if_wb.c revision 72813
1258945Sroberto/* 2280849Scy * Copyright (c) 1997, 1998 3258945Sroberto * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 4258945Sroberto * 5258945Sroberto * Redistribution and use in source and binary forms, with or without 6258945Sroberto * modification, are permitted provided that the following conditions 7258945Sroberto * are met: 8258945Sroberto * 1. Redistributions of source code must retain the above copyright 9258945Sroberto * notice, this list of conditions and the following disclaimer. 10258945Sroberto * 2. Redistributions in binary form must reproduce the above copyright 11258945Sroberto * notice, this list of conditions and the following disclaimer in the 12258945Sroberto * documentation and/or other materials provided with the distribution. 13258945Sroberto * 3. All advertising materials mentioning features or use of this software 14258945Sroberto * must display the following acknowledgement: 15258945Sroberto * This product includes software developed by Bill Paul. 16258945Sroberto * 4. Neither the name of the author nor the names of any co-contributors 17258945Sroberto * may be used to endorse or promote products derived from this software 18280849Scy * without specific prior written permission. 19258945Sroberto * 20258945Sroberto * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21258945Sroberto * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22258945Sroberto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23258945Sroberto * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 24258945Sroberto * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25258945Sroberto * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26258945Sroberto * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27258945Sroberto * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28258945Sroberto * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29258945Sroberto * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30258945Sroberto * THE POSSIBILITY OF SUCH DAMAGE. 31258945Sroberto * 32258945Sroberto * $FreeBSD: head/sys/pci/if_wb.c 72813 2001-02-21 20:54:22Z wpaul $ 33258945Sroberto */ 34258945Sroberto 35258945Sroberto/* 36258945Sroberto * Winbond fast ethernet PCI NIC driver 37258945Sroberto * 38258945Sroberto * Supports various cheap network adapters based on the Winbond W89C840F 39258945Sroberto * fast ethernet controller chip. This includes adapters manufactured by 40258945Sroberto * Winbond itself and some made by Linksys. 41258945Sroberto * 42258945Sroberto * Written by Bill Paul <wpaul@ctr.columbia.edu> 43258945Sroberto * Electrical Engineering Department 44258945Sroberto * Columbia University, New York City 45258945Sroberto */ 46258945Sroberto 47258945Sroberto/* 48258945Sroberto * The Winbond W89C840F chip is a bus master; in some ways it resembles 49258945Sroberto * a DEC 'tulip' chip, only not as complicated. Unfortunately, it has 50258945Sroberto * one major difference which is that while the registers do many of 51258945Sroberto * the same things as a tulip adapter, the offsets are different: where 52258945Sroberto * tulip registers are typically spaced 8 bytes apart, the Winbond 53258945Sroberto * registers are spaced 4 bytes apart. The receiver filter is also 54258945Sroberto * programmed differently. 55258945Sroberto * 56258945Sroberto * Like the tulip, the Winbond chip uses small descriptors containing 57258945Sroberto * a status word, a control word and 32-bit areas that can either be used 58258945Sroberto * to point to two external data blocks, or to point to a single block 59258945Sroberto * and another descriptor in a linked list. Descriptors can be grouped 60258945Sroberto * together in blocks to form fixed length rings or can be chained 61258945Sroberto * together in linked lists. A single packet may be spread out over 62258945Sroberto * several descriptors if necessary. 63258945Sroberto * 64258945Sroberto * For the receive ring, this driver uses a linked list of descriptors, 65258945Sroberto * each pointing to a single mbuf cluster buffer, which us large enough 66258945Sroberto * to hold an entire packet. The link list is looped back to created a 67258945Sroberto * closed ring. 68258945Sroberto * 69258945Sroberto * For transmission, the driver creates a linked list of 'super descriptors' 70258945Sroberto * which each contain several individual descriptors linked toghether. 71258945Sroberto * Each 'super descriptor' contains WB_MAXFRAGS descriptors, which we 72258945Sroberto * abuse as fragment pointers. This allows us to use a buffer managment 73258945Sroberto * scheme very similar to that used in the ThunderLAN and Etherlink XL 74258945Sroberto * drivers. 75258945Sroberto * 76258945Sroberto * Autonegotiation is performed using the external PHY via the MII bus. 77258945Sroberto * The sample boards I have all use a Davicom PHY. 78258945Sroberto * 79258945Sroberto * Note: the author of the Linux driver for the Winbond chip alludes 80258945Sroberto * to some sort of flaw in the chip's design that seems to mandate some 81258945Sroberto * drastic workaround which signigicantly impairs transmit performance. 82258945Sroberto * I have no idea what he's on about: transmit performance with all 83258945Sroberto * three of my test boards seems fine. 84258945Sroberto */ 85258945Sroberto 86258945Sroberto#include "opt_bdg.h" 87258945Sroberto 88258945Sroberto#include <sys/param.h> 89258945Sroberto#include <sys/systm.h> 90280849Scy#include <sys/sockio.h> 91280849Scy#include <sys/mbuf.h> 92258945Sroberto#include <sys/malloc.h> 93258945Sroberto#include <sys/kernel.h> 94258945Sroberto#include <sys/socket.h> 95258945Sroberto#include <sys/queue.h> 96258945Sroberto 97258945Sroberto#include <net/if.h> 98258945Sroberto#include <net/if_arp.h> 99258945Sroberto#include <net/ethernet.h> 100258945Sroberto#include <net/if_dl.h> 101258945Sroberto#include <net/if_media.h> 102258945Sroberto 103258945Sroberto#include <net/bpf.h> 104258945Sroberto 105258945Sroberto#include <vm/vm.h> /* for vtophys */ 106258945Sroberto#include <vm/pmap.h> /* for vtophys */ 107258945Sroberto#include <machine/bus_memio.h> 108258945Sroberto#include <machine/bus_pio.h> 109258945Sroberto#include <machine/bus.h> 110258945Sroberto#include <machine/resource.h> 111258945Sroberto#include <sys/bus.h> 112258945Sroberto#include <sys/rman.h> 113258945Sroberto 114258945Sroberto#include <pci/pcireg.h> 115258945Sroberto#include <pci/pcivar.h> 116258945Sroberto 117258945Sroberto#include <dev/mii/mii.h> 118280849Scy#include <dev/mii/miivar.h> 119258945Sroberto 120258945Sroberto/* "controller miibus0" required. See GENERIC if you get errors here. */ 121258945Sroberto#include "miibus_if.h" 122258945Sroberto 123258945Sroberto#define WB_USEIOSPACE 124258945Sroberto 125258945Sroberto#include <pci/if_wbreg.h> 126258945Sroberto 127258945SrobertoMODULE_DEPEND(wb, miibus, 1, 1, 1); 128258945Sroberto 129258945Sroberto#ifndef lint 130258945Srobertostatic const char rcsid[] = 131258945Sroberto "$FreeBSD: head/sys/pci/if_wb.c 72813 2001-02-21 20:54:22Z wpaul $"; 132258945Sroberto#endif 133258945Sroberto 134258945Sroberto/* 135258945Sroberto * Various supported device vendors/types and their names. 136258945Sroberto */ 137258945Srobertostatic struct wb_type wb_devs[] = { 138258945Sroberto { WB_VENDORID, WB_DEVICEID_840F, 139258945Sroberto "Winbond W89C840F 10/100BaseTX" }, 140258945Sroberto { CP_VENDORID, CP_DEVICEID_RL100, 141258945Sroberto "Compex RL100-ATX 10/100baseTX" }, 142258945Sroberto { 0, 0, NULL } 143258945Sroberto}; 144258945Sroberto 145258945Srobertostatic int wb_probe __P((device_t)); 146258945Srobertostatic int wb_attach __P((device_t)); 147258945Srobertostatic int wb_detach __P((device_t)); 148258945Sroberto 149258945Srobertostatic void wb_bfree __P((caddr_t, void *args)); 150258945Srobertostatic int wb_newbuf __P((struct wb_softc *, 151258945Sroberto struct wb_chain_onefrag *, 152258945Sroberto struct mbuf *)); 153258945Srobertostatic int wb_encap __P((struct wb_softc *, struct wb_chain *, 154258945Sroberto struct mbuf *)); 155258945Sroberto 156258945Srobertostatic void wb_rxeof __P((struct wb_softc *)); 157258945Srobertostatic void wb_rxeoc __P((struct wb_softc *)); 158258945Srobertostatic void wb_txeof __P((struct wb_softc *)); 159258945Srobertostatic void wb_txeoc __P((struct wb_softc *)); 160258945Srobertostatic void wb_intr __P((void *)); 161258945Srobertostatic void wb_tick __P((void *)); 162258945Srobertostatic void wb_start __P((struct ifnet *)); 163258945Srobertostatic int wb_ioctl __P((struct ifnet *, u_long, caddr_t)); 164258945Srobertostatic void wb_init __P((void *)); 165258945Srobertostatic void wb_stop __P((struct wb_softc *)); 166258945Srobertostatic void wb_watchdog __P((struct ifnet *)); 167258945Srobertostatic void wb_shutdown __P((device_t)); 168258945Srobertostatic int wb_ifmedia_upd __P((struct ifnet *)); 169258945Srobertostatic void wb_ifmedia_sts __P((struct ifnet *, struct ifmediareq *)); 170258945Sroberto 171258945Srobertostatic void wb_eeprom_putbyte __P((struct wb_softc *, int)); 172258945Srobertostatic void wb_eeprom_getword __P((struct wb_softc *, int, u_int16_t *)); 173258945Srobertostatic void wb_read_eeprom __P((struct wb_softc *, caddr_t, int, 174258945Sroberto int, int)); 175258945Srobertostatic void wb_mii_sync __P((struct wb_softc *)); 176258945Srobertostatic void wb_mii_send __P((struct wb_softc *, u_int32_t, int)); 177258945Srobertostatic int wb_mii_readreg __P((struct wb_softc *, struct wb_mii_frame *)); 178258945Srobertostatic int wb_mii_writereg __P((struct wb_softc *, struct wb_mii_frame *)); 179258945Sroberto 180258945Srobertostatic void wb_setcfg __P((struct wb_softc *, u_int32_t)); 181258945Srobertostatic u_int8_t wb_calchash __P((caddr_t)); 182258945Srobertostatic void wb_setmulti __P((struct wb_softc *)); 183258945Srobertostatic void wb_reset __P((struct wb_softc *)); 184258945Srobertostatic void wb_fixmedia __P((struct wb_softc *)); 185258945Srobertostatic int wb_list_rx_init __P((struct wb_softc *)); 186258945Srobertostatic int wb_list_tx_init __P((struct wb_softc *)); 187258945Sroberto 188258945Srobertostatic int wb_miibus_readreg __P((device_t, int, int)); 189258945Srobertostatic int wb_miibus_writereg __P((device_t, int, int, int)); 190280849Scystatic void wb_miibus_statchg __P((device_t)); 191258945Sroberto 192258945Sroberto#ifdef WB_USEIOSPACE 193258945Sroberto#define WB_RES SYS_RES_IOPORT 194280849Scy#define WB_RID WB_PCI_LOIO 195280849Scy#else 196280849Scy#define WB_RES SYS_RES_MEMORY 197258945Sroberto#define WB_RID WB_PCI_LOMEM 198280849Scy#endif 199258945Sroberto 200280849Scystatic device_method_t wb_methods[] = { 201258945Sroberto /* Device interface */ 202258945Sroberto DEVMETHOD(device_probe, wb_probe), 203258945Sroberto DEVMETHOD(device_attach, wb_attach), 204258945Sroberto DEVMETHOD(device_detach, wb_detach), 205258945Sroberto DEVMETHOD(device_shutdown, wb_shutdown), 206258945Sroberto 207258945Sroberto /* bus interface, for miibus */ 208258945Sroberto DEVMETHOD(bus_print_child, bus_generic_print_child), 209258945Sroberto DEVMETHOD(bus_driver_added, bus_generic_driver_added), 210258945Sroberto 211258945Sroberto /* MII interface */ 212258945Sroberto DEVMETHOD(miibus_readreg, wb_miibus_readreg), 213258945Sroberto DEVMETHOD(miibus_writereg, wb_miibus_writereg), 214258945Sroberto DEVMETHOD(miibus_statchg, wb_miibus_statchg), 215258945Sroberto { 0, 0 } 216258945Sroberto}; 217258945Sroberto 218258945Srobertostatic driver_t wb_driver = { 219258945Sroberto "wb", 220258945Sroberto wb_methods, 221258945Sroberto sizeof(struct wb_softc) 222258945Sroberto}; 223258945Sroberto 224258945Srobertostatic devclass_t wb_devclass; 225258945Sroberto 226258945SrobertoDRIVER_MODULE(if_wb, pci, wb_driver, wb_devclass, 0, 0); 227258945SrobertoDRIVER_MODULE(miibus, wb, miibus_driver, miibus_devclass, 0, 0); 228258945Sroberto 229258945Sroberto#define WB_SETBIT(sc, reg, x) \ 230258945Sroberto CSR_WRITE_4(sc, reg, \ 231258945Sroberto CSR_READ_4(sc, reg) | x) 232258945Sroberto 233258945Sroberto#define WB_CLRBIT(sc, reg, x) \ 234258945Sroberto CSR_WRITE_4(sc, reg, \ 235258945Sroberto CSR_READ_4(sc, reg) & ~x) 236258945Sroberto 237258945Sroberto#define SIO_SET(x) \ 238258945Sroberto CSR_WRITE_4(sc, WB_SIO, \ 239258945Sroberto CSR_READ_4(sc, WB_SIO) | x) 240258945Sroberto 241258945Sroberto#define SIO_CLR(x) \ 242258945Sroberto CSR_WRITE_4(sc, WB_SIO, \ 243258945Sroberto CSR_READ_4(sc, WB_SIO) & ~x) 244258945Sroberto 245258945Sroberto/* 246258945Sroberto * Send a read command and address to the EEPROM, check for ACK. 247258945Sroberto */ 248258945Srobertostatic void wb_eeprom_putbyte(sc, addr) 249258945Sroberto struct wb_softc *sc; 250258945Sroberto int addr; 251258945Sroberto{ 252258945Sroberto register int d, i; 253258945Sroberto 254258945Sroberto d = addr | WB_EECMD_READ; 255258945Sroberto 256258945Sroberto /* 257258945Sroberto * Feed in each bit and stobe the clock. 258258945Sroberto */ 259258945Sroberto for (i = 0x400; i; i >>= 1) { 260258945Sroberto if (d & i) { 261258945Sroberto SIO_SET(WB_SIO_EE_DATAIN); 262258945Sroberto } else { 263258945Sroberto SIO_CLR(WB_SIO_EE_DATAIN); 264258945Sroberto } 265258945Sroberto DELAY(100); 266 SIO_SET(WB_SIO_EE_CLK); 267 DELAY(150); 268 SIO_CLR(WB_SIO_EE_CLK); 269 DELAY(100); 270 } 271 272 return; 273} 274 275/* 276 * Read a word of data stored in the EEPROM at address 'addr.' 277 */ 278static void wb_eeprom_getword(sc, addr, dest) 279 struct wb_softc *sc; 280 int addr; 281 u_int16_t *dest; 282{ 283 register int i; 284 u_int16_t word = 0; 285 286 /* Enter EEPROM access mode. */ 287 CSR_WRITE_4(sc, WB_SIO, WB_SIO_EESEL|WB_SIO_EE_CS); 288 289 /* 290 * Send address of word we want to read. 291 */ 292 wb_eeprom_putbyte(sc, addr); 293 294 CSR_WRITE_4(sc, WB_SIO, WB_SIO_EESEL|WB_SIO_EE_CS); 295 296 /* 297 * Start reading bits from EEPROM. 298 */ 299 for (i = 0x8000; i; i >>= 1) { 300 SIO_SET(WB_SIO_EE_CLK); 301 DELAY(100); 302 if (CSR_READ_4(sc, WB_SIO) & WB_SIO_EE_DATAOUT) 303 word |= i; 304 SIO_CLR(WB_SIO_EE_CLK); 305 DELAY(100); 306 } 307 308 /* Turn off EEPROM access mode. */ 309 CSR_WRITE_4(sc, WB_SIO, 0); 310 311 *dest = word; 312 313 return; 314} 315 316/* 317 * Read a sequence of words from the EEPROM. 318 */ 319static void wb_read_eeprom(sc, dest, off, cnt, swap) 320 struct wb_softc *sc; 321 caddr_t dest; 322 int off; 323 int cnt; 324 int swap; 325{ 326 int i; 327 u_int16_t word = 0, *ptr; 328 329 for (i = 0; i < cnt; i++) { 330 wb_eeprom_getword(sc, off + i, &word); 331 ptr = (u_int16_t *)(dest + (i * 2)); 332 if (swap) 333 *ptr = ntohs(word); 334 else 335 *ptr = word; 336 } 337 338 return; 339} 340 341/* 342 * Sync the PHYs by setting data bit and strobing the clock 32 times. 343 */ 344static void wb_mii_sync(sc) 345 struct wb_softc *sc; 346{ 347 register int i; 348 349 SIO_SET(WB_SIO_MII_DIR|WB_SIO_MII_DATAIN); 350 351 for (i = 0; i < 32; i++) { 352 SIO_SET(WB_SIO_MII_CLK); 353 DELAY(1); 354 SIO_CLR(WB_SIO_MII_CLK); 355 DELAY(1); 356 } 357 358 return; 359} 360 361/* 362 * Clock a series of bits through the MII. 363 */ 364static void wb_mii_send(sc, bits, cnt) 365 struct wb_softc *sc; 366 u_int32_t bits; 367 int cnt; 368{ 369 int i; 370 371 SIO_CLR(WB_SIO_MII_CLK); 372 373 for (i = (0x1 << (cnt - 1)); i; i >>= 1) { 374 if (bits & i) { 375 SIO_SET(WB_SIO_MII_DATAIN); 376 } else { 377 SIO_CLR(WB_SIO_MII_DATAIN); 378 } 379 DELAY(1); 380 SIO_CLR(WB_SIO_MII_CLK); 381 DELAY(1); 382 SIO_SET(WB_SIO_MII_CLK); 383 } 384} 385 386/* 387 * Read an PHY register through the MII. 388 */ 389static int wb_mii_readreg(sc, frame) 390 struct wb_softc *sc; 391 struct wb_mii_frame *frame; 392 393{ 394 int i, ack; 395 396 WB_LOCK(sc); 397 398 /* 399 * Set up frame for RX. 400 */ 401 frame->mii_stdelim = WB_MII_STARTDELIM; 402 frame->mii_opcode = WB_MII_READOP; 403 frame->mii_turnaround = 0; 404 frame->mii_data = 0; 405 406 CSR_WRITE_4(sc, WB_SIO, 0); 407 408 /* 409 * Turn on data xmit. 410 */ 411 SIO_SET(WB_SIO_MII_DIR); 412 413 wb_mii_sync(sc); 414 415 /* 416 * Send command/address info. 417 */ 418 wb_mii_send(sc, frame->mii_stdelim, 2); 419 wb_mii_send(sc, frame->mii_opcode, 2); 420 wb_mii_send(sc, frame->mii_phyaddr, 5); 421 wb_mii_send(sc, frame->mii_regaddr, 5); 422 423 /* Idle bit */ 424 SIO_CLR((WB_SIO_MII_CLK|WB_SIO_MII_DATAIN)); 425 DELAY(1); 426 SIO_SET(WB_SIO_MII_CLK); 427 DELAY(1); 428 429 /* Turn off xmit. */ 430 SIO_CLR(WB_SIO_MII_DIR); 431 /* Check for ack */ 432 SIO_CLR(WB_SIO_MII_CLK); 433 DELAY(1); 434 SIO_SET(WB_SIO_MII_CLK); 435 DELAY(1); 436 ack = CSR_READ_4(sc, WB_SIO) & WB_SIO_MII_DATAOUT; 437 SIO_CLR(WB_SIO_MII_CLK); 438 DELAY(1); 439 SIO_SET(WB_SIO_MII_CLK); 440 DELAY(1); 441 442 /* 443 * Now try reading data bits. If the ack failed, we still 444 * need to clock through 16 cycles to keep the PHY(s) in sync. 445 */ 446 if (ack) { 447 for(i = 0; i < 16; i++) { 448 SIO_CLR(WB_SIO_MII_CLK); 449 DELAY(1); 450 SIO_SET(WB_SIO_MII_CLK); 451 DELAY(1); 452 } 453 goto fail; 454 } 455 456 for (i = 0x8000; i; i >>= 1) { 457 SIO_CLR(WB_SIO_MII_CLK); 458 DELAY(1); 459 if (!ack) { 460 if (CSR_READ_4(sc, WB_SIO) & WB_SIO_MII_DATAOUT) 461 frame->mii_data |= i; 462 DELAY(1); 463 } 464 SIO_SET(WB_SIO_MII_CLK); 465 DELAY(1); 466 } 467 468fail: 469 470 SIO_CLR(WB_SIO_MII_CLK); 471 DELAY(1); 472 SIO_SET(WB_SIO_MII_CLK); 473 DELAY(1); 474 475 WB_UNLOCK(sc); 476 477 if (ack) 478 return(1); 479 return(0); 480} 481 482/* 483 * Write to a PHY register through the MII. 484 */ 485static int wb_mii_writereg(sc, frame) 486 struct wb_softc *sc; 487 struct wb_mii_frame *frame; 488 489{ 490 WB_LOCK(sc); 491 492 /* 493 * Set up frame for TX. 494 */ 495 496 frame->mii_stdelim = WB_MII_STARTDELIM; 497 frame->mii_opcode = WB_MII_WRITEOP; 498 frame->mii_turnaround = WB_MII_TURNAROUND; 499 500 /* 501 * Turn on data output. 502 */ 503 SIO_SET(WB_SIO_MII_DIR); 504 505 wb_mii_sync(sc); 506 507 wb_mii_send(sc, frame->mii_stdelim, 2); 508 wb_mii_send(sc, frame->mii_opcode, 2); 509 wb_mii_send(sc, frame->mii_phyaddr, 5); 510 wb_mii_send(sc, frame->mii_regaddr, 5); 511 wb_mii_send(sc, frame->mii_turnaround, 2); 512 wb_mii_send(sc, frame->mii_data, 16); 513 514 /* Idle bit. */ 515 SIO_SET(WB_SIO_MII_CLK); 516 DELAY(1); 517 SIO_CLR(WB_SIO_MII_CLK); 518 DELAY(1); 519 520 /* 521 * Turn off xmit. 522 */ 523 SIO_CLR(WB_SIO_MII_DIR); 524 525 WB_UNLOCK(sc); 526 527 return(0); 528} 529 530static int wb_miibus_readreg(dev, phy, reg) 531 device_t dev; 532 int phy, reg; 533{ 534 struct wb_softc *sc; 535 struct wb_mii_frame frame; 536 537 sc = device_get_softc(dev); 538 539 bzero((char *)&frame, sizeof(frame)); 540 541 frame.mii_phyaddr = phy; 542 frame.mii_regaddr = reg; 543 wb_mii_readreg(sc, &frame); 544 545 return(frame.mii_data); 546} 547 548static int wb_miibus_writereg(dev, phy, reg, data) 549 device_t dev; 550 int phy, reg, data; 551{ 552 struct wb_softc *sc; 553 struct wb_mii_frame frame; 554 555 sc = device_get_softc(dev); 556 557 bzero((char *)&frame, sizeof(frame)); 558 559 frame.mii_phyaddr = phy; 560 frame.mii_regaddr = reg; 561 frame.mii_data = data; 562 563 wb_mii_writereg(sc, &frame); 564 565 return(0); 566} 567 568static void wb_miibus_statchg(dev) 569 device_t dev; 570{ 571 struct wb_softc *sc; 572 struct mii_data *mii; 573 574 sc = device_get_softc(dev); 575 WB_LOCK(sc); 576 mii = device_get_softc(sc->wb_miibus); 577 wb_setcfg(sc, mii->mii_media_active); 578 WB_UNLOCK(sc); 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 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 645 if (ifma->ifma_addr->sa_family != AF_LINK) 646 continue; 647 h = wb_calchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr)); 648 if (h < 32) 649 hashes[0] |= (1 << h); 650 else 651 hashes[1] |= (1 << (h - 32)); 652 mcnt++; 653 } 654 655 if (mcnt) 656 rxfilt |= WB_NETCFG_RX_MULTI; 657 else 658 rxfilt &= ~WB_NETCFG_RX_MULTI; 659 660 CSR_WRITE_4(sc, WB_MAR0, hashes[0]); 661 CSR_WRITE_4(sc, WB_MAR1, hashes[1]); 662 CSR_WRITE_4(sc, WB_NETCFG, rxfilt); 663 664 return; 665} 666 667/* 668 * The Winbond manual states that in order to fiddle with the 669 * 'full-duplex' and '100Mbps' bits in the netconfig register, we 670 * first have to put the transmit and/or receive logic in the idle state. 671 */ 672static void wb_setcfg(sc, media) 673 struct wb_softc *sc; 674 u_int32_t media; 675{ 676 int i, restart = 0; 677 678 if (CSR_READ_4(sc, WB_NETCFG) & (WB_NETCFG_TX_ON|WB_NETCFG_RX_ON)) { 679 restart = 1; 680 WB_CLRBIT(sc, WB_NETCFG, (WB_NETCFG_TX_ON|WB_NETCFG_RX_ON)); 681 682 for (i = 0; i < WB_TIMEOUT; i++) { 683 DELAY(10); 684 if ((CSR_READ_4(sc, WB_ISR) & WB_ISR_TX_IDLE) && 685 (CSR_READ_4(sc, WB_ISR) & WB_ISR_RX_IDLE)) 686 break; 687 } 688 689 if (i == WB_TIMEOUT) 690 printf("wb%d: failed to force tx and " 691 "rx to idle state\n", sc->wb_unit); 692 } 693 694 if (IFM_SUBTYPE(media) == IFM_10_T) 695 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_100MBPS); 696 else 697 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_100MBPS); 698 699 if ((media & IFM_GMASK) == IFM_FDX) 700 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_FULLDUPLEX); 701 else 702 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_FULLDUPLEX); 703 704 if (restart) 705 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON|WB_NETCFG_RX_ON); 706 707 return; 708} 709 710static void wb_reset(sc) 711 struct wb_softc *sc; 712{ 713 register int i; 714 struct mii_data *mii; 715 716 CSR_WRITE_4(sc, WB_NETCFG, 0); 717 CSR_WRITE_4(sc, WB_BUSCTL, 0); 718 CSR_WRITE_4(sc, WB_TXADDR, 0); 719 CSR_WRITE_4(sc, WB_RXADDR, 0); 720 721 WB_SETBIT(sc, WB_BUSCTL, WB_BUSCTL_RESET); 722 WB_SETBIT(sc, WB_BUSCTL, WB_BUSCTL_RESET); 723 724 for (i = 0; i < WB_TIMEOUT; i++) { 725 DELAY(10); 726 if (!(CSR_READ_4(sc, WB_BUSCTL) & WB_BUSCTL_RESET)) 727 break; 728 } 729 if (i == WB_TIMEOUT) 730 printf("wb%d: reset never completed!\n", sc->wb_unit); 731 732 /* Wait a little while for the chip to get its brains in order. */ 733 DELAY(1000); 734 735 if (sc->wb_miibus == NULL) 736 return; 737 738 mii = device_get_softc(sc->wb_miibus); 739 if (mii == NULL) 740 return; 741 742 if (mii->mii_instance) { 743 struct mii_softc *miisc; 744 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 745 mii_phy_reset(miisc); 746 } 747 748 return; 749} 750 751static void wb_fixmedia(sc) 752 struct wb_softc *sc; 753{ 754 struct mii_data *mii = NULL; 755 struct ifnet *ifp; 756 u_int32_t media; 757 758 if (sc->wb_miibus == NULL) 759 return; 760 761 mii = device_get_softc(sc->wb_miibus); 762 ifp = &sc->arpcom.ac_if; 763 764 mii_pollstat(mii); 765 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) { 766 media = mii->mii_media_active & ~IFM_10_T; 767 media |= IFM_100_TX; 768 } else if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) { 769 media = mii->mii_media_active & ~IFM_100_TX; 770 media |= IFM_10_T; 771 } else 772 return; 773 774 ifmedia_set(&mii->mii_media, media); 775 776 return; 777} 778 779/* 780 * Probe for a Winbond chip. Check the PCI vendor and device 781 * IDs against our list and return a device name if we find a match. 782 */ 783static int wb_probe(dev) 784 device_t dev; 785{ 786 struct wb_type *t; 787 788 t = wb_devs; 789 790 while(t->wb_name != NULL) { 791 if ((pci_get_vendor(dev) == t->wb_vid) && 792 (pci_get_device(dev) == t->wb_did)) { 793 device_set_desc(dev, t->wb_name); 794 return(0); 795 } 796 t++; 797 } 798 799 return(ENXIO); 800} 801 802/* 803 * Attach the interface. Allocate softc structures, do ifmedia 804 * setup and ethernet/BPF attach. 805 */ 806static int wb_attach(dev) 807 device_t dev; 808{ 809 u_char eaddr[ETHER_ADDR_LEN]; 810 u_int32_t command; 811 struct wb_softc *sc; 812 struct ifnet *ifp; 813 int unit, error = 0, rid; 814 815 sc = device_get_softc(dev); 816 unit = device_get_unit(dev); 817 818 mtx_init(&sc->wb_mtx, device_get_nameunit(dev), MTX_DEF | MTX_RECURSE); 819 WB_LOCK(sc); 820 821 /* 822 * Handle power management nonsense. 823 */ 824 825 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) { 826 u_int32_t iobase, membase, irq; 827 828 /* Save important PCI config data. */ 829 iobase = pci_read_config(dev, WB_PCI_LOIO, 4); 830 membase = pci_read_config(dev, WB_PCI_LOMEM, 4); 831 irq = pci_read_config(dev, WB_PCI_INTLINE, 4); 832 833 /* Reset the power state. */ 834 printf("wb%d: chip is in D%d power mode " 835 "-- setting to D0\n", unit, 836 pci_get_powerstate(dev)); 837 pci_set_powerstate(dev, PCI_POWERSTATE_D0); 838 839 /* Restore PCI config data. */ 840 pci_write_config(dev, WB_PCI_LOIO, iobase, 4); 841 pci_write_config(dev, WB_PCI_LOMEM, membase, 4); 842 pci_write_config(dev, WB_PCI_INTLINE, irq, 4); 843 } 844 845 /* 846 * Map control/status registers. 847 */ 848 pci_enable_busmaster(dev); 849 pci_enable_io(dev, PCIM_CMD_PORTEN); 850 pci_enable_io(dev, PCIM_CMD_MEMEN); 851 command = pci_read_config(dev, PCIR_COMMAND, 4); 852 853#ifdef WB_USEIOSPACE 854 if (!(command & PCIM_CMD_PORTEN)) { 855 printf("wb%d: failed to enable I/O ports!\n", unit); 856 error = ENXIO; 857 goto fail; 858 } 859#else 860 if (!(command & PCIM_CMD_MEMEN)) { 861 printf("wb%d: failed to enable memory mapping!\n", unit); 862 error = ENXIO; 863 goto fail; 864 } 865#endif 866 867 rid = WB_RID; 868 sc->wb_res = bus_alloc_resource(dev, WB_RES, &rid, 869 0, ~0, 1, RF_ACTIVE); 870 871 if (sc->wb_res == NULL) { 872 printf("wb%d: couldn't map ports/memory\n", unit); 873 error = ENXIO; 874 goto fail; 875 } 876 877 sc->wb_btag = rman_get_bustag(sc->wb_res); 878 sc->wb_bhandle = rman_get_bushandle(sc->wb_res); 879 880 /* Allocate interrupt */ 881 rid = 0; 882 sc->wb_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 883 RF_SHAREABLE | RF_ACTIVE); 884 885 if (sc->wb_irq == NULL) { 886 printf("wb%d: couldn't map interrupt\n", unit); 887 bus_release_resource(dev, WB_RES, WB_RID, sc->wb_res); 888 error = ENXIO; 889 goto fail; 890 } 891 892 error = bus_setup_intr(dev, sc->wb_irq, INTR_TYPE_NET, 893 wb_intr, sc, &sc->wb_intrhand); 894 895 if (error) { 896 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->wb_irq); 897 bus_release_resource(dev, WB_RES, WB_RID, sc->wb_res); 898 printf("wb%d: couldn't set up irq\n", unit); 899 goto fail; 900 } 901 902 /* Save the cache line size. */ 903 sc->wb_cachesize = pci_read_config(dev, WB_PCI_CACHELEN, 4) & 0xFF; 904 905 /* Reset the adapter. */ 906 wb_reset(sc); 907 908 /* 909 * Get station address from the EEPROM. 910 */ 911 wb_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 0); 912 913 /* 914 * A Winbond chip was detected. Inform the world. 915 */ 916 printf("wb%d: Ethernet address: %6D\n", unit, eaddr, ":"); 917 918 sc->wb_unit = unit; 919 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); 920 921 sc->wb_ldata = contigmalloc(sizeof(struct wb_list_data) + 8, M_DEVBUF, 922 M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0); 923 924 if (sc->wb_ldata == NULL) { 925 printf("wb%d: no memory for list buffers!\n", unit); 926 bus_teardown_intr(dev, sc->wb_irq, sc->wb_intrhand); 927 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->wb_irq); 928 bus_release_resource(dev, WB_RES, WB_RID, sc->wb_res); 929 error = ENXIO; 930 goto fail; 931 } 932 933 bzero(sc->wb_ldata, sizeof(struct wb_list_data)); 934 935 ifp = &sc->arpcom.ac_if; 936 ifp->if_softc = sc; 937 ifp->if_unit = unit; 938 ifp->if_name = "wb"; 939 ifp->if_mtu = ETHERMTU; 940 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 941 ifp->if_ioctl = wb_ioctl; 942 ifp->if_output = ether_output; 943 ifp->if_start = wb_start; 944 ifp->if_watchdog = wb_watchdog; 945 ifp->if_init = wb_init; 946 ifp->if_baudrate = 10000000; 947 ifp->if_snd.ifq_maxlen = WB_TX_LIST_CNT - 1; 948 949 /* 950 * Do MII setup. 951 */ 952 if (mii_phy_probe(dev, &sc->wb_miibus, 953 wb_ifmedia_upd, wb_ifmedia_sts)) { 954 bus_teardown_intr(dev, sc->wb_irq, sc->wb_intrhand); 955 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->wb_irq); 956 bus_release_resource(dev, WB_RES, WB_RID, sc->wb_res); 957 free(sc->wb_ldata_ptr, M_DEVBUF); 958 error = ENXIO; 959 goto fail; 960 } 961 962 /* 963 * Call MI attach routine. 964 */ 965 ether_ifattach(ifp, ETHER_BPF_SUPPORTED); 966 WB_UNLOCK(sc); 967 return(0); 968 969fail: 970 if (error) 971 device_delete_child(dev, sc->wb_miibus); 972 WB_UNLOCK(sc); 973 mtx_destroy(&sc->wb_mtx); 974 975 return(error); 976} 977 978static int wb_detach(dev) 979 device_t dev; 980{ 981 struct wb_softc *sc; 982 struct ifnet *ifp; 983 984 sc = device_get_softc(dev); 985 WB_LOCK(sc); 986 ifp = &sc->arpcom.ac_if; 987 988 wb_stop(sc); 989 ether_ifdetach(ifp, ETHER_BPF_SUPPORTED); 990 991 /* Delete any miibus and phy devices attached to this interface */ 992 bus_generic_detach(dev); 993 device_delete_child(dev, sc->wb_miibus); 994 995 bus_teardown_intr(dev, sc->wb_irq, sc->wb_intrhand); 996 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->wb_irq); 997 bus_release_resource(dev, WB_RES, WB_RID, sc->wb_res); 998 999 free(sc->wb_ldata_ptr, M_DEVBUF); 1000 1001 WB_UNLOCK(sc); 1002 mtx_destroy(&sc->wb_mtx); 1003 1004 return(0); 1005} 1006 1007/* 1008 * Initialize the transmit descriptors. 1009 */ 1010static int wb_list_tx_init(sc) 1011 struct wb_softc *sc; 1012{ 1013 struct wb_chain_data *cd; 1014 struct wb_list_data *ld; 1015 int i; 1016 1017 cd = &sc->wb_cdata; 1018 ld = sc->wb_ldata; 1019 1020 for (i = 0; i < WB_TX_LIST_CNT; i++) { 1021 cd->wb_tx_chain[i].wb_ptr = &ld->wb_tx_list[i]; 1022 if (i == (WB_TX_LIST_CNT - 1)) { 1023 cd->wb_tx_chain[i].wb_nextdesc = 1024 &cd->wb_tx_chain[0]; 1025 } else { 1026 cd->wb_tx_chain[i].wb_nextdesc = 1027 &cd->wb_tx_chain[i + 1]; 1028 } 1029 } 1030 1031 cd->wb_tx_free = &cd->wb_tx_chain[0]; 1032 cd->wb_tx_tail = cd->wb_tx_head = NULL; 1033 1034 return(0); 1035} 1036 1037 1038/* 1039 * Initialize the RX descriptors and allocate mbufs for them. Note that 1040 * we arrange the descriptors in a closed ring, so that the last descriptor 1041 * points back to the first. 1042 */ 1043static int wb_list_rx_init(sc) 1044 struct wb_softc *sc; 1045{ 1046 struct wb_chain_data *cd; 1047 struct wb_list_data *ld; 1048 int i; 1049 1050 cd = &sc->wb_cdata; 1051 ld = sc->wb_ldata; 1052 1053 for (i = 0; i < WB_RX_LIST_CNT; i++) { 1054 cd->wb_rx_chain[i].wb_ptr = 1055 (struct wb_desc *)&ld->wb_rx_list[i]; 1056 cd->wb_rx_chain[i].wb_buf = (void *)&ld->wb_rxbufs[i]; 1057 if (wb_newbuf(sc, &cd->wb_rx_chain[i], NULL) == ENOBUFS) 1058 return(ENOBUFS); 1059 if (i == (WB_RX_LIST_CNT - 1)) { 1060 cd->wb_rx_chain[i].wb_nextdesc = &cd->wb_rx_chain[0]; 1061 ld->wb_rx_list[i].wb_next = 1062 vtophys(&ld->wb_rx_list[0]); 1063 } else { 1064 cd->wb_rx_chain[i].wb_nextdesc = 1065 &cd->wb_rx_chain[i + 1]; 1066 ld->wb_rx_list[i].wb_next = 1067 vtophys(&ld->wb_rx_list[i + 1]); 1068 } 1069 } 1070 1071 cd->wb_rx_head = &cd->wb_rx_chain[0]; 1072 1073 return(0); 1074} 1075 1076static void wb_bfree(buf, args) 1077 caddr_t buf; 1078 void *args; 1079{ 1080 return; 1081} 1082 1083/* 1084 * Initialize an RX descriptor and attach an MBUF cluster. 1085 */ 1086static int wb_newbuf(sc, c, m) 1087 struct wb_softc *sc; 1088 struct wb_chain_onefrag *c; 1089 struct mbuf *m; 1090{ 1091 struct mbuf *m_new = NULL; 1092 1093 if (m == NULL) { 1094 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 1095 if (m_new == NULL) { 1096 printf("wb%d: no memory for rx " 1097 "list -- packet dropped!\n", sc->wb_unit); 1098 return(ENOBUFS); 1099 } 1100 m_new->m_data = c->wb_buf; 1101 m_new->m_pkthdr.len = m_new->m_len = WB_BUFBYTES; 1102 MEXTADD(m_new, c->wb_buf, WB_BUFBYTES, wb_bfree, NULL, 0, 1103 EXT_NET_DRV); 1104 } else { 1105 m_new = m; 1106 m_new->m_len = m_new->m_pkthdr.len = WB_BUFBYTES; 1107 m_new->m_data = m_new->m_ext.ext_buf; 1108 } 1109 1110 m_adj(m_new, sizeof(u_int64_t)); 1111 1112 c->wb_mbuf = m_new; 1113 c->wb_ptr->wb_data = vtophys(mtod(m_new, caddr_t)); 1114 c->wb_ptr->wb_ctl = WB_RXCTL_RLINK | 1536; 1115 c->wb_ptr->wb_status = WB_RXSTAT; 1116 1117 return(0); 1118} 1119 1120/* 1121 * A frame has been uploaded: pass the resulting mbuf chain up to 1122 * the higher level protocols. 1123 */ 1124static void wb_rxeof(sc) 1125 struct wb_softc *sc; 1126{ 1127 struct ether_header *eh; 1128 struct mbuf *m = NULL; 1129 struct ifnet *ifp; 1130 struct wb_chain_onefrag *cur_rx; 1131 int total_len = 0; 1132 u_int32_t rxstat; 1133 1134 ifp = &sc->arpcom.ac_if; 1135 1136 while(!((rxstat = sc->wb_cdata.wb_rx_head->wb_ptr->wb_status) & 1137 WB_RXSTAT_OWN)) { 1138 struct mbuf *m0 = NULL; 1139 1140 cur_rx = sc->wb_cdata.wb_rx_head; 1141 sc->wb_cdata.wb_rx_head = cur_rx->wb_nextdesc; 1142 1143 m = cur_rx->wb_mbuf; 1144 1145 if ((rxstat & WB_RXSTAT_MIIERR) || 1146 (WB_RXBYTES(cur_rx->wb_ptr->wb_status) < WB_MIN_FRAMELEN) || 1147 (WB_RXBYTES(cur_rx->wb_ptr->wb_status) > 1536) || 1148 !(rxstat & WB_RXSTAT_LASTFRAG) || 1149 !(rxstat & WB_RXSTAT_RXCMP)) { 1150 ifp->if_ierrors++; 1151 wb_newbuf(sc, cur_rx, m); 1152 printf("wb%x: receiver babbling: possible chip " 1153 "bug, forcing reset\n", sc->wb_unit); 1154 wb_fixmedia(sc); 1155 wb_reset(sc); 1156 wb_init(sc); 1157 return; 1158 } 1159 1160 if (rxstat & WB_RXSTAT_RXERR) { 1161 ifp->if_ierrors++; 1162 wb_newbuf(sc, cur_rx, m); 1163 break; 1164 } 1165 1166 /* No errors; receive the packet. */ 1167 total_len = WB_RXBYTES(cur_rx->wb_ptr->wb_status); 1168 1169 /* 1170 * XXX The Winbond chip includes the CRC with every 1171 * received frame, and there's no way to turn this 1172 * behavior off (at least, I can't find anything in 1173 * the manual that explains how to do it) so we have 1174 * to trim off the CRC manually. 1175 */ 1176 total_len -= ETHER_CRC_LEN; 1177 1178 m0 = m_devget(mtod(m, char *) - ETHER_ALIGN, 1179 total_len + ETHER_ALIGN, 0, ifp, NULL); 1180 wb_newbuf(sc, cur_rx, m); 1181 if (m0 == NULL) { 1182 ifp->if_ierrors++; 1183 break; 1184 } 1185 m_adj(m0, ETHER_ALIGN); 1186 m = m0; 1187 1188 ifp->if_ipackets++; 1189 eh = mtod(m, struct ether_header *); 1190 1191 /* Remove header from mbuf and pass it on. */ 1192 m_adj(m, sizeof(struct ether_header)); 1193 ether_input(ifp, eh, m); 1194 } 1195} 1196 1197void wb_rxeoc(sc) 1198 struct wb_softc *sc; 1199{ 1200 wb_rxeof(sc); 1201 1202 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON); 1203 CSR_WRITE_4(sc, WB_RXADDR, vtophys(&sc->wb_ldata->wb_rx_list[0])); 1204 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON); 1205 if (CSR_READ_4(sc, WB_ISR) & WB_RXSTATE_SUSPEND) 1206 CSR_WRITE_4(sc, WB_RXSTART, 0xFFFFFFFF); 1207 1208 return; 1209} 1210 1211/* 1212 * A frame was downloaded to the chip. It's safe for us to clean up 1213 * the list buffers. 1214 */ 1215static void wb_txeof(sc) 1216 struct wb_softc *sc; 1217{ 1218 struct wb_chain *cur_tx; 1219 struct ifnet *ifp; 1220 1221 ifp = &sc->arpcom.ac_if; 1222 1223 /* Clear the timeout timer. */ 1224 ifp->if_timer = 0; 1225 1226 if (sc->wb_cdata.wb_tx_head == NULL) 1227 return; 1228 1229 /* 1230 * Go through our tx list and free mbufs for those 1231 * frames that have been transmitted. 1232 */ 1233 while(sc->wb_cdata.wb_tx_head->wb_mbuf != NULL) { 1234 u_int32_t txstat; 1235 1236 cur_tx = sc->wb_cdata.wb_tx_head; 1237 txstat = WB_TXSTATUS(cur_tx); 1238 1239 if ((txstat & WB_TXSTAT_OWN) || txstat == WB_UNSENT) 1240 break; 1241 1242 if (txstat & WB_TXSTAT_TXERR) { 1243 ifp->if_oerrors++; 1244 if (txstat & WB_TXSTAT_ABORT) 1245 ifp->if_collisions++; 1246 if (txstat & WB_TXSTAT_LATECOLL) 1247 ifp->if_collisions++; 1248 } 1249 1250 ifp->if_collisions += (txstat & WB_TXSTAT_COLLCNT) >> 3; 1251 1252 ifp->if_opackets++; 1253 m_freem(cur_tx->wb_mbuf); 1254 cur_tx->wb_mbuf = NULL; 1255 1256 if (sc->wb_cdata.wb_tx_head == sc->wb_cdata.wb_tx_tail) { 1257 sc->wb_cdata.wb_tx_head = NULL; 1258 sc->wb_cdata.wb_tx_tail = NULL; 1259 break; 1260 } 1261 1262 sc->wb_cdata.wb_tx_head = cur_tx->wb_nextdesc; 1263 } 1264 1265 return; 1266} 1267 1268/* 1269 * TX 'end of channel' interrupt handler. 1270 */ 1271static void wb_txeoc(sc) 1272 struct wb_softc *sc; 1273{ 1274 struct ifnet *ifp; 1275 1276 ifp = &sc->arpcom.ac_if; 1277 1278 ifp->if_timer = 0; 1279 1280 if (sc->wb_cdata.wb_tx_head == NULL) { 1281 ifp->if_flags &= ~IFF_OACTIVE; 1282 sc->wb_cdata.wb_tx_tail = NULL; 1283 } else { 1284 if (WB_TXOWN(sc->wb_cdata.wb_tx_head) == WB_UNSENT) { 1285 WB_TXOWN(sc->wb_cdata.wb_tx_head) = WB_TXSTAT_OWN; 1286 ifp->if_timer = 5; 1287 CSR_WRITE_4(sc, WB_TXSTART, 0xFFFFFFFF); 1288 } 1289 } 1290 1291 return; 1292} 1293 1294static void wb_intr(arg) 1295 void *arg; 1296{ 1297 struct wb_softc *sc; 1298 struct ifnet *ifp; 1299 u_int32_t status; 1300 1301 sc = arg; 1302 WB_LOCK(sc); 1303 ifp = &sc->arpcom.ac_if; 1304 1305 if (!(ifp->if_flags & IFF_UP)) { 1306 WB_UNLOCK(sc); 1307 return; 1308 } 1309 1310 /* Disable interrupts. */ 1311 CSR_WRITE_4(sc, WB_IMR, 0x00000000); 1312 1313 for (;;) { 1314 1315 status = CSR_READ_4(sc, WB_ISR); 1316 if (status) 1317 CSR_WRITE_4(sc, WB_ISR, status); 1318 1319 if ((status & WB_INTRS) == 0) 1320 break; 1321 1322 if ((status & WB_ISR_RX_NOBUF) || (status & WB_ISR_RX_ERR)) { 1323 ifp->if_ierrors++; 1324 wb_reset(sc); 1325 if (status & WB_ISR_RX_ERR) 1326 wb_fixmedia(sc); 1327 wb_init(sc); 1328 continue; 1329 } 1330 1331 if (status & WB_ISR_RX_OK) 1332 wb_rxeof(sc); 1333 1334 if (status & WB_ISR_RX_IDLE) 1335 wb_rxeoc(sc); 1336 1337 if (status & WB_ISR_TX_OK) 1338 wb_txeof(sc); 1339 1340 if (status & WB_ISR_TX_NOBUF) 1341 wb_txeoc(sc); 1342 1343 if (status & WB_ISR_TX_IDLE) { 1344 wb_txeof(sc); 1345 if (sc->wb_cdata.wb_tx_head != NULL) { 1346 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON); 1347 CSR_WRITE_4(sc, WB_TXSTART, 0xFFFFFFFF); 1348 } 1349 } 1350 1351 if (status & WB_ISR_TX_UNDERRUN) { 1352 ifp->if_oerrors++; 1353 wb_txeof(sc); 1354 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON); 1355 /* Jack up TX threshold */ 1356 sc->wb_txthresh += WB_TXTHRESH_CHUNK; 1357 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_THRESH); 1358 WB_SETBIT(sc, WB_NETCFG, WB_TXTHRESH(sc->wb_txthresh)); 1359 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON); 1360 } 1361 1362 if (status & WB_ISR_BUS_ERR) { 1363 wb_reset(sc); 1364 wb_init(sc); 1365 } 1366 1367 } 1368 1369 /* Re-enable interrupts. */ 1370 CSR_WRITE_4(sc, WB_IMR, WB_INTRS); 1371 1372 if (ifp->if_snd.ifq_head != NULL) { 1373 wb_start(ifp); 1374 } 1375 1376 WB_UNLOCK(sc); 1377 1378 return; 1379} 1380 1381static void wb_tick(xsc) 1382 void *xsc; 1383{ 1384 struct wb_softc *sc; 1385 struct mii_data *mii; 1386 1387 sc = xsc; 1388 WB_LOCK(sc); 1389 mii = device_get_softc(sc->wb_miibus); 1390 1391 mii_tick(mii); 1392 1393 sc->wb_stat_ch = timeout(wb_tick, sc, hz); 1394 1395 WB_UNLOCK(sc); 1396 1397 return; 1398} 1399 1400/* 1401 * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data 1402 * pointers to the fragment pointers. 1403 */ 1404static int wb_encap(sc, c, m_head) 1405 struct wb_softc *sc; 1406 struct wb_chain *c; 1407 struct mbuf *m_head; 1408{ 1409 int frag = 0; 1410 struct wb_desc *f = NULL; 1411 int total_len; 1412 struct mbuf *m; 1413 1414 /* 1415 * Start packing the mbufs in this chain into 1416 * the fragment pointers. Stop when we run out 1417 * of fragments or hit the end of the mbuf chain. 1418 */ 1419 m = m_head; 1420 total_len = 0; 1421 1422 for (m = m_head, frag = 0; m != NULL; m = m->m_next) { 1423 if (m->m_len != 0) { 1424 if (frag == WB_MAXFRAGS) 1425 break; 1426 total_len += m->m_len; 1427 f = &c->wb_ptr->wb_frag[frag]; 1428 f->wb_ctl = WB_TXCTL_TLINK | m->m_len; 1429 if (frag == 0) { 1430 f->wb_ctl |= WB_TXCTL_FIRSTFRAG; 1431 f->wb_status = 0; 1432 } else 1433 f->wb_status = WB_TXSTAT_OWN; 1434 f->wb_next = vtophys(&c->wb_ptr->wb_frag[frag + 1]); 1435 f->wb_data = vtophys(mtod(m, vm_offset_t)); 1436 frag++; 1437 } 1438 } 1439 1440 /* 1441 * Handle special case: we used up all 16 fragments, 1442 * but we have more mbufs left in the chain. Copy the 1443 * data into an mbuf cluster. Note that we don't 1444 * bother clearing the values in the other fragment 1445 * pointers/counters; it wouldn't gain us anything, 1446 * and would waste cycles. 1447 */ 1448 if (m != NULL) { 1449 struct mbuf *m_new = NULL; 1450 1451 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 1452 if (m_new == NULL) { 1453 printf("wb%d: no memory for tx list", sc->wb_unit); 1454 return(1); 1455 } 1456 if (m_head->m_pkthdr.len > MHLEN) { 1457 MCLGET(m_new, M_DONTWAIT); 1458 if (!(m_new->m_flags & M_EXT)) { 1459 m_freem(m_new); 1460 printf("wb%d: no memory for tx list", 1461 sc->wb_unit); 1462 return(1); 1463 } 1464 } 1465 m_copydata(m_head, 0, m_head->m_pkthdr.len, 1466 mtod(m_new, caddr_t)); 1467 m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len; 1468 m_freem(m_head); 1469 m_head = m_new; 1470 f = &c->wb_ptr->wb_frag[0]; 1471 f->wb_status = 0; 1472 f->wb_data = vtophys(mtod(m_new, caddr_t)); 1473 f->wb_ctl = total_len = m_new->m_len; 1474 f->wb_ctl |= WB_TXCTL_TLINK|WB_TXCTL_FIRSTFRAG; 1475 frag = 1; 1476 } 1477 1478 if (total_len < WB_MIN_FRAMELEN) { 1479 f = &c->wb_ptr->wb_frag[frag]; 1480 f->wb_ctl = WB_MIN_FRAMELEN - total_len; 1481 f->wb_data = vtophys(&sc->wb_cdata.wb_pad); 1482 f->wb_ctl |= WB_TXCTL_TLINK; 1483 f->wb_status = WB_TXSTAT_OWN; 1484 frag++; 1485 } 1486 1487 c->wb_mbuf = m_head; 1488 c->wb_lastdesc = frag - 1; 1489 WB_TXCTL(c) |= WB_TXCTL_LASTFRAG; 1490 WB_TXNEXT(c) = vtophys(&c->wb_nextdesc->wb_ptr->wb_frag[0]); 1491 1492 return(0); 1493} 1494 1495/* 1496 * Main transmit routine. To avoid having to do mbuf copies, we put pointers 1497 * to the mbuf data regions directly in the transmit lists. We also save a 1498 * copy of the pointers since the transmit list fragment pointers are 1499 * physical addresses. 1500 */ 1501 1502static void wb_start(ifp) 1503 struct ifnet *ifp; 1504{ 1505 struct wb_softc *sc; 1506 struct mbuf *m_head = NULL; 1507 struct wb_chain *cur_tx = NULL, *start_tx; 1508 1509 sc = ifp->if_softc; 1510 WB_LOCK(sc); 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 WB_UNLOCK(sc); 1519 return; 1520 } 1521 1522 start_tx = sc->wb_cdata.wb_tx_free; 1523 1524 while(sc->wb_cdata.wb_tx_free->wb_mbuf == NULL) { 1525 IF_DEQUEUE(&ifp->if_snd, m_head); 1526 if (m_head == NULL) 1527 break; 1528 1529 /* Pick a descriptor off the free list. */ 1530 cur_tx = sc->wb_cdata.wb_tx_free; 1531 sc->wb_cdata.wb_tx_free = cur_tx->wb_nextdesc; 1532 1533 /* Pack the data into the descriptor. */ 1534 wb_encap(sc, cur_tx, m_head); 1535 1536 if (cur_tx != start_tx) 1537 WB_TXOWN(cur_tx) = WB_TXSTAT_OWN; 1538 1539 /* 1540 * If there's a BPF listener, bounce a copy of this frame 1541 * to him. 1542 */ 1543 if (ifp->if_bpf) 1544 bpf_mtap(ifp, cur_tx->wb_mbuf); 1545 } 1546 1547 /* 1548 * If there are no packets queued, bail. 1549 */ 1550 if (cur_tx == NULL) { 1551 WB_UNLOCK(sc); 1552 return; 1553 } 1554 1555 /* 1556 * Place the request for the upload interrupt 1557 * in the last descriptor in the chain. This way, if 1558 * we're chaining several packets at once, we'll only 1559 * get an interupt once for the whole chain rather than 1560 * once for each packet. 1561 */ 1562 WB_TXCTL(cur_tx) |= WB_TXCTL_FINT; 1563 cur_tx->wb_ptr->wb_frag[0].wb_ctl |= WB_TXCTL_FINT; 1564 sc->wb_cdata.wb_tx_tail = cur_tx; 1565 1566 if (sc->wb_cdata.wb_tx_head == NULL) { 1567 sc->wb_cdata.wb_tx_head = start_tx; 1568 WB_TXOWN(start_tx) = WB_TXSTAT_OWN; 1569 CSR_WRITE_4(sc, WB_TXSTART, 0xFFFFFFFF); 1570 } else { 1571 /* 1572 * We need to distinguish between the case where 1573 * the own bit is clear because the chip cleared it 1574 * and where the own bit is clear because we haven't 1575 * set it yet. The magic value WB_UNSET is just some 1576 * ramdomly chosen number which doesn't have the own 1577 * bit set. When we actually transmit the frame, the 1578 * status word will have _only_ the own bit set, so 1579 * the txeoc handler will be able to tell if it needs 1580 * to initiate another transmission to flush out pending 1581 * frames. 1582 */ 1583 WB_TXOWN(start_tx) = WB_UNSENT; 1584 } 1585 1586 /* 1587 * Set a timeout in case the chip goes out to lunch. 1588 */ 1589 ifp->if_timer = 5; 1590 WB_UNLOCK(sc); 1591 1592 return; 1593} 1594 1595static void wb_init(xsc) 1596 void *xsc; 1597{ 1598 struct wb_softc *sc = xsc; 1599 struct ifnet *ifp = &sc->arpcom.ac_if; 1600 int i; 1601 struct mii_data *mii; 1602 1603 WB_LOCK(sc); 1604 mii = device_get_softc(sc->wb_miibus); 1605 1606 /* 1607 * Cancel pending I/O and free all RX/TX buffers. 1608 */ 1609 wb_stop(sc); 1610 wb_reset(sc); 1611 1612 sc->wb_txthresh = WB_TXTHRESH_INIT; 1613 1614 /* 1615 * Set cache alignment and burst length. 1616 */ 1617#ifdef foo 1618 CSR_WRITE_4(sc, WB_BUSCTL, WB_BUSCTL_CONFIG); 1619 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_THRESH); 1620 WB_SETBIT(sc, WB_NETCFG, WB_TXTHRESH(sc->wb_txthresh)); 1621#endif 1622 1623 CSR_WRITE_4(sc, WB_BUSCTL, WB_BUSCTL_MUSTBEONE|WB_BUSCTL_ARBITRATION); 1624 WB_SETBIT(sc, WB_BUSCTL, WB_BURSTLEN_16LONG); 1625 switch(sc->wb_cachesize) { 1626 case 32: 1627 WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_32LONG); 1628 break; 1629 case 16: 1630 WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_16LONG); 1631 break; 1632 case 8: 1633 WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_8LONG); 1634 break; 1635 case 0: 1636 default: 1637 WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_NONE); 1638 break; 1639 } 1640 1641 /* This doesn't tend to work too well at 100Mbps. */ 1642 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_EARLY_ON); 1643 1644 /* Init our MAC address */ 1645 for (i = 0; i < ETHER_ADDR_LEN; i++) { 1646 CSR_WRITE_1(sc, WB_NODE0 + i, sc->arpcom.ac_enaddr[i]); 1647 } 1648 1649 /* Init circular RX list. */ 1650 if (wb_list_rx_init(sc) == ENOBUFS) { 1651 printf("wb%d: initialization failed: no " 1652 "memory for rx buffers\n", sc->wb_unit); 1653 wb_stop(sc); 1654 WB_UNLOCK(sc); 1655 return; 1656 } 1657 1658 /* Init TX descriptors. */ 1659 wb_list_tx_init(sc); 1660 1661 /* If we want promiscuous mode, set the allframes bit. */ 1662 if (ifp->if_flags & IFF_PROMISC) { 1663 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_ALLPHYS); 1664 } else { 1665 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_ALLPHYS); 1666 } 1667 1668 /* 1669 * Set capture broadcast bit to capture broadcast frames. 1670 */ 1671 if (ifp->if_flags & IFF_BROADCAST) { 1672 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_BROAD); 1673 } else { 1674 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_BROAD); 1675 } 1676 1677 /* 1678 * Program the multicast filter, if necessary. 1679 */ 1680 wb_setmulti(sc); 1681 1682 /* 1683 * Load the address of the RX list. 1684 */ 1685 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON); 1686 CSR_WRITE_4(sc, WB_RXADDR, vtophys(&sc->wb_ldata->wb_rx_list[0])); 1687 1688 /* 1689 * Enable interrupts. 1690 */ 1691 CSR_WRITE_4(sc, WB_IMR, WB_INTRS); 1692 CSR_WRITE_4(sc, WB_ISR, 0xFFFFFFFF); 1693 1694 /* Enable receiver and transmitter. */ 1695 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON); 1696 CSR_WRITE_4(sc, WB_RXSTART, 0xFFFFFFFF); 1697 1698 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON); 1699 CSR_WRITE_4(sc, WB_TXADDR, vtophys(&sc->wb_ldata->wb_tx_list[0])); 1700 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON); 1701 1702 mii_mediachg(mii); 1703 1704 ifp->if_flags |= IFF_RUNNING; 1705 ifp->if_flags &= ~IFF_OACTIVE; 1706 1707 sc->wb_stat_ch = timeout(wb_tick, sc, hz); 1708 WB_UNLOCK(sc); 1709 1710 return; 1711} 1712 1713/* 1714 * Set media options. 1715 */ 1716static int wb_ifmedia_upd(ifp) 1717 struct ifnet *ifp; 1718{ 1719 struct wb_softc *sc; 1720 1721 sc = ifp->if_softc; 1722 1723 if (ifp->if_flags & IFF_UP) 1724 wb_init(sc); 1725 1726 return(0); 1727} 1728 1729/* 1730 * Report current media status. 1731 */ 1732static void wb_ifmedia_sts(ifp, ifmr) 1733 struct ifnet *ifp; 1734 struct ifmediareq *ifmr; 1735{ 1736 struct wb_softc *sc; 1737 struct mii_data *mii; 1738 1739 sc = ifp->if_softc; 1740 1741 mii = device_get_softc(sc->wb_miibus); 1742 1743 mii_pollstat(mii); 1744 ifmr->ifm_active = mii->mii_media_active; 1745 ifmr->ifm_status = mii->mii_media_status; 1746 1747 return; 1748} 1749 1750static int wb_ioctl(ifp, command, data) 1751 struct ifnet *ifp; 1752 u_long command; 1753 caddr_t data; 1754{ 1755 struct wb_softc *sc = ifp->if_softc; 1756 struct mii_data *mii; 1757 struct ifreq *ifr = (struct ifreq *) data; 1758 int error = 0; 1759 1760 WB_LOCK(sc); 1761 1762 switch(command) { 1763 case SIOCSIFADDR: 1764 case SIOCGIFADDR: 1765 case SIOCSIFMTU: 1766 error = ether_ioctl(ifp, command, data); 1767 break; 1768 case SIOCSIFFLAGS: 1769 if (ifp->if_flags & IFF_UP) { 1770 wb_init(sc); 1771 } else { 1772 if (ifp->if_flags & IFF_RUNNING) 1773 wb_stop(sc); 1774 } 1775 error = 0; 1776 break; 1777 case SIOCADDMULTI: 1778 case SIOCDELMULTI: 1779 wb_setmulti(sc); 1780 error = 0; 1781 break; 1782 case SIOCGIFMEDIA: 1783 case SIOCSIFMEDIA: 1784 mii = device_get_softc(sc->wb_miibus); 1785 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 1786 break; 1787 default: 1788 error = EINVAL; 1789 break; 1790 } 1791 1792 WB_UNLOCK(sc); 1793 1794 return(error); 1795} 1796 1797static void wb_watchdog(ifp) 1798 struct ifnet *ifp; 1799{ 1800 struct wb_softc *sc; 1801 1802 sc = ifp->if_softc; 1803 1804 WB_LOCK(sc); 1805 ifp->if_oerrors++; 1806 printf("wb%d: watchdog timeout\n", sc->wb_unit); 1807#ifdef foo 1808 if (!(wb_phy_readreg(sc, PHY_BMSR) & PHY_BMSR_LINKSTAT)) 1809 printf("wb%d: no carrier - transceiver cable problem?\n", 1810 sc->wb_unit); 1811#endif 1812 wb_stop(sc); 1813 wb_reset(sc); 1814 wb_init(sc); 1815 1816 if (ifp->if_snd.ifq_head != NULL) 1817 wb_start(ifp); 1818 WB_UNLOCK(sc); 1819 1820 return; 1821} 1822 1823/* 1824 * Stop the adapter and free any mbufs allocated to the 1825 * RX and TX lists. 1826 */ 1827static void wb_stop(sc) 1828 struct wb_softc *sc; 1829{ 1830 register int i; 1831 struct ifnet *ifp; 1832 1833 WB_LOCK(sc); 1834 ifp = &sc->arpcom.ac_if; 1835 ifp->if_timer = 0; 1836 1837 untimeout(wb_tick, sc, sc->wb_stat_ch); 1838 1839 WB_CLRBIT(sc, WB_NETCFG, (WB_NETCFG_RX_ON|WB_NETCFG_TX_ON)); 1840 CSR_WRITE_4(sc, WB_IMR, 0x00000000); 1841 CSR_WRITE_4(sc, WB_TXADDR, 0x00000000); 1842 CSR_WRITE_4(sc, WB_RXADDR, 0x00000000); 1843 1844 /* 1845 * Free data in the RX lists. 1846 */ 1847 for (i = 0; i < WB_RX_LIST_CNT; i++) { 1848 if (sc->wb_cdata.wb_rx_chain[i].wb_mbuf != NULL) { 1849 m_freem(sc->wb_cdata.wb_rx_chain[i].wb_mbuf); 1850 sc->wb_cdata.wb_rx_chain[i].wb_mbuf = NULL; 1851 } 1852 } 1853 bzero((char *)&sc->wb_ldata->wb_rx_list, 1854 sizeof(sc->wb_ldata->wb_rx_list)); 1855 1856 /* 1857 * Free the TX list buffers. 1858 */ 1859 for (i = 0; i < WB_TX_LIST_CNT; i++) { 1860 if (sc->wb_cdata.wb_tx_chain[i].wb_mbuf != NULL) { 1861 m_freem(sc->wb_cdata.wb_tx_chain[i].wb_mbuf); 1862 sc->wb_cdata.wb_tx_chain[i].wb_mbuf = NULL; 1863 } 1864 } 1865 1866 bzero((char *)&sc->wb_ldata->wb_tx_list, 1867 sizeof(sc->wb_ldata->wb_tx_list)); 1868 1869 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 1870 WB_UNLOCK(sc); 1871 1872 return; 1873} 1874 1875/* 1876 * Stop all chip I/O so that the kernel's probe routines don't 1877 * get confused by errant DMAs when rebooting. 1878 */ 1879static void wb_shutdown(dev) 1880 device_t dev; 1881{ 1882 struct wb_softc *sc; 1883 1884 sc = device_get_softc(dev); 1885 wb_stop(sc); 1886 1887 return; 1888} 1889