if_wb.c revision 229767
156160Sru/*- 2146515Sru * Copyright (c) 1997, 1998 356160Sru * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 4146515Sru * 5146515Sru * Redistribution and use in source and binary forms, with or without 656160Sru * modification, are permitted provided that the following conditions 756160Sru * are met: 856160Sru * 1. Redistributions of source code must retain the above copyright 956160Sru * notice, this list of conditions and the following disclaimer. 1056160Sru * 2. Redistributions in binary form must reproduce the above copyright 1156160Sru * notice, this list of conditions and the following disclaimer in the 1256160Sru * documentation and/or other materials provided with the distribution. 1356160Sru * 3. All advertising materials mentioning features or use of this software 1456160Sru * must display the following acknowledgement: 1556160Sru * This product includes software developed by Bill Paul. 1656160Sru * 4. Neither the name of the author nor the names of any co-contributors 1756160Sru * may be used to endorse or promote products derived from this software 1856160Sru * without specific prior written permission. 1956160Sru * 2056160Sru * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 2156160Sru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2256160Sru * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23146515Sru * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 2456160Sru * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2556160Sru * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2656160Sru * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27146515Sru * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2856160Sru * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2956160Sru * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 3056160Sru * THE POSSIBILITY OF SUCH DAMAGE. 31116525Sru */ 32116525Sru 33116525Sru#include <sys/cdefs.h> 34116525Sru__FBSDID("$FreeBSD: head/sys/dev/wb/if_wb.c 229767 2012-01-07 09:41:57Z kevlo $"); 35116525Sru 36116525Sru/* 37116525Sru * Winbond fast ethernet PCI NIC driver 38116525Sru * 39116525Sru * Supports various cheap network adapters based on the Winbond W89C840F 40116525Sru * fast ethernet controller chip. This includes adapters manufactured by 41116525Sru * Winbond itself and some made by Linksys. 42146515Sru * 43116525Sru * Written by Bill Paul <wpaul@ctr.columbia.edu> 44116525Sru * Electrical Engineering Department 45116525Sru * Columbia University, New York City 46116525Sru */ 47116525Sru/* 48116525Sru * The Winbond W89C840F chip is a bus master; in some ways it resembles 49116525Sru * a DEC 'tulip' chip, only not as complicated. Unfortunately, it has 50116525Sru * one major difference which is that while the registers do many of 51116525Sru * the same things as a tulip adapter, the offsets are different: where 52116525Sru * tulip registers are typically spaced 8 bytes apart, the Winbond 53146515Sru * registers are spaced 4 bytes apart. The receiver filter is also 54116525Sru * programmed differently. 55116525Sru * 56116525Sru * Like the tulip, the Winbond chip uses small descriptors containing 57116525Sru * a status word, a control word and 32-bit areas that can either be used 58116525Sru * to point to two external data blocks, or to point to a single block 59116525Sru * and another descriptor in a linked list. Descriptors can be grouped 60116525Sru * together in blocks to form fixed length rings or can be chained 61116525Sru * together in linked lists. A single packet may be spread out over 62116525Sru * several descriptors if necessary. 63116525Sru * 64116525Sru * For the receive ring, this driver uses a linked list of descriptors, 65116525Sru * each pointing to a single mbuf cluster buffer, which us large enough 66116525Sru * to hold an entire packet. The link list is looped back to created a 67116525Sru * closed ring. 68116525Sru * 69116525Sru * For transmission, the driver creates a linked list of 'super descriptors' 70146515Sru * which each contain several individual descriptors linked toghether. 71116525Sru * Each 'super descriptor' contains WB_MAXFRAGS descriptors, which we 72146515Sru * abuse as fragment pointers. This allows us to use a buffer managment 73146515Sru * scheme very similar to that used in the ThunderLAN and Etherlink XL 74116525Sru * drivers. 75116525Sru * 76116525Sru * Autonegotiation is performed using the external PHY via the MII bus. 77116525Sru * The sample boards I have all use a Davicom PHY. 78116525Sru * 79116525Sru * Note: the author of the Linux driver for the Winbond chip alludes 80116525Sru * to some sort of flaw in the chip's design that seems to mandate some 81146515Sru * drastic workaround which signigicantly impairs transmit performance. 82146515Sru * I have no idea what he's on about: transmit performance with all 83116525Sru * three of my test boards seems fine. 84116525Sru */ 85116525Sru 86116525Sru#include <sys/param.h> 87116525Sru#include <sys/systm.h> 88116525Sru#include <sys/sockio.h> 89116525Sru#include <sys/mbuf.h> 90116525Sru#include <sys/malloc.h> 91116525Sru#include <sys/module.h> 92116525Sru#include <sys/kernel.h> 93116525Sru#include <sys/socket.h> 94116525Sru#include <sys/queue.h> 95116525Sru 96116525Sru#include <net/if.h> 97116525Sru#include <net/if_arp.h> 98116525Sru#include <net/ethernet.h> 99116525Sru#include <net/if_dl.h> 100116525Sru#include <net/if_media.h> 101116525Sru#include <net/if_types.h> 102146515Sru 103146515Sru#include <net/bpf.h> 104146515Sru 105146515Sru#include <vm/vm.h> /* for vtophys */ 106146515Sru#include <vm/pmap.h> /* for vtophys */ 107146515Sru#include <machine/bus.h> 108146515Sru#include <machine/resource.h> 109146515Sru#include <sys/bus.h> 110146515Sru#include <sys/rman.h> 111146515Sru 112146515Sru#include <dev/pci/pcireg.h> 113146515Sru#include <dev/pci/pcivar.h> 114146515Sru 115146515Sru#include <dev/mii/mii.h> 116146515Sru#include <dev/mii/mii_bitbang.h> 117146515Sru#include <dev/mii/miivar.h> 118116525Sru 119116525Sru/* "device miibus" required. See GENERIC if you get errors here. */ 120116525Sru#include "miibus_if.h" 121116525Sru 122116525Sru#define WB_USEIOSPACE 123116525Sru 124116525Sru#include <dev/wb/if_wbreg.h> 125116525Sru 126116525SruMODULE_DEPEND(wb, pci, 1, 1, 1); 127116525SruMODULE_DEPEND(wb, ether, 1, 1, 1); 128116525SruMODULE_DEPEND(wb, miibus, 1, 1, 1); 129116525Sru 130116525Sru/* 131116525Sru * Various supported device vendors/types and their names. 132116525Sru */ 133116525Srustatic const struct wb_type const wb_devs[] = { 134116525Sru { WB_VENDORID, WB_DEVICEID_840F, 135116525Sru "Winbond W89C840F 10/100BaseTX" }, 136116525Sru { CP_VENDORID, CP_DEVICEID_RL100, 137116525Sru "Compex RL100-ATX 10/100baseTX" }, 138116525Sru { 0, 0, NULL } 139116525Sru}; 140116525Sru 141116525Srustatic int wb_probe(device_t); 142116525Srustatic int wb_attach(device_t); 143116525Srustatic int wb_detach(device_t); 144116525Sru 145116525Srustatic void wb_bfree(void *addr, void *args); 146116525Srustatic int wb_newbuf(struct wb_softc *, struct wb_chain_onefrag *, 147116525Sru struct mbuf *); 148116525Srustatic int wb_encap(struct wb_softc *, struct wb_chain *, struct mbuf *); 149116525Sru 150116525Srustatic void wb_rxeof(struct wb_softc *); 151116525Srustatic void wb_rxeoc(struct wb_softc *); 152116525Srustatic void wb_txeof(struct wb_softc *); 153116525Srustatic void wb_txeoc(struct wb_softc *); 154116525Srustatic void wb_intr(void *); 155116525Srustatic void wb_tick(void *); 156116525Srustatic void wb_start(struct ifnet *); 157116525Srustatic void wb_start_locked(struct ifnet *); 158116525Srustatic int wb_ioctl(struct ifnet *, u_long, caddr_t); 159116525Srustatic void wb_init(void *); 160116525Srustatic void wb_init_locked(struct wb_softc *); 161116525Srustatic void wb_stop(struct wb_softc *); 162116525Srustatic void wb_watchdog(struct wb_softc *); 163116525Srustatic int wb_shutdown(device_t); 164116525Srustatic int wb_ifmedia_upd(struct ifnet *); 165116525Srustatic void wb_ifmedia_sts(struct ifnet *, struct ifmediareq *); 166116525Sru 167116525Srustatic void wb_eeprom_putbyte(struct wb_softc *, int); 168116525Srustatic void wb_eeprom_getword(struct wb_softc *, int, u_int16_t *); 169116525Srustatic void wb_read_eeprom(struct wb_softc *, caddr_t, int, int, int); 170116525Sru 171116525Srustatic void wb_setcfg(struct wb_softc *, u_int32_t); 172116525Srustatic void wb_setmulti(struct wb_softc *); 173116525Srustatic void wb_reset(struct wb_softc *); 174116525Srustatic void wb_fixmedia(struct wb_softc *); 175116525Srustatic int wb_list_rx_init(struct wb_softc *); 176116525Srustatic int wb_list_tx_init(struct wb_softc *); 177116525Sru 178116525Srustatic int wb_miibus_readreg(device_t, int, int); 179116525Srustatic int wb_miibus_writereg(device_t, int, int, int); 180116525Srustatic void wb_miibus_statchg(device_t); 181116525Sru 182116525Sru/* 183116525Sru * MII bit-bang glue 184116525Sru */ 185146515Srustatic uint32_t wb_mii_bitbang_read(device_t); 186146515Srustatic void wb_mii_bitbang_write(device_t, uint32_t); 187116525Sru 188146515Srustatic const struct mii_bitbang_ops wb_mii_bitbang_ops = { 189146515Sru wb_mii_bitbang_read, 190146515Sru wb_mii_bitbang_write, 191116525Sru { 192146515Sru WB_SIO_MII_DATAOUT, /* MII_BIT_MDO */ 193146515Sru WB_SIO_MII_DATAIN, /* MII_BIT_MDI */ 194146515Sru WB_SIO_MII_CLK, /* MII_BIT_MDC */ 195146515Sru WB_SIO_MII_DIR, /* MII_BIT_DIR_HOST_PHY */ 196146515Sru 0, /* MII_BIT_DIR_PHY_HOST */ 197146515Sru } 198146515Sru}; 199146515Sru 200146515Sru#ifdef WB_USEIOSPACE 201146515Sru#define WB_RES SYS_RES_IOPORT 202146515Sru#define WB_RID WB_PCI_LOIO 203146515Sru#else 204146515Sru#define WB_RES SYS_RES_MEMORY 205146515Sru#define WB_RID WB_PCI_LOMEM 206146515Sru#endif 207146515Sru 208146515Srustatic device_method_t wb_methods[] = { 209146515Sru /* Device interface */ 210146515Sru DEVMETHOD(device_probe, wb_probe), 211146515Sru DEVMETHOD(device_attach, wb_attach), 212146515Sru DEVMETHOD(device_detach, wb_detach), 213146515Sru DEVMETHOD(device_shutdown, wb_shutdown), 214146515Sru 215146515Sru /* MII interface */ 216146515Sru DEVMETHOD(miibus_readreg, wb_miibus_readreg), 217146515Sru DEVMETHOD(miibus_writereg, wb_miibus_writereg), 218146515Sru DEVMETHOD(miibus_statchg, wb_miibus_statchg), 219146515Sru 220146515Sru DEVMETHOD_END 221146515Sru}; 222146515Sru 223146515Srustatic driver_t wb_driver = { 224146515Sru "wb", 225146515Sru wb_methods, 226146515Sru sizeof(struct wb_softc) 227146515Sru}; 228146515Sru 229146515Srustatic devclass_t wb_devclass; 230146515Sru 231146515SruDRIVER_MODULE(wb, pci, wb_driver, wb_devclass, 0, 0); 232146515SruDRIVER_MODULE(miibus, wb, miibus_driver, miibus_devclass, 0, 0); 233146515Sru 234146515Sru#define WB_SETBIT(sc, reg, x) \ 235146515Sru CSR_WRITE_4(sc, reg, \ 236146515Sru CSR_READ_4(sc, reg) | (x)) 237146515Sru 238146515Sru#define WB_CLRBIT(sc, reg, x) \ 239146515Sru CSR_WRITE_4(sc, reg, \ 240146515Sru CSR_READ_4(sc, reg) & ~(x)) 241146515Sru 242146515Sru#define SIO_SET(x) \ 243146515Sru CSR_WRITE_4(sc, WB_SIO, \ 244146515Sru CSR_READ_4(sc, WB_SIO) | (x)) 245146515Sru 246146515Sru#define SIO_CLR(x) \ 247146515Sru CSR_WRITE_4(sc, WB_SIO, \ 248146515Sru CSR_READ_4(sc, WB_SIO) & ~(x)) 249146515Sru 250146515Sru/* 251146515Sru * Send a read command and address to the EEPROM, check for ACK. 252146515Sru */ 253146515Srustatic void 254146515Sruwb_eeprom_putbyte(sc, addr) 255146515Sru struct wb_softc *sc; 256146515Sru int addr; 257146515Sru{ 258146515Sru register int d, i; 259146515Sru 260146515Sru d = addr | WB_EECMD_READ; 261146515Sru 262146515Sru /* 263146515Sru * Feed in each bit and stobe the clock. 264146515Sru */ 265146515Sru for (i = 0x400; i; i >>= 1) { 266146515Sru if (d & i) { 267146515Sru SIO_SET(WB_SIO_EE_DATAIN); 268146515Sru } else { 269146515Sru SIO_CLR(WB_SIO_EE_DATAIN); 270146515Sru } 271146515Sru DELAY(100); 272146515Sru SIO_SET(WB_SIO_EE_CLK); 273146515Sru DELAY(150); 274146515Sru SIO_CLR(WB_SIO_EE_CLK); 275146515Sru DELAY(100); 276146515Sru } 277146515Sru} 278146515Sru 279146515Sru/* 280146515Sru * Read a word of data stored in the EEPROM at address 'addr.' 281146515Sru */ 282146515Srustatic void 283146515Sruwb_eeprom_getword(sc, addr, dest) 284146515Sru struct wb_softc *sc; 285146515Sru int addr; 286146515Sru u_int16_t *dest; 287146515Sru{ 288146515Sru register int i; 289146515Sru u_int16_t word = 0; 290146515Sru 291146515Sru /* Enter EEPROM access mode. */ 292146515Sru CSR_WRITE_4(sc, WB_SIO, WB_SIO_EESEL|WB_SIO_EE_CS); 293146515Sru 294146515Sru /* 295146515Sru * Send address of word we want to read. 296146515Sru */ 297146515Sru wb_eeprom_putbyte(sc, addr); 298146515Sru 299146515Sru CSR_WRITE_4(sc, WB_SIO, WB_SIO_EESEL|WB_SIO_EE_CS); 300146515Sru 301146515Sru /* 302146515Sru * Start reading bits from EEPROM. 303146515Sru */ 304146515Sru for (i = 0x8000; i; i >>= 1) { 305146515Sru SIO_SET(WB_SIO_EE_CLK); 306146515Sru DELAY(100); 307146515Sru if (CSR_READ_4(sc, WB_SIO) & WB_SIO_EE_DATAOUT) 308146515Sru word |= i; 309146515Sru SIO_CLR(WB_SIO_EE_CLK); 310146515Sru DELAY(100); 311146515Sru } 312146515Sru 313146515Sru /* Turn off EEPROM access mode. */ 314146515Sru CSR_WRITE_4(sc, WB_SIO, 0); 315146515Sru 316146515Sru *dest = word; 317146515Sru} 318146515Sru 319146515Sru/* 320146515Sru * Read a sequence of words from the EEPROM. 321146515Sru */ 322146515Srustatic void 323146515Sruwb_read_eeprom(sc, dest, off, cnt, swap) 324146515Sru struct wb_softc *sc; 325146515Sru caddr_t dest; 326146515Sru int off; 327146515Sru int cnt; 328146515Sru int swap; 329146515Sru{ 330146515Sru int i; 331146515Sru u_int16_t word = 0, *ptr; 332146515Sru 333146515Sru for (i = 0; i < cnt; i++) { 334146515Sru wb_eeprom_getword(sc, off + i, &word); 335146515Sru ptr = (u_int16_t *)(dest + (i * 2)); 336146515Sru if (swap) 337146515Sru *ptr = ntohs(word); 338116525Sru else 33956160Sru *ptr = word; 34056160Sru } 34156160Sru} 342146515Sru 34356160Sru/* 344146515Sru * Read the MII serial port for the MII bit-bang module. 345146515Sru */ 34656160Srustatic uint32_t 34756160Sruwb_mii_bitbang_read(device_t dev) 34856160Sru{ 34956160Sru struct wb_softc *sc; 35056160Sru uint32_t val; 35156160Sru 352146515Sru sc = device_get_softc(dev); 353146515Sru 354146515Sru val = CSR_READ_4(sc, WB_SIO); 35556160Sru CSR_BARRIER(sc, WB_SIO, 4, 35656160Sru BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 35756160Sru 35856160Sru return (val); 35956160Sru} 36056160Sru 36156160Sru/* 36256160Sru * Write the MII serial port for the MII bit-bang module. 36356160Sru */ 36456160Srustatic void 36556160Sruwb_mii_bitbang_write(device_t dev, uint32_t val) 36693139Sru{ 36756160Sru struct wb_softc *sc; 36856160Sru 36956160Sru sc = device_get_softc(dev); 37093139Sru 37156160Sru CSR_WRITE_4(sc, WB_SIO, val); 37256160Sru CSR_BARRIER(sc, WB_SIO, 4, 37356160Sru BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 37456160Sru} 37556160Sru 376146515Srustatic int 377146515Sruwb_miibus_readreg(dev, phy, reg) 378146515Sru device_t dev; 379146515Sru int phy, reg; 38056160Sru{ 38156160Sru 38256160Sru return (mii_bitbang_readreg(dev, &wb_mii_bitbang_ops, phy, reg)); 38356160Sru} 38456160Sru 38556160Srustatic int 38656160Sruwb_miibus_writereg(dev, phy, reg, data) 38756160Sru device_t dev; 38856160Sru int phy, reg, data; 38956160Sru{ 39056160Sru 39156160Sru mii_bitbang_writereg(dev, &wb_mii_bitbang_ops, phy, reg, data); 39256160Sru 39356160Sru return(0); 39456160Sru} 39556160Sru 39656160Srustatic void 39756160Sruwb_miibus_statchg(dev) 39856160Sru device_t dev; 39993139Sru{ 40056160Sru struct wb_softc *sc; 401114472Sru struct mii_data *mii; 402114472Sru 403146515Sru sc = device_get_softc(dev); 404146515Sru mii = device_get_softc(sc->wb_miibus); 405114472Sru wb_setcfg(sc, mii->mii_media_active); 406114472Sru} 40756160Sru 408114472Sru/* 409146515Sru * Program the 64-bit multicast hash filter. 410114472Sru */ 411114472Srustatic void 412114472Sruwb_setmulti(sc) 413114472Sru struct wb_softc *sc; 414114472Sru{ 415146515Sru struct ifnet *ifp; 416146515Sru int h = 0; 417114472Sru u_int32_t hashes[2] = { 0, 0 }; 418114472Sru struct ifmultiaddr *ifma; 419114472Sru u_int32_t rxfilt; 420114472Sru int mcnt = 0; 421114472Sru 422114472Sru ifp = sc->wb_ifp; 423114472Sru 424114472Sru rxfilt = CSR_READ_4(sc, WB_NETCFG); 425114472Sru 426146515Sru if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 427146515Sru rxfilt |= WB_NETCFG_RX_MULTI; 428114472Sru CSR_WRITE_4(sc, WB_NETCFG, rxfilt); 429114472Sru CSR_WRITE_4(sc, WB_MAR0, 0xFFFFFFFF); 430114472Sru CSR_WRITE_4(sc, WB_MAR1, 0xFFFFFFFF); 431114472Sru return; 432146515Sru } 433146515Sru 434146515Sru /* first, zot all the existing hash bits */ 435146515Sru CSR_WRITE_4(sc, WB_MAR0, 0); 436146515Sru CSR_WRITE_4(sc, WB_MAR1, 0); 437146515Sru 438146515Sru /* now program new ones */ 439146515Sru if_maddr_rlock(ifp); 440146515Sru TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 441146515Sru if (ifma->ifma_addr->sa_family != AF_LINK) 442146515Sru continue; 443146515Sru h = ~ether_crc32_be(LLADDR((struct sockaddr_dl *) 444146515Sru ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 445146515Sru if (h < 32) 446146515Sru hashes[0] |= (1 << h); 447146515Sru else 448146515Sru hashes[1] |= (1 << (h - 32)); 449146515Sru mcnt++; 450146515Sru } 451146515Sru if_maddr_runlock(ifp); 452146515Sru 453146515Sru if (mcnt) 454146515Sru rxfilt |= WB_NETCFG_RX_MULTI; 455146515Sru else 456146515Sru rxfilt &= ~WB_NETCFG_RX_MULTI; 457146515Sru 458146515Sru CSR_WRITE_4(sc, WB_MAR0, hashes[0]); 459146515Sru CSR_WRITE_4(sc, WB_MAR1, hashes[1]); 460146515Sru CSR_WRITE_4(sc, WB_NETCFG, rxfilt); 461146515Sru} 462146515Sru 463146515Sru/* 464146515Sru * The Winbond manual states that in order to fiddle with the 465146515Sru * 'full-duplex' and '100Mbps' bits in the netconfig register, we 466146515Sru * first have to put the transmit and/or receive logic in the idle state. 467146515Sru */ 468146515Srustatic void 469146515Sruwb_setcfg(sc, media) 470146515Sru struct wb_softc *sc; 471146515Sru u_int32_t media; 472146515Sru{ 473146515Sru int i, restart = 0; 474146515Sru 475146515Sru if (CSR_READ_4(sc, WB_NETCFG) & (WB_NETCFG_TX_ON|WB_NETCFG_RX_ON)) { 476146515Sru restart = 1; 477146515Sru WB_CLRBIT(sc, WB_NETCFG, (WB_NETCFG_TX_ON|WB_NETCFG_RX_ON)); 478146515Sru 479146515Sru for (i = 0; i < WB_TIMEOUT; i++) { 480146515Sru DELAY(10); 481146515Sru if ((CSR_READ_4(sc, WB_ISR) & WB_ISR_TX_IDLE) && 482146515Sru (CSR_READ_4(sc, WB_ISR) & WB_ISR_RX_IDLE)) 483146515Sru break; 484146515Sru } 485146515Sru 486146515Sru if (i == WB_TIMEOUT) 487146515Sru device_printf(sc->wb_dev, 488146515Sru "failed to force tx and rx to idle state\n"); 489146515Sru } 490146515Sru 49156160Sru if (IFM_SUBTYPE(media) == IFM_10_T) 49256160Sru WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_100MBPS); 493146515Sru else 494146515Sru WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_100MBPS); 495146515Sru 496146515Sru if ((media & IFM_GMASK) == IFM_FDX) 49756160Sru WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_FULLDUPLEX); 49856160Sru else 499146515Sru WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_FULLDUPLEX); 500146515Sru 501146515Sru if (restart) 50256160Sru WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON|WB_NETCFG_RX_ON); 503114472Sru} 504146515Sru 505146515Srustatic void 506114472Sruwb_reset(sc) 507146515Sru struct wb_softc *sc; 508114472Sru{ 50956160Sru register int i; 510116525Sru struct mii_data *mii; 511114472Sru struct mii_softc *miisc; 512114472Sru 513146515Sru CSR_WRITE_4(sc, WB_NETCFG, 0); 514146515Sru CSR_WRITE_4(sc, WB_BUSCTL, 0); 515146515Sru CSR_WRITE_4(sc, WB_TXADDR, 0); 516146515Sru CSR_WRITE_4(sc, WB_RXADDR, 0); 517146515Sru 518146515Sru WB_SETBIT(sc, WB_BUSCTL, WB_BUSCTL_RESET); 519146515Sru WB_SETBIT(sc, WB_BUSCTL, WB_BUSCTL_RESET); 520146515Sru 521146515Sru for (i = 0; i < WB_TIMEOUT; i++) { 522146515Sru DELAY(10); 523114472Sru if (!(CSR_READ_4(sc, WB_BUSCTL) & WB_BUSCTL_RESET)) 524146515Sru break; 525146515Sru } 526146515Sru if (i == WB_TIMEOUT) 527146515Sru device_printf(sc->wb_dev, "reset never completed!\n"); 528146515Sru 529146515Sru /* Wait a little while for the chip to get its brains in order. */ 530146515Sru DELAY(1000); 531146515Sru 532146515Sru if (sc->wb_miibus == NULL) 533146515Sru return; 534146515Sru 535146515Sru mii = device_get_softc(sc->wb_miibus); 536114472Sru LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 537146515Sru PHY_RESET(miisc); 538146515Sru} 539114472Sru 540114472Srustatic void 541114472Sruwb_fixmedia(sc) 542146515Sru struct wb_softc *sc; 543114472Sru{ 544114472Sru struct mii_data *mii = NULL; 545114472Sru struct ifnet *ifp; 546114472Sru u_int32_t media; 547146515Sru 548146515Sru mii = device_get_softc(sc->wb_miibus); 549114472Sru ifp = sc->wb_ifp; 550114472Sru 551146515Sru mii_pollstat(mii); 552146515Sru if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) { 553146515Sru media = mii->mii_media_active & ~IFM_10_T; 554114472Sru media |= IFM_100_TX; 555114472Sru } else if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) { 556114472Sru media = mii->mii_media_active & ~IFM_100_TX; 557146515Sru media |= IFM_10_T; 558146515Sru } else 559146515Sru return; 560146515Sru 561146515Sru ifmedia_set(&mii->mii_media, media); 562146515Sru} 563114472Sru 564114472Sru/* 565114472Sru * Probe for a Winbond chip. Check the PCI vendor and device 566146515Sru * IDs against our list and return a device name if we find a match. 567146515Sru */ 568146515Srustatic int 569146515Sruwb_probe(dev) 57056160Sru device_t dev; 57156160Sru{ 572146515Sru const struct wb_type *t; 573146515Sru 574146515Sru t = wb_devs; 575146515Sru 576146515Sru while(t->wb_name != NULL) { 577116525Sru if ((pci_get_vendor(dev) == t->wb_vid) && 57856160Sru (pci_get_device(dev) == t->wb_did)) { 57956160Sru device_set_desc(dev, t->wb_name); 58056160Sru return (BUS_PROBE_DEFAULT); 581146515Sru } 58256160Sru t++; 58393139Sru } 58456160Sru 58593139Sru return(ENXIO); 58693139Sru} 58793139Sru 58893139Sru/* 589146515Sru * Attach the interface. Allocate softc structures, do ifmedia 59056160Sru * setup and ethernet/BPF attach. 59156160Sru */ 59256160Srustatic int 59356160Sruwb_attach(dev) 594146515Sru device_t dev; 595146515Sru{ 596146515Sru u_char eaddr[ETHER_ADDR_LEN]; 597146515Sru struct wb_softc *sc; 598146515Sru struct ifnet *ifp; 599146515Sru int error = 0, rid; 600146515Sru 601146515Sru sc = device_get_softc(dev); 602146515Sru sc->wb_dev = dev; 60356160Sru 604146515Sru mtx_init(&sc->wb_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 60556160Sru MTX_DEF); 606146515Sru callout_init_mtx(&sc->wb_stat_callout, &sc->wb_mtx, 0); 607146515Sru 608146515Sru /* 609146515Sru * Map control/status registers. 610146515Sru */ 611146515Sru pci_enable_busmaster(dev); 612146515Sru 613146515Sru rid = WB_RID; 614146515Sru sc->wb_res = bus_alloc_resource_any(dev, WB_RES, &rid, RF_ACTIVE); 615146515Sru 61656160Sru if (sc->wb_res == NULL) { 61756160Sru device_printf(dev, "couldn't map ports/memory\n"); 618146515Sru error = ENXIO; 619146515Sru goto fail; 620146515Sru } 621146515Sru 62256160Sru /* Allocate interrupt */ 62356160Sru rid = 0; 624146515Sru sc->wb_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 625146515Sru RF_SHAREABLE | RF_ACTIVE); 626146515Sru 627146515Sru if (sc->wb_irq == NULL) { 62856160Sru device_printf(dev, "couldn't map interrupt\n"); 629146515Sru error = ENXIO; 63056160Sru goto fail; 63156160Sru } 63256160Sru 63356160Sru /* Save the cache line size. */ 63456160Sru sc->wb_cachesize = pci_read_config(dev, WB_PCI_CACHELEN, 4) & 0xFF; 63556160Sru 636146515Sru /* Reset the adapter. */ 637146515Sru wb_reset(sc); 638146515Sru 639146515Sru /* 640146515Sru * Get station address from the EEPROM. 641146515Sru */ 642146515Sru wb_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 0); 64356160Sru 644146515Sru sc->wb_ldata = contigmalloc(sizeof(struct wb_list_data) + 8, M_DEVBUF, 64556160Sru M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0); 646146515Sru 64793139Sru if (sc->wb_ldata == NULL) { 64893139Sru device_printf(dev, "no memory for list buffers!\n"); 64993139Sru error = ENXIO; 65093139Sru goto fail; 65193139Sru } 65293139Sru 65393139Sru bzero(sc->wb_ldata, sizeof(struct wb_list_data)); 65493139Sru 65593139Sru ifp = sc->wb_ifp = if_alloc(IFT_ETHER); 65693139Sru if (ifp == NULL) { 65793139Sru device_printf(dev, "can not if_alloc()\n"); 65893139Sru error = ENOSPC; 659146515Sru goto fail; 660146515Sru } 66193139Sru ifp->if_softc = sc; 662146515Sru if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 66393139Sru ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 66456160Sru ifp->if_ioctl = wb_ioctl; 66593139Sru ifp->if_start = wb_start; 66693139Sru ifp->if_init = wb_init; 66793139Sru ifp->if_snd.ifq_maxlen = WB_TX_LIST_CNT - 1; 668146515Sru 66993139Sru /* 67093139Sru * Do MII setup. 67156160Sru */ 67293139Sru error = mii_attach(dev, &sc->wb_miibus, ifp, wb_ifmedia_upd, 673146515Sru wb_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0); 674146515Sru if (error != 0) { 675146515Sru device_printf(dev, "attaching PHYs failed\n"); 676146515Sru goto fail; 677146515Sru } 678146515Sru 679146515Sru /* 680146515Sru * Call MI attach routine. 681146515Sru */ 68293139Sru ether_ifattach(ifp, eaddr); 683146515Sru 68493139Sru /* Hook interrupt last to avoid having to lock softc */ 685146515Sru error = bus_setup_intr(dev, sc->wb_irq, INTR_TYPE_NET | INTR_MPSAFE, 68693139Sru NULL, wb_intr, sc, &sc->wb_intrhand); 687146515Sru 688146515Sru if (error) { 689146515Sru device_printf(dev, "couldn't set up irq\n"); 690146515Sru ether_ifdetach(ifp); 691146515Sru goto fail; 692146515Sru } 693146515Sru 694146515Srufail: 695146515Sru if (error) 696146515Sru wb_detach(dev); 697146515Sru 698146515Sru return(error); 699146515Sru} 700146515Sru 701146515Sru/* 70293139Sru * Shutdown hardware and free up resources. This can be called any 703146515Sru * time after the mutex has been initialized. It is called in both 704146515Sru * the error case in attach and the normal detach case so it needs 70593139Sru * to be careful about only freeing resources that have actually been 70693139Sru * allocated. 70793139Sru */ 70893139Srustatic int 70993139Sruwb_detach(dev) 71093139Sru device_t dev; 71193139Sru{ 712146515Sru struct wb_softc *sc; 71393139Sru struct ifnet *ifp; 71493139Sru 71593139Sru sc = device_get_softc(dev); 71693139Sru KASSERT(mtx_initialized(&sc->wb_mtx), ("wb mutex not initialized")); 71793139Sru ifp = sc->wb_ifp; 71893139Sru 71993139Sru /* 72093139Sru * Delete any miibus and phy devices attached to this interface. 72193139Sru * This should only be done if attach succeeded. 72293139Sru */ 72393139Sru if (device_is_attached(dev)) { 72493139Sru ether_ifdetach(ifp); 72593139Sru WB_LOCK(sc); 72693139Sru wb_stop(sc); 72793139Sru WB_UNLOCK(sc); 72893139Sru callout_drain(&sc->wb_stat_callout); 72993139Sru } 73093139Sru if (sc->wb_miibus) 73193139Sru device_delete_child(dev, sc->wb_miibus); 73293139Sru bus_generic_detach(dev); 73393139Sru 73493139Sru if (sc->wb_intrhand) 73593139Sru bus_teardown_intr(dev, sc->wb_irq, sc->wb_intrhand); 73693139Sru if (sc->wb_irq) 73793139Sru bus_release_resource(dev, SYS_RES_IRQ, 0, sc->wb_irq); 73893139Sru if (sc->wb_res) 73993139Sru bus_release_resource(dev, WB_RES, WB_RID, sc->wb_res); 74093139Sru 74193139Sru if (ifp) 74293139Sru if_free(ifp); 74393139Sru 74493139Sru if (sc->wb_ldata) { 74593139Sru contigfree(sc->wb_ldata, sizeof(struct wb_list_data) + 8, 74693139Sru M_DEVBUF); 74793139Sru } 74893139Sru 74993139Sru mtx_destroy(&sc->wb_mtx); 75093139Sru 75193139Sru return(0); 752114472Sru} 753114472Sru 75493139Sru/* 75593139Sru * Initialize the transmit descriptors. 75693139Sru */ 75793139Srustatic int 75893139Sruwb_list_tx_init(sc) 75993139Sru struct wb_softc *sc; 76093139Sru{ 76193139Sru struct wb_chain_data *cd; 76293139Sru struct wb_list_data *ld; 76393139Sru int i; 76493139Sru 76593139Sru cd = &sc->wb_cdata; 76693139Sru ld = sc->wb_ldata; 76793139Sru 76893139Sru for (i = 0; i < WB_TX_LIST_CNT; i++) { 76993139Sru cd->wb_tx_chain[i].wb_ptr = &ld->wb_tx_list[i]; 77093139Sru if (i == (WB_TX_LIST_CNT - 1)) { 77193139Sru cd->wb_tx_chain[i].wb_nextdesc = 77293139Sru &cd->wb_tx_chain[0]; 77393139Sru } else { 77493139Sru cd->wb_tx_chain[i].wb_nextdesc = 77593139Sru &cd->wb_tx_chain[i + 1]; 77693139Sru } 77793139Sru } 77893139Sru 77993139Sru cd->wb_tx_free = &cd->wb_tx_chain[0]; 78093139Sru cd->wb_tx_tail = cd->wb_tx_head = NULL; 78193139Sru 78293139Sru return(0); 78393139Sru} 78493139Sru 78593139Sru 78693139Sru/* 78793139Sru * Initialize the RX descriptors and allocate mbufs for them. Note that 78893139Sru * we arrange the descriptors in a closed ring, so that the last descriptor 78993139Sru * points back to the first. 79093139Sru */ 79193139Srustatic int 79293139Sruwb_list_rx_init(sc) 79393139Sru struct wb_softc *sc; 79493139Sru{ 79593139Sru struct wb_chain_data *cd; 79693139Sru struct wb_list_data *ld; 79793139Sru int i; 79893139Sru 79993139Sru cd = &sc->wb_cdata; 80093139Sru ld = sc->wb_ldata; 80193139Sru 80293139Sru for (i = 0; i < WB_RX_LIST_CNT; i++) { 80393139Sru cd->wb_rx_chain[i].wb_ptr = 80493139Sru (struct wb_desc *)&ld->wb_rx_list[i]; 80593139Sru cd->wb_rx_chain[i].wb_buf = (void *)&ld->wb_rxbufs[i]; 80693139Sru if (wb_newbuf(sc, &cd->wb_rx_chain[i], NULL) == ENOBUFS) 80793139Sru return(ENOBUFS); 808146515Sru if (i == (WB_RX_LIST_CNT - 1)) { 80993139Sru cd->wb_rx_chain[i].wb_nextdesc = &cd->wb_rx_chain[0]; 81093139Sru ld->wb_rx_list[i].wb_next = 81193139Sru vtophys(&ld->wb_rx_list[0]); 81293139Sru } else { 81393139Sru cd->wb_rx_chain[i].wb_nextdesc = 81493139Sru &cd->wb_rx_chain[i + 1]; 81593139Sru ld->wb_rx_list[i].wb_next = 81693139Sru vtophys(&ld->wb_rx_list[i + 1]); 817146515Sru } 81893139Sru } 81993139Sru 82093139Sru cd->wb_rx_head = &cd->wb_rx_chain[0]; 82193139Sru 82293139Sru return(0); 823} 824 825static void 826wb_bfree(buf, args) 827 void *buf; 828 void *args; 829{ 830 831} 832 833/* 834 * Initialize an RX descriptor and attach an MBUF cluster. 835 */ 836static int 837wb_newbuf(sc, c, m) 838 struct wb_softc *sc; 839 struct wb_chain_onefrag *c; 840 struct mbuf *m; 841{ 842 struct mbuf *m_new = NULL; 843 844 if (m == NULL) { 845 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 846 if (m_new == NULL) 847 return(ENOBUFS); 848 m_new->m_data = c->wb_buf; 849 m_new->m_pkthdr.len = m_new->m_len = WB_BUFBYTES; 850 MEXTADD(m_new, c->wb_buf, WB_BUFBYTES, wb_bfree, c->wb_buf, 851 NULL, 0, EXT_NET_DRV); 852 } else { 853 m_new = m; 854 m_new->m_len = m_new->m_pkthdr.len = WB_BUFBYTES; 855 m_new->m_data = m_new->m_ext.ext_buf; 856 } 857 858 m_adj(m_new, sizeof(u_int64_t)); 859 860 c->wb_mbuf = m_new; 861 c->wb_ptr->wb_data = vtophys(mtod(m_new, caddr_t)); 862 c->wb_ptr->wb_ctl = WB_RXCTL_RLINK | 1536; 863 c->wb_ptr->wb_status = WB_RXSTAT; 864 865 return(0); 866} 867 868/* 869 * A frame has been uploaded: pass the resulting mbuf chain up to 870 * the higher level protocols. 871 */ 872static void 873wb_rxeof(sc) 874 struct wb_softc *sc; 875{ 876 struct mbuf *m = NULL; 877 struct ifnet *ifp; 878 struct wb_chain_onefrag *cur_rx; 879 int total_len = 0; 880 u_int32_t rxstat; 881 882 WB_LOCK_ASSERT(sc); 883 884 ifp = sc->wb_ifp; 885 886 while(!((rxstat = sc->wb_cdata.wb_rx_head->wb_ptr->wb_status) & 887 WB_RXSTAT_OWN)) { 888 struct mbuf *m0 = NULL; 889 890 cur_rx = sc->wb_cdata.wb_rx_head; 891 sc->wb_cdata.wb_rx_head = cur_rx->wb_nextdesc; 892 893 m = cur_rx->wb_mbuf; 894 895 if ((rxstat & WB_RXSTAT_MIIERR) || 896 (WB_RXBYTES(cur_rx->wb_ptr->wb_status) < WB_MIN_FRAMELEN) || 897 (WB_RXBYTES(cur_rx->wb_ptr->wb_status) > 1536) || 898 !(rxstat & WB_RXSTAT_LASTFRAG) || 899 !(rxstat & WB_RXSTAT_RXCMP)) { 900 ifp->if_ierrors++; 901 wb_newbuf(sc, cur_rx, m); 902 device_printf(sc->wb_dev, 903 "receiver babbling: possible chip bug," 904 " forcing reset\n"); 905 wb_fixmedia(sc); 906 wb_reset(sc); 907 wb_init_locked(sc); 908 return; 909 } 910 911 if (rxstat & WB_RXSTAT_RXERR) { 912 ifp->if_ierrors++; 913 wb_newbuf(sc, cur_rx, m); 914 break; 915 } 916 917 /* No errors; receive the packet. */ 918 total_len = WB_RXBYTES(cur_rx->wb_ptr->wb_status); 919 920 /* 921 * XXX The Winbond chip includes the CRC with every 922 * received frame, and there's no way to turn this 923 * behavior off (at least, I can't find anything in 924 * the manual that explains how to do it) so we have 925 * to trim off the CRC manually. 926 */ 927 total_len -= ETHER_CRC_LEN; 928 929 m0 = m_devget(mtod(m, char *), total_len, ETHER_ALIGN, ifp, 930 NULL); 931 wb_newbuf(sc, cur_rx, m); 932 if (m0 == NULL) { 933 ifp->if_ierrors++; 934 break; 935 } 936 m = m0; 937 938 ifp->if_ipackets++; 939 WB_UNLOCK(sc); 940 (*ifp->if_input)(ifp, m); 941 WB_LOCK(sc); 942 } 943} 944 945static void 946wb_rxeoc(sc) 947 struct wb_softc *sc; 948{ 949 wb_rxeof(sc); 950 951 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON); 952 CSR_WRITE_4(sc, WB_RXADDR, vtophys(&sc->wb_ldata->wb_rx_list[0])); 953 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON); 954 if (CSR_READ_4(sc, WB_ISR) & WB_RXSTATE_SUSPEND) 955 CSR_WRITE_4(sc, WB_RXSTART, 0xFFFFFFFF); 956} 957 958/* 959 * A frame was downloaded to the chip. It's safe for us to clean up 960 * the list buffers. 961 */ 962static void 963wb_txeof(sc) 964 struct wb_softc *sc; 965{ 966 struct wb_chain *cur_tx; 967 struct ifnet *ifp; 968 969 ifp = sc->wb_ifp; 970 971 /* Clear the timeout timer. */ 972 sc->wb_timer = 0; 973 974 if (sc->wb_cdata.wb_tx_head == NULL) 975 return; 976 977 /* 978 * Go through our tx list and free mbufs for those 979 * frames that have been transmitted. 980 */ 981 while(sc->wb_cdata.wb_tx_head->wb_mbuf != NULL) { 982 u_int32_t txstat; 983 984 cur_tx = sc->wb_cdata.wb_tx_head; 985 txstat = WB_TXSTATUS(cur_tx); 986 987 if ((txstat & WB_TXSTAT_OWN) || txstat == WB_UNSENT) 988 break; 989 990 if (txstat & WB_TXSTAT_TXERR) { 991 ifp->if_oerrors++; 992 if (txstat & WB_TXSTAT_ABORT) 993 ifp->if_collisions++; 994 if (txstat & WB_TXSTAT_LATECOLL) 995 ifp->if_collisions++; 996 } 997 998 ifp->if_collisions += (txstat & WB_TXSTAT_COLLCNT) >> 3; 999 1000 ifp->if_opackets++; 1001 m_freem(cur_tx->wb_mbuf); 1002 cur_tx->wb_mbuf = NULL; 1003 1004 if (sc->wb_cdata.wb_tx_head == sc->wb_cdata.wb_tx_tail) { 1005 sc->wb_cdata.wb_tx_head = NULL; 1006 sc->wb_cdata.wb_tx_tail = NULL; 1007 break; 1008 } 1009 1010 sc->wb_cdata.wb_tx_head = cur_tx->wb_nextdesc; 1011 } 1012} 1013 1014/* 1015 * TX 'end of channel' interrupt handler. 1016 */ 1017static void 1018wb_txeoc(sc) 1019 struct wb_softc *sc; 1020{ 1021 struct ifnet *ifp; 1022 1023 ifp = sc->wb_ifp; 1024 1025 sc->wb_timer = 0; 1026 1027 if (sc->wb_cdata.wb_tx_head == NULL) { 1028 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1029 sc->wb_cdata.wb_tx_tail = NULL; 1030 } else { 1031 if (WB_TXOWN(sc->wb_cdata.wb_tx_head) == WB_UNSENT) { 1032 WB_TXOWN(sc->wb_cdata.wb_tx_head) = WB_TXSTAT_OWN; 1033 sc->wb_timer = 5; 1034 CSR_WRITE_4(sc, WB_TXSTART, 0xFFFFFFFF); 1035 } 1036 } 1037} 1038 1039static void 1040wb_intr(arg) 1041 void *arg; 1042{ 1043 struct wb_softc *sc; 1044 struct ifnet *ifp; 1045 u_int32_t status; 1046 1047 sc = arg; 1048 WB_LOCK(sc); 1049 ifp = sc->wb_ifp; 1050 1051 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1052 WB_UNLOCK(sc); 1053 return; 1054 } 1055 1056 /* Disable interrupts. */ 1057 CSR_WRITE_4(sc, WB_IMR, 0x00000000); 1058 1059 for (;;) { 1060 1061 status = CSR_READ_4(sc, WB_ISR); 1062 if (status) 1063 CSR_WRITE_4(sc, WB_ISR, status); 1064 1065 if ((status & WB_INTRS) == 0) 1066 break; 1067 1068 if ((status & WB_ISR_RX_NOBUF) || (status & WB_ISR_RX_ERR)) { 1069 ifp->if_ierrors++; 1070 wb_reset(sc); 1071 if (status & WB_ISR_RX_ERR) 1072 wb_fixmedia(sc); 1073 wb_init_locked(sc); 1074 continue; 1075 } 1076 1077 if (status & WB_ISR_RX_OK) 1078 wb_rxeof(sc); 1079 1080 if (status & WB_ISR_RX_IDLE) 1081 wb_rxeoc(sc); 1082 1083 if (status & WB_ISR_TX_OK) 1084 wb_txeof(sc); 1085 1086 if (status & WB_ISR_TX_NOBUF) 1087 wb_txeoc(sc); 1088 1089 if (status & WB_ISR_TX_IDLE) { 1090 wb_txeof(sc); 1091 if (sc->wb_cdata.wb_tx_head != NULL) { 1092 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON); 1093 CSR_WRITE_4(sc, WB_TXSTART, 0xFFFFFFFF); 1094 } 1095 } 1096 1097 if (status & WB_ISR_TX_UNDERRUN) { 1098 ifp->if_oerrors++; 1099 wb_txeof(sc); 1100 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON); 1101 /* Jack up TX threshold */ 1102 sc->wb_txthresh += WB_TXTHRESH_CHUNK; 1103 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_THRESH); 1104 WB_SETBIT(sc, WB_NETCFG, WB_TXTHRESH(sc->wb_txthresh)); 1105 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON); 1106 } 1107 1108 if (status & WB_ISR_BUS_ERR) { 1109 wb_reset(sc); 1110 wb_init_locked(sc); 1111 } 1112 1113 } 1114 1115 /* Re-enable interrupts. */ 1116 CSR_WRITE_4(sc, WB_IMR, WB_INTRS); 1117 1118 if (ifp->if_snd.ifq_head != NULL) { 1119 wb_start_locked(ifp); 1120 } 1121 1122 WB_UNLOCK(sc); 1123} 1124 1125static void 1126wb_tick(xsc) 1127 void *xsc; 1128{ 1129 struct wb_softc *sc; 1130 struct mii_data *mii; 1131 1132 sc = xsc; 1133 WB_LOCK_ASSERT(sc); 1134 mii = device_get_softc(sc->wb_miibus); 1135 1136 mii_tick(mii); 1137 1138 if (sc->wb_timer > 0 && --sc->wb_timer == 0) 1139 wb_watchdog(sc); 1140 callout_reset(&sc->wb_stat_callout, hz, wb_tick, sc); 1141} 1142 1143/* 1144 * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data 1145 * pointers to the fragment pointers. 1146 */ 1147static int 1148wb_encap(sc, c, m_head) 1149 struct wb_softc *sc; 1150 struct wb_chain *c; 1151 struct mbuf *m_head; 1152{ 1153 int frag = 0; 1154 struct wb_desc *f = NULL; 1155 int total_len; 1156 struct mbuf *m; 1157 1158 /* 1159 * Start packing the mbufs in this chain into 1160 * the fragment pointers. Stop when we run out 1161 * of fragments or hit the end of the mbuf chain. 1162 */ 1163 m = m_head; 1164 total_len = 0; 1165 1166 for (m = m_head, frag = 0; m != NULL; m = m->m_next) { 1167 if (m->m_len != 0) { 1168 if (frag == WB_MAXFRAGS) 1169 break; 1170 total_len += m->m_len; 1171 f = &c->wb_ptr->wb_frag[frag]; 1172 f->wb_ctl = WB_TXCTL_TLINK | m->m_len; 1173 if (frag == 0) { 1174 f->wb_ctl |= WB_TXCTL_FIRSTFRAG; 1175 f->wb_status = 0; 1176 } else 1177 f->wb_status = WB_TXSTAT_OWN; 1178 f->wb_next = vtophys(&c->wb_ptr->wb_frag[frag + 1]); 1179 f->wb_data = vtophys(mtod(m, vm_offset_t)); 1180 frag++; 1181 } 1182 } 1183 1184 /* 1185 * Handle special case: we used up all 16 fragments, 1186 * but we have more mbufs left in the chain. Copy the 1187 * data into an mbuf cluster. Note that we don't 1188 * bother clearing the values in the other fragment 1189 * pointers/counters; it wouldn't gain us anything, 1190 * and would waste cycles. 1191 */ 1192 if (m != NULL) { 1193 struct mbuf *m_new = NULL; 1194 1195 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 1196 if (m_new == NULL) 1197 return(1); 1198 if (m_head->m_pkthdr.len > MHLEN) { 1199 MCLGET(m_new, M_DONTWAIT); 1200 if (!(m_new->m_flags & M_EXT)) { 1201 m_freem(m_new); 1202 return(1); 1203 } 1204 } 1205 m_copydata(m_head, 0, m_head->m_pkthdr.len, 1206 mtod(m_new, caddr_t)); 1207 m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len; 1208 m_freem(m_head); 1209 m_head = m_new; 1210 f = &c->wb_ptr->wb_frag[0]; 1211 f->wb_status = 0; 1212 f->wb_data = vtophys(mtod(m_new, caddr_t)); 1213 f->wb_ctl = total_len = m_new->m_len; 1214 f->wb_ctl |= WB_TXCTL_TLINK|WB_TXCTL_FIRSTFRAG; 1215 frag = 1; 1216 } 1217 1218 if (total_len < WB_MIN_FRAMELEN) { 1219 f = &c->wb_ptr->wb_frag[frag]; 1220 f->wb_ctl = WB_MIN_FRAMELEN - total_len; 1221 f->wb_data = vtophys(&sc->wb_cdata.wb_pad); 1222 f->wb_ctl |= WB_TXCTL_TLINK; 1223 f->wb_status = WB_TXSTAT_OWN; 1224 frag++; 1225 } 1226 1227 c->wb_mbuf = m_head; 1228 c->wb_lastdesc = frag - 1; 1229 WB_TXCTL(c) |= WB_TXCTL_LASTFRAG; 1230 WB_TXNEXT(c) = vtophys(&c->wb_nextdesc->wb_ptr->wb_frag[0]); 1231 1232 return(0); 1233} 1234 1235/* 1236 * Main transmit routine. To avoid having to do mbuf copies, we put pointers 1237 * to the mbuf data regions directly in the transmit lists. We also save a 1238 * copy of the pointers since the transmit list fragment pointers are 1239 * physical addresses. 1240 */ 1241 1242static void 1243wb_start(ifp) 1244 struct ifnet *ifp; 1245{ 1246 struct wb_softc *sc; 1247 1248 sc = ifp->if_softc; 1249 WB_LOCK(sc); 1250 wb_start_locked(ifp); 1251 WB_UNLOCK(sc); 1252} 1253 1254static void 1255wb_start_locked(ifp) 1256 struct ifnet *ifp; 1257{ 1258 struct wb_softc *sc; 1259 struct mbuf *m_head = NULL; 1260 struct wb_chain *cur_tx = NULL, *start_tx; 1261 1262 sc = ifp->if_softc; 1263 WB_LOCK_ASSERT(sc); 1264 1265 /* 1266 * Check for an available queue slot. If there are none, 1267 * punt. 1268 */ 1269 if (sc->wb_cdata.wb_tx_free->wb_mbuf != NULL) { 1270 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1271 return; 1272 } 1273 1274 start_tx = sc->wb_cdata.wb_tx_free; 1275 1276 while(sc->wb_cdata.wb_tx_free->wb_mbuf == NULL) { 1277 IF_DEQUEUE(&ifp->if_snd, m_head); 1278 if (m_head == NULL) 1279 break; 1280 1281 /* Pick a descriptor off the free list. */ 1282 cur_tx = sc->wb_cdata.wb_tx_free; 1283 sc->wb_cdata.wb_tx_free = cur_tx->wb_nextdesc; 1284 1285 /* Pack the data into the descriptor. */ 1286 wb_encap(sc, cur_tx, m_head); 1287 1288 if (cur_tx != start_tx) 1289 WB_TXOWN(cur_tx) = WB_TXSTAT_OWN; 1290 1291 /* 1292 * If there's a BPF listener, bounce a copy of this frame 1293 * to him. 1294 */ 1295 BPF_MTAP(ifp, cur_tx->wb_mbuf); 1296 } 1297 1298 /* 1299 * If there are no packets queued, bail. 1300 */ 1301 if (cur_tx == NULL) 1302 return; 1303 1304 /* 1305 * Place the request for the upload interrupt 1306 * in the last descriptor in the chain. This way, if 1307 * we're chaining several packets at once, we'll only 1308 * get an interrupt once for the whole chain rather than 1309 * once for each packet. 1310 */ 1311 WB_TXCTL(cur_tx) |= WB_TXCTL_FINT; 1312 cur_tx->wb_ptr->wb_frag[0].wb_ctl |= WB_TXCTL_FINT; 1313 sc->wb_cdata.wb_tx_tail = cur_tx; 1314 1315 if (sc->wb_cdata.wb_tx_head == NULL) { 1316 sc->wb_cdata.wb_tx_head = start_tx; 1317 WB_TXOWN(start_tx) = WB_TXSTAT_OWN; 1318 CSR_WRITE_4(sc, WB_TXSTART, 0xFFFFFFFF); 1319 } else { 1320 /* 1321 * We need to distinguish between the case where 1322 * the own bit is clear because the chip cleared it 1323 * and where the own bit is clear because we haven't 1324 * set it yet. The magic value WB_UNSET is just some 1325 * ramdomly chosen number which doesn't have the own 1326 * bit set. When we actually transmit the frame, the 1327 * status word will have _only_ the own bit set, so 1328 * the txeoc handler will be able to tell if it needs 1329 * to initiate another transmission to flush out pending 1330 * frames. 1331 */ 1332 WB_TXOWN(start_tx) = WB_UNSENT; 1333 } 1334 1335 /* 1336 * Set a timeout in case the chip goes out to lunch. 1337 */ 1338 sc->wb_timer = 5; 1339} 1340 1341static void 1342wb_init(xsc) 1343 void *xsc; 1344{ 1345 struct wb_softc *sc = xsc; 1346 1347 WB_LOCK(sc); 1348 wb_init_locked(sc); 1349 WB_UNLOCK(sc); 1350} 1351 1352static void 1353wb_init_locked(sc) 1354 struct wb_softc *sc; 1355{ 1356 struct ifnet *ifp = sc->wb_ifp; 1357 int i; 1358 struct mii_data *mii; 1359 1360 WB_LOCK_ASSERT(sc); 1361 mii = device_get_softc(sc->wb_miibus); 1362 1363 /* 1364 * Cancel pending I/O and free all RX/TX buffers. 1365 */ 1366 wb_stop(sc); 1367 wb_reset(sc); 1368 1369 sc->wb_txthresh = WB_TXTHRESH_INIT; 1370 1371 /* 1372 * Set cache alignment and burst length. 1373 */ 1374#ifdef foo 1375 CSR_WRITE_4(sc, WB_BUSCTL, WB_BUSCTL_CONFIG); 1376 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_THRESH); 1377 WB_SETBIT(sc, WB_NETCFG, WB_TXTHRESH(sc->wb_txthresh)); 1378#endif 1379 1380 CSR_WRITE_4(sc, WB_BUSCTL, WB_BUSCTL_MUSTBEONE|WB_BUSCTL_ARBITRATION); 1381 WB_SETBIT(sc, WB_BUSCTL, WB_BURSTLEN_16LONG); 1382 switch(sc->wb_cachesize) { 1383 case 32: 1384 WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_32LONG); 1385 break; 1386 case 16: 1387 WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_16LONG); 1388 break; 1389 case 8: 1390 WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_8LONG); 1391 break; 1392 case 0: 1393 default: 1394 WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_NONE); 1395 break; 1396 } 1397 1398 /* This doesn't tend to work too well at 100Mbps. */ 1399 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_EARLY_ON); 1400 1401 /* Init our MAC address */ 1402 for (i = 0; i < ETHER_ADDR_LEN; i++) { 1403 CSR_WRITE_1(sc, WB_NODE0 + i, IF_LLADDR(sc->wb_ifp)[i]); 1404 } 1405 1406 /* Init circular RX list. */ 1407 if (wb_list_rx_init(sc) == ENOBUFS) { 1408 device_printf(sc->wb_dev, 1409 "initialization failed: no memory for rx buffers\n"); 1410 wb_stop(sc); 1411 return; 1412 } 1413 1414 /* Init TX descriptors. */ 1415 wb_list_tx_init(sc); 1416 1417 /* If we want promiscuous mode, set the allframes bit. */ 1418 if (ifp->if_flags & IFF_PROMISC) { 1419 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_ALLPHYS); 1420 } else { 1421 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_ALLPHYS); 1422 } 1423 1424 /* 1425 * Set capture broadcast bit to capture broadcast frames. 1426 */ 1427 if (ifp->if_flags & IFF_BROADCAST) { 1428 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_BROAD); 1429 } else { 1430 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_BROAD); 1431 } 1432 1433 /* 1434 * Program the multicast filter, if necessary. 1435 */ 1436 wb_setmulti(sc); 1437 1438 /* 1439 * Load the address of the RX list. 1440 */ 1441 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON); 1442 CSR_WRITE_4(sc, WB_RXADDR, vtophys(&sc->wb_ldata->wb_rx_list[0])); 1443 1444 /* 1445 * Enable interrupts. 1446 */ 1447 CSR_WRITE_4(sc, WB_IMR, WB_INTRS); 1448 CSR_WRITE_4(sc, WB_ISR, 0xFFFFFFFF); 1449 1450 /* Enable receiver and transmitter. */ 1451 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON); 1452 CSR_WRITE_4(sc, WB_RXSTART, 0xFFFFFFFF); 1453 1454 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON); 1455 CSR_WRITE_4(sc, WB_TXADDR, vtophys(&sc->wb_ldata->wb_tx_list[0])); 1456 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON); 1457 1458 mii_mediachg(mii); 1459 1460 ifp->if_drv_flags |= IFF_DRV_RUNNING; 1461 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1462 1463 callout_reset(&sc->wb_stat_callout, hz, wb_tick, sc); 1464} 1465 1466/* 1467 * Set media options. 1468 */ 1469static int 1470wb_ifmedia_upd(ifp) 1471 struct ifnet *ifp; 1472{ 1473 struct wb_softc *sc; 1474 1475 sc = ifp->if_softc; 1476 1477 WB_LOCK(sc); 1478 if (ifp->if_flags & IFF_UP) 1479 wb_init_locked(sc); 1480 WB_UNLOCK(sc); 1481 1482 return(0); 1483} 1484 1485/* 1486 * Report current media status. 1487 */ 1488static void 1489wb_ifmedia_sts(ifp, ifmr) 1490 struct ifnet *ifp; 1491 struct ifmediareq *ifmr; 1492{ 1493 struct wb_softc *sc; 1494 struct mii_data *mii; 1495 1496 sc = ifp->if_softc; 1497 1498 WB_LOCK(sc); 1499 mii = device_get_softc(sc->wb_miibus); 1500 1501 mii_pollstat(mii); 1502 ifmr->ifm_active = mii->mii_media_active; 1503 ifmr->ifm_status = mii->mii_media_status; 1504 WB_UNLOCK(sc); 1505} 1506 1507static int 1508wb_ioctl(ifp, command, data) 1509 struct ifnet *ifp; 1510 u_long command; 1511 caddr_t data; 1512{ 1513 struct wb_softc *sc = ifp->if_softc; 1514 struct mii_data *mii; 1515 struct ifreq *ifr = (struct ifreq *) data; 1516 int error = 0; 1517 1518 switch(command) { 1519 case SIOCSIFFLAGS: 1520 WB_LOCK(sc); 1521 if (ifp->if_flags & IFF_UP) { 1522 wb_init_locked(sc); 1523 } else { 1524 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1525 wb_stop(sc); 1526 } 1527 WB_UNLOCK(sc); 1528 error = 0; 1529 break; 1530 case SIOCADDMULTI: 1531 case SIOCDELMULTI: 1532 WB_LOCK(sc); 1533 wb_setmulti(sc); 1534 WB_UNLOCK(sc); 1535 error = 0; 1536 break; 1537 case SIOCGIFMEDIA: 1538 case SIOCSIFMEDIA: 1539 mii = device_get_softc(sc->wb_miibus); 1540 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 1541 break; 1542 default: 1543 error = ether_ioctl(ifp, command, data); 1544 break; 1545 } 1546 1547 return(error); 1548} 1549 1550static void 1551wb_watchdog(sc) 1552 struct wb_softc *sc; 1553{ 1554 struct ifnet *ifp; 1555 1556 WB_LOCK_ASSERT(sc); 1557 ifp = sc->wb_ifp; 1558 ifp->if_oerrors++; 1559 if_printf(ifp, "watchdog timeout\n"); 1560#ifdef foo 1561 if (!(wb_phy_readreg(sc, PHY_BMSR) & PHY_BMSR_LINKSTAT)) 1562 if_printf(ifp, "no carrier - transceiver cable problem?\n"); 1563#endif 1564 wb_stop(sc); 1565 wb_reset(sc); 1566 wb_init_locked(sc); 1567 1568 if (ifp->if_snd.ifq_head != NULL) 1569 wb_start_locked(ifp); 1570} 1571 1572/* 1573 * Stop the adapter and free any mbufs allocated to the 1574 * RX and TX lists. 1575 */ 1576static void 1577wb_stop(sc) 1578 struct wb_softc *sc; 1579{ 1580 register int i; 1581 struct ifnet *ifp; 1582 1583 WB_LOCK_ASSERT(sc); 1584 ifp = sc->wb_ifp; 1585 sc->wb_timer = 0; 1586 1587 callout_stop(&sc->wb_stat_callout); 1588 1589 WB_CLRBIT(sc, WB_NETCFG, (WB_NETCFG_RX_ON|WB_NETCFG_TX_ON)); 1590 CSR_WRITE_4(sc, WB_IMR, 0x00000000); 1591 CSR_WRITE_4(sc, WB_TXADDR, 0x00000000); 1592 CSR_WRITE_4(sc, WB_RXADDR, 0x00000000); 1593 1594 /* 1595 * Free data in the RX lists. 1596 */ 1597 for (i = 0; i < WB_RX_LIST_CNT; i++) { 1598 if (sc->wb_cdata.wb_rx_chain[i].wb_mbuf != NULL) { 1599 m_freem(sc->wb_cdata.wb_rx_chain[i].wb_mbuf); 1600 sc->wb_cdata.wb_rx_chain[i].wb_mbuf = NULL; 1601 } 1602 } 1603 bzero((char *)&sc->wb_ldata->wb_rx_list, 1604 sizeof(sc->wb_ldata->wb_rx_list)); 1605 1606 /* 1607 * Free the TX list buffers. 1608 */ 1609 for (i = 0; i < WB_TX_LIST_CNT; i++) { 1610 if (sc->wb_cdata.wb_tx_chain[i].wb_mbuf != NULL) { 1611 m_freem(sc->wb_cdata.wb_tx_chain[i].wb_mbuf); 1612 sc->wb_cdata.wb_tx_chain[i].wb_mbuf = NULL; 1613 } 1614 } 1615 1616 bzero((char *)&sc->wb_ldata->wb_tx_list, 1617 sizeof(sc->wb_ldata->wb_tx_list)); 1618 1619 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 1620} 1621 1622/* 1623 * Stop all chip I/O so that the kernel's probe routines don't 1624 * get confused by errant DMAs when rebooting. 1625 */ 1626static int 1627wb_shutdown(dev) 1628 device_t dev; 1629{ 1630 struct wb_softc *sc; 1631 1632 sc = device_get_softc(dev); 1633 1634 WB_LOCK(sc); 1635 wb_stop(sc); 1636 WB_UNLOCK(sc); 1637 1638 return (0); 1639} 1640