if_pcn.c revision 113565
150477Speter/* 2139749Simp * Copyright (c) 2000 Berkeley Software Design, Inc. 3196008Smjacob * Copyright (c) 1997, 1998, 1999, 2000 4167403Smjacob * Bill Paul <wpaul@osd.bsdi.com>. All rights reserved. 5196008Smjacob * 6167403Smjacob * Redistribution and use in source and binary forms, with or without 7167403Smjacob * modification, are permitted provided that the following conditions 8167403Smjacob * are met: 9196008Smjacob * 1. Redistributions of source code must retain the above copyright 10167403Smjacob * notice, this list of conditions and the following disclaimer. 11167403Smjacob * 2. Redistributions in binary form must reproduce the above copyright 12167403Smjacob * notice, this list of conditions and the following disclaimer in the 13167403Smjacob * documentation and/or other materials provided with the distribution. 14167403Smjacob * 3. All advertising materials mentioning features or use of this software 15196008Smjacob * must display the following acknowledgement: 16167403Smjacob * This product includes software developed by Bill Paul. 17167403Smjacob * 4. Neither the name of the author nor the names of any co-contributors 18167403Smjacob * may be used to endorse or promote products derived from this software 19167403Smjacob * without specific prior written permission. 20167403Smjacob * 21167403Smjacob * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 22167403Smjacob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23167403Smjacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24167403Smjacob * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 25167403Smjacob * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26167403Smjacob * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27196008Smjacob * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28167403Smjacob * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29167403Smjacob * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3035388Smjacob * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 3135388Smjacob * THE POSSIBILITY OF SUCH DAMAGE. 3235388Smjacob */ 3335388Smjacob 3435388Smjacob/* 3535388Smjacob * AMD Am79c972 fast ethernet PCI NIC driver. Datatheets are available 3644819Smjacob * from http://www.amd.com. 37163899Smjacob * 3835388Smjacob * The AMD PCnet/PCI controllers are more advanced and functional 3935388Smjacob * versions of the venerable 7990 LANCE. The PCnet/PCI chips retain 4035388Smjacob * backwards compatibility with the LANCE and thus can be made 41163899Smjacob * to work with older LANCE drivers. This is in fact how the 4235388Smjacob * PCnet/PCI chips were supported in FreeBSD originally. The trouble 4335388Smjacob * is that the PCnet/PCI devices offer several performance enhancements 4435388Smjacob * which can't be exploited in LANCE compatibility mode. Chief among 45163899Smjacob * these enhancements is the ability to perform PCI DMA operations 4642131Smjacob * using 32-bit addressing (which eliminates the need for ISA 4735388Smjacob * bounce-buffering), and special receive buffer alignment (which 48155704Smjacob * allows the receive handler to pass packets to the upper protocol 49163899Smjacob * layers without copying on both the x86 and alpha platforms). 50155704Smjacob */ 5153487Smjacob 5235388Smjacob#include <sys/cdefs.h> 53203444Smjacob__FBSDID("$FreeBSD: head/sys/pci/if_pcn.c 113565 2003-04-16 15:24:17Z jhb $"); 54163899Smjacob 5539235Sgibbs#include <sys/param.h> 5635388Smjacob#include <sys/systm.h> 5743420Smjacob#include <sys/sockio.h> 5835388Smjacob#include <sys/mbuf.h> 59155704Smjacob#include <sys/malloc.h> 6035388Smjacob#include <sys/kernel.h> 61289812Smav#include <sys/socket.h> 62163899Smjacob 63163899Smjacob#include <net/if.h> 64155704Smjacob#include <net/if_arp.h> 65196008Smjacob#include <net/ethernet.h> 66163899Smjacob#include <net/if_dl.h> 67155704Smjacob#include <net/if_media.h> 68155704Smjacob 69155704Smjacob#include <net/bpf.h> 70167821Smjacob 71155704Smjacob#include <vm/vm.h> /* for vtophys */ 72155704Smjacob#include <vm/pmap.h> /* for vtophys */ 7335388Smjacob#include <machine/bus_pio.h> 7435388Smjacob#include <machine/bus_memio.h> 7564087Smjacob#include <machine/bus.h> 7664087Smjacob#include <machine/resource.h> 7764087Smjacob#include <sys/bus.h> 7882689Smjacob#include <sys/rman.h> 79196008Smjacob 80292765Smav#include <dev/mii/mii.h> 81196008Smjacob#include <dev/mii/miivar.h> 8249909Smjacob 8361772Smjacob#include <pci/pcireg.h> 8449909Smjacob#include <pci/pcivar.h> 8582689Smjacob 8664087Smjacob#define PCN_USEIOSPACE 8764087Smjacob 8853487Smjacob#include <pci/if_pcnreg.h> 89289812Smav 90289812SmavMODULE_DEPEND(pcn, pci, 1, 1, 1); 9153487SmjacobMODULE_DEPEND(pcn, ether, 1, 1, 1); 9253487SmjacobMODULE_DEPEND(pcn, miibus, 1, 1, 1); 9353487Smjacob 9453487Smjacob/* "controller miibus0" required. See GENERIC if you get errors here. */ 9553487Smjacob#include "miibus_if.h" 9653487Smjacob 9753487Smjacob/* 9853487Smjacob * Various supported device vendors/types and their names. 9953487Smjacob */ 10053487Smjacobstatic struct pcn_type pcn_devs[] = { 101196008Smjacob { PCN_VENDORID, PCN_DEVICEID_PCNET, "AMD PCnet/PCI 10/100BaseTX" }, 102196008Smjacob { PCN_VENDORID, PCN_DEVICEID_HOME, "AMD PCnet/Home HomePNA" }, 10353487Smjacob { 0, 0, NULL } 104155704Smjacob}; 105155704Smjacob 10653487Smjacobstatic u_int32_t pcn_csr_read (struct pcn_softc *, int); 10753487Smjacobstatic u_int16_t pcn_csr_read16 (struct pcn_softc *, int); 10853487Smjacobstatic u_int16_t pcn_bcr_read16 (struct pcn_softc *, int); 10953487Smjacobstatic void pcn_csr_write (struct pcn_softc *, int, int); 11053487Smjacobstatic u_int32_t pcn_bcr_read (struct pcn_softc *, int); 11153487Smjacobstatic void pcn_bcr_write (struct pcn_softc *, int, int); 11264087Smjacob 11364087Smjacobstatic int pcn_probe (device_t); 11453487Smjacobstatic int pcn_attach (device_t); 11553487Smjacobstatic int pcn_detach (device_t); 11653487Smjacob 11753487Smjacobstatic int pcn_newbuf (struct pcn_softc *, int, struct mbuf *); 11853487Smjacobstatic int pcn_encap (struct pcn_softc *, 11953487Smjacob struct mbuf *, u_int32_t *); 12053487Smjacobstatic void pcn_rxeof (struct pcn_softc *); 12164087Smjacobstatic void pcn_txeof (struct pcn_softc *); 12264087Smjacobstatic void pcn_intr (void *); 12364087Smjacobstatic void pcn_tick (void *); 12464087Smjacobstatic void pcn_start (struct ifnet *); 12564087Smjacobstatic int pcn_ioctl (struct ifnet *, u_long, caddr_t); 12664087Smjacobstatic void pcn_init (void *); 12764087Smjacobstatic void pcn_stop (struct pcn_softc *); 12864087Smjacobstatic void pcn_watchdog (struct ifnet *); 12964087Smjacobstatic void pcn_shutdown (device_t); 13064087Smjacobstatic int pcn_ifmedia_upd (struct ifnet *); 13164087Smjacobstatic void pcn_ifmedia_sts (struct ifnet *, struct ifmediareq *); 132163899Smjacob 133297858Smavstatic int pcn_miibus_readreg (device_t, int, int); 134297858Smavstatic int pcn_miibus_writereg (device_t, int, int, int); 13564087Smjacobstatic void pcn_miibus_statchg (device_t); 13664087Smjacob 13764087Smjacobstatic void pcn_setfilt (struct ifnet *); 13864087Smjacobstatic void pcn_setmulti (struct pcn_softc *); 13964087Smjacobstatic u_int32_t pcn_crc (caddr_t); 14064087Smjacobstatic void pcn_reset (struct pcn_softc *); 14139235Sgibbsstatic int pcn_list_rx_init (struct pcn_softc *); 14264087Smjacobstatic int pcn_list_tx_init (struct pcn_softc *); 14364087Smjacob 14493706Smjacob#ifdef PCN_USEIOSPACE 14593706Smjacob#define PCN_RES SYS_RES_IOPORT 14693706Smjacob#define PCN_RID PCN_PCI_LOIO 14764087Smjacob#else 14864087Smjacob#define PCN_RES SYS_RES_MEMORY 14993706Smjacob#define PCN_RID PCN_PCI_LOMEM 150155704Smjacob#endif 15139235Sgibbs 15239235Sgibbsstatic device_method_t pcn_methods[] = { 15365140Smjacob /* Device interface */ 15439235Sgibbs DEVMETHOD(device_probe, pcn_probe), 15552347Smjacob DEVMETHOD(device_attach, pcn_attach), 15665140Smjacob DEVMETHOD(device_detach, pcn_detach), 15765140Smjacob DEVMETHOD(device_shutdown, pcn_shutdown), 15853487Smjacob 159218691Smarius /* bus interface */ 160218691Smarius DEVMETHOD(bus_print_child, bus_generic_print_child), 161218691Smarius DEVMETHOD(bus_driver_added, bus_generic_driver_added), 16287635Smjacob 16353487Smjacob /* MII interface */ 164196008Smjacob DEVMETHOD(miibus_readreg, pcn_miibus_readreg), 165218691Smarius DEVMETHOD(miibus_writereg, pcn_miibus_writereg), 166196008Smjacob DEVMETHOD(miibus_statchg, pcn_miibus_statchg), 167196008Smjacob 168196008Smjacob { 0, 0 } 16935388Smjacob}; 17046968Smjacob 17135388Smjacobstatic driver_t pcn_driver = { 17235388Smjacob "pcn", 173196008Smjacob pcn_methods, 174196008Smjacob sizeof(struct pcn_softc) 175196008Smjacob}; 17652347Smjacob 17752347Smjacobstatic devclass_t pcn_devclass; 17835388Smjacob 17935388SmjacobDRIVER_MODULE(pcn, pci, pcn_driver, pcn_devclass, 0, 0); 18042461SmjacobDRIVER_MODULE(miibus, pcn, miibus_driver, miibus_devclass, 0, 0); 181163899Smjacob 18243420Smjacob#define PCN_CSR_SETBIT(sc, reg, x) \ 18335388Smjacob pcn_csr_write(sc, reg, pcn_csr_read(sc, reg) | (x)) 18445040Smjacob 18565140Smjacob#define PCN_CSR_CLRBIT(sc, reg, x) \ 18635388Smjacob pcn_csr_write(sc, reg, pcn_csr_read(sc, reg) & ~(x)) 18752347Smjacob 188155704Smjacob#define PCN_BCR_SETBIT(sc, reg, x) \ 189155704Smjacob pcn_bcr_write(sc, reg, pcn_bcr_read(sc, reg) | (x)) 190155704Smjacob 191155704Smjacob#define PCN_BCR_CLRBIT(sc, reg, x) \ 192155704Smjacob pcn_bcr_write(sc, reg, pcn_bcr_read(sc, reg) & ~(x)) 193155704Smjacob 19435388Smjacobstatic u_int32_t 195196008Smjacobpcn_csr_read(sc, reg) 19680582Smjacob struct pcn_softc *sc; 19746968Smjacob int reg; 19880582Smjacob{ 19943420Smjacob CSR_WRITE_4(sc, PCN_IO32_RAP, reg); 20043420Smjacob return(CSR_READ_4(sc, PCN_IO32_RDP)); 20180582Smjacob} 20280582Smjacob 20380582Smjacobstatic u_int16_t 204155704Smjacobpcn_csr_read16(sc, reg) 205155704Smjacob struct pcn_softc *sc; 206155704Smjacob int reg; 207155704Smjacob{ 208155704Smjacob CSR_WRITE_2(sc, PCN_IO16_RAP, reg); 209155704Smjacob return(CSR_READ_2(sc, PCN_IO16_RDP)); 21035388Smjacob} 21146968Smjacob 21235388Smjacobstatic void 21335388Smjacobpcn_csr_write(sc, reg, val) 21435388Smjacob struct pcn_softc *sc; 21535388Smjacob int reg; 21639235Sgibbs{ 21739235Sgibbs CSR_WRITE_4(sc, PCN_IO32_RAP, reg); 21839235Sgibbs CSR_WRITE_4(sc, PCN_IO32_RDP, val); 21939235Sgibbs return; 22039235Sgibbs} 22139235Sgibbs 22239235Sgibbsstatic u_int32_t 22339235Sgibbspcn_bcr_read(sc, reg) 22465140Smjacob struct pcn_softc *sc; 22565140Smjacob int reg; 22665140Smjacob{ 22743793Smjacob CSR_WRITE_4(sc, PCN_IO32_RAP, reg); 22839235Sgibbs return(CSR_READ_4(sc, PCN_IO32_BDP)); 22935388Smjacob} 23045040Smjacob 23165140Smjacobstatic u_int16_t 23265140Smjacobpcn_bcr_read16(sc, reg) 23365140Smjacob struct pcn_softc *sc; 23465140Smjacob int reg; 23552347Smjacob{ 23652347Smjacob CSR_WRITE_2(sc, PCN_IO16_RAP, reg); 23752347Smjacob return(CSR_READ_2(sc, PCN_IO16_BDP)); 23852347Smjacob} 23935388Smjacob 24035388Smjacobstatic void 24135388Smjacobpcn_bcr_write(sc, reg, val) 24235388Smjacob struct pcn_softc *sc; 243164909Smjacob int reg; 244163899Smjacob{ 245163899Smjacob CSR_WRITE_4(sc, PCN_IO32_RAP, reg); 246163899Smjacob CSR_WRITE_4(sc, PCN_IO32_BDP, val); 24752347Smjacob return; 248164909Smjacob} 249163899Smjacob 250163899Smjacobstatic int 251163899Smjacobpcn_miibus_readreg(dev, phy, reg) 252196008Smjacob device_t dev; 253196008Smjacob int phy, reg; 254289890Smav{ 255164909Smjacob struct pcn_softc *sc; 25693837Smjacob int val; 257163899Smjacob 258164909Smjacob sc = device_get_softc(dev); 259164909Smjacob 260164909Smjacob if (sc->pcn_phyaddr && phy > sc->pcn_phyaddr) 261164909Smjacob return(0); 262164909Smjacob 263163899Smjacob pcn_bcr_write(sc, PCN_BCR_MIIADDR, reg | (phy << 5)); 264163899Smjacob val = pcn_bcr_read(sc, PCN_BCR_MIIDATA) & 0xFFFF; 265163899Smjacob if (val == 0xFFFF) 266163899Smjacob return(0); 267163899Smjacob 268196008Smjacob sc->pcn_phyaddr = phy; 269196008Smjacob 270196008Smjacob return(val); 271196008Smjacob} 272196008Smjacob 273196008Smjacobstatic int 274196008Smjacobpcn_miibus_writereg(dev, phy, reg, data) 275196008Smjacob device_t dev; 276196008Smjacob int phy, reg, data; 277196008Smjacob{ 278196008Smjacob struct pcn_softc *sc; 279163899Smjacob 280163899Smjacob sc = device_get_softc(dev); 281163899Smjacob 282163899Smjacob pcn_bcr_write(sc, PCN_BCR_MIIADDR, reg | (phy << 5)); 283163899Smjacob pcn_bcr_write(sc, PCN_BCR_MIIDATA, data); 284196008Smjacob 285196008Smjacob return(0); 286291144Smav} 287196008Smjacob 288196008Smjacobstatic void 289163899Smjacobpcn_miibus_statchg(dev) 290203444Smjacob device_t dev; 291203444Smjacob{ 292203444Smjacob struct pcn_softc *sc; 293203444Smjacob struct mii_data *mii; 294203444Smjacob 295203444Smjacob sc = device_get_softc(dev); 296203444Smjacob mii = device_get_softc(sc->pcn_miibus); 297203444Smjacob 298203444Smjacob if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) { 299203444Smjacob PCN_BCR_SETBIT(sc, PCN_BCR_DUPLEX, PCN_DUPLEX_FDEN); 300203444Smjacob } else { 301203444Smjacob PCN_BCR_CLRBIT(sc, PCN_BCR_DUPLEX, PCN_DUPLEX_FDEN); 302203444Smjacob } 303203444Smjacob 304203444Smjacob return; 305203444Smjacob} 306203444Smjacob 307203444Smjacob#define DC_POLY 0xEDB88320 308203444Smjacob 309203444Smjacobstatic u_int32_t 310203444Smjacobpcn_crc(addr) 311203444Smjacob caddr_t addr; 312203444Smjacob{ 313203444Smjacob u_int32_t idx, bit, data, crc; 314203444Smjacob 315203444Smjacob /* Compute CRC for the address value. */ 316203444Smjacob crc = 0xFFFFFFFF; /* initial value */ 317203444Smjacob 318203444Smjacob for (idx = 0; idx < 6; idx++) { 319203444Smjacob for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1) 320292739Smav crc = (crc >> 1) ^ (((crc ^ data) & 1) ? DC_POLY : 0); 321203444Smjacob } 322203444Smjacob 323203444Smjacob return ((crc >> 26) & 0x3F); 324203444Smjacob} 325292725Smav 326292739Smavstatic void 327292739Smavpcn_setmulti(sc) 328292725Smav struct pcn_softc *sc; 329292725Smav{ 330203444Smjacob struct ifnet *ifp; 331163899Smjacob struct ifmultiaddr *ifma; 332203444Smjacob u_int32_t h, i; 333163899Smjacob u_int16_t hashes[4] = { 0, 0, 0, 0 }; 334163899Smjacob 335163899Smjacob ifp = &sc->arpcom.ac_if; 336163899Smjacob 337163899Smjacob PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL1, PCN_EXTCTL1_SPND); 338163899Smjacob 339163899Smjacob if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 340163899Smjacob for (i = 0; i < 4; i++) 341285154Smav pcn_csr_write(sc, PCN_CSR_MAR0 + i, 0xFFFF); 342163899Smjacob PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1, PCN_EXTCTL1_SPND); 343163899Smjacob return; 344163899Smjacob } 345163899Smjacob 346163899Smjacob /* first, zot all the existing hash bits */ 347163899Smjacob for (i = 0; i < 4; i++) 348163899Smjacob pcn_csr_write(sc, PCN_CSR_MAR0 + i, 0); 349163899Smjacob 350163899Smjacob /* now program new ones */ 351163899Smjacob TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 352163899Smjacob if (ifma->ifma_addr->sa_family != AF_LINK) 353163899Smjacob continue; 354163899Smjacob h = pcn_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr)); 355163899Smjacob hashes[h >> 4] |= 1 << (h & 0xF); 356163899Smjacob } 357163899Smjacob 358163899Smjacob for (i = 0; i < 4; i++) 359163899Smjacob pcn_csr_write(sc, PCN_CSR_MAR0 + i, hashes[i]); 360163899Smjacob 361163899Smjacob PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1, PCN_EXTCTL1_SPND); 362163899Smjacob 363163899Smjacob return; 364163899Smjacob} 365163899Smjacob 366163899Smjacobstatic void 367163899Smjacobpcn_reset(sc) 368163899Smjacob struct pcn_softc *sc; 369163899Smjacob{ 370163899Smjacob /* 371163899Smjacob * Issue a reset by reading from the RESET register. 372163899Smjacob * Note that we don't know if the chip is operating in 373163899Smjacob * 16-bit or 32-bit mode at this point, so we attempt 37435388Smjacob * to reset the chip both ways. If one fails, the other 375164272Smjacob * will succeed. 376164272Smjacob */ 377164272Smjacob CSR_READ_2(sc, PCN_IO16_RESET); 378164272Smjacob CSR_READ_4(sc, PCN_IO32_RESET); 379164272Smjacob 380163899Smjacob /* Wait a little while for the chip to get its brains in order. */ 381196008Smjacob DELAY(1000); 382164272Smjacob 383164272Smjacob /* Select 32-bit (DWIO) mode */ 384164272Smjacob CSR_WRITE_4(sc, PCN_IO32_RDP, 0); 385164272Smjacob 386238869Smjacob /* Select software style 3. */ 387238869Smjacob pcn_bcr_write(sc, PCN_BCR_SSTYLE, PCN_SWSTYLE_PCNETPCI_BURST); 388196008Smjacob 389164272Smjacob return; 390285459Smav} 391285459Smav 392285459Smav/* 393285459Smav * Probe for an AMD chip. Check the PCI vendor and device 394196008Smjacob * IDs against our list and return a device name if we find a match. 395164272Smjacob */ 396164272Smjacobstatic int 397238869Smjacobpcn_probe(dev) 398238869Smjacob device_t dev; 399291080Smav{ 400163899Smjacob struct pcn_type *t; 401291080Smav struct pcn_softc *sc; 402163899Smjacob int rid; 403285459Smav u_int32_t chip_id; 404285459Smav 405285459Smav t = pcn_devs; 406163899Smjacob sc = device_get_softc(dev); 407196008Smjacob 408285459Smav while(t->pcn_name != NULL) { 409163899Smjacob if ((pci_get_vendor(dev) == t->pcn_vid) && 410163899Smjacob (pci_get_device(dev) == t->pcn_did)) { 411163899Smjacob /* 412208119Smjacob * Temporarily map the I/O space 413163899Smjacob * so we can read the chip ID register. 414163899Smjacob */ 415291080Smav rid = PCN_RID; 416291080Smav sc->pcn_res = bus_alloc_resource(dev, PCN_RES, &rid, 417291080Smav 0, ~0, 1, RF_ACTIVE); 418291080Smav if (sc->pcn_res == NULL) { 419291080Smav device_printf(dev, 420291080Smav "couldn't map ports/memory\n"); 421163899Smjacob return(ENXIO); 422285459Smav } 423285459Smav sc->pcn_btag = rman_get_bustag(sc->pcn_res); 424163899Smjacob sc->pcn_bhandle = rman_get_bushandle(sc->pcn_res); 425163899Smjacob mtx_init(&sc->pcn_mtx, 426196008Smjacob device_get_nameunit(dev), MTX_NETWORK_LOCK, 427196008Smjacob MTX_DEF); 428196008Smjacob PCN_LOCK(sc); 429163899Smjacob /* 430196008Smjacob * Note: we can *NOT* put the chip into 431163899Smjacob * 32-bit mode yet. The lnc driver will only 432289219Smav * work in 16-bit mode, and once the chip 433289219Smav * goes into 32-bit mode, the only way to 434289219Smav * get it out again is with a hardware reset. 435289219Smav * So if pcn_probe() is called before the 436289219Smav * lnc driver's probe routine, the chip will 437289219Smav * be locked into 32-bit operation and the lnc 438291365Smav * driver will be unable to attach to it. 439238869Smjacob * Note II: if the chip happens to already 440196008Smjacob * be in 32-bit mode, we still need to check 441196008Smjacob * the chip ID, but first we have to detect 442154704Smjacob * 32-bit mode using only 16-bit operations. 443196008Smjacob * The safest way to do this is to read the 444155704Smjacob * PCI subsystem ID from BCR23/24 and compare 445160080Smjacob * that with the value read from PCI config 446160080Smjacob * space. 447196008Smjacob */ 448196008Smjacob chip_id = pcn_bcr_read16(sc, PCN_BCR_PCISUBSYSID); 449196008Smjacob chip_id <<= 16; 450196008Smjacob chip_id |= pcn_bcr_read16(sc, PCN_BCR_PCISUBVENID); 451237210Smjacob /* 452297915Smav * Note III: the test for 0x10001000 is a hack to 453155704Smjacob * pacify VMware, who's pseudo-PCnet interface is 454155704Smjacob * broken. Reading the subsystem register from PCI 455196008Smjacob * config space yeilds 0x00000000 while reading the 456196008Smjacob * same value from I/O space yeilds 0x10001000. It's 457196008Smjacob * not supposed to be that way. 458196008Smjacob */ 459196008Smjacob if (chip_id == pci_read_config(dev, 460196008Smjacob PCIR_SUBVEND_0, 4) || chip_id == 0x10001000) { 461196008Smjacob /* We're in 16-bit mode. */ 462196008Smjacob chip_id = pcn_csr_read16(sc, PCN_CSR_CHIPID1); 463196008Smjacob chip_id <<= 16; 464196008Smjacob chip_id |= pcn_csr_read16(sc, PCN_CSR_CHIPID0); 465166120Smjacob } else { 466166120Smjacob /* We're in 32-bit mode. */ 467164272Smjacob chip_id = pcn_csr_read(sc, PCN_CSR_CHIPID1); 468164272Smjacob chip_id <<= 16; 469164272Smjacob chip_id |= pcn_csr_read(sc, PCN_CSR_CHIPID0); 470164272Smjacob } 471163899Smjacob bus_release_resource(dev, PCN_RES, 472164272Smjacob PCN_RID, sc->pcn_res); 47335388Smjacob PCN_UNLOCK(sc); 47435388Smjacob mtx_destroy(&sc->pcn_mtx); 47535388Smjacob chip_id >>= 12; 476155704Smjacob sc->pcn_type = chip_id & PART_MASK; 477155704Smjacob switch(sc->pcn_type) { 478292765Smav case Am79C971: 479292765Smav case Am79C972: 48035388Smjacob case Am79C973: 48135388Smjacob case Am79C975: 48248484Smjacob case Am79C976: 483290993Smav case Am79C978: 48448484Smjacob break; 48548484Smjacob default: 48648484Smjacob return(ENXIO); 48748484Smjacob break; 48848484Smjacob } 48948484Smjacob device_set_desc(dev, t->pcn_name); 49035388Smjacob return(0); 49148484Smjacob } 492291365Smav t++; 493291365Smav } 494291365Smav 495291365Smav return(ENXIO); 496291365Smav} 497291365Smav 498291365Smav/* 499291365Smav * Attach the interface. Allocate softc structures, do ifmedia 500291365Smav * setup and ethernet/BPF attach. 50135388Smjacob */ 50259454Smjacobstatic int 50359454Smjacobpcn_attach(dev) 50459454Smjacob device_t dev; 50559454Smjacob{ 50659454Smjacob u_int32_t eaddr[2]; 50759454Smjacob struct pcn_softc *sc; 508291080Smav struct ifnet *ifp; 509291080Smav int unit, error = 0, rid; 51042131Smjacob 51135388Smjacob sc = device_get_softc(dev); 51235388Smjacob unit = device_get_unit(dev); 513155704Smjacob 51435388Smjacob /* Initialize our mutex. */ 51535388Smjacob mtx_init(&sc->pcn_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 51635388Smjacob MTX_DEF | MTX_RECURSE); 51735388Smjacob 51835388Smjacob /* 51935388Smjacob * Handle power management nonsense. 52053487Smjacob */ 52135388Smjacob if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) { 52235388Smjacob u_int32_t iobase, membase, irq; 52335388Smjacob 52435388Smjacob /* Save important PCI config data. */ 52553487Smjacob iobase = pci_read_config(dev, PCN_PCI_LOIO, 4); 52653487Smjacob membase = pci_read_config(dev, PCN_PCI_LOMEM, 4); 52735388Smjacob irq = pci_read_config(dev, PCN_PCI_INTLINE, 4); 52835388Smjacob 52953487Smjacob /* Reset the power state. */ 530237210Smjacob printf("pcn%d: chip is in D%d power mode " 531155704Smjacob "-- setting to D0\n", unit, 532155704Smjacob pci_get_powerstate(dev)); 533155704Smjacob pci_set_powerstate(dev, PCI_POWERSTATE_D0); 534155704Smjacob 535237210Smjacob /* Restore PCI config data. */ 536155704Smjacob pci_write_config(dev, PCN_PCI_LOIO, iobase, 4); 53735388Smjacob pci_write_config(dev, PCN_PCI_LOMEM, membase, 4); 538155704Smjacob pci_write_config(dev, PCN_PCI_INTLINE, irq, 4); 53990224Smjacob } 540163899Smjacob 54187635Smjacob /* 54253487Smjacob * Map control/status registers. 54365140Smjacob */ 544196008Smjacob pci_enable_busmaster(dev); 54571079Smjacob 54671079Smjacob rid = PCN_RID; 547196008Smjacob sc->pcn_res = bus_alloc_resource(dev, PCN_RES, &rid, 548196008Smjacob 0, ~0, 1, RF_ACTIVE); 549163899Smjacob 550163899Smjacob if (sc->pcn_res == NULL) { 551163899Smjacob printf("pcn%d: couldn't map ports/memory\n", unit); 552163899Smjacob error = ENXIO; 55382689Smjacob goto fail; 55469522Smjacob } 55569522Smjacob 55669522Smjacob sc->pcn_btag = rman_get_bustag(sc->pcn_res); 557155704Smjacob sc->pcn_bhandle = rman_get_bushandle(sc->pcn_res); 558155704Smjacob 559155704Smjacob /* Allocate interrupt */ 560155704Smjacob rid = 0; 561155704Smjacob sc->pcn_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 562155704Smjacob RF_SHAREABLE | RF_ACTIVE); 563155704Smjacob 564155704Smjacob if (sc->pcn_irq == NULL) { 565196008Smjacob printf("pcn%d: couldn't map interrupt\n", unit); 56639235Sgibbs error = ENXIO; 56735388Smjacob goto fail; 56839235Sgibbs } 56935388Smjacob 57035388Smjacob /* Reset the adapter. */ 571160080Smjacob pcn_reset(sc); 572196008Smjacob 573196008Smjacob /* 574196008Smjacob * Get station address from the EEPROM. 57587635Smjacob */ 57639235Sgibbs eaddr[0] = CSR_READ_4(sc, PCN_IO32_APROM00); 57752347Smjacob eaddr[1] = CSR_READ_4(sc, PCN_IO32_APROM01); 578203444Smjacob bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); 579163899Smjacob 580163899Smjacob /* 581256705Smav * An AMD chip was detected. Inform the world. 582163899Smjacob */ 583256705Smav printf("pcn%d: Ethernet address: %6D\n", unit, 584163899Smjacob sc->arpcom.ac_enaddr, ":"); 585196008Smjacob 586237210Smjacob sc->pcn_unit = unit; 587155704Smjacob callout_handle_init(&sc->pcn_stat_ch); 588155704Smjacob 589155704Smjacob sc->pcn_ldata = contigmalloc(sizeof(struct pcn_list_data), M_DEVBUF, 590155704Smjacob M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0); 591160080Smjacob 592203444Smjacob if (sc->pcn_ldata == NULL) { 59390224Smjacob printf("pcn%d: no memory for list buffers!\n", unit); 59435388Smjacob error = ENXIO; 59535388Smjacob goto fail; 59653487Smjacob } 59735388Smjacob bzero(sc->pcn_ldata, sizeof(struct pcn_list_data)); 598203444Smjacob 599203444Smjacob ifp = &sc->arpcom.ac_if; 60035388Smjacob ifp->if_softc = sc; 60135388Smjacob ifp->if_unit = unit; 602297858Smav ifp->if_name = "pcn"; 603297858Smav ifp->if_mtu = ETHERMTU; 604297858Smav ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 605297858Smav ifp->if_ioctl = pcn_ioctl; 606297858Smav ifp->if_output = ether_output; 607297858Smav ifp->if_start = pcn_start; 608120018Smjacob ifp->if_watchdog = pcn_watchdog; 60935388Smjacob ifp->if_init = pcn_init; 610155704Smjacob ifp->if_baudrate = 10000000; 611155704Smjacob ifp->if_snd.ifq_maxlen = PCN_TX_LIST_CNT - 1; 612155704Smjacob 613155704Smjacob /* 614163899Smjacob * Do MII setup. 615163899Smjacob */ 616163899Smjacob if (mii_phy_probe(dev, &sc->pcn_miibus, 617163899Smjacob pcn_ifmedia_upd, pcn_ifmedia_sts)) { 618163899Smjacob printf("pcn%d: MII without any PHY!\n", sc->pcn_unit); 619155704Smjacob error = ENXIO; 62035388Smjacob goto fail; 621196008Smjacob } 622196008Smjacob 62349909Smjacob /* 624196008Smjacob * Call MI attach routine. 625196008Smjacob */ 626196008Smjacob ether_ifattach(ifp, (u_int8_t *) eaddr); 627196008Smjacob 628196008Smjacob error = bus_setup_intr(dev, sc->pcn_irq, INTR_TYPE_NET, 629196008Smjacob pcn_intr, sc, &sc->pcn_intrhand); 630196008Smjacob 631196008Smjacob if (error) { 632196008Smjacob printf("pcn%d: couldn't set up irq\n", unit); 633196008Smjacob goto fail; 634196008Smjacob } 635196008Smjacob 63635388Smjacobfail: 63764087Smjacob if (error) 63835388Smjacob pcn_detach(dev); 63935388Smjacob 640160080Smjacob return(error); 641160080Smjacob} 642160080Smjacob 643160080Smjacobstatic int 64435388Smjacobpcn_detach(dev) 64535388Smjacob device_t dev; 646237537Smjacob{ 64735388Smjacob struct pcn_softc *sc; 64853487Smjacob struct ifnet *ifp; 649297912Smav 650297912Smav sc = device_get_softc(dev); 651297912Smav ifp = &sc->arpcom.ac_if; 652297912Smav 653297912Smav KASSERT(mtx_initialized(&sc->pcn_mtx), ("pcn mutex not initialized")); 654297912Smav PCN_LOCK(sc); 655291654Smav 656291654Smav if (device_is_alive(dev)) { 657238869Smjacob if (bus_child_present(dev)) { 658238869Smjacob pcn_reset(sc); 659238869Smjacob pcn_stop(sc); 660238869Smjacob } 66198281Smjacob ether_ifdetach(ifp); 66298281Smjacob device_delete_child(dev, sc->pcn_miibus); 66398281Smjacob bus_generic_detach(dev); 664291654Smav } 665291654Smav 666291654Smav if (sc->pcn_intrhand) 66735388Smjacob bus_teardown_intr(dev, sc->pcn_irq, sc->pcn_intrhand); 66864087Smjacob if (sc->pcn_irq) 669196008Smjacob bus_release_resource(dev, SYS_RES_IRQ, 0, sc->pcn_irq); 670196008Smjacob if (sc->pcn_res) 671196008Smjacob bus_release_resource(dev, PCN_RES, PCN_RID, sc->pcn_res); 67271079Smjacob 67371079Smjacob if (sc->pcn_ldata) { 67471079Smjacob contigfree(sc->pcn_ldata, sizeof(struct pcn_list_data), 67571079Smjacob M_DEVBUF); 67671079Smjacob } 67771079Smjacob PCN_UNLOCK(sc); 678196008Smjacob 67971079Smjacob mtx_destroy(&sc->pcn_mtx); 68071079Smjacob 68171079Smjacob return(0); 68271079Smjacob} 68371079Smjacob 68471079Smjacob/* 68571079Smjacob * Initialize the transmit descriptors. 68671079Smjacob */ 68771079Smjacobstatic int 68879235Smjacobpcn_list_tx_init(sc) 68971079Smjacob struct pcn_softc *sc; 69071079Smjacob{ 691125544Smjacob struct pcn_list_data *ld; 692125544Smjacob struct pcn_ring_data *cd; 69371079Smjacob int i; 69471079Smjacob 69571079Smjacob cd = &sc->pcn_cdata; 696236427Smjacob ld = sc->pcn_ldata; 697236427Smjacob 698236427Smjacob for (i = 0; i < PCN_TX_LIST_CNT; i++) { 699236427Smjacob cd->pcn_tx_chain[i] = NULL; 700236427Smjacob ld->pcn_tx_list[i].pcn_tbaddr = 0; 701236427Smjacob ld->pcn_tx_list[i].pcn_txctl = 0; 702236427Smjacob ld->pcn_tx_list[i].pcn_txstat = 0; 703236427Smjacob } 70471079Smjacob 70571079Smjacob cd->pcn_tx_prod = cd->pcn_tx_cons = cd->pcn_tx_cnt = 0; 706236427Smjacob 70771079Smjacob return(0); 70871079Smjacob} 70971079Smjacob 71064087Smjacob 71164087Smjacob/* 71271079Smjacob * Initialize the RX descriptors and allocate mbufs for them. 71382689Smjacob */ 714163899Smjacobstatic int 71545282Smjacobpcn_list_rx_init(sc) 71682841Smjacob struct pcn_softc *sc; 71782841Smjacob{ 71882841Smjacob struct pcn_list_data *ld; 71971079Smjacob struct pcn_ring_data *cd; 72082841Smjacob int i; 72182841Smjacob 72282841Smjacob ld = sc->pcn_ldata; 723102012Smjacob cd = &sc->pcn_cdata; 724102012Smjacob 725163899Smjacob for (i = 0; i < PCN_RX_LIST_CNT; i++) { 726163899Smjacob if (pcn_newbuf(sc, i, NULL) == ENOBUFS) 72739235Sgibbs return(ENOBUFS); 72835388Smjacob } 72939235Sgibbs 73035388Smjacob cd->pcn_rx_prod = 0; 73139235Sgibbs 73239235Sgibbs return(0); 73339235Sgibbs} 73439235Sgibbs 73587635Smjacob/* 73687635Smjacob * Initialize an RX descriptor and attach an MBUF cluster. 73787635Smjacob */ 73887635Smjacobstatic int 73987635Smjacobpcn_newbuf(sc, idx, m) 74087635Smjacob struct pcn_softc *sc; 74187635Smjacob int idx; 74287635Smjacob struct mbuf *m; 74339235Sgibbs{ 74439235Sgibbs struct mbuf *m_new = NULL; 74535388Smjacob struct pcn_rx_desc *c; 74639235Sgibbs 74739235Sgibbs c = &sc->pcn_ldata->pcn_rx_list[idx]; 74839235Sgibbs 74939235Sgibbs if (m == NULL) { 75039235Sgibbs MGETHDR(m_new, M_DONTWAIT, MT_DATA); 75139235Sgibbs if (m_new == NULL) 75245282Smjacob return(ENOBUFS); 75354671Smjacob 75454671Smjacob MCLGET(m_new, M_DONTWAIT); 75554671Smjacob if (!(m_new->m_flags & M_EXT)) { 756104916Smjacob m_freem(m_new); 757104916Smjacob return(ENOBUFS); 75835388Smjacob } 75935388Smjacob m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 76048484Smjacob } else { 76177365Smjacob m_new = m; 76288855Smjacob m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 763154704Smjacob m_new->m_data = m_new->m_ext.ext_buf; 764154704Smjacob } 765196008Smjacob 766291654Smav m_adj(m_new, ETHER_ALIGN); 76735388Smjacob 76844819Smjacob sc->pcn_cdata.pcn_rx_chain[idx] = m_new; 769196008Smjacob c->pcn_rbaddr = vtophys(mtod(m_new, caddr_t)); 77054671Smjacob c->pcn_bufsz = (~(PCN_RXLEN) + 1) & PCN_RXLEN_BUFSZ; 77144819Smjacob c->pcn_bufsz |= PCN_RXLEN_MBO; 77254671Smjacob c->pcn_rxstat = PCN_RXSTAT_STP|PCN_RXSTAT_ENP|PCN_RXSTAT_OWN; 773104916Smjacob 77457145Smjacob return(0); 77557145Smjacob} 77657145Smjacob 777104916Smjacob/* 77857145Smjacob * A frame has been uploaded: pass the resulting mbuf chain up to 779104916Smjacob * the higher level protocols. 780104916Smjacob */ 78157145Smjacobstatic void 78277365Smjacobpcn_rxeof(sc) 78377365Smjacob struct pcn_softc *sc; 78477365Smjacob{ 785163899Smjacob struct ether_header *eh; 786163899Smjacob struct mbuf *m; 78788855Smjacob struct ifnet *ifp; 78888855Smjacob struct pcn_rx_desc *cur_rx; 789154704Smjacob int i; 790154704Smjacob 791196008Smjacob ifp = &sc->arpcom.ac_if; 792291654Smav i = sc->pcn_cdata.pcn_rx_prod; 79344819Smjacob 79464087Smjacob while(PCN_OWN_RXDESC(&sc->pcn_ldata->pcn_rx_list[i])) { 795155704Smjacob cur_rx = &sc->pcn_ldata->pcn_rx_list[i]; 79664087Smjacob m = sc->pcn_cdata.pcn_rx_chain[i]; 797163899Smjacob sc->pcn_cdata.pcn_rx_chain[i] = NULL; 798163899Smjacob 799163899Smjacob /* 800163899Smjacob * If an error occurs, update stats, clear the 80157145Smjacob * status word and leave the mbuf cluster in place: 802155704Smjacob * it should simply get re-used next time this descriptor 803155704Smjacob * comes up in the ring. 804155704Smjacob */ 80535388Smjacob if (cur_rx->pcn_rxstat & PCN_RXSTAT_ERR) { 80664087Smjacob ifp->if_ierrors++; 80735388Smjacob pcn_newbuf(sc, i, m); 80835388Smjacob PCN_INC(i, PCN_RX_LIST_CNT); 80935388Smjacob continue; 810196008Smjacob } 81135388Smjacob 812196008Smjacob if (pcn_newbuf(sc, i, NULL)) { 81335388Smjacob /* Ran out of mbufs; recycle this one. */ 81435388Smjacob pcn_newbuf(sc, i, m); 81535388Smjacob ifp->if_ierrors++; 81635388Smjacob PCN_INC(i, PCN_RX_LIST_CNT); 817155704Smjacob continue; 81835388Smjacob } 81935388Smjacob 82039235Sgibbs PCN_INC(i, PCN_RX_LIST_CNT); 82139235Sgibbs 822285459Smav /* No errors; receive the packet. */ 82339235Sgibbs ifp->if_ipackets++; 82439235Sgibbs eh = mtod(m, struct ether_header *); 82582689Smjacob m->m_len = m->m_pkthdr.len = 82682689Smjacob cur_rx->pcn_rxlen - ETHER_CRC_LEN; 82782689Smjacob m->m_pkthdr.rcvif = ifp; 82882689Smjacob 82982689Smjacob (*ifp->if_input)(ifp, m); 83035388Smjacob } 831289812Smav 83235388Smjacob sc->pcn_cdata.pcn_rx_prod = i; 83382689Smjacob 83435388Smjacob return; 83564087Smjacob} 83635388Smjacob 83774229Smjacob/* 838155704Smjacob * A frame was downloaded to the chip. It's safe for us to clean up 83964087Smjacob * the list buffers. 84064087Smjacob */ 84164087Smjacob 84264087Smjacobstatic void 84364087Smjacobpcn_txeof(sc) 84435388Smjacob struct pcn_softc *sc; 84539235Sgibbs{ 84664087Smjacob struct pcn_tx_desc *cur_tx = NULL; 84764087Smjacob struct ifnet *ifp; 84874229Smjacob u_int32_t idx; 84964087Smjacob 85064087Smjacob ifp = &sc->arpcom.ac_if; 85143420Smjacob 85239235Sgibbs /* 85372355Smjacob * Go through our tx list and free mbufs for those 854196008Smjacob * frames that have been transmitted. 85539235Sgibbs */ 856196008Smjacob idx = sc->pcn_cdata.pcn_tx_cons; 857196008Smjacob while (idx != sc->pcn_cdata.pcn_tx_prod) { 858196008Smjacob cur_tx = &sc->pcn_ldata->pcn_tx_list[idx]; 859196008Smjacob 860196008Smjacob if (!PCN_OWN_TXDESC(cur_tx)) 861196008Smjacob break; 862196008Smjacob 863196008Smjacob if (!(cur_tx->pcn_txctl & PCN_TXCTL_ENP)) { 864196008Smjacob sc->pcn_cdata.pcn_tx_cnt--; 865196008Smjacob PCN_INC(idx, PCN_TX_LIST_CNT); 866290993Smav continue; 867290993Smav } 868196008Smjacob 869196008Smjacob if (cur_tx->pcn_txctl & PCN_TXCTL_ERR) { 870196008Smjacob ifp->if_oerrors++; 871196008Smjacob if (cur_tx->pcn_txstat & PCN_TXSTAT_EXDEF) 872196008Smjacob ifp->if_collisions++; 873196008Smjacob if (cur_tx->pcn_txstat & PCN_TXSTAT_RTRY) 874196008Smjacob ifp->if_collisions++; 875196008Smjacob } 876196008Smjacob 877196008Smjacob ifp->if_collisions += 878196008Smjacob cur_tx->pcn_txstat & PCN_TXSTAT_TRC; 879196008Smjacob 880196008Smjacob ifp->if_opackets++; 881196008Smjacob if (sc->pcn_cdata.pcn_tx_chain[idx] != NULL) { 882285146Smav m_freem(sc->pcn_cdata.pcn_tx_chain[idx]); 883285146Smav sc->pcn_cdata.pcn_tx_chain[idx] = NULL; 88472355Smjacob } 88572355Smjacob 886290993Smav sc->pcn_cdata.pcn_tx_cnt--; 88772355Smjacob PCN_INC(idx, PCN_TX_LIST_CNT); 88872355Smjacob } 88972355Smjacob 890290993Smav if (idx != sc->pcn_cdata.pcn_tx_cons) { 89139235Sgibbs /* Some buffers have been freed. */ 89239235Sgibbs sc->pcn_cdata.pcn_tx_cons = idx; 893196008Smjacob ifp->if_flags &= ~IFF_OACTIVE; 894196008Smjacob } 895196008Smjacob ifp->if_timer = (sc->pcn_cdata.pcn_tx_cnt == 0) ? 0 : 5; 896196008Smjacob 897196008Smjacob return; 898196008Smjacob} 899196008Smjacob 900196008Smjacobstatic void 901196008Smjacobpcn_tick(xsc) 902196008Smjacob void *xsc; 903196008Smjacob{ 904196008Smjacob struct pcn_softc *sc; 905285146Smav struct mii_data *mii; 906285146Smav struct ifnet *ifp; 90739235Sgibbs 908196008Smjacob sc = xsc; 90939235Sgibbs ifp = &sc->arpcom.ac_if; 91039235Sgibbs PCN_LOCK(sc); 91143420Smjacob 91243420Smjacob mii = device_get_softc(sc->pcn_miibus); 91343420Smjacob mii_tick(mii); 91443420Smjacob 915196008Smjacob /* link just died */ 916196008Smjacob if (sc->pcn_link & !(mii->mii_media_status & IFM_ACTIVE)) 91748484Smjacob sc->pcn_link = 0; 91848484Smjacob 919196008Smjacob /* link just came up, restart */ 920196008Smjacob if (!sc->pcn_link && mii->mii_media_status & IFM_ACTIVE && 92172355Smjacob IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 922196008Smjacob sc->pcn_link++; 923196008Smjacob if (ifp->if_snd.ifq_head != NULL) 924196008Smjacob pcn_start(ifp); 925196008Smjacob } 926196008Smjacob 927238869Smjacob sc->pcn_stat_ch = timeout(pcn_tick, sc, hz); 928196008Smjacob 929196008Smjacob PCN_UNLOCK(sc); 930196008Smjacob 93143420Smjacob return; 932196008Smjacob} 93343420Smjacob 934196008Smjacobstatic void 935196008Smjacobpcn_intr(arg) 936196008Smjacob void *arg; 93772355Smjacob{ 93843420Smjacob struct pcn_softc *sc; 939224856Smjacob struct ifnet *ifp; 940224856Smjacob u_int32_t status; 941224856Smjacob 942224856Smjacob sc = arg; 943224856Smjacob ifp = &sc->arpcom.ac_if; 94464087Smjacob 945146073Smjacob /* Supress unwanted interrupts */ 946205698Smjacob if (!(ifp->if_flags & IFF_UP)) { 947146073Smjacob pcn_stop(sc); 948155704Smjacob return; 949205698Smjacob } 950146073Smjacob 951146073Smjacob PCN_LOCK(sc); 952205698Smjacob 95339235Sgibbs CSR_WRITE_4(sc, PCN_IO32_RAP, PCN_CSR_CSR); 95470490Smjacob 95564087Smjacob while ((status = CSR_READ_4(sc, PCN_IO32_RDP)) & PCN_CSR_INTR) { 95664087Smjacob CSR_WRITE_4(sc, PCN_IO32_RDP, status); 95764087Smjacob 95864087Smjacob if (status & PCN_CSR_RINT) 95964087Smjacob pcn_rxeof(sc); 96064087Smjacob 96164087Smjacob if (status & PCN_CSR_TINT) 96264087Smjacob pcn_txeof(sc); 96379235Smjacob 964238869Smjacob if (status & PCN_CSR_ERR) { 965205698Smjacob pcn_init(sc); 966238869Smjacob break; 967196008Smjacob } 968196008Smjacob } 969196008Smjacob 970196008Smjacob if (ifp->if_snd.ifq_head != NULL) 97139235Sgibbs pcn_start(ifp); 97264087Smjacob 97364087Smjacob PCN_UNLOCK(sc); 97464087Smjacob return; 97564087Smjacob} 97664087Smjacob 97764087Smjacob/* 97864087Smjacob * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data 979196008Smjacob * pointers to the fragment pointers. 98064087Smjacob */ 981196008Smjacobstatic int 982196008Smjacobpcn_encap(sc, m_head, txidx) 983196008Smjacob struct pcn_softc *sc; 984196008Smjacob struct mbuf *m_head; 985196008Smjacob u_int32_t *txidx; 98664087Smjacob{ 987196008Smjacob struct pcn_tx_desc *f = NULL; 988196008Smjacob struct mbuf *m; 989219098Smjacob int frag, cur, cnt = 0; 990196008Smjacob 99164087Smjacob /* 99264087Smjacob * Start packing the mbufs in this chain into 99364087Smjacob * the fragment pointers. Stop when we run out 99464087Smjacob * of fragments or hit the end of the mbuf chain. 995155704Smjacob */ 99664087Smjacob m = m_head; 99764087Smjacob cur = frag = *txidx; 99864087Smjacob 99964087Smjacob for (m = m_head; m != NULL; m = m->m_next) { 1000196008Smjacob if (m->m_len != 0) { 100164087Smjacob if ((PCN_TX_LIST_CNT - 100264087Smjacob (sc->pcn_cdata.pcn_tx_cnt + cnt)) < 2) 1003218691Smarius return(ENOBUFS); 100464087Smjacob f = &sc->pcn_ldata->pcn_tx_list[frag]; 100564087Smjacob f->pcn_txctl = (~(m->m_len) + 1) & PCN_TXCTL_BUFSZ; 100664087Smjacob f->pcn_txctl |= PCN_TXCTL_MBO; 100764087Smjacob f->pcn_tbaddr = vtophys(mtod(m, vm_offset_t)); 100864087Smjacob if (cnt == 0) 1009155704Smjacob f->pcn_txctl |= PCN_TXCTL_STP; 1010163899Smjacob else 1011155704Smjacob f->pcn_txctl |= PCN_TXCTL_OWN; 1012155704Smjacob cur = frag; 101364087Smjacob PCN_INC(frag, PCN_TX_LIST_CNT); 1014196008Smjacob cnt++; 1015196008Smjacob } 1016196008Smjacob } 101772355Smjacob 1018237210Smjacob if (m != NULL) 1019237210Smjacob return(ENOBUFS); 102064087Smjacob 102164087Smjacob sc->pcn_cdata.pcn_tx_chain[cur] = m_head; 102264087Smjacob sc->pcn_ldata->pcn_tx_list[cur].pcn_txctl |= 102364087Smjacob PCN_TXCTL_ENP|PCN_TXCTL_ADD_FCS|PCN_TXCTL_MORE_LTINT; 102464087Smjacob sc->pcn_ldata->pcn_tx_list[*txidx].pcn_txctl |= PCN_TXCTL_OWN; 1025196008Smjacob sc->pcn_cdata.pcn_tx_cnt += cnt; 1026196008Smjacob *txidx = frag; 1027196008Smjacob 1028196008Smjacob return(0); 1029196008Smjacob} 1030196008Smjacob 1031196008Smjacob/* 1032196008Smjacob * Main transmit routine. To avoid having to do mbuf copies, we put pointers 1033196008Smjacob * to the mbuf data regions directly in the transmit lists. We also save a 1034196008Smjacob * copy of the pointers since the transmit list fragment pointers are 1035196008Smjacob * physical addresses. 1036196008Smjacob */ 1037196008Smjacobstatic void 1038196008Smjacobpcn_start(ifp) 1039196008Smjacob struct ifnet *ifp; 1040196008Smjacob{ 1041238869Smjacob struct pcn_softc *sc; 1042238869Smjacob struct mbuf *m_head = NULL; 1043196008Smjacob u_int32_t idx; 1044205698Smjacob 1045205698Smjacob sc = ifp->if_softc; 1046196008Smjacob 1047196008Smjacob PCN_LOCK(sc); 1048196008Smjacob 104964087Smjacob if (!sc->pcn_link) { 105064087Smjacob PCN_UNLOCK(sc); 105164087Smjacob return; 105264087Smjacob } 105364087Smjacob 105464087Smjacob idx = sc->pcn_cdata.pcn_tx_prod; 105564087Smjacob 105664087Smjacob if (ifp->if_flags & IFF_OACTIVE) { 105764087Smjacob PCN_UNLOCK(sc); 105864087Smjacob return; 105964087Smjacob } 106064087Smjacob 106164087Smjacob while(sc->pcn_cdata.pcn_tx_chain[idx] == NULL) { 106264087Smjacob IF_DEQUEUE(&ifp->if_snd, m_head); 106364087Smjacob if (m_head == NULL) 1064238869Smjacob break; 106564087Smjacob 1066238869Smjacob if (pcn_encap(sc, m_head, &idx)) { 1067238869Smjacob IF_PREPEND(&ifp->if_snd, m_head); 1068205698Smjacob ifp->if_flags |= IFF_OACTIVE; 1069205698Smjacob break; 1070196008Smjacob } 1071196008Smjacob 107264087Smjacob /* 1073291188Smav * If there's a BPF listener, bounce a copy of this frame 1074196008Smjacob * to him. 1075196008Smjacob */ 107664087Smjacob BPF_MTAP(ifp, m_head); 107767047Smjacob 107867047Smjacob } 107967047Smjacob 108064087Smjacob /* Transmit */ 1081196008Smjacob sc->pcn_cdata.pcn_tx_prod = idx; 1082196008Smjacob pcn_csr_write(sc, PCN_CSR_CSR, PCN_CSR_TX|PCN_CSR_INTEN); 108364087Smjacob 1084196008Smjacob /* 1085196008Smjacob * Set a timeout in case the chip goes out to lunch. 108664087Smjacob */ 1087196008Smjacob ifp->if_timer = 5; 1088196008Smjacob 108987635Smjacob PCN_UNLOCK(sc); 1090196008Smjacob 1091196008Smjacob return; 1092196008Smjacob} 1093196008Smjacob 1094196008Smjacobstatic void 1095155704Smjacobpcn_setfilt(ifp) 1096155704Smjacob struct ifnet *ifp; 1097155704Smjacob{ 109887635Smjacob struct pcn_softc *sc; 1099155704Smjacob 1100155704Smjacob sc = ifp->if_softc; 1101155704Smjacob 110287635Smjacob /* If we want promiscuous mode, set the allframes bit. */ 1103155704Smjacob if (ifp->if_flags & IFF_PROMISC) { 1104171159Smjacob PCN_CSR_SETBIT(sc, PCN_CSR_MODE, PCN_MODE_PROMISC); 1105171159Smjacob } else { 1106171159Smjacob PCN_CSR_CLRBIT(sc, PCN_CSR_MODE, PCN_MODE_PROMISC); 110764087Smjacob } 110877365Smjacob 1109196008Smjacob /* Set the capture broadcast bit to capture broadcast frames. */ 1110196008Smjacob if (ifp->if_flags & IFF_BROADCAST) { 1111196008Smjacob PCN_CSR_CLRBIT(sc, PCN_CSR_MODE, PCN_MODE_RXNOBROAD); 1112196008Smjacob } else { 1113196008Smjacob PCN_CSR_SETBIT(sc, PCN_CSR_MODE, PCN_MODE_RXNOBROAD); 1114196008Smjacob } 1115196008Smjacob 1116196008Smjacob return; 1117196008Smjacob} 1118196008Smjacob 1119196008Smjacobstatic void 1120196008Smjacobpcn_init(xsc) 1121196008Smjacob void *xsc; 1122196008Smjacob{ 1123196008Smjacob struct pcn_softc *sc = xsc; 1124196008Smjacob struct ifnet *ifp = &sc->arpcom.ac_if; 1125196008Smjacob struct mii_data *mii = NULL; 1126196008Smjacob 1127196008Smjacob PCN_LOCK(sc); 1128196008Smjacob 1129196008Smjacob /* 1130196008Smjacob * Cancel pending I/O and free all RX/TX buffers. 1131196008Smjacob */ 1132196008Smjacob pcn_stop(sc); 1133196008Smjacob pcn_reset(sc); 1134196008Smjacob 1135196008Smjacob mii = device_get_softc(sc->pcn_miibus); 1136196008Smjacob 1137196008Smjacob /* Set MAC address */ 1138196008Smjacob pcn_csr_write(sc, PCN_CSR_PAR0, 1139196008Smjacob ((u_int16_t *)sc->arpcom.ac_enaddr)[0]); 1140196008Smjacob pcn_csr_write(sc, PCN_CSR_PAR1, 1141196008Smjacob ((u_int16_t *)sc->arpcom.ac_enaddr)[1]); 1142196008Smjacob pcn_csr_write(sc, PCN_CSR_PAR2, 1143196008Smjacob ((u_int16_t *)sc->arpcom.ac_enaddr)[2]); 1144196008Smjacob 1145196008Smjacob /* Init circular RX list. */ 1146196008Smjacob if (pcn_list_rx_init(sc) == ENOBUFS) { 1147196008Smjacob printf("pcn%d: initialization failed: no " 1148196008Smjacob "memory for rx buffers\n", sc->pcn_unit); 1149196008Smjacob pcn_stop(sc); 1150196008Smjacob PCN_UNLOCK(sc); 1151196008Smjacob return; 1152196008Smjacob } 1153196008Smjacob 1154196008Smjacob /* 1155196008Smjacob * Init tx descriptors. 1156196008Smjacob */ 1157196008Smjacob pcn_list_tx_init(sc); 115835388Smjacob 1159 /* Set up the mode register. */ 1160 pcn_csr_write(sc, PCN_CSR_MODE, PCN_PORT_MII); 1161 1162 /* Set up RX filter. */ 1163 pcn_setfilt(ifp); 1164 1165 /* 1166 * Load the multicast filter. 1167 */ 1168 pcn_setmulti(sc); 1169 1170 /* 1171 * Load the addresses of the RX and TX lists. 1172 */ 1173 pcn_csr_write(sc, PCN_CSR_RXADDR0, 1174 vtophys(&sc->pcn_ldata->pcn_rx_list[0]) & 0xFFFF); 1175 pcn_csr_write(sc, PCN_CSR_RXADDR1, 1176 (vtophys(&sc->pcn_ldata->pcn_rx_list[0]) >> 16) & 0xFFFF); 1177 pcn_csr_write(sc, PCN_CSR_TXADDR0, 1178 vtophys(&sc->pcn_ldata->pcn_tx_list[0]) & 0xFFFF); 1179 pcn_csr_write(sc, PCN_CSR_TXADDR1, 1180 (vtophys(&sc->pcn_ldata->pcn_tx_list[0]) >> 16) & 0xFFFF); 1181 1182 /* Set the RX and TX ring sizes. */ 1183 pcn_csr_write(sc, PCN_CSR_RXRINGLEN, (~PCN_RX_LIST_CNT) + 1); 1184 pcn_csr_write(sc, PCN_CSR_TXRINGLEN, (~PCN_TX_LIST_CNT) + 1); 1185 1186 /* We're not using the initialization block. */ 1187 pcn_csr_write(sc, PCN_CSR_IAB1, 0); 1188 1189 /* Enable fast suspend mode. */ 1190 PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL2, PCN_EXTCTL2_FASTSPNDE); 1191 1192 /* 1193 * Enable burst read and write. Also set the no underflow 1194 * bit. This will avoid transmit underruns in certain 1195 * conditions while still providing decent performance. 1196 */ 1197 PCN_BCR_SETBIT(sc, PCN_BCR_BUSCTL, PCN_BUSCTL_NOUFLOW| 1198 PCN_BUSCTL_BREAD|PCN_BUSCTL_BWRITE); 1199 1200 /* Enable graceful recovery from underflow. */ 1201 PCN_CSR_SETBIT(sc, PCN_CSR_IMR, PCN_IMR_DXSUFLO); 1202 1203 /* Enable auto-padding of short TX frames. */ 1204 PCN_CSR_SETBIT(sc, PCN_CSR_TFEAT, PCN_TFEAT_PAD_TX); 1205 1206 /* Disable MII autoneg (we handle this ourselves). */ 1207 PCN_BCR_SETBIT(sc, PCN_BCR_MIICTL, PCN_MIICTL_DANAS); 1208 1209 if (sc->pcn_type == Am79C978) 1210 pcn_bcr_write(sc, PCN_BCR_PHYSEL, 1211 PCN_PHYSEL_PCNET|PCN_PHY_HOMEPNA); 1212 1213 /* Enable interrupts and start the controller running. */ 1214 pcn_csr_write(sc, PCN_CSR_CSR, PCN_CSR_INTEN|PCN_CSR_START); 1215 1216 mii_mediachg(mii); 1217 1218 ifp->if_flags |= IFF_RUNNING; 1219 ifp->if_flags &= ~IFF_OACTIVE; 1220 1221 sc->pcn_stat_ch = timeout(pcn_tick, sc, hz); 1222 PCN_UNLOCK(sc); 1223 1224 return; 1225} 1226 1227/* 1228 * Set media options. 1229 */ 1230static int 1231pcn_ifmedia_upd(ifp) 1232 struct ifnet *ifp; 1233{ 1234 struct pcn_softc *sc; 1235 struct mii_data *mii; 1236 1237 sc = ifp->if_softc; 1238 mii = device_get_softc(sc->pcn_miibus); 1239 1240 sc->pcn_link = 0; 1241 if (mii->mii_instance) { 1242 struct mii_softc *miisc; 1243 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 1244 mii_phy_reset(miisc); 1245 } 1246 mii_mediachg(mii); 1247 1248 return(0); 1249} 1250 1251/* 1252 * Report current media status. 1253 */ 1254static void 1255pcn_ifmedia_sts(ifp, ifmr) 1256 struct ifnet *ifp; 1257 struct ifmediareq *ifmr; 1258{ 1259 struct pcn_softc *sc; 1260 struct mii_data *mii; 1261 1262 sc = ifp->if_softc; 1263 1264 mii = device_get_softc(sc->pcn_miibus); 1265 mii_pollstat(mii); 1266 ifmr->ifm_active = mii->mii_media_active; 1267 ifmr->ifm_status = mii->mii_media_status; 1268 1269 return; 1270} 1271 1272static int 1273pcn_ioctl(ifp, command, data) 1274 struct ifnet *ifp; 1275 u_long command; 1276 caddr_t data; 1277{ 1278 struct pcn_softc *sc = ifp->if_softc; 1279 struct ifreq *ifr = (struct ifreq *) data; 1280 struct mii_data *mii = NULL; 1281 int error = 0; 1282 1283 PCN_LOCK(sc); 1284 1285 switch(command) { 1286 case SIOCSIFFLAGS: 1287 if (ifp->if_flags & IFF_UP) { 1288 if (ifp->if_flags & IFF_RUNNING && 1289 ifp->if_flags & IFF_PROMISC && 1290 !(sc->pcn_if_flags & IFF_PROMISC)) { 1291 PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL1, 1292 PCN_EXTCTL1_SPND); 1293 pcn_setfilt(ifp); 1294 PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1, 1295 PCN_EXTCTL1_SPND); 1296 pcn_csr_write(sc, PCN_CSR_CSR, 1297 PCN_CSR_INTEN|PCN_CSR_START); 1298 } else if (ifp->if_flags & IFF_RUNNING && 1299 !(ifp->if_flags & IFF_PROMISC) && 1300 sc->pcn_if_flags & IFF_PROMISC) { 1301 PCN_CSR_SETBIT(sc, PCN_CSR_EXTCTL1, 1302 PCN_EXTCTL1_SPND); 1303 pcn_setfilt(ifp); 1304 PCN_CSR_CLRBIT(sc, PCN_CSR_EXTCTL1, 1305 PCN_EXTCTL1_SPND); 1306 pcn_csr_write(sc, PCN_CSR_CSR, 1307 PCN_CSR_INTEN|PCN_CSR_START); 1308 } else if (!(ifp->if_flags & IFF_RUNNING)) 1309 pcn_init(sc); 1310 } else { 1311 if (ifp->if_flags & IFF_RUNNING) 1312 pcn_stop(sc); 1313 } 1314 sc->pcn_if_flags = ifp->if_flags; 1315 error = 0; 1316 break; 1317 case SIOCADDMULTI: 1318 case SIOCDELMULTI: 1319 pcn_setmulti(sc); 1320 error = 0; 1321 break; 1322 case SIOCGIFMEDIA: 1323 case SIOCSIFMEDIA: 1324 mii = device_get_softc(sc->pcn_miibus); 1325 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 1326 break; 1327 default: 1328 error = ether_ioctl(ifp, command, data); 1329 break; 1330 } 1331 1332 PCN_UNLOCK(sc); 1333 1334 return(error); 1335} 1336 1337static void 1338pcn_watchdog(ifp) 1339 struct ifnet *ifp; 1340{ 1341 struct pcn_softc *sc; 1342 1343 sc = ifp->if_softc; 1344 1345 PCN_LOCK(sc); 1346 1347 ifp->if_oerrors++; 1348 printf("pcn%d: watchdog timeout\n", sc->pcn_unit); 1349 1350 pcn_stop(sc); 1351 pcn_reset(sc); 1352 pcn_init(sc); 1353 1354 if (ifp->if_snd.ifq_head != NULL) 1355 pcn_start(ifp); 1356 1357 PCN_UNLOCK(sc); 1358 1359 return; 1360} 1361 1362/* 1363 * Stop the adapter and free any mbufs allocated to the 1364 * RX and TX lists. 1365 */ 1366static void 1367pcn_stop(sc) 1368 struct pcn_softc *sc; 1369{ 1370 register int i; 1371 struct ifnet *ifp; 1372 1373 ifp = &sc->arpcom.ac_if; 1374 PCN_LOCK(sc); 1375 ifp->if_timer = 0; 1376 1377 untimeout(pcn_tick, sc, sc->pcn_stat_ch); 1378 1379 /* Turn off interrupts */ 1380 PCN_CSR_CLRBIT(sc, PCN_CSR_CSR, PCN_CSR_INTEN); 1381 /* Stop adapter */ 1382 PCN_CSR_SETBIT(sc, PCN_CSR_CSR, PCN_CSR_STOP); 1383 sc->pcn_link = 0; 1384 1385 /* 1386 * Free data in the RX lists. 1387 */ 1388 for (i = 0; i < PCN_RX_LIST_CNT; i++) { 1389 if (sc->pcn_cdata.pcn_rx_chain[i] != NULL) { 1390 m_freem(sc->pcn_cdata.pcn_rx_chain[i]); 1391 sc->pcn_cdata.pcn_rx_chain[i] = NULL; 1392 } 1393 } 1394 bzero((char *)&sc->pcn_ldata->pcn_rx_list, 1395 sizeof(sc->pcn_ldata->pcn_rx_list)); 1396 1397 /* 1398 * Free the TX list buffers. 1399 */ 1400 for (i = 0; i < PCN_TX_LIST_CNT; i++) { 1401 if (sc->pcn_cdata.pcn_tx_chain[i] != NULL) { 1402 m_freem(sc->pcn_cdata.pcn_tx_chain[i]); 1403 sc->pcn_cdata.pcn_tx_chain[i] = NULL; 1404 } 1405 } 1406 1407 bzero((char *)&sc->pcn_ldata->pcn_tx_list, 1408 sizeof(sc->pcn_ldata->pcn_tx_list)); 1409 1410 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 1411 PCN_UNLOCK(sc); 1412 1413 return; 1414} 1415 1416/* 1417 * Stop all chip I/O so that the kernel's probe routines don't 1418 * get confused by errant DMAs when rebooting. 1419 */ 1420static void 1421pcn_shutdown(dev) 1422 device_t dev; 1423{ 1424 struct pcn_softc *sc; 1425 1426 sc = device_get_softc(dev); 1427 1428 PCN_LOCK(sc); 1429 pcn_reset(sc); 1430 pcn_stop(sc); 1431 PCN_UNLOCK(sc); 1432 1433 return; 1434} 1435