1139825Simp/*- 266131Swpaul * Copyright (c) 2000 Berkeley Software Design, Inc. 366131Swpaul * Copyright (c) 1997, 1998, 1999, 2000 466131Swpaul * Bill Paul <wpaul@osd.bsdi.com>. All rights reserved. 566131Swpaul * 666131Swpaul * Redistribution and use in source and binary forms, with or without 766131Swpaul * modification, are permitted provided that the following conditions 866131Swpaul * are met: 966131Swpaul * 1. Redistributions of source code must retain the above copyright 1066131Swpaul * notice, this list of conditions and the following disclaimer. 1166131Swpaul * 2. Redistributions in binary form must reproduce the above copyright 1266131Swpaul * notice, this list of conditions and the following disclaimer in the 1366131Swpaul * documentation and/or other materials provided with the distribution. 1466131Swpaul * 3. All advertising materials mentioning features or use of this software 1566131Swpaul * must display the following acknowledgement: 1666131Swpaul * This product includes software developed by Bill Paul. 1766131Swpaul * 4. Neither the name of the author nor the names of any co-contributors 1866131Swpaul * may be used to endorse or promote products derived from this software 1966131Swpaul * without specific prior written permission. 2066131Swpaul * 2166131Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 2266131Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2366131Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2466131Swpaul * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 2566131Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2666131Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2766131Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2866131Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2966131Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3066131Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 3166131Swpaul * THE POSSIBILITY OF SUCH DAMAGE. 3266131Swpaul */ 3366131Swpaul 34122678Sobrien#include <sys/cdefs.h> 35122678Sobrien__FBSDID("$FreeBSD$"); 36122678Sobrien 3766131Swpaul/* 38134842Sbrueffer * AMD Am79c972 fast ethernet PCI NIC driver. Datasheets are available 3966131Swpaul * from http://www.amd.com. 4066131Swpaul * 4166131Swpaul * The AMD PCnet/PCI controllers are more advanced and functional 4266131Swpaul * versions of the venerable 7990 LANCE. The PCnet/PCI chips retain 4366131Swpaul * backwards compatibility with the LANCE and thus can be made 4466131Swpaul * to work with older LANCE drivers. This is in fact how the 4566131Swpaul * PCnet/PCI chips were supported in FreeBSD originally. The trouble 4666131Swpaul * is that the PCnet/PCI devices offer several performance enhancements 4766131Swpaul * which can't be exploited in LANCE compatibility mode. Chief among 4866131Swpaul * these enhancements is the ability to perform PCI DMA operations 4966131Swpaul * using 32-bit addressing (which eliminates the need for ISA 5066131Swpaul * bounce-buffering), and special receive buffer alignment (which 5166131Swpaul * allows the receive handler to pass packets to the upper protocol 5266131Swpaul * layers without copying on both the x86 and alpha platforms). 5366131Swpaul */ 5466131Swpaul 5566131Swpaul#include <sys/param.h> 5666131Swpaul#include <sys/systm.h> 5766131Swpaul#include <sys/sockio.h> 5866131Swpaul#include <sys/mbuf.h> 5966131Swpaul#include <sys/malloc.h> 6066131Swpaul#include <sys/kernel.h> 61129878Sphk#include <sys/module.h> 6266131Swpaul#include <sys/socket.h> 6366131Swpaul 6466131Swpaul#include <net/if.h> 6566131Swpaul#include <net/if_arp.h> 6666131Swpaul#include <net/ethernet.h> 6766131Swpaul#include <net/if_dl.h> 6866131Swpaul#include <net/if_media.h> 69147256Sbrooks#include <net/if_types.h> 7066131Swpaul 7166131Swpaul#include <net/bpf.h> 7266131Swpaul 7366131Swpaul#include <vm/vm.h> /* for vtophys */ 7466131Swpaul#include <vm/pmap.h> /* for vtophys */ 7566131Swpaul#include <machine/bus.h> 7666131Swpaul#include <machine/resource.h> 7766131Swpaul#include <sys/bus.h> 7866131Swpaul#include <sys/rman.h> 7966131Swpaul 8066131Swpaul#include <dev/mii/mii.h> 8166131Swpaul#include <dev/mii/miivar.h> 8266131Swpaul 83119288Simp#include <dev/pci/pcireg.h> 84119288Simp#include <dev/pci/pcivar.h> 8566131Swpaul 8666131Swpaul#define PCN_USEIOSPACE 8766131Swpaul 88181740Simp#include <dev/pcn/if_pcnreg.h> 8966131Swpaul 90113506SmdoddMODULE_DEPEND(pcn, pci, 1, 1, 1); 91113506SmdoddMODULE_DEPEND(pcn, ether, 1, 1, 1); 9266131SwpaulMODULE_DEPEND(pcn, miibus, 1, 1, 1); 9366131Swpaul 94149584Simp/* "device miibus" required. See GENERIC if you get errors here. */ 9566131Swpaul#include "miibus_if.h" 9666131Swpaul 9766131Swpaul/* 9866131Swpaul * Various supported device vendors/types and their names. 9966131Swpaul */ 100164072Smariusstatic const struct pcn_type pcn_devs[] = { 10166131Swpaul { PCN_VENDORID, PCN_DEVICEID_PCNET, "AMD PCnet/PCI 10/100BaseTX" }, 10266131Swpaul { PCN_VENDORID, PCN_DEVICEID_HOME, "AMD PCnet/Home HomePNA" }, 10366131Swpaul { 0, 0, NULL } 10466131Swpaul}; 10566131Swpaul 106164072Smariusstatic const struct pcn_chipid { 107138354Smdodd u_int32_t id; 108164072Smarius const char *name; 109138354Smdodd} pcn_chipid[] = { 110138354Smdodd { Am79C971, "Am79C971" }, 111138354Smdodd { Am79C972, "Am79C972" }, 112138354Smdodd { Am79C973, "Am79C973" }, 113138354Smdodd { Am79C978, "Am79C978" }, 114138354Smdodd { Am79C975, "Am79C975" }, 115138354Smdodd { Am79C976, "Am79C976" }, 116138354Smdodd { 0, NULL }, 117138354Smdodd}; 118138354Smdodd 119164072Smariusstatic const char *pcn_chipid_name(u_int32_t); 120142407Simpstatic u_int32_t pcn_chip_id(device_t); 121164072Smariusstatic const struct pcn_type *pcn_match(u_int16_t, u_int16_t); 122138354Smdodd 123142407Simpstatic u_int32_t pcn_csr_read(struct pcn_softc *, int); 124142407Simpstatic u_int16_t pcn_csr_read16(struct pcn_softc *, int); 125142407Simpstatic u_int16_t pcn_bcr_read16(struct pcn_softc *, int); 126142407Simpstatic void pcn_csr_write(struct pcn_softc *, int, int); 127142407Simpstatic u_int32_t pcn_bcr_read(struct pcn_softc *, int); 128142407Simpstatic void pcn_bcr_write(struct pcn_softc *, int, int); 12966131Swpaul 130142407Simpstatic int pcn_probe(device_t); 131142407Simpstatic int pcn_attach(device_t); 132142407Simpstatic int pcn_detach(device_t); 13366131Swpaul 134142407Simpstatic int pcn_newbuf(struct pcn_softc *, int, struct mbuf *); 135142407Simpstatic int pcn_encap(struct pcn_softc *, struct mbuf *, u_int32_t *); 136142407Simpstatic void pcn_rxeof(struct pcn_softc *); 137142407Simpstatic void pcn_txeof(struct pcn_softc *); 138142407Simpstatic void pcn_intr(void *); 139142407Simpstatic void pcn_tick(void *); 140142407Simpstatic void pcn_start(struct ifnet *); 141148738Sjhbstatic void pcn_start_locked(struct ifnet *); 142142407Simpstatic int pcn_ioctl(struct ifnet *, u_long, caddr_t); 143142407Simpstatic void pcn_init(void *); 144148738Sjhbstatic void pcn_init_locked(struct pcn_softc *); 145142407Simpstatic void pcn_stop(struct pcn_softc *); 146199560Sjhbstatic void pcn_watchdog(struct pcn_softc *); 147188177Simpstatic int pcn_shutdown(device_t); 148142407Simpstatic int pcn_ifmedia_upd(struct ifnet *); 149142407Simpstatic void pcn_ifmedia_sts(struct ifnet *, struct ifmediareq *); 15066131Swpaul 151142407Simpstatic int pcn_miibus_readreg(device_t, int, int); 152142407Simpstatic int pcn_miibus_writereg(device_t, int, int, int); 153142407Simpstatic void pcn_miibus_statchg(device_t); 15466131Swpaul 155142407Simpstatic void pcn_setfilt(struct ifnet *); 156142407Simpstatic void pcn_setmulti(struct pcn_softc *); 157142407Simpstatic void pcn_reset(struct pcn_softc *); 158142407Simpstatic int pcn_list_rx_init(struct pcn_softc *); 159142407Simpstatic int pcn_list_tx_init(struct pcn_softc *); 16066131Swpaul 16166131Swpaul#ifdef PCN_USEIOSPACE 16266131Swpaul#define PCN_RES SYS_RES_IOPORT 16366131Swpaul#define PCN_RID PCN_PCI_LOIO 16466131Swpaul#else 16566131Swpaul#define PCN_RES SYS_RES_MEMORY 16666131Swpaul#define PCN_RID PCN_PCI_LOMEM 16766131Swpaul#endif 16866131Swpaul 16966131Swpaulstatic device_method_t pcn_methods[] = { 17066131Swpaul /* Device interface */ 17166131Swpaul DEVMETHOD(device_probe, pcn_probe), 17266131Swpaul DEVMETHOD(device_attach, pcn_attach), 17366131Swpaul DEVMETHOD(device_detach, pcn_detach), 17466131Swpaul DEVMETHOD(device_shutdown, pcn_shutdown), 17566131Swpaul 17666131Swpaul /* MII interface */ 17766131Swpaul DEVMETHOD(miibus_readreg, pcn_miibus_readreg), 17866131Swpaul DEVMETHOD(miibus_writereg, pcn_miibus_writereg), 17966131Swpaul DEVMETHOD(miibus_statchg, pcn_miibus_statchg), 18066131Swpaul 181227843Smarius DEVMETHOD_END 18266131Swpaul}; 18366131Swpaul 18466131Swpaulstatic driver_t pcn_driver = { 18566131Swpaul "pcn", 18666131Swpaul pcn_methods, 18766131Swpaul sizeof(struct pcn_softc) 18866131Swpaul}; 18966131Swpaul 19066131Swpaulstatic devclass_t pcn_devclass; 19166131Swpaul 192113506SmdoddDRIVER_MODULE(pcn, pci, pcn_driver, pcn_devclass, 0, 0); 19366131SwpaulDRIVER_MODULE(miibus, pcn, miibus_driver, miibus_devclass, 0, 0); 19466131Swpaul 19566131Swpaul#define PCN_CSR_SETBIT(sc, reg, x) \ 19666131Swpaul pcn_csr_write(sc, reg, pcn_csr_read(sc, reg) | (x)) 19766131Swpaul 19866131Swpaul#define PCN_CSR_CLRBIT(sc, reg, x) \ 19966131Swpaul pcn_csr_write(sc, reg, pcn_csr_read(sc, reg) & ~(x)) 20066131Swpaul 20166131Swpaul#define PCN_BCR_SETBIT(sc, reg, x) \ 20266131Swpaul pcn_bcr_write(sc, reg, pcn_bcr_read(sc, reg) | (x)) 20366131Swpaul 20466131Swpaul#define PCN_BCR_CLRBIT(sc, reg, x) \ 20566131Swpaul pcn_bcr_write(sc, reg, pcn_bcr_read(sc, reg) & ~(x)) 20666131Swpaul 207102335Salfredstatic u_int32_t 208102335Salfredpcn_csr_read(sc, reg) 20966131Swpaul struct pcn_softc *sc; 21066131Swpaul int reg; 21166131Swpaul{ 21266131Swpaul CSR_WRITE_4(sc, PCN_IO32_RAP, reg); 21366131Swpaul return(CSR_READ_4(sc, PCN_IO32_RDP)); 21466131Swpaul} 21566131Swpaul 216102335Salfredstatic u_int16_t 217102335Salfredpcn_csr_read16(sc, reg) 21868837Swpaul struct pcn_softc *sc; 21968837Swpaul int reg; 22068837Swpaul{ 22168837Swpaul CSR_WRITE_2(sc, PCN_IO16_RAP, reg); 22268837Swpaul return(CSR_READ_2(sc, PCN_IO16_RDP)); 22368837Swpaul} 22468837Swpaul 225102335Salfredstatic void 226102335Salfredpcn_csr_write(sc, reg, val) 22766131Swpaul struct pcn_softc *sc; 22866131Swpaul int reg; 229113799Sobrien int val; 23066131Swpaul{ 23166131Swpaul CSR_WRITE_4(sc, PCN_IO32_RAP, reg); 23266131Swpaul CSR_WRITE_4(sc, PCN_IO32_RDP, val); 23366131Swpaul return; 23466131Swpaul} 23566131Swpaul 236102335Salfredstatic u_int32_t 237102335Salfredpcn_bcr_read(sc, reg) 23866131Swpaul struct pcn_softc *sc; 23966131Swpaul int reg; 24066131Swpaul{ 24166131Swpaul CSR_WRITE_4(sc, PCN_IO32_RAP, reg); 24266131Swpaul return(CSR_READ_4(sc, PCN_IO32_BDP)); 24366131Swpaul} 24466131Swpaul 245102335Salfredstatic u_int16_t 246102335Salfredpcn_bcr_read16(sc, reg) 24769067Swpaul struct pcn_softc *sc; 24869067Swpaul int reg; 24969067Swpaul{ 25069067Swpaul CSR_WRITE_2(sc, PCN_IO16_RAP, reg); 25169067Swpaul return(CSR_READ_2(sc, PCN_IO16_BDP)); 25269067Swpaul} 25369067Swpaul 254102335Salfredstatic void 255102335Salfredpcn_bcr_write(sc, reg, val) 25666131Swpaul struct pcn_softc *sc; 25766131Swpaul int reg; 258113799Sobrien int val; 25966131Swpaul{ 26066131Swpaul CSR_WRITE_4(sc, PCN_IO32_RAP, reg); 26166131Swpaul CSR_WRITE_4(sc, PCN_IO32_BDP, val); 26266131Swpaul return; 26366131Swpaul} 26466131Swpaul 265102335Salfredstatic int 266102335Salfredpcn_miibus_readreg(dev, phy, reg) 26766131Swpaul device_t dev; 26866131Swpaul int phy, reg; 26966131Swpaul{ 27066131Swpaul struct pcn_softc *sc; 27166131Swpaul int val; 27266131Swpaul 27366131Swpaul sc = device_get_softc(dev); 27466131Swpaul 275164712Smarius /* 276164712Smarius * At least Am79C971 with DP83840A wedge when isolating the 277164712Smarius * external PHY so we can't allow multiple external PHYs. 278165997Smarius * There are cards that use Am79C971 with both the internal 279165997Smarius * and an external PHY though. 280164712Smarius * For internal PHYs it doesn't really matter whether we can 281164712Smarius * isolate the remaining internal and the external ones in 282164712Smarius * the PHY drivers as the internal PHYs have to be enabled 283164712Smarius * individually in PCN_BCR_PHYSEL, PCN_CSR_MODE, etc. 284165997Smarius * With Am79C97{3,5,8} we don't support switching beetween 285165997Smarius * the internal and external PHYs, yet, so we can't allow 286165997Smarius * multiple PHYs with these either. 287165997Smarius * Am79C97{2,6} actually only support external PHYs (not 288165997Smarius * connectable internal ones respond at the usual addresses, 289165997Smarius * which don't hurt if we let them show up on the bus) and 290165997Smarius * isolating them works. 291164712Smarius */ 292165997Smarius if (((sc->pcn_type == Am79C971 && phy != PCN_PHYAD_10BT) || 293165997Smarius sc->pcn_type == Am79C973 || sc->pcn_type == Am79C975 || 294165997Smarius sc->pcn_type == Am79C978) && sc->pcn_extphyaddr != -1 && 295164712Smarius phy != sc->pcn_extphyaddr) 29666131Swpaul return(0); 29766131Swpaul 29866131Swpaul pcn_bcr_write(sc, PCN_BCR_MIIADDR, reg | (phy << 5)); 29966131Swpaul val = pcn_bcr_read(sc, PCN_BCR_MIIDATA) & 0xFFFF; 30066131Swpaul if (val == 0xFFFF) 30166131Swpaul return(0); 30266208Swpaul 303165997Smarius if (((sc->pcn_type == Am79C971 && phy != PCN_PHYAD_10BT) || 304165997Smarius sc->pcn_type == Am79C973 || sc->pcn_type == Am79C975 || 305165997Smarius sc->pcn_type == Am79C978) && sc->pcn_extphyaddr == -1) 306164712Smarius sc->pcn_extphyaddr = phy; 30766208Swpaul 30866131Swpaul return(val); 30966131Swpaul} 31066131Swpaul 311102335Salfredstatic int 312102335Salfredpcn_miibus_writereg(dev, phy, reg, data) 31366131Swpaul device_t dev; 31466131Swpaul int phy, reg, data; 31566131Swpaul{ 31666131Swpaul struct pcn_softc *sc; 31766131Swpaul 31866131Swpaul sc = device_get_softc(dev); 31966131Swpaul 32066131Swpaul pcn_bcr_write(sc, PCN_BCR_MIIADDR, reg | (phy << 5)); 32166131Swpaul pcn_bcr_write(sc, PCN_BCR_MIIDATA, data); 32266131Swpaul 32366131Swpaul return(0); 32466131Swpaul} 32566131Swpaul 326102335Salfredstatic void 327102335Salfredpcn_miibus_statchg(dev) 32866131Swpaul device_t dev; 32966131Swpaul{ 33066131Swpaul struct pcn_softc *sc; 33166131Swpaul struct mii_data *mii; 33266131Swpaul 33366131Swpaul sc = device_get_softc(dev); 33466131Swpaul mii = device_get_softc(sc->pcn_miibus); 33566131Swpaul 33666131Swpaul if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) { 33766131Swpaul PCN_BCR_SETBIT(sc, PCN_BCR_DUPLEX, PCN_DUPLEX_FDEN); 33866131Swpaul } else { 33966131Swpaul PCN_BCR_CLRBIT(sc, PCN_BCR_DUPLEX, PCN_DUPLEX_FDEN); 34066131Swpaul } 34166131Swpaul 34266131Swpaul return; 34366131Swpaul} 34466131Swpaul 345102335Salfredstatic void 346102335Salfredpcn_setmulti(sc) 34766131Swpaul struct pcn_softc *sc; 34866131Swpaul{ 34966131Swpaul struct ifnet *ifp; 35066131Swpaul struct ifmultiaddr *ifma; 35166131Swpaul u_int32_t h, i; 35266131Swpaul u_int16_t hashes[4] = { 0, 0, 0, 0 }; 35366131Swpaul 354147256Sbrooks ifp = sc->pcn_ifp; 35566131Swpaul 35666131Swpaul PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL1, PCN_EXTCTL1_SPND); 35766131Swpaul 35866131Swpaul if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 35966131Swpaul for (i = 0; i < 4; i++) 36066131Swpaul pcn_csr_write(sc, PCN_CSR_MAR0 + i, 0xFFFF); 36166131Swpaul PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1, PCN_EXTCTL1_SPND); 36266131Swpaul return; 36366131Swpaul } 36466131Swpaul 36566131Swpaul /* first, zot all the existing hash bits */ 36666131Swpaul for (i = 0; i < 4; i++) 36766131Swpaul pcn_csr_write(sc, PCN_CSR_MAR0 + i, 0); 36866131Swpaul 36966131Swpaul /* now program new ones */ 370195049Srwatson if_maddr_rlock(ifp); 37172084Sphk TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 37266131Swpaul if (ifma->ifma_addr->sa_family != AF_LINK) 37366131Swpaul continue; 374130270Snaddy h = ether_crc32_le(LLADDR((struct sockaddr_dl *) 375130270Snaddy ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 37666131Swpaul hashes[h >> 4] |= 1 << (h & 0xF); 37766131Swpaul } 378195049Srwatson if_maddr_runlock(ifp); 37966131Swpaul 38066131Swpaul for (i = 0; i < 4; i++) 38166131Swpaul pcn_csr_write(sc, PCN_CSR_MAR0 + i, hashes[i]); 38266131Swpaul 38366131Swpaul PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1, PCN_EXTCTL1_SPND); 38466131Swpaul 38566131Swpaul return; 38666131Swpaul} 38766131Swpaul 388102335Salfredstatic void 389102335Salfredpcn_reset(sc) 39066131Swpaul struct pcn_softc *sc; 39166131Swpaul{ 39266131Swpaul /* 39366131Swpaul * Issue a reset by reading from the RESET register. 39466131Swpaul * Note that we don't know if the chip is operating in 39566131Swpaul * 16-bit or 32-bit mode at this point, so we attempt 39666131Swpaul * to reset the chip both ways. If one fails, the other 39766131Swpaul * will succeed. 39866131Swpaul */ 39966131Swpaul CSR_READ_2(sc, PCN_IO16_RESET); 40066131Swpaul CSR_READ_4(sc, PCN_IO32_RESET); 40166131Swpaul 40266131Swpaul /* Wait a little while for the chip to get its brains in order. */ 40366131Swpaul DELAY(1000); 40466131Swpaul 40566131Swpaul /* Select 32-bit (DWIO) mode */ 40666131Swpaul CSR_WRITE_4(sc, PCN_IO32_RDP, 0); 40766131Swpaul 40866131Swpaul /* Select software style 3. */ 40966131Swpaul pcn_bcr_write(sc, PCN_BCR_SSTYLE, PCN_SWSTYLE_PCNETPCI_BURST); 41066131Swpaul 41166131Swpaul return; 41266131Swpaul} 41366131Swpaul 414164072Smariusstatic const char * 415164072Smariuspcn_chipid_name(u_int32_t id) 416138354Smdodd{ 417164072Smarius const struct pcn_chipid *p; 418138354Smdodd 419164072Smarius p = pcn_chipid; 420138354Smdodd while (p->name) { 421138354Smdodd if (id == p->id) 422138354Smdodd return (p->name); 423138354Smdodd p++; 424138354Smdodd } 425138354Smdodd return ("Unknown"); 426138354Smdodd} 427138354Smdodd 428138354Smdoddstatic u_int32_t 429164072Smariuspcn_chip_id(device_t dev) 430138354Smdodd{ 431138354Smdodd struct pcn_softc *sc; 432138354Smdodd u_int32_t chip_id; 433138354Smdodd 434138354Smdodd sc = device_get_softc(dev); 435138354Smdodd /* 436138354Smdodd * Note: we can *NOT* put the chip into 437158614Smarius * 32-bit mode yet. The le(4) driver will only 438138354Smdodd * work in 16-bit mode, and once the chip 439138354Smdodd * goes into 32-bit mode, the only way to 440138354Smdodd * get it out again is with a hardware reset. 441138354Smdodd * So if pcn_probe() is called before the 442158614Smarius * le(4) driver's probe routine, the chip will 443158614Smarius * be locked into 32-bit operation and the 444158614Smarius * le(4) driver will be unable to attach to it. 445138354Smdodd * Note II: if the chip happens to already 446138354Smdodd * be in 32-bit mode, we still need to check 447138354Smdodd * the chip ID, but first we have to detect 448138354Smdodd * 32-bit mode using only 16-bit operations. 449138354Smdodd * The safest way to do this is to read the 450138354Smdodd * PCI subsystem ID from BCR23/24 and compare 451138354Smdodd * that with the value read from PCI config 452138354Smdodd * space. 453138354Smdodd */ 454138354Smdodd chip_id = pcn_bcr_read16(sc, PCN_BCR_PCISUBSYSID); 455138354Smdodd chip_id <<= 16; 456138354Smdodd chip_id |= pcn_bcr_read16(sc, PCN_BCR_PCISUBVENID); 457138354Smdodd /* 458138354Smdodd * Note III: the test for 0x10001000 is a hack to 459138354Smdodd * pacify VMware, who's pseudo-PCnet interface is 460138354Smdodd * broken. Reading the subsystem register from PCI 461138354Smdodd * config space yields 0x00000000 while reading the 462138354Smdodd * same value from I/O space yields 0x10001000. It's 463138354Smdodd * not supposed to be that way. 464138354Smdodd */ 465138354Smdodd if (chip_id == pci_read_config(dev, 466138354Smdodd PCIR_SUBVEND_0, 4) || chip_id == 0x10001000) { 467138354Smdodd /* We're in 16-bit mode. */ 468138354Smdodd chip_id = pcn_csr_read16(sc, PCN_CSR_CHIPID1); 469138354Smdodd chip_id <<= 16; 470138354Smdodd chip_id |= pcn_csr_read16(sc, PCN_CSR_CHIPID0); 471138354Smdodd } else { 472138354Smdodd /* We're in 32-bit mode. */ 473138354Smdodd chip_id = pcn_csr_read(sc, PCN_CSR_CHIPID1); 474138354Smdodd chip_id <<= 16; 475138354Smdodd chip_id |= pcn_csr_read(sc, PCN_CSR_CHIPID0); 476138354Smdodd } 477138354Smdodd 478138354Smdodd return (chip_id); 479138354Smdodd} 480138354Smdodd 481164072Smariusstatic const struct pcn_type * 482164072Smariuspcn_match(u_int16_t vid, u_int16_t did) 483138355Smdodd{ 484164072Smarius const struct pcn_type *t; 485164072Smarius 486138355Smdodd t = pcn_devs; 487164072Smarius while (t->pcn_name != NULL) { 488138355Smdodd if ((vid == t->pcn_vid) && (did == t->pcn_did)) 489138355Smdodd return (t); 490138355Smdodd t++; 491138355Smdodd } 492138355Smdodd return (NULL); 493138355Smdodd} 494138355Smdodd 49566131Swpaul/* 49666131Swpaul * Probe for an AMD chip. Check the PCI vendor and device 49766131Swpaul * IDs against our list and return a device name if we find a match. 49866131Swpaul */ 499102335Salfredstatic int 500102335Salfredpcn_probe(dev) 50166131Swpaul device_t dev; 50266131Swpaul{ 503164072Smarius const struct pcn_type *t; 50466131Swpaul struct pcn_softc *sc; 50566131Swpaul int rid; 50666131Swpaul u_int32_t chip_id; 50766131Swpaul 508138355Smdodd t = pcn_match(pci_get_vendor(dev), pci_get_device(dev)); 509138355Smdodd if (t == NULL) 510138355Smdodd return (ENXIO); 51166131Swpaul sc = device_get_softc(dev); 51266131Swpaul 513138355Smdodd /* 514138355Smdodd * Temporarily map the I/O space so we can read the chip ID register. 515138355Smdodd */ 516138355Smdodd rid = PCN_RID; 517138355Smdodd sc->pcn_res = bus_alloc_resource_any(dev, PCN_RES, &rid, RF_ACTIVE); 518138355Smdodd if (sc->pcn_res == NULL) { 519138355Smdodd device_printf(dev, "couldn't map ports/memory\n"); 520138355Smdodd return(ENXIO); 52166131Swpaul } 522138355Smdodd sc->pcn_btag = rman_get_bustag(sc->pcn_res); 523138355Smdodd sc->pcn_bhandle = rman_get_bushandle(sc->pcn_res); 52466131Swpaul 525138355Smdodd chip_id = pcn_chip_id(dev); 526138355Smdodd 527138355Smdodd bus_release_resource(dev, PCN_RES, PCN_RID, sc->pcn_res); 528138355Smdodd 529138355Smdodd switch((chip_id >> 12) & PART_MASK) { 530138355Smdodd case Am79C971: 531138355Smdodd case Am79C972: 532138355Smdodd case Am79C973: 533138355Smdodd case Am79C975: 534138355Smdodd case Am79C976: 535138355Smdodd case Am79C978: 536138355Smdodd break; 537138355Smdodd default: 538138355Smdodd return(ENXIO); 539138355Smdodd } 540138355Smdodd device_set_desc(dev, t->pcn_name); 541142398Simp return(BUS_PROBE_DEFAULT); 54266131Swpaul} 54366131Swpaul 54466131Swpaul/* 54566131Swpaul * Attach the interface. Allocate softc structures, do ifmedia 54666131Swpaul * setup and ethernet/BPF attach. 54766131Swpaul */ 548102335Salfredstatic int 549102335Salfredpcn_attach(dev) 55066131Swpaul device_t dev; 55166131Swpaul{ 55266131Swpaul u_int32_t eaddr[2]; 55366131Swpaul struct pcn_softc *sc; 554164712Smarius struct mii_data *mii; 555164712Smarius struct mii_softc *miisc; 55666131Swpaul struct ifnet *ifp; 557164072Smarius int error = 0, rid; 55866131Swpaul 55966131Swpaul sc = device_get_softc(dev); 56066131Swpaul 56169583Swpaul /* Initialize our mutex. */ 56293818Sjhb mtx_init(&sc->pcn_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 563148738Sjhb MTX_DEF); 56466131Swpaul /* 56566131Swpaul * Map control/status registers. 56666131Swpaul */ 56772813Swpaul pci_enable_busmaster(dev); 56866131Swpaul 569138354Smdodd /* Retrieve the chip ID */ 570138354Smdodd sc->pcn_type = (pcn_chip_id(dev) >> 12) & PART_MASK; 571138354Smdodd device_printf(dev, "Chip ID %04x (%s)\n", 572138354Smdodd sc->pcn_type, pcn_chipid_name(sc->pcn_type)); 573138354Smdodd 57466131Swpaul rid = PCN_RID; 575127135Snjl sc->pcn_res = bus_alloc_resource_any(dev, PCN_RES, &rid, RF_ACTIVE); 57666131Swpaul 57766131Swpaul if (sc->pcn_res == NULL) { 578164072Smarius device_printf(dev, "couldn't map ports/memory\n"); 57966131Swpaul error = ENXIO; 58066131Swpaul goto fail; 58166131Swpaul } 58266131Swpaul 58366131Swpaul sc->pcn_btag = rman_get_bustag(sc->pcn_res); 58466131Swpaul sc->pcn_bhandle = rman_get_bushandle(sc->pcn_res); 58566131Swpaul 58666131Swpaul /* Allocate interrupt */ 58766131Swpaul rid = 0; 588127135Snjl sc->pcn_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 58966131Swpaul RF_SHAREABLE | RF_ACTIVE); 59066131Swpaul 59166131Swpaul if (sc->pcn_irq == NULL) { 592164072Smarius device_printf(dev, "couldn't map interrupt\n"); 59366131Swpaul error = ENXIO; 59466131Swpaul goto fail; 59566131Swpaul } 59666131Swpaul 59766131Swpaul /* Reset the adapter. */ 59866131Swpaul pcn_reset(sc); 59966131Swpaul 60066131Swpaul /* 60166131Swpaul * Get station address from the EEPROM. 60266131Swpaul */ 60366131Swpaul eaddr[0] = CSR_READ_4(sc, PCN_IO32_APROM00); 60466131Swpaul eaddr[1] = CSR_READ_4(sc, PCN_IO32_APROM01); 60566131Swpaul 606149204Sjhb callout_init_mtx(&sc->pcn_stat_callout, &sc->pcn_mtx, 0); 60766131Swpaul 60866131Swpaul sc->pcn_ldata = contigmalloc(sizeof(struct pcn_list_data), M_DEVBUF, 60966131Swpaul M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0); 61066131Swpaul 61166131Swpaul if (sc->pcn_ldata == NULL) { 612164072Smarius device_printf(dev, "no memory for list buffers!\n"); 61366131Swpaul error = ENXIO; 61466131Swpaul goto fail; 61566131Swpaul } 61666131Swpaul bzero(sc->pcn_ldata, sizeof(struct pcn_list_data)); 61766131Swpaul 618147256Sbrooks ifp = sc->pcn_ifp = if_alloc(IFT_ETHER); 619147256Sbrooks if (ifp == NULL) { 620164072Smarius device_printf(dev, "can not if_alloc()\n"); 621147256Sbrooks error = ENOSPC; 622147256Sbrooks goto fail; 623147256Sbrooks } 62466131Swpaul ifp->if_softc = sc; 625121816Sbrooks if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 626148738Sjhb ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 62766131Swpaul ifp->if_ioctl = pcn_ioctl; 62866131Swpaul ifp->if_start = pcn_start; 62966131Swpaul ifp->if_init = pcn_init; 63066131Swpaul ifp->if_snd.ifq_maxlen = PCN_TX_LIST_CNT - 1; 63166131Swpaul 63266131Swpaul /* 633221407Smarius * Do MII setup. 634213893Smarius * See the comment in pcn_miibus_readreg() for why we can't 635213893Smarius * universally pass MIIF_NOISOLATE here. 63666131Swpaul */ 637164712Smarius sc->pcn_extphyaddr = -1; 638213893Smarius error = mii_attach(dev, &sc->pcn_miibus, ifp, pcn_ifmedia_upd, 639221407Smarius pcn_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0); 640213893Smarius if (error != 0) { 641213893Smarius device_printf(dev, "attaching PHYs failed\n"); 64266131Swpaul goto fail; 64366131Swpaul } 644164712Smarius /* 645164712Smarius * Record the media instances of internal PHYs, which map the 646164712Smarius * built-in interfaces to the MII, so we can set the active 647164712Smarius * PHY/port based on the currently selected media. 648164712Smarius */ 649164712Smarius sc->pcn_inst_10bt = -1; 650164712Smarius mii = device_get_softc(sc->pcn_miibus); 651164712Smarius LIST_FOREACH(miisc, &mii->mii_phys, mii_list) { 652164712Smarius switch (miisc->mii_phy) { 653164712Smarius case PCN_PHYAD_10BT: 654164712Smarius sc->pcn_inst_10bt = miisc->mii_inst; 655164712Smarius break; 656164712Smarius /* 657164712Smarius * XXX deal with the Am79C97{3,5} internal 100baseT 658164712Smarius * and the Am79C978 internal HomePNA PHYs. 659164712Smarius */ 660164712Smarius } 661164712Smarius } 66266131Swpaul 66366131Swpaul /* 66466131Swpaul * Call MI attach routine. 66566131Swpaul */ 666106936Ssam ether_ifattach(ifp, (u_int8_t *) eaddr); 66766131Swpaul 668113609Snjl /* Hook interrupt last to avoid having to lock softc */ 669148738Sjhb error = bus_setup_intr(dev, sc->pcn_irq, INTR_TYPE_NET | INTR_MPSAFE, 670166901Spiso NULL, pcn_intr, sc, &sc->pcn_intrhand); 671110472Smdodd 672112872Snjl if (error) { 673164072Smarius device_printf(dev, "couldn't set up irq\n"); 674113609Snjl ether_ifdetach(ifp); 675112872Snjl goto fail; 676112872Snjl } 677110472Smdodd 678112872Snjlfail: 679112872Snjl if (error) 680112872Snjl pcn_detach(dev); 68167087Swpaul 68266131Swpaul return(error); 68366131Swpaul} 68466131Swpaul 685113609Snjl/* 686113609Snjl * Shutdown hardware and free up resources. This can be called any 687113609Snjl * time after the mutex has been initialized. It is called in both 688113609Snjl * the error case in attach and the normal detach case so it needs 689113609Snjl * to be careful about only freeing resources that have actually been 690113609Snjl * allocated. 691113609Snjl */ 692102335Salfredstatic int 693102335Salfredpcn_detach(dev) 69466131Swpaul device_t dev; 69566131Swpaul{ 69666131Swpaul struct pcn_softc *sc; 69766131Swpaul struct ifnet *ifp; 69866131Swpaul 69966131Swpaul sc = device_get_softc(dev); 700147256Sbrooks ifp = sc->pcn_ifp; 70166131Swpaul 702112880Sjhb KASSERT(mtx_initialized(&sc->pcn_mtx), ("pcn mutex not initialized")); 70367087Swpaul 704113609Snjl /* These should only be active if attach succeeded */ 705113812Simp if (device_is_attached(dev)) { 706148738Sjhb PCN_LOCK(sc); 707113609Snjl pcn_reset(sc); 708113609Snjl pcn_stop(sc); 709148738Sjhb PCN_UNLOCK(sc); 710148738Sjhb callout_drain(&sc->pcn_stat_callout); 711112872Snjl ether_ifdetach(ifp); 712150213Sru } 713113609Snjl if (sc->pcn_miibus) 714112872Snjl device_delete_child(dev, sc->pcn_miibus); 715113609Snjl bus_generic_detach(dev); 71666131Swpaul 717112872Snjl if (sc->pcn_intrhand) 718112872Snjl bus_teardown_intr(dev, sc->pcn_irq, sc->pcn_intrhand); 719112872Snjl if (sc->pcn_irq) 720112872Snjl bus_release_resource(dev, SYS_RES_IRQ, 0, sc->pcn_irq); 721112872Snjl if (sc->pcn_res) 722112872Snjl bus_release_resource(dev, PCN_RES, PCN_RID, sc->pcn_res); 72366131Swpaul 724151297Sru if (ifp) 725151297Sru if_free(ifp); 726151297Sru 727112872Snjl if (sc->pcn_ldata) { 728112872Snjl contigfree(sc->pcn_ldata, sizeof(struct pcn_list_data), 729112872Snjl M_DEVBUF); 730112872Snjl } 73166131Swpaul 73267087Swpaul mtx_destroy(&sc->pcn_mtx); 73366131Swpaul 73466131Swpaul return(0); 73566131Swpaul} 73666131Swpaul 73766131Swpaul/* 73866131Swpaul * Initialize the transmit descriptors. 73966131Swpaul */ 740102335Salfredstatic int 741102335Salfredpcn_list_tx_init(sc) 74266131Swpaul struct pcn_softc *sc; 74366131Swpaul{ 74466131Swpaul struct pcn_list_data *ld; 74566131Swpaul struct pcn_ring_data *cd; 74666131Swpaul int i; 74766131Swpaul 74866131Swpaul cd = &sc->pcn_cdata; 74966131Swpaul ld = sc->pcn_ldata; 75066131Swpaul 75166131Swpaul for (i = 0; i < PCN_TX_LIST_CNT; i++) { 75266131Swpaul cd->pcn_tx_chain[i] = NULL; 75366131Swpaul ld->pcn_tx_list[i].pcn_tbaddr = 0; 75466131Swpaul ld->pcn_tx_list[i].pcn_txctl = 0; 75566131Swpaul ld->pcn_tx_list[i].pcn_txstat = 0; 75666131Swpaul } 75766131Swpaul 75866131Swpaul cd->pcn_tx_prod = cd->pcn_tx_cons = cd->pcn_tx_cnt = 0; 75966131Swpaul 76066131Swpaul return(0); 76166131Swpaul} 76266131Swpaul 76366131Swpaul 76466131Swpaul/* 76566131Swpaul * Initialize the RX descriptors and allocate mbufs for them. 76666131Swpaul */ 767102335Salfredstatic int 768102335Salfredpcn_list_rx_init(sc) 76966131Swpaul struct pcn_softc *sc; 77066131Swpaul{ 77166131Swpaul struct pcn_ring_data *cd; 77266131Swpaul int i; 77366131Swpaul 77466131Swpaul cd = &sc->pcn_cdata; 77566131Swpaul 77666131Swpaul for (i = 0; i < PCN_RX_LIST_CNT; i++) { 77766131Swpaul if (pcn_newbuf(sc, i, NULL) == ENOBUFS) 77866131Swpaul return(ENOBUFS); 77966131Swpaul } 78066131Swpaul 78166131Swpaul cd->pcn_rx_prod = 0; 78266131Swpaul 78366131Swpaul return(0); 78466131Swpaul} 78566131Swpaul 78666131Swpaul/* 78766131Swpaul * Initialize an RX descriptor and attach an MBUF cluster. 78866131Swpaul */ 789102335Salfredstatic int 790102335Salfredpcn_newbuf(sc, idx, m) 79166131Swpaul struct pcn_softc *sc; 79266131Swpaul int idx; 79366131Swpaul struct mbuf *m; 79466131Swpaul{ 79566131Swpaul struct mbuf *m_new = NULL; 79666131Swpaul struct pcn_rx_desc *c; 79766131Swpaul 79866131Swpaul c = &sc->pcn_ldata->pcn_rx_list[idx]; 79966131Swpaul 80066131Swpaul if (m == NULL) { 801243857Sglebius MGETHDR(m_new, M_NOWAIT, MT_DATA); 80287846Sluigi if (m_new == NULL) 80366131Swpaul return(ENOBUFS); 80466131Swpaul 805243857Sglebius MCLGET(m_new, M_NOWAIT); 80666131Swpaul if (!(m_new->m_flags & M_EXT)) { 80766131Swpaul m_freem(m_new); 80866131Swpaul return(ENOBUFS); 80966131Swpaul } 81066131Swpaul m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 81166131Swpaul } else { 81266131Swpaul m_new = m; 81366131Swpaul m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 81466131Swpaul m_new->m_data = m_new->m_ext.ext_buf; 81566131Swpaul } 81666131Swpaul 81766131Swpaul m_adj(m_new, ETHER_ALIGN); 81866131Swpaul 81966131Swpaul sc->pcn_cdata.pcn_rx_chain[idx] = m_new; 82066131Swpaul c->pcn_rbaddr = vtophys(mtod(m_new, caddr_t)); 82166131Swpaul c->pcn_bufsz = (~(PCN_RXLEN) + 1) & PCN_RXLEN_BUFSZ; 82266131Swpaul c->pcn_bufsz |= PCN_RXLEN_MBO; 82366131Swpaul c->pcn_rxstat = PCN_RXSTAT_STP|PCN_RXSTAT_ENP|PCN_RXSTAT_OWN; 82466131Swpaul 82566131Swpaul return(0); 82666131Swpaul} 82766131Swpaul 82866131Swpaul/* 82966131Swpaul * A frame has been uploaded: pass the resulting mbuf chain up to 83066131Swpaul * the higher level protocols. 83166131Swpaul */ 832102335Salfredstatic void 833102335Salfredpcn_rxeof(sc) 83466131Swpaul struct pcn_softc *sc; 83566131Swpaul{ 83666131Swpaul struct mbuf *m; 83766131Swpaul struct ifnet *ifp; 83866131Swpaul struct pcn_rx_desc *cur_rx; 83966131Swpaul int i; 84066131Swpaul 841122689Ssam PCN_LOCK_ASSERT(sc); 842122689Ssam 843147256Sbrooks ifp = sc->pcn_ifp; 84466131Swpaul i = sc->pcn_cdata.pcn_rx_prod; 84566131Swpaul 84666131Swpaul while(PCN_OWN_RXDESC(&sc->pcn_ldata->pcn_rx_list[i])) { 84766131Swpaul cur_rx = &sc->pcn_ldata->pcn_rx_list[i]; 84866131Swpaul m = sc->pcn_cdata.pcn_rx_chain[i]; 84966131Swpaul sc->pcn_cdata.pcn_rx_chain[i] = NULL; 85066131Swpaul 85166131Swpaul /* 85266131Swpaul * If an error occurs, update stats, clear the 85366131Swpaul * status word and leave the mbuf cluster in place: 85466131Swpaul * it should simply get re-used next time this descriptor 85566131Swpaul * comes up in the ring. 85666131Swpaul */ 85766131Swpaul if (cur_rx->pcn_rxstat & PCN_RXSTAT_ERR) { 85866131Swpaul ifp->if_ierrors++; 85966131Swpaul pcn_newbuf(sc, i, m); 86066131Swpaul PCN_INC(i, PCN_RX_LIST_CNT); 86166131Swpaul continue; 86266131Swpaul } 86366131Swpaul 86466592Swpaul if (pcn_newbuf(sc, i, NULL)) { 86566592Swpaul /* Ran out of mbufs; recycle this one. */ 86666592Swpaul pcn_newbuf(sc, i, m); 86766592Swpaul ifp->if_ierrors++; 86866592Swpaul PCN_INC(i, PCN_RX_LIST_CNT); 86966592Swpaul continue; 87066592Swpaul } 87166592Swpaul 87266131Swpaul PCN_INC(i, PCN_RX_LIST_CNT); 87366131Swpaul 87466131Swpaul /* No errors; receive the packet. */ 87566131Swpaul ifp->if_ipackets++; 87666131Swpaul m->m_len = m->m_pkthdr.len = 87766131Swpaul cur_rx->pcn_rxlen - ETHER_CRC_LEN; 87866131Swpaul m->m_pkthdr.rcvif = ifp; 87966131Swpaul 880122689Ssam PCN_UNLOCK(sc); 881106936Ssam (*ifp->if_input)(ifp, m); 882122689Ssam PCN_LOCK(sc); 88366131Swpaul } 88466131Swpaul 88566131Swpaul sc->pcn_cdata.pcn_rx_prod = i; 88666131Swpaul 88766131Swpaul return; 88866131Swpaul} 88966131Swpaul 89066131Swpaul/* 89166131Swpaul * A frame was downloaded to the chip. It's safe for us to clean up 89266131Swpaul * the list buffers. 89366131Swpaul */ 89466131Swpaul 895102335Salfredstatic void 896102335Salfredpcn_txeof(sc) 89766131Swpaul struct pcn_softc *sc; 89866131Swpaul{ 89966131Swpaul struct pcn_tx_desc *cur_tx = NULL; 90066131Swpaul struct ifnet *ifp; 90166131Swpaul u_int32_t idx; 90266131Swpaul 903147256Sbrooks ifp = sc->pcn_ifp; 90466131Swpaul 90566131Swpaul /* 90666131Swpaul * Go through our tx list and free mbufs for those 90766131Swpaul * frames that have been transmitted. 90866131Swpaul */ 90966131Swpaul idx = sc->pcn_cdata.pcn_tx_cons; 91066131Swpaul while (idx != sc->pcn_cdata.pcn_tx_prod) { 91166131Swpaul cur_tx = &sc->pcn_ldata->pcn_tx_list[idx]; 91266131Swpaul 91366131Swpaul if (!PCN_OWN_TXDESC(cur_tx)) 91466131Swpaul break; 91566131Swpaul 91666131Swpaul if (!(cur_tx->pcn_txctl & PCN_TXCTL_ENP)) { 91766131Swpaul sc->pcn_cdata.pcn_tx_cnt--; 91866131Swpaul PCN_INC(idx, PCN_TX_LIST_CNT); 91966131Swpaul continue; 92066131Swpaul } 92166131Swpaul 92266131Swpaul if (cur_tx->pcn_txctl & PCN_TXCTL_ERR) { 92366131Swpaul ifp->if_oerrors++; 92466131Swpaul if (cur_tx->pcn_txstat & PCN_TXSTAT_EXDEF) 92566131Swpaul ifp->if_collisions++; 92666131Swpaul if (cur_tx->pcn_txstat & PCN_TXSTAT_RTRY) 92766131Swpaul ifp->if_collisions++; 92866131Swpaul } 92966131Swpaul 93066131Swpaul ifp->if_collisions += 93166131Swpaul cur_tx->pcn_txstat & PCN_TXSTAT_TRC; 93266131Swpaul 93366131Swpaul ifp->if_opackets++; 93466131Swpaul if (sc->pcn_cdata.pcn_tx_chain[idx] != NULL) { 93566131Swpaul m_freem(sc->pcn_cdata.pcn_tx_chain[idx]); 93666131Swpaul sc->pcn_cdata.pcn_tx_chain[idx] = NULL; 93766131Swpaul } 93866131Swpaul 93966131Swpaul sc->pcn_cdata.pcn_tx_cnt--; 94066131Swpaul PCN_INC(idx, PCN_TX_LIST_CNT); 94166131Swpaul } 94266131Swpaul 94399165Sluigi if (idx != sc->pcn_cdata.pcn_tx_cons) { 94499165Sluigi /* Some buffers have been freed. */ 94599165Sluigi sc->pcn_cdata.pcn_tx_cons = idx; 946148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 94799165Sluigi } 948199560Sjhb sc->pcn_timer = (sc->pcn_cdata.pcn_tx_cnt == 0) ? 0 : 5; 94966131Swpaul 95066131Swpaul return; 95166131Swpaul} 95266131Swpaul 953102335Salfredstatic void 954102335Salfredpcn_tick(xsc) 95566131Swpaul void *xsc; 95666131Swpaul{ 95766131Swpaul struct pcn_softc *sc; 95866131Swpaul struct mii_data *mii; 95966131Swpaul struct ifnet *ifp; 96066131Swpaul 96166131Swpaul sc = xsc; 962147256Sbrooks ifp = sc->pcn_ifp; 963149204Sjhb PCN_LOCK_ASSERT(sc); 96466131Swpaul 96566131Swpaul mii = device_get_softc(sc->pcn_miibus); 96666131Swpaul mii_tick(mii); 96766131Swpaul 96884147Sjlemon /* link just died */ 969232204Skevlo if (sc->pcn_link && !(mii->mii_media_status & IFM_ACTIVE)) 97066131Swpaul sc->pcn_link = 0; 97166131Swpaul 97284147Sjlemon /* link just came up, restart */ 97384147Sjlemon if (!sc->pcn_link && mii->mii_media_status & IFM_ACTIVE && 97484147Sjlemon IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 97584147Sjlemon sc->pcn_link++; 97684147Sjlemon if (ifp->if_snd.ifq_head != NULL) 977148738Sjhb pcn_start_locked(ifp); 97866131Swpaul } 97966131Swpaul 980199560Sjhb if (sc->pcn_timer > 0 && --sc->pcn_timer == 0) 981199560Sjhb pcn_watchdog(sc); 982148738Sjhb callout_reset(&sc->pcn_stat_callout, hz, pcn_tick, sc); 98366131Swpaul 98466131Swpaul return; 98566131Swpaul} 98666131Swpaul 987102335Salfredstatic void 988102335Salfredpcn_intr(arg) 98966131Swpaul void *arg; 99066131Swpaul{ 99166131Swpaul struct pcn_softc *sc; 99266131Swpaul struct ifnet *ifp; 99366131Swpaul u_int32_t status; 99466131Swpaul 99566131Swpaul sc = arg; 996147256Sbrooks ifp = sc->pcn_ifp; 99766131Swpaul 998148738Sjhb PCN_LOCK(sc); 999148738Sjhb 1000134842Sbrueffer /* Suppress unwanted interrupts */ 100166131Swpaul if (!(ifp->if_flags & IFF_UP)) { 100266131Swpaul pcn_stop(sc); 1003148738Sjhb PCN_UNLOCK(sc); 100466131Swpaul return; 100566131Swpaul } 100666131Swpaul 100766131Swpaul CSR_WRITE_4(sc, PCN_IO32_RAP, PCN_CSR_CSR); 100866131Swpaul 100966131Swpaul while ((status = CSR_READ_4(sc, PCN_IO32_RDP)) & PCN_CSR_INTR) { 101066131Swpaul CSR_WRITE_4(sc, PCN_IO32_RDP, status); 101166131Swpaul 101266131Swpaul if (status & PCN_CSR_RINT) 101366131Swpaul pcn_rxeof(sc); 101466131Swpaul 101566131Swpaul if (status & PCN_CSR_TINT) 101666131Swpaul pcn_txeof(sc); 101766131Swpaul 101866131Swpaul if (status & PCN_CSR_ERR) { 1019148738Sjhb pcn_init_locked(sc); 102066131Swpaul break; 102166131Swpaul } 102266131Swpaul } 102366131Swpaul 102466131Swpaul if (ifp->if_snd.ifq_head != NULL) 1025148738Sjhb pcn_start_locked(ifp); 102666131Swpaul 1027110567Smdodd PCN_UNLOCK(sc); 102866131Swpaul return; 102966131Swpaul} 103066131Swpaul 103166131Swpaul/* 103266131Swpaul * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data 103366131Swpaul * pointers to the fragment pointers. 103466131Swpaul */ 1035102335Salfredstatic int 1036102335Salfredpcn_encap(sc, m_head, txidx) 103766131Swpaul struct pcn_softc *sc; 103866131Swpaul struct mbuf *m_head; 103966131Swpaul u_int32_t *txidx; 104066131Swpaul{ 104166131Swpaul struct pcn_tx_desc *f = NULL; 104266131Swpaul struct mbuf *m; 104366131Swpaul int frag, cur, cnt = 0; 104466131Swpaul 104566131Swpaul /* 104666131Swpaul * Start packing the mbufs in this chain into 104766131Swpaul * the fragment pointers. Stop when we run out 104866131Swpaul * of fragments or hit the end of the mbuf chain. 104966131Swpaul */ 105066131Swpaul m = m_head; 105166131Swpaul cur = frag = *txidx; 105266131Swpaul 105366131Swpaul for (m = m_head; m != NULL; m = m->m_next) { 1054144981Smdodd if (m->m_len == 0) 1055144981Smdodd continue; 1056144981Smdodd 1057144981Smdodd if ((PCN_TX_LIST_CNT - (sc->pcn_cdata.pcn_tx_cnt + cnt)) < 2) 1058144981Smdodd return(ENOBUFS); 1059144981Smdodd f = &sc->pcn_ldata->pcn_tx_list[frag]; 1060144981Smdodd f->pcn_txctl = (~(m->m_len) + 1) & PCN_TXCTL_BUFSZ; 1061144981Smdodd f->pcn_txctl |= PCN_TXCTL_MBO; 1062144981Smdodd f->pcn_tbaddr = vtophys(mtod(m, vm_offset_t)); 1063144981Smdodd if (cnt == 0) 1064144981Smdodd f->pcn_txctl |= PCN_TXCTL_STP; 1065144981Smdodd else 1066144981Smdodd f->pcn_txctl |= PCN_TXCTL_OWN; 1067144981Smdodd cur = frag; 1068144981Smdodd PCN_INC(frag, PCN_TX_LIST_CNT); 1069144981Smdodd cnt++; 107066131Swpaul } 107166131Swpaul 107266131Swpaul if (m != NULL) 107366131Swpaul return(ENOBUFS); 107466131Swpaul 107566131Swpaul sc->pcn_cdata.pcn_tx_chain[cur] = m_head; 107666131Swpaul sc->pcn_ldata->pcn_tx_list[cur].pcn_txctl |= 107766131Swpaul PCN_TXCTL_ENP|PCN_TXCTL_ADD_FCS|PCN_TXCTL_MORE_LTINT; 107866131Swpaul sc->pcn_ldata->pcn_tx_list[*txidx].pcn_txctl |= PCN_TXCTL_OWN; 107966131Swpaul sc->pcn_cdata.pcn_tx_cnt += cnt; 108066131Swpaul *txidx = frag; 108166131Swpaul 108266131Swpaul return(0); 108366131Swpaul} 108466131Swpaul 108566131Swpaul/* 108666131Swpaul * Main transmit routine. To avoid having to do mbuf copies, we put pointers 108766131Swpaul * to the mbuf data regions directly in the transmit lists. We also save a 108866131Swpaul * copy of the pointers since the transmit list fragment pointers are 108966131Swpaul * physical addresses. 109066131Swpaul */ 1091102335Salfredstatic void 1092102335Salfredpcn_start(ifp) 109366131Swpaul struct ifnet *ifp; 109466131Swpaul{ 109566131Swpaul struct pcn_softc *sc; 1096148738Sjhb 1097148738Sjhb sc = ifp->if_softc; 1098148738Sjhb PCN_LOCK(sc); 1099148738Sjhb pcn_start_locked(ifp); 1100148738Sjhb PCN_UNLOCK(sc); 1101148738Sjhb} 1102148738Sjhb 1103148738Sjhbstatic void 1104148738Sjhbpcn_start_locked(ifp) 1105148738Sjhb struct ifnet *ifp; 1106148738Sjhb{ 1107148738Sjhb struct pcn_softc *sc; 110866131Swpaul struct mbuf *m_head = NULL; 110966131Swpaul u_int32_t idx; 111066131Swpaul 111166131Swpaul sc = ifp->if_softc; 111266131Swpaul 1113148738Sjhb PCN_LOCK_ASSERT(sc); 111467087Swpaul 1115148738Sjhb if (!sc->pcn_link) 111666131Swpaul return; 111766131Swpaul 111866131Swpaul idx = sc->pcn_cdata.pcn_tx_prod; 111966131Swpaul 1120148887Srwatson if (ifp->if_drv_flags & IFF_DRV_OACTIVE) 112166131Swpaul return; 112266131Swpaul 112366131Swpaul while(sc->pcn_cdata.pcn_tx_chain[idx] == NULL) { 112466131Swpaul IF_DEQUEUE(&ifp->if_snd, m_head); 112566131Swpaul if (m_head == NULL) 112666131Swpaul break; 112766131Swpaul 112866131Swpaul if (pcn_encap(sc, m_head, &idx)) { 112966131Swpaul IF_PREPEND(&ifp->if_snd, m_head); 1130148887Srwatson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 113166131Swpaul break; 113266131Swpaul } 113366131Swpaul 113466131Swpaul /* 113566131Swpaul * If there's a BPF listener, bounce a copy of this frame 113666131Swpaul * to him. 113766131Swpaul */ 1138106936Ssam BPF_MTAP(ifp, m_head); 113966131Swpaul 114066131Swpaul } 114166131Swpaul 114266131Swpaul /* Transmit */ 114366131Swpaul sc->pcn_cdata.pcn_tx_prod = idx; 114466131Swpaul pcn_csr_write(sc, PCN_CSR_CSR, PCN_CSR_TX|PCN_CSR_INTEN); 114566131Swpaul 114666131Swpaul /* 114766131Swpaul * Set a timeout in case the chip goes out to lunch. 114866131Swpaul */ 1149199560Sjhb sc->pcn_timer = 5; 115066131Swpaul 115166131Swpaul return; 115266131Swpaul} 115366131Swpaul 1154102335Salfredstatic void 1155102335Salfredpcn_setfilt(ifp) 115668270Swpaul struct ifnet *ifp; 115768270Swpaul{ 115868270Swpaul struct pcn_softc *sc; 115968270Swpaul 116068270Swpaul sc = ifp->if_softc; 116168270Swpaul 116268270Swpaul /* If we want promiscuous mode, set the allframes bit. */ 116368270Swpaul if (ifp->if_flags & IFF_PROMISC) { 116468270Swpaul PCN_CSR_SETBIT(sc, PCN_CSR_MODE, PCN_MODE_PROMISC); 116568270Swpaul } else { 116668270Swpaul PCN_CSR_CLRBIT(sc, PCN_CSR_MODE, PCN_MODE_PROMISC); 116768270Swpaul } 116868270Swpaul 116968270Swpaul /* Set the capture broadcast bit to capture broadcast frames. */ 117068270Swpaul if (ifp->if_flags & IFF_BROADCAST) { 117168270Swpaul PCN_CSR_CLRBIT(sc, PCN_CSR_MODE, PCN_MODE_RXNOBROAD); 117268270Swpaul } else { 117368270Swpaul PCN_CSR_SETBIT(sc, PCN_CSR_MODE, PCN_MODE_RXNOBROAD); 117468270Swpaul } 117568270Swpaul 117668270Swpaul return; 117768270Swpaul} 117868270Swpaul 1179102335Salfredstatic void 1180102335Salfredpcn_init(xsc) 118166131Swpaul void *xsc; 118266131Swpaul{ 118366131Swpaul struct pcn_softc *sc = xsc; 1184148738Sjhb 1185148738Sjhb PCN_LOCK(sc); 1186148738Sjhb pcn_init_locked(sc); 1187148738Sjhb PCN_UNLOCK(sc); 1188148738Sjhb} 1189148738Sjhb 1190148738Sjhbstatic void 1191148738Sjhbpcn_init_locked(sc) 1192148738Sjhb struct pcn_softc *sc; 1193148738Sjhb{ 1194147256Sbrooks struct ifnet *ifp = sc->pcn_ifp; 119566131Swpaul struct mii_data *mii = NULL; 1196164712Smarius struct ifmedia_entry *ife; 119766131Swpaul 1198148738Sjhb PCN_LOCK_ASSERT(sc); 119966131Swpaul 120066131Swpaul /* 120166131Swpaul * Cancel pending I/O and free all RX/TX buffers. 120266131Swpaul */ 120366131Swpaul pcn_stop(sc); 120466131Swpaul pcn_reset(sc); 120566131Swpaul 120666131Swpaul mii = device_get_softc(sc->pcn_miibus); 1207164712Smarius ife = mii->mii_media.ifm_cur; 120866131Swpaul 120966131Swpaul /* Set MAC address */ 121066131Swpaul pcn_csr_write(sc, PCN_CSR_PAR0, 1211152315Sru ((u_int16_t *)IF_LLADDR(sc->pcn_ifp))[0]); 121266131Swpaul pcn_csr_write(sc, PCN_CSR_PAR1, 1213152315Sru ((u_int16_t *)IF_LLADDR(sc->pcn_ifp))[1]); 121466131Swpaul pcn_csr_write(sc, PCN_CSR_PAR2, 1215152315Sru ((u_int16_t *)IF_LLADDR(sc->pcn_ifp))[2]); 121666131Swpaul 121766131Swpaul /* Init circular RX list. */ 121866131Swpaul if (pcn_list_rx_init(sc) == ENOBUFS) { 1219164072Smarius if_printf(ifp, "initialization failed: no " 1220164072Smarius "memory for rx buffers\n"); 122166131Swpaul pcn_stop(sc); 122266131Swpaul return; 122366131Swpaul } 122466131Swpaul 122566131Swpaul /* 122666131Swpaul * Init tx descriptors. 122766131Swpaul */ 122866131Swpaul pcn_list_tx_init(sc); 122966131Swpaul 1230164712Smarius /* Clear PCN_MISC_ASEL so we can set the port via PCN_CSR_MODE. */ 1231164712Smarius PCN_BCR_CLRBIT(sc, PCN_BCR_MISCCFG, PCN_MISC_ASEL); 123266131Swpaul 1233164712Smarius /* 1234164712Smarius * Set up the port based on the currently selected media. 1235164712Smarius * For Am79C978 we've to unconditionally set PCN_PORT_MII and 1236164712Smarius * set the PHY in PCN_BCR_PHYSEL instead. 1237164712Smarius */ 1238164712Smarius if (sc->pcn_type != Am79C978 && 1239164712Smarius IFM_INST(ife->ifm_media) == sc->pcn_inst_10bt) 1240164712Smarius pcn_csr_write(sc, PCN_CSR_MODE, PCN_PORT_10BASET); 1241164712Smarius else 1242164712Smarius pcn_csr_write(sc, PCN_CSR_MODE, PCN_PORT_MII); 1243164712Smarius 124468270Swpaul /* Set up RX filter. */ 124568270Swpaul pcn_setfilt(ifp); 124666131Swpaul 124766131Swpaul /* 124866131Swpaul * Load the multicast filter. 124966131Swpaul */ 125066131Swpaul pcn_setmulti(sc); 125166131Swpaul 125266131Swpaul /* 125366131Swpaul * Load the addresses of the RX and TX lists. 125466131Swpaul */ 125566131Swpaul pcn_csr_write(sc, PCN_CSR_RXADDR0, 125666131Swpaul vtophys(&sc->pcn_ldata->pcn_rx_list[0]) & 0xFFFF); 125766131Swpaul pcn_csr_write(sc, PCN_CSR_RXADDR1, 125866131Swpaul (vtophys(&sc->pcn_ldata->pcn_rx_list[0]) >> 16) & 0xFFFF); 125966131Swpaul pcn_csr_write(sc, PCN_CSR_TXADDR0, 126066131Swpaul vtophys(&sc->pcn_ldata->pcn_tx_list[0]) & 0xFFFF); 126166131Swpaul pcn_csr_write(sc, PCN_CSR_TXADDR1, 126266131Swpaul (vtophys(&sc->pcn_ldata->pcn_tx_list[0]) >> 16) & 0xFFFF); 126366131Swpaul 126466131Swpaul /* Set the RX and TX ring sizes. */ 126566131Swpaul pcn_csr_write(sc, PCN_CSR_RXRINGLEN, (~PCN_RX_LIST_CNT) + 1); 126666131Swpaul pcn_csr_write(sc, PCN_CSR_TXRINGLEN, (~PCN_TX_LIST_CNT) + 1); 126766131Swpaul 126866131Swpaul /* We're not using the initialization block. */ 126966131Swpaul pcn_csr_write(sc, PCN_CSR_IAB1, 0); 127066131Swpaul 127166131Swpaul /* Enable fast suspend mode. */ 127266131Swpaul PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL2, PCN_EXTCTL2_FASTSPNDE); 127366131Swpaul 127466131Swpaul /* 127566131Swpaul * Enable burst read and write. Also set the no underflow 127666131Swpaul * bit. This will avoid transmit underruns in certain 127766210Swpaul * conditions while still providing decent performance. 127866131Swpaul */ 127966131Swpaul PCN_BCR_SETBIT(sc, PCN_BCR_BUSCTL, PCN_BUSCTL_NOUFLOW| 128066131Swpaul PCN_BUSCTL_BREAD|PCN_BUSCTL_BWRITE); 128166131Swpaul 128266131Swpaul /* Enable graceful recovery from underflow. */ 128366131Swpaul PCN_CSR_SETBIT(sc, PCN_CSR_IMR, PCN_IMR_DXSUFLO); 128466131Swpaul 128566131Swpaul /* Enable auto-padding of short TX frames. */ 128666131Swpaul PCN_CSR_SETBIT(sc, PCN_CSR_TFEAT, PCN_TFEAT_PAD_TX); 128766131Swpaul 128866131Swpaul /* Disable MII autoneg (we handle this ourselves). */ 128977786Swpaul PCN_BCR_SETBIT(sc, PCN_BCR_MIICTL, PCN_MIICTL_DANAS); 129066131Swpaul 129166131Swpaul if (sc->pcn_type == Am79C978) 1292164712Smarius /* XXX support other PHYs? */ 129366131Swpaul pcn_bcr_write(sc, PCN_BCR_PHYSEL, 129466131Swpaul PCN_PHYSEL_PCNET|PCN_PHY_HOMEPNA); 129566131Swpaul 129666131Swpaul /* Enable interrupts and start the controller running. */ 129766131Swpaul pcn_csr_write(sc, PCN_CSR_CSR, PCN_CSR_INTEN|PCN_CSR_START); 129866131Swpaul 129966131Swpaul mii_mediachg(mii); 130066131Swpaul 1301148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 1302148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 130366131Swpaul 1304148738Sjhb callout_reset(&sc->pcn_stat_callout, hz, pcn_tick, sc); 130566131Swpaul 130666131Swpaul return; 130766131Swpaul} 130866131Swpaul 130966131Swpaul/* 131066131Swpaul * Set media options. 131166131Swpaul */ 1312102335Salfredstatic int 1313102335Salfredpcn_ifmedia_upd(ifp) 131466131Swpaul struct ifnet *ifp; 131566131Swpaul{ 131666131Swpaul struct pcn_softc *sc; 131766131Swpaul 131866131Swpaul sc = ifp->if_softc; 131966131Swpaul 1320148738Sjhb PCN_LOCK(sc); 1321164712Smarius 1322164712Smarius /* 1323164712Smarius * At least Am79C971 with DP83840A can wedge when switching 1324164712Smarius * from the internal 10baseT PHY to the external PHY without 1325164712Smarius * issuing pcn_reset(). For setting the port in PCN_CSR_MODE 1326164712Smarius * the PCnet chip has to be powered down or stopped anyway 1327164712Smarius * and although documented otherwise it doesn't take effect 1328164712Smarius * until the next initialization. 1329164712Smarius */ 133066131Swpaul sc->pcn_link = 0; 1331164712Smarius pcn_stop(sc); 1332164712Smarius pcn_reset(sc); 1333164712Smarius pcn_init_locked(sc); 1334164712Smarius if (ifp->if_snd.ifq_head != NULL) 1335164712Smarius pcn_start_locked(ifp); 1336164712Smarius 1337148738Sjhb PCN_UNLOCK(sc); 133866131Swpaul 133966131Swpaul return(0); 134066131Swpaul} 134166131Swpaul 134266131Swpaul/* 134366131Swpaul * Report current media status. 134466131Swpaul */ 1345102335Salfredstatic void 1346102335Salfredpcn_ifmedia_sts(ifp, ifmr) 134766131Swpaul struct ifnet *ifp; 134866131Swpaul struct ifmediareq *ifmr; 134966131Swpaul{ 135066131Swpaul struct pcn_softc *sc; 135166131Swpaul struct mii_data *mii; 135266131Swpaul 135366131Swpaul sc = ifp->if_softc; 135466131Swpaul 135566131Swpaul mii = device_get_softc(sc->pcn_miibus); 1356148738Sjhb PCN_LOCK(sc); 135766131Swpaul mii_pollstat(mii); 135866131Swpaul ifmr->ifm_active = mii->mii_media_active; 135966131Swpaul ifmr->ifm_status = mii->mii_media_status; 1360148738Sjhb PCN_UNLOCK(sc); 136166131Swpaul 136266131Swpaul return; 136366131Swpaul} 136466131Swpaul 1365102335Salfredstatic int 1366102335Salfredpcn_ioctl(ifp, command, data) 136766131Swpaul struct ifnet *ifp; 136866131Swpaul u_long command; 136966131Swpaul caddr_t data; 137066131Swpaul{ 137166131Swpaul struct pcn_softc *sc = ifp->if_softc; 137266131Swpaul struct ifreq *ifr = (struct ifreq *) data; 137366131Swpaul struct mii_data *mii = NULL; 137467087Swpaul int error = 0; 137566131Swpaul 137666131Swpaul switch(command) { 137766131Swpaul case SIOCSIFFLAGS: 1378148738Sjhb PCN_LOCK(sc); 137966131Swpaul if (ifp->if_flags & IFF_UP) { 1380148887Srwatson if (ifp->if_drv_flags & IFF_DRV_RUNNING && 138166131Swpaul ifp->if_flags & IFF_PROMISC && 138266131Swpaul !(sc->pcn_if_flags & IFF_PROMISC)) { 138366131Swpaul PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL1, 138466131Swpaul PCN_EXTCTL1_SPND); 138568270Swpaul pcn_setfilt(ifp); 138666131Swpaul PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1, 138766131Swpaul PCN_EXTCTL1_SPND); 138866771Swpaul pcn_csr_write(sc, PCN_CSR_CSR, 138966771Swpaul PCN_CSR_INTEN|PCN_CSR_START); 1390148887Srwatson } else if (ifp->if_drv_flags & IFF_DRV_RUNNING && 139166131Swpaul !(ifp->if_flags & IFF_PROMISC) && 139266131Swpaul sc->pcn_if_flags & IFF_PROMISC) { 139366131Swpaul PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL1, 139466131Swpaul PCN_EXTCTL1_SPND); 139568270Swpaul pcn_setfilt(ifp); 139666131Swpaul PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1, 139766131Swpaul PCN_EXTCTL1_SPND); 139866771Swpaul pcn_csr_write(sc, PCN_CSR_CSR, 139966771Swpaul PCN_CSR_INTEN|PCN_CSR_START); 1400148887Srwatson } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) 1401148738Sjhb pcn_init_locked(sc); 140266131Swpaul } else { 1403148887Srwatson if (ifp->if_drv_flags & IFF_DRV_RUNNING) 140466131Swpaul pcn_stop(sc); 140566131Swpaul } 140666131Swpaul sc->pcn_if_flags = ifp->if_flags; 1407148738Sjhb PCN_UNLOCK(sc); 140866131Swpaul error = 0; 140966131Swpaul break; 141066131Swpaul case SIOCADDMULTI: 141166131Swpaul case SIOCDELMULTI: 1412148738Sjhb PCN_LOCK(sc); 141366131Swpaul pcn_setmulti(sc); 1414148738Sjhb PCN_UNLOCK(sc); 141566131Swpaul error = 0; 141666131Swpaul break; 141766131Swpaul case SIOCGIFMEDIA: 141866131Swpaul case SIOCSIFMEDIA: 141966131Swpaul mii = device_get_softc(sc->pcn_miibus); 142066131Swpaul error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 142166131Swpaul break; 142266131Swpaul default: 1423106936Ssam error = ether_ioctl(ifp, command, data); 142466131Swpaul break; 142566131Swpaul } 142666131Swpaul 142766131Swpaul return(error); 142866131Swpaul} 142966131Swpaul 1430102335Salfredstatic void 1431199560Sjhbpcn_watchdog(struct pcn_softc *sc) 1432199560Sjhb{ 143366131Swpaul struct ifnet *ifp; 143466131Swpaul 1435199560Sjhb PCN_LOCK_ASSERT(sc); 1436199560Sjhb ifp = sc->pcn_ifp; 143766131Swpaul 143866131Swpaul ifp->if_oerrors++; 1439164072Smarius if_printf(ifp, "watchdog timeout\n"); 144066131Swpaul 144166131Swpaul pcn_stop(sc); 144266131Swpaul pcn_reset(sc); 1443148738Sjhb pcn_init_locked(sc); 144466131Swpaul 144566131Swpaul if (ifp->if_snd.ifq_head != NULL) 1446186715Sbrueffer pcn_start_locked(ifp); 144766131Swpaul} 144866131Swpaul 144966131Swpaul/* 145066131Swpaul * Stop the adapter and free any mbufs allocated to the 145166131Swpaul * RX and TX lists. 145266131Swpaul */ 1453102335Salfredstatic void 1454188177Simppcn_stop(struct pcn_softc *sc) 145566131Swpaul{ 145666131Swpaul register int i; 145766131Swpaul struct ifnet *ifp; 145866131Swpaul 1459148738Sjhb PCN_LOCK_ASSERT(sc); 1460147256Sbrooks ifp = sc->pcn_ifp; 1461199560Sjhb sc->pcn_timer = 0; 146266131Swpaul 1463148738Sjhb callout_stop(&sc->pcn_stat_callout); 1464110495Smdodd 1465110495Smdodd /* Turn off interrupts */ 1466110495Smdodd PCN_CSR_CLRBIT(sc, PCN_CSR_CSR, PCN_CSR_INTEN); 1467110495Smdodd /* Stop adapter */ 146866131Swpaul PCN_CSR_SETBIT(sc, PCN_CSR_CSR, PCN_CSR_STOP); 146966131Swpaul sc->pcn_link = 0; 147066131Swpaul 147166131Swpaul /* 147266131Swpaul * Free data in the RX lists. 147366131Swpaul */ 147466131Swpaul for (i = 0; i < PCN_RX_LIST_CNT; i++) { 147566131Swpaul if (sc->pcn_cdata.pcn_rx_chain[i] != NULL) { 147666131Swpaul m_freem(sc->pcn_cdata.pcn_rx_chain[i]); 147766131Swpaul sc->pcn_cdata.pcn_rx_chain[i] = NULL; 147866131Swpaul } 147966131Swpaul } 148066131Swpaul bzero((char *)&sc->pcn_ldata->pcn_rx_list, 148166131Swpaul sizeof(sc->pcn_ldata->pcn_rx_list)); 148266131Swpaul 148366131Swpaul /* 148466131Swpaul * Free the TX list buffers. 148566131Swpaul */ 148666131Swpaul for (i = 0; i < PCN_TX_LIST_CNT; i++) { 148766131Swpaul if (sc->pcn_cdata.pcn_tx_chain[i] != NULL) { 148866131Swpaul m_freem(sc->pcn_cdata.pcn_tx_chain[i]); 148966131Swpaul sc->pcn_cdata.pcn_tx_chain[i] = NULL; 149066131Swpaul } 149166131Swpaul } 149266131Swpaul 149366131Swpaul bzero((char *)&sc->pcn_ldata->pcn_tx_list, 149466131Swpaul sizeof(sc->pcn_ldata->pcn_tx_list)); 149566131Swpaul 1496148887Srwatson ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 149766131Swpaul 149866131Swpaul return; 149966131Swpaul} 150066131Swpaul 150166131Swpaul/* 150266131Swpaul * Stop all chip I/O so that the kernel's probe routines don't 150366131Swpaul * get confused by errant DMAs when rebooting. 150466131Swpaul */ 1505188177Simpstatic int 1506188177Simppcn_shutdown(device_t dev) 150766131Swpaul{ 150866131Swpaul struct pcn_softc *sc; 150966131Swpaul 151066131Swpaul sc = device_get_softc(dev); 151166131Swpaul 151267087Swpaul PCN_LOCK(sc); 151366131Swpaul pcn_reset(sc); 151466131Swpaul pcn_stop(sc); 151567087Swpaul PCN_UNLOCK(sc); 151666131Swpaul 1517188177Simp return 0; 151866131Swpaul} 1519