if_rl.c revision 105221
140516Swpaul/* 240516Swpaul * Copyright (c) 1997, 1998 340516Swpaul * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 440516Swpaul * 540516Swpaul * Redistribution and use in source and binary forms, with or without 640516Swpaul * modification, are permitted provided that the following conditions 740516Swpaul * are met: 840516Swpaul * 1. Redistributions of source code must retain the above copyright 940516Swpaul * notice, this list of conditions and the following disclaimer. 1040516Swpaul * 2. Redistributions in binary form must reproduce the above copyright 1140516Swpaul * notice, this list of conditions and the following disclaimer in the 1240516Swpaul * documentation and/or other materials provided with the distribution. 1340516Swpaul * 3. All advertising materials mentioning features or use of this software 1440516Swpaul * must display the following acknowledgement: 1540516Swpaul * This product includes software developed by Bill Paul. 1640516Swpaul * 4. Neither the name of the author nor the names of any co-contributors 1740516Swpaul * may be used to endorse or promote products derived from this software 1840516Swpaul * without specific prior written permission. 1940516Swpaul * 2040516Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 2140516Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2240516Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2340516Swpaul * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 2440516Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2540516Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2640516Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2740516Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2840516Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2940516Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 3040516Swpaul * THE POSSIBILITY OF SUCH DAMAGE. 3140516Swpaul * 3250477Speter * $FreeBSD: head/sys/pci/if_rl.c 105221 2002-10-16 09:14:59Z phk $ 3340516Swpaul */ 3440516Swpaul 3540516Swpaul/* 3640516Swpaul * RealTek 8129/8139 PCI NIC driver 3740516Swpaul * 3840516Swpaul * Supports several extremely cheap PCI 10/100 adapters based on 3940516Swpaul * the RealTek chipset. Datasheets can be obtained from 4040516Swpaul * www.realtek.com.tw. 4140516Swpaul * 4240516Swpaul * Written by Bill Paul <wpaul@ctr.columbia.edu> 4340516Swpaul * Electrical Engineering Department 4440516Swpaul * Columbia University, New York City 4540516Swpaul */ 4640516Swpaul 4740516Swpaul/* 4840516Swpaul * The RealTek 8139 PCI NIC redefines the meaning of 'low end.' This is 4940516Swpaul * probably the worst PCI ethernet controller ever made, with the possible 5040516Swpaul * exception of the FEAST chip made by SMC. The 8139 supports bus-master 5140516Swpaul * DMA, but it has a terrible interface that nullifies any performance 5240516Swpaul * gains that bus-master DMA usually offers. 5340516Swpaul * 5440516Swpaul * For transmission, the chip offers a series of four TX descriptor 5540516Swpaul * registers. Each transmit frame must be in a contiguous buffer, aligned 5641569Swpaul * on a longword (32-bit) boundary. This means we almost always have to 5740516Swpaul * do mbuf copies in order to transmit a frame, except in the unlikely 5840516Swpaul * case where a) the packet fits into a single mbuf, and b) the packet 5940516Swpaul * is 32-bit aligned within the mbuf's data area. The presence of only 6040516Swpaul * four descriptor registers means that we can never have more than four 6140516Swpaul * packets queued for transmission at any one time. 6240516Swpaul * 6340516Swpaul * Reception is not much better. The driver has to allocate a single large 6440516Swpaul * buffer area (up to 64K in size) into which the chip will DMA received 6540516Swpaul * frames. Because we don't know where within this region received packets 6640516Swpaul * will begin or end, we have no choice but to copy data from the buffer 6740516Swpaul * area into mbufs in order to pass the packets up to the higher protocol 6840516Swpaul * levels. 6940516Swpaul * 7040516Swpaul * It's impossible given this rotten design to really achieve decent 7140516Swpaul * performance at 100Mbps, unless you happen to have a 400Mhz PII or 7240516Swpaul * some equally overmuscled CPU to drive it. 7340516Swpaul * 7440516Swpaul * On the bright side, the 8139 does have a built-in PHY, although 7540516Swpaul * rather than using an MDIO serial interface like most other NICs, the 7640516Swpaul * PHY registers are directly accessible through the 8139's register 7740516Swpaul * space. The 8139 supports autonegotiation, as well as a 64-bit multicast 7840516Swpaul * filter. 7940516Swpaul * 8040516Swpaul * The 8129 chip is an older version of the 8139 that uses an external PHY 8140516Swpaul * chip. The 8129 has a serial MDIO interface for accessing the MII where 8240516Swpaul * the 8139 lets you directly access the on-board PHY registers. We need 8340516Swpaul * to select which interface to use depending on the chip type. 8440516Swpaul */ 8540516Swpaul 8640516Swpaul#include <sys/param.h> 8740516Swpaul#include <sys/systm.h> 8840516Swpaul#include <sys/sockio.h> 8940516Swpaul#include <sys/mbuf.h> 9040516Swpaul#include <sys/malloc.h> 9140516Swpaul#include <sys/kernel.h> 9240516Swpaul#include <sys/socket.h> 9340516Swpaul 9440516Swpaul#include <net/if.h> 9540516Swpaul#include <net/if_arp.h> 9640516Swpaul#include <net/ethernet.h> 9740516Swpaul#include <net/if_dl.h> 9840516Swpaul#include <net/if_media.h> 9940516Swpaul 10040516Swpaul#include <net/bpf.h> 10140516Swpaul 10241569Swpaul#include <machine/bus_pio.h> 10341569Swpaul#include <machine/bus_memio.h> 10441569Swpaul#include <machine/bus.h> 10550703Swpaul#include <machine/resource.h> 10650703Swpaul#include <sys/bus.h> 10750703Swpaul#include <sys/rman.h> 10840516Swpaul 10950703Swpaul#include <dev/mii/mii.h> 11050703Swpaul#include <dev/mii/miivar.h> 11150703Swpaul 11240516Swpaul#include <pci/pcireg.h> 11340516Swpaul#include <pci/pcivar.h> 11440516Swpaul 11559758SpeterMODULE_DEPEND(rl, miibus, 1, 1, 1); 11659758Speter 11751089Speter/* "controller miibus0" required. See GENERIC if you get errors here. */ 11850703Swpaul#include "miibus_if.h" 11950703Swpaul 12040516Swpaul/* 12140516Swpaul * Default to using PIO access for this driver. On SMP systems, 12240516Swpaul * there appear to be problems with memory mapped mode: it looks like 12340516Swpaul * doing too many memory mapped access back to back in rapid succession 12440516Swpaul * can hang the bus. I'm inclined to blame this on crummy design/construction 12540516Swpaul * on the part of RealTek. Memory mapped mode does appear to work on 12640516Swpaul * uniprocessor systems though. 12740516Swpaul */ 12840516Swpaul#define RL_USEIOSPACE 12940516Swpaul 13040516Swpaul#include <pci/if_rlreg.h> 13140516Swpaul 13240516Swpaul#ifndef lint 13341591Sarchiestatic const char rcsid[] = 13450477Speter "$FreeBSD: head/sys/pci/if_rl.c 105221 2002-10-16 09:14:59Z phk $"; 13540516Swpaul#endif 13640516Swpaul 13740516Swpaul/* 13840516Swpaul * Various supported device vendors/types and their names. 13940516Swpaul */ 14040516Swpaulstatic struct rl_type rl_devs[] = { 14140516Swpaul { RT_VENDORID, RT_DEVICEID_8129, 14240516Swpaul "RealTek 8129 10/100BaseTX" }, 14340516Swpaul { RT_VENDORID, RT_DEVICEID_8139, 14440516Swpaul "RealTek 8139 10/100BaseTX" }, 14567771Swpaul { RT_VENDORID, RT_DEVICEID_8138, 14667771Swpaul "RealTek 8139 10/100BaseTX CardBus" }, 14741243Swpaul { ACCTON_VENDORID, ACCTON_DEVICEID_5030, 14841243Swpaul "Accton MPX 5030/5038 10/100BaseTX" }, 14944238Swpaul { DELTA_VENDORID, DELTA_DEVICEID_8139, 15044238Swpaul "Delta Electronics 8139 10/100BaseTX" }, 15144238Swpaul { ADDTRON_VENDORID, ADDTRON_DEVICEID_8139, 15244238Swpaul "Addtron Technolgy 8139 10/100BaseTX" }, 15372813Swpaul { DLINK_VENDORID, DLINK_DEVICEID_530TXPLUS, 15472813Swpaul "D-Link DFE-530TX+ 10/100BaseTX" }, 15596112Sjhb { DLINK_VENDORID, DLINK_DEVICEID_690TXD, 15696112Sjhb "D-Link DFE-690TXD 10/100BaseTX" }, 15794400Swpaul { NORTEL_VENDORID, ACCTON_DEVICEID_5030, 15894400Swpaul "Nortel Networks 10/100BaseTX" }, 159103020Siwasaki { COREGA_VENDORID, COREGA_DEVICEID_CBTXD, 160103020Siwasaki "Corega FEther CB-TXD" }, 16140516Swpaul { 0, 0, NULL } 16240516Swpaul}; 16340516Swpaul 16492739Salfredstatic int rl_probe (device_t); 16592739Salfredstatic int rl_attach (device_t); 16692739Salfredstatic int rl_detach (device_t); 16740516Swpaul 16892739Salfredstatic int rl_encap (struct rl_softc *, struct mbuf * ); 16940516Swpaul 17092739Salfredstatic void rl_rxeof (struct rl_softc *); 17192739Salfredstatic void rl_txeof (struct rl_softc *); 17292739Salfredstatic void rl_intr (void *); 17392739Salfredstatic void rl_tick (void *); 17492739Salfredstatic void rl_start (struct ifnet *); 17592739Salfredstatic int rl_ioctl (struct ifnet *, u_long, caddr_t); 17692739Salfredstatic void rl_init (void *); 17792739Salfredstatic void rl_stop (struct rl_softc *); 17892739Salfredstatic void rl_watchdog (struct ifnet *); 17992739Salfredstatic int rl_suspend (device_t); 18092739Salfredstatic int rl_resume (device_t); 18192739Salfredstatic void rl_shutdown (device_t); 18292739Salfredstatic int rl_ifmedia_upd (struct ifnet *); 18392739Salfredstatic void rl_ifmedia_sts (struct ifnet *, struct ifmediareq *); 18440516Swpaul 18592739Salfredstatic void rl_eeprom_putbyte (struct rl_softc *, int); 18692739Salfredstatic void rl_eeprom_getword (struct rl_softc *, int, u_int16_t *); 18792739Salfredstatic void rl_read_eeprom (struct rl_softc *, caddr_t, int, int, int); 18892739Salfredstatic void rl_mii_sync (struct rl_softc *); 18992739Salfredstatic void rl_mii_send (struct rl_softc *, u_int32_t, int); 19092739Salfredstatic int rl_mii_readreg (struct rl_softc *, struct rl_mii_frame *); 19192739Salfredstatic int rl_mii_writereg (struct rl_softc *, struct rl_mii_frame *); 19240516Swpaul 19392739Salfredstatic int rl_miibus_readreg (device_t, int, int); 19492739Salfredstatic int rl_miibus_writereg (device_t, int, int, int); 19592739Salfredstatic void rl_miibus_statchg (device_t); 19640516Swpaul 19792739Salfredstatic u_int8_t rl_calchash (caddr_t); 19892739Salfredstatic void rl_setmulti (struct rl_softc *); 19992739Salfredstatic void rl_reset (struct rl_softc *); 20092739Salfredstatic int rl_list_tx_init (struct rl_softc *); 20140516Swpaul 20292739Salfredstatic void rl_dma_map_rxbuf (void *, bus_dma_segment_t *, int, int); 20392739Salfredstatic void rl_dma_map_txbuf (void *, bus_dma_segment_t *, int, int); 20481713Swpaul 20550703Swpaul#ifdef RL_USEIOSPACE 20650703Swpaul#define RL_RES SYS_RES_IOPORT 20750703Swpaul#define RL_RID RL_PCI_LOIO 20850703Swpaul#else 20950703Swpaul#define RL_RES SYS_RES_MEMORY 21050703Swpaul#define RL_RID RL_PCI_LOMEM 21150703Swpaul#endif 21250703Swpaul 21350703Swpaulstatic device_method_t rl_methods[] = { 21450703Swpaul /* Device interface */ 21550703Swpaul DEVMETHOD(device_probe, rl_probe), 21650703Swpaul DEVMETHOD(device_attach, rl_attach), 21750703Swpaul DEVMETHOD(device_detach, rl_detach), 21886822Siwasaki DEVMETHOD(device_suspend, rl_suspend), 21986822Siwasaki DEVMETHOD(device_resume, rl_resume), 22050703Swpaul DEVMETHOD(device_shutdown, rl_shutdown), 22150703Swpaul 22250703Swpaul /* bus interface */ 22350703Swpaul DEVMETHOD(bus_print_child, bus_generic_print_child), 22450703Swpaul DEVMETHOD(bus_driver_added, bus_generic_driver_added), 22550703Swpaul 22650703Swpaul /* MII interface */ 22750703Swpaul DEVMETHOD(miibus_readreg, rl_miibus_readreg), 22850703Swpaul DEVMETHOD(miibus_writereg, rl_miibus_writereg), 22950703Swpaul DEVMETHOD(miibus_statchg, rl_miibus_statchg), 23050703Swpaul 23150703Swpaul { 0, 0 } 23250703Swpaul}; 23350703Swpaul 23450703Swpaulstatic driver_t rl_driver = { 23551455Swpaul "rl", 23650703Swpaul rl_methods, 23750703Swpaul sizeof(struct rl_softc) 23850703Swpaul}; 23950703Swpaul 24050703Swpaulstatic devclass_t rl_devclass; 24150703Swpaul 24251533SwpaulDRIVER_MODULE(if_rl, pci, rl_driver, rl_devclass, 0, 0); 24367931SwpaulDRIVER_MODULE(if_rl, cardbus, rl_driver, rl_devclass, 0, 0); 24451473SwpaulDRIVER_MODULE(miibus, rl, miibus_driver, miibus_devclass, 0, 0); 24550703Swpaul 24640516Swpaul#define EE_SET(x) \ 24740516Swpaul CSR_WRITE_1(sc, RL_EECMD, \ 24840516Swpaul CSR_READ_1(sc, RL_EECMD) | x) 24940516Swpaul 25040516Swpaul#define EE_CLR(x) \ 25140516Swpaul CSR_WRITE_1(sc, RL_EECMD, \ 25240516Swpaul CSR_READ_1(sc, RL_EECMD) & ~x) 25340516Swpaul 25481713Swpaulstatic void 25581713Swpaulrl_dma_map_rxbuf(arg, segs, nseg, error) 25681713Swpaul void *arg; 25781713Swpaul bus_dma_segment_t *segs; 25881713Swpaul int nseg, error; 25981713Swpaul{ 26081713Swpaul struct rl_softc *sc; 26181713Swpaul 26281713Swpaul sc = arg; 26381713Swpaul CSR_WRITE_4(sc, RL_RXADDR, segs->ds_addr & 0xFFFFFFFF); 26481713Swpaul 26581713Swpaul return; 26681713Swpaul} 26781713Swpaul 26881713Swpaulstatic void 26981713Swpaulrl_dma_map_txbuf(arg, segs, nseg, error) 27081713Swpaul void *arg; 27181713Swpaul bus_dma_segment_t *segs; 27281713Swpaul int nseg, error; 27381713Swpaul{ 27481713Swpaul struct rl_softc *sc; 27581713Swpaul 27681713Swpaul sc = arg; 27781713Swpaul CSR_WRITE_4(sc, RL_CUR_TXADDR(sc), segs->ds_addr & 0xFFFFFFFF); 27881713Swpaul 27981713Swpaul return; 28081713Swpaul} 28181713Swpaul 28240516Swpaul/* 28340516Swpaul * Send a read command and address to the EEPROM, check for ACK. 28440516Swpaul */ 285102335Salfredstatic void 286102335Salfredrl_eeprom_putbyte(sc, addr) 28740516Swpaul struct rl_softc *sc; 28841656Swpaul int addr; 28940516Swpaul{ 29040516Swpaul register int d, i; 29140516Swpaul 29267931Swpaul d = addr | sc->rl_eecmd_read; 29340516Swpaul 29440516Swpaul /* 29555170Sbillf * Feed in each bit and strobe the clock. 29640516Swpaul */ 29740516Swpaul for (i = 0x400; i; i >>= 1) { 29840516Swpaul if (d & i) { 29940516Swpaul EE_SET(RL_EE_DATAIN); 30040516Swpaul } else { 30140516Swpaul EE_CLR(RL_EE_DATAIN); 30240516Swpaul } 30340516Swpaul DELAY(100); 30440516Swpaul EE_SET(RL_EE_CLK); 30540516Swpaul DELAY(150); 30640516Swpaul EE_CLR(RL_EE_CLK); 30740516Swpaul DELAY(100); 30840516Swpaul } 30940516Swpaul 31040516Swpaul return; 31140516Swpaul} 31240516Swpaul 31340516Swpaul/* 31440516Swpaul * Read a word of data stored in the EEPROM at address 'addr.' 31540516Swpaul */ 316102335Salfredstatic void 317102335Salfredrl_eeprom_getword(sc, addr, dest) 31840516Swpaul struct rl_softc *sc; 31941656Swpaul int addr; 32040516Swpaul u_int16_t *dest; 32140516Swpaul{ 32240516Swpaul register int i; 32340516Swpaul u_int16_t word = 0; 32440516Swpaul 32540516Swpaul /* Enter EEPROM access mode. */ 32640516Swpaul CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL); 32740516Swpaul 32840516Swpaul /* 32940516Swpaul * Send address of word we want to read. 33040516Swpaul */ 33140516Swpaul rl_eeprom_putbyte(sc, addr); 33240516Swpaul 33340516Swpaul CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL); 33440516Swpaul 33540516Swpaul /* 33640516Swpaul * Start reading bits from EEPROM. 33740516Swpaul */ 33840516Swpaul for (i = 0x8000; i; i >>= 1) { 33940516Swpaul EE_SET(RL_EE_CLK); 34040516Swpaul DELAY(100); 34140516Swpaul if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT) 34240516Swpaul word |= i; 34340516Swpaul EE_CLR(RL_EE_CLK); 34440516Swpaul DELAY(100); 34540516Swpaul } 34640516Swpaul 34740516Swpaul /* Turn off EEPROM access mode. */ 34840516Swpaul CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 34940516Swpaul 35040516Swpaul *dest = word; 35140516Swpaul 35240516Swpaul return; 35340516Swpaul} 35440516Swpaul 35540516Swpaul/* 35640516Swpaul * Read a sequence of words from the EEPROM. 35740516Swpaul */ 358102335Salfredstatic void 359102335Salfredrl_read_eeprom(sc, dest, off, cnt, swap) 36040516Swpaul struct rl_softc *sc; 36140516Swpaul caddr_t dest; 36240516Swpaul int off; 36340516Swpaul int cnt; 36440516Swpaul int swap; 36540516Swpaul{ 36640516Swpaul int i; 36740516Swpaul u_int16_t word = 0, *ptr; 36840516Swpaul 36940516Swpaul for (i = 0; i < cnt; i++) { 37040516Swpaul rl_eeprom_getword(sc, off + i, &word); 37140516Swpaul ptr = (u_int16_t *)(dest + (i * 2)); 37240516Swpaul if (swap) 37340516Swpaul *ptr = ntohs(word); 37440516Swpaul else 37540516Swpaul *ptr = word; 37640516Swpaul } 37740516Swpaul 37840516Swpaul return; 37940516Swpaul} 38040516Swpaul 38140516Swpaul 38240516Swpaul/* 38340516Swpaul * MII access routines are provided for the 8129, which 38440516Swpaul * doesn't have a built-in PHY. For the 8139, we fake things 38540516Swpaul * up by diverting rl_phy_readreg()/rl_phy_writereg() to the 38640516Swpaul * direct access PHY registers. 38740516Swpaul */ 38840516Swpaul#define MII_SET(x) \ 38940516Swpaul CSR_WRITE_1(sc, RL_MII, \ 390105221Sphk CSR_READ_1(sc, RL_MII) | (x)) 39140516Swpaul 39240516Swpaul#define MII_CLR(x) \ 39340516Swpaul CSR_WRITE_1(sc, RL_MII, \ 394105221Sphk CSR_READ_1(sc, RL_MII) & ~(x)) 39540516Swpaul 39640516Swpaul/* 39740516Swpaul * Sync the PHYs by setting data bit and strobing the clock 32 times. 39840516Swpaul */ 399102335Salfredstatic void 400102335Salfredrl_mii_sync(sc) 40140516Swpaul struct rl_softc *sc; 40240516Swpaul{ 40340516Swpaul register int i; 40440516Swpaul 40540516Swpaul MII_SET(RL_MII_DIR|RL_MII_DATAOUT); 40640516Swpaul 40740516Swpaul for (i = 0; i < 32; i++) { 40840516Swpaul MII_SET(RL_MII_CLK); 40940516Swpaul DELAY(1); 41040516Swpaul MII_CLR(RL_MII_CLK); 41140516Swpaul DELAY(1); 41240516Swpaul } 41340516Swpaul 41440516Swpaul return; 41540516Swpaul} 41640516Swpaul 41740516Swpaul/* 41840516Swpaul * Clock a series of bits through the MII. 41940516Swpaul */ 420102335Salfredstatic void 421102335Salfredrl_mii_send(sc, bits, cnt) 42240516Swpaul struct rl_softc *sc; 42340516Swpaul u_int32_t bits; 42440516Swpaul int cnt; 42540516Swpaul{ 42640516Swpaul int i; 42740516Swpaul 42840516Swpaul MII_CLR(RL_MII_CLK); 42940516Swpaul 43040516Swpaul for (i = (0x1 << (cnt - 1)); i; i >>= 1) { 43140516Swpaul if (bits & i) { 43240516Swpaul MII_SET(RL_MII_DATAOUT); 43340516Swpaul } else { 43440516Swpaul MII_CLR(RL_MII_DATAOUT); 43540516Swpaul } 43640516Swpaul DELAY(1); 43740516Swpaul MII_CLR(RL_MII_CLK); 43840516Swpaul DELAY(1); 43940516Swpaul MII_SET(RL_MII_CLK); 44040516Swpaul } 44140516Swpaul} 44240516Swpaul 44340516Swpaul/* 44440516Swpaul * Read an PHY register through the MII. 44540516Swpaul */ 446102335Salfredstatic int 447102335Salfredrl_mii_readreg(sc, frame) 44840516Swpaul struct rl_softc *sc; 44940516Swpaul struct rl_mii_frame *frame; 45040516Swpaul 45140516Swpaul{ 45267087Swpaul int i, ack; 45340516Swpaul 45467087Swpaul RL_LOCK(sc); 45540516Swpaul 45640516Swpaul /* 45740516Swpaul * Set up frame for RX. 45840516Swpaul */ 45940516Swpaul frame->mii_stdelim = RL_MII_STARTDELIM; 46040516Swpaul frame->mii_opcode = RL_MII_READOP; 46140516Swpaul frame->mii_turnaround = 0; 46240516Swpaul frame->mii_data = 0; 46340516Swpaul 46440516Swpaul CSR_WRITE_2(sc, RL_MII, 0); 46540516Swpaul 46640516Swpaul /* 46740516Swpaul * Turn on data xmit. 46840516Swpaul */ 46940516Swpaul MII_SET(RL_MII_DIR); 47040516Swpaul 47140516Swpaul rl_mii_sync(sc); 47240516Swpaul 47340516Swpaul /* 47440516Swpaul * Send command/address info. 47540516Swpaul */ 47640516Swpaul rl_mii_send(sc, frame->mii_stdelim, 2); 47740516Swpaul rl_mii_send(sc, frame->mii_opcode, 2); 47840516Swpaul rl_mii_send(sc, frame->mii_phyaddr, 5); 47940516Swpaul rl_mii_send(sc, frame->mii_regaddr, 5); 48040516Swpaul 48140516Swpaul /* Idle bit */ 48240516Swpaul MII_CLR((RL_MII_CLK|RL_MII_DATAOUT)); 48340516Swpaul DELAY(1); 48440516Swpaul MII_SET(RL_MII_CLK); 48540516Swpaul DELAY(1); 48640516Swpaul 48740516Swpaul /* Turn off xmit. */ 48840516Swpaul MII_CLR(RL_MII_DIR); 48940516Swpaul 49040516Swpaul /* Check for ack */ 49140516Swpaul MII_CLR(RL_MII_CLK); 49240516Swpaul DELAY(1); 49340516Swpaul MII_SET(RL_MII_CLK); 49440516Swpaul DELAY(1); 49540516Swpaul ack = CSR_READ_2(sc, RL_MII) & RL_MII_DATAIN; 49640516Swpaul 49740516Swpaul /* 49840516Swpaul * Now try reading data bits. If the ack failed, we still 49940516Swpaul * need to clock through 16 cycles to keep the PHY(s) in sync. 50040516Swpaul */ 50140516Swpaul if (ack) { 50240516Swpaul for(i = 0; i < 16; i++) { 50340516Swpaul MII_CLR(RL_MII_CLK); 50440516Swpaul DELAY(1); 50540516Swpaul MII_SET(RL_MII_CLK); 50640516Swpaul DELAY(1); 50740516Swpaul } 50840516Swpaul goto fail; 50940516Swpaul } 51040516Swpaul 51140516Swpaul for (i = 0x8000; i; i >>= 1) { 51240516Swpaul MII_CLR(RL_MII_CLK); 51340516Swpaul DELAY(1); 51440516Swpaul if (!ack) { 51540516Swpaul if (CSR_READ_2(sc, RL_MII) & RL_MII_DATAIN) 51640516Swpaul frame->mii_data |= i; 51740516Swpaul DELAY(1); 51840516Swpaul } 51940516Swpaul MII_SET(RL_MII_CLK); 52040516Swpaul DELAY(1); 52140516Swpaul } 52240516Swpaul 52340516Swpaulfail: 52440516Swpaul 52540516Swpaul MII_CLR(RL_MII_CLK); 52640516Swpaul DELAY(1); 52740516Swpaul MII_SET(RL_MII_CLK); 52840516Swpaul DELAY(1); 52940516Swpaul 53067087Swpaul RL_UNLOCK(sc); 53140516Swpaul 53240516Swpaul if (ack) 53340516Swpaul return(1); 53440516Swpaul return(0); 53540516Swpaul} 53640516Swpaul 53740516Swpaul/* 53840516Swpaul * Write to a PHY register through the MII. 53940516Swpaul */ 540102335Salfredstatic int 541102335Salfredrl_mii_writereg(sc, frame) 54240516Swpaul struct rl_softc *sc; 54340516Swpaul struct rl_mii_frame *frame; 54440516Swpaul 54540516Swpaul{ 54667087Swpaul RL_LOCK(sc); 54740516Swpaul 54840516Swpaul /* 54940516Swpaul * Set up frame for TX. 55040516Swpaul */ 55140516Swpaul 55240516Swpaul frame->mii_stdelim = RL_MII_STARTDELIM; 55340516Swpaul frame->mii_opcode = RL_MII_WRITEOP; 55440516Swpaul frame->mii_turnaround = RL_MII_TURNAROUND; 55540516Swpaul 55640516Swpaul /* 55740516Swpaul * Turn on data output. 55840516Swpaul */ 55940516Swpaul MII_SET(RL_MII_DIR); 56040516Swpaul 56140516Swpaul rl_mii_sync(sc); 56240516Swpaul 56340516Swpaul rl_mii_send(sc, frame->mii_stdelim, 2); 56440516Swpaul rl_mii_send(sc, frame->mii_opcode, 2); 56540516Swpaul rl_mii_send(sc, frame->mii_phyaddr, 5); 56640516Swpaul rl_mii_send(sc, frame->mii_regaddr, 5); 56740516Swpaul rl_mii_send(sc, frame->mii_turnaround, 2); 56840516Swpaul rl_mii_send(sc, frame->mii_data, 16); 56940516Swpaul 57040516Swpaul /* Idle bit. */ 57140516Swpaul MII_SET(RL_MII_CLK); 57240516Swpaul DELAY(1); 57340516Swpaul MII_CLR(RL_MII_CLK); 57440516Swpaul DELAY(1); 57540516Swpaul 57640516Swpaul /* 57740516Swpaul * Turn off xmit. 57840516Swpaul */ 57940516Swpaul MII_CLR(RL_MII_DIR); 58040516Swpaul 58167087Swpaul RL_UNLOCK(sc); 58240516Swpaul 58340516Swpaul return(0); 58440516Swpaul} 58540516Swpaul 586102335Salfredstatic int 587102335Salfredrl_miibus_readreg(dev, phy, reg) 58850703Swpaul device_t dev; 58950703Swpaul int phy, reg; 59050703Swpaul{ 59140516Swpaul struct rl_softc *sc; 59240516Swpaul struct rl_mii_frame frame; 59340516Swpaul u_int16_t rval = 0; 59440516Swpaul u_int16_t rl8139_reg = 0; 59540516Swpaul 59650703Swpaul sc = device_get_softc(dev); 59767087Swpaul RL_LOCK(sc); 59850703Swpaul 59940516Swpaul if (sc->rl_type == RL_8139) { 60050703Swpaul /* Pretend the internal PHY is only at address 0 */ 60167087Swpaul if (phy) { 60267087Swpaul RL_UNLOCK(sc); 60350703Swpaul return(0); 60467087Swpaul } 60540516Swpaul switch(reg) { 60650703Swpaul case MII_BMCR: 60740516Swpaul rl8139_reg = RL_BMCR; 60840516Swpaul break; 60950703Swpaul case MII_BMSR: 61040516Swpaul rl8139_reg = RL_BMSR; 61140516Swpaul break; 61250703Swpaul case MII_ANAR: 61340516Swpaul rl8139_reg = RL_ANAR; 61440516Swpaul break; 61550703Swpaul case MII_ANER: 61650703Swpaul rl8139_reg = RL_ANER; 61750703Swpaul break; 61850703Swpaul case MII_ANLPAR: 61940516Swpaul rl8139_reg = RL_LPAR; 62040516Swpaul break; 62150703Swpaul case MII_PHYIDR1: 62250703Swpaul case MII_PHYIDR2: 62367087Swpaul RL_UNLOCK(sc); 62450703Swpaul return(0); 62550703Swpaul break; 62694149Swpaul /* 62794149Swpaul * Allow the rlphy driver to read the media status 62894149Swpaul * register. If we have a link partner which does not 62994149Swpaul * support NWAY, this is the register which will tell 63094149Swpaul * us the results of parallel detection. 63194149Swpaul */ 63294149Swpaul case RL_MEDIASTAT: 63394149Swpaul rval = CSR_READ_1(sc, RL_MEDIASTAT); 63494149Swpaul RL_UNLOCK(sc); 63594149Swpaul return(rval); 63694149Swpaul break; 63740516Swpaul default: 63840516Swpaul printf("rl%d: bad phy register\n", sc->rl_unit); 63967087Swpaul RL_UNLOCK(sc); 64040516Swpaul return(0); 64140516Swpaul } 64240516Swpaul rval = CSR_READ_2(sc, rl8139_reg); 64367087Swpaul RL_UNLOCK(sc); 64440516Swpaul return(rval); 64540516Swpaul } 64640516Swpaul 64740516Swpaul bzero((char *)&frame, sizeof(frame)); 64840516Swpaul 64950703Swpaul frame.mii_phyaddr = phy; 65040516Swpaul frame.mii_regaddr = reg; 65140516Swpaul rl_mii_readreg(sc, &frame); 65267087Swpaul RL_UNLOCK(sc); 65340516Swpaul 65440516Swpaul return(frame.mii_data); 65540516Swpaul} 65640516Swpaul 657102335Salfredstatic int 658102335Salfredrl_miibus_writereg(dev, phy, reg, data) 65950703Swpaul device_t dev; 66050703Swpaul int phy, reg, data; 66150703Swpaul{ 66240516Swpaul struct rl_softc *sc; 66340516Swpaul struct rl_mii_frame frame; 66440516Swpaul u_int16_t rl8139_reg = 0; 66540516Swpaul 66650703Swpaul sc = device_get_softc(dev); 66767087Swpaul RL_LOCK(sc); 66850703Swpaul 66940516Swpaul if (sc->rl_type == RL_8139) { 67050703Swpaul /* Pretend the internal PHY is only at address 0 */ 67167087Swpaul if (phy) { 67267087Swpaul RL_UNLOCK(sc); 67350703Swpaul return(0); 67467087Swpaul } 67540516Swpaul switch(reg) { 67650703Swpaul case MII_BMCR: 67740516Swpaul rl8139_reg = RL_BMCR; 67840516Swpaul break; 67950703Swpaul case MII_BMSR: 68040516Swpaul rl8139_reg = RL_BMSR; 68140516Swpaul break; 68250703Swpaul case MII_ANAR: 68340516Swpaul rl8139_reg = RL_ANAR; 68440516Swpaul break; 68550703Swpaul case MII_ANER: 68650703Swpaul rl8139_reg = RL_ANER; 68750703Swpaul break; 68850703Swpaul case MII_ANLPAR: 68940516Swpaul rl8139_reg = RL_LPAR; 69040516Swpaul break; 69150703Swpaul case MII_PHYIDR1: 69250703Swpaul case MII_PHYIDR2: 69367087Swpaul RL_UNLOCK(sc); 69450703Swpaul return(0); 69550703Swpaul break; 69640516Swpaul default: 69740516Swpaul printf("rl%d: bad phy register\n", sc->rl_unit); 69867087Swpaul RL_UNLOCK(sc); 69950703Swpaul return(0); 70040516Swpaul } 70140516Swpaul CSR_WRITE_2(sc, rl8139_reg, data); 70267087Swpaul RL_UNLOCK(sc); 70350703Swpaul return(0); 70440516Swpaul } 70540516Swpaul 70640516Swpaul bzero((char *)&frame, sizeof(frame)); 70740516Swpaul 70850703Swpaul frame.mii_phyaddr = phy; 70940516Swpaul frame.mii_regaddr = reg; 71040516Swpaul frame.mii_data = data; 71140516Swpaul 71240516Swpaul rl_mii_writereg(sc, &frame); 71340516Swpaul 71467087Swpaul RL_UNLOCK(sc); 71550703Swpaul return(0); 71650703Swpaul} 71750703Swpaul 718102335Salfredstatic void 719102335Salfredrl_miibus_statchg(dev) 72050703Swpaul device_t dev; 72150703Swpaul{ 72240516Swpaul return; 72340516Swpaul} 72440516Swpaul 72540516Swpaul/* 72643062Swpaul * Calculate CRC of a multicast group address, return the upper 6 bits. 72740516Swpaul */ 728102335Salfredstatic u_int8_t 729102335Salfredrl_calchash(addr) 73041656Swpaul caddr_t addr; 73140516Swpaul{ 73240516Swpaul u_int32_t crc, carry; 73340516Swpaul int i, j; 73440516Swpaul u_int8_t c; 73540516Swpaul 73640516Swpaul /* Compute CRC for the address value. */ 73740516Swpaul crc = 0xFFFFFFFF; /* initial value */ 73840516Swpaul 73940516Swpaul for (i = 0; i < 6; i++) { 74040516Swpaul c = *(addr + i); 74140516Swpaul for (j = 0; j < 8; j++) { 74240516Swpaul carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01); 74340516Swpaul crc <<= 1; 74440516Swpaul c >>= 1; 74540516Swpaul if (carry) 74640516Swpaul crc = (crc ^ 0x04c11db6) | carry; 74740516Swpaul } 74840516Swpaul } 74940516Swpaul 75040516Swpaul /* return the filter bit position */ 75143062Swpaul return(crc >> 26); 75240516Swpaul} 75340516Swpaul 75440516Swpaul/* 75540516Swpaul * Program the 64-bit multicast hash filter. 75640516Swpaul */ 757102335Salfredstatic void 758102335Salfredrl_setmulti(sc) 75940516Swpaul struct rl_softc *sc; 76040516Swpaul{ 76140516Swpaul struct ifnet *ifp; 76240516Swpaul int h = 0; 76340516Swpaul u_int32_t hashes[2] = { 0, 0 }; 76440516Swpaul struct ifmultiaddr *ifma; 76540516Swpaul u_int32_t rxfilt; 76640516Swpaul int mcnt = 0; 76740516Swpaul 76840516Swpaul ifp = &sc->arpcom.ac_if; 76940516Swpaul 77040516Swpaul rxfilt = CSR_READ_4(sc, RL_RXCFG); 77140516Swpaul 77243062Swpaul if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 77340516Swpaul rxfilt |= RL_RXCFG_RX_MULTI; 77440516Swpaul CSR_WRITE_4(sc, RL_RXCFG, rxfilt); 77540516Swpaul CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF); 77640516Swpaul CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF); 77740516Swpaul return; 77840516Swpaul } 77940516Swpaul 78040516Swpaul /* first, zot all the existing hash bits */ 78140516Swpaul CSR_WRITE_4(sc, RL_MAR0, 0); 78240516Swpaul CSR_WRITE_4(sc, RL_MAR4, 0); 78340516Swpaul 78440516Swpaul /* now program new ones */ 78572084Sphk TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 78640516Swpaul if (ifma->ifma_addr->sa_family != AF_LINK) 78740516Swpaul continue; 78840516Swpaul h = rl_calchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr)); 78940516Swpaul if (h < 32) 79040516Swpaul hashes[0] |= (1 << h); 79140516Swpaul else 79240516Swpaul hashes[1] |= (1 << (h - 32)); 79340516Swpaul mcnt++; 79440516Swpaul } 79540516Swpaul 79640516Swpaul if (mcnt) 79740516Swpaul rxfilt |= RL_RXCFG_RX_MULTI; 79840516Swpaul else 79940516Swpaul rxfilt &= ~RL_RXCFG_RX_MULTI; 80040516Swpaul 80140516Swpaul CSR_WRITE_4(sc, RL_RXCFG, rxfilt); 80240516Swpaul CSR_WRITE_4(sc, RL_MAR0, hashes[0]); 80340516Swpaul CSR_WRITE_4(sc, RL_MAR4, hashes[1]); 80440516Swpaul 80540516Swpaul return; 80640516Swpaul} 80740516Swpaul 808102335Salfredstatic void 809102335Salfredrl_reset(sc) 81040516Swpaul struct rl_softc *sc; 81140516Swpaul{ 81240516Swpaul register int i; 81340516Swpaul 81440516Swpaul CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET); 81540516Swpaul 81640516Swpaul for (i = 0; i < RL_TIMEOUT; i++) { 81740516Swpaul DELAY(10); 81840516Swpaul if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET)) 81940516Swpaul break; 82040516Swpaul } 82140516Swpaul if (i == RL_TIMEOUT) 82240516Swpaul printf("rl%d: reset never completed!\n", sc->rl_unit); 82340516Swpaul 82440516Swpaul return; 82540516Swpaul} 82640516Swpaul 82740516Swpaul/* 82840516Swpaul * Probe for a RealTek 8129/8139 chip. Check the PCI vendor and device 82940516Swpaul * IDs against our list and return a device name if we find a match. 83040516Swpaul */ 831102335Salfredstatic int 832102335Salfredrl_probe(dev) 83350703Swpaul device_t dev; 83440516Swpaul{ 83540516Swpaul struct rl_type *t; 83640516Swpaul 83740516Swpaul t = rl_devs; 83840516Swpaul 83940516Swpaul while(t->rl_name != NULL) { 84050703Swpaul if ((pci_get_vendor(dev) == t->rl_vid) && 84150703Swpaul (pci_get_device(dev) == t->rl_did)) { 84250703Swpaul device_set_desc(dev, t->rl_name); 84350703Swpaul return(0); 84440516Swpaul } 84540516Swpaul t++; 84640516Swpaul } 84740516Swpaul 84850703Swpaul return(ENXIO); 84940516Swpaul} 85040516Swpaul 85140516Swpaul/* 85240516Swpaul * Attach the interface. Allocate softc structures, do ifmedia 85340516Swpaul * setup and ethernet/BPF attach. 85440516Swpaul */ 855102335Salfredstatic int 856102335Salfredrl_attach(dev) 85750703Swpaul device_t dev; 85840516Swpaul{ 85940516Swpaul u_char eaddr[ETHER_ADDR_LEN]; 86040516Swpaul u_int32_t command; 86140516Swpaul struct rl_softc *sc; 86240516Swpaul struct ifnet *ifp; 86340516Swpaul u_int16_t rl_did = 0; 86450703Swpaul int unit, error = 0, rid; 86540516Swpaul 86650703Swpaul sc = device_get_softc(dev); 86750703Swpaul unit = device_get_unit(dev); 86840516Swpaul bzero(sc, sizeof(struct rl_softc)); 86940516Swpaul 87093818Sjhb mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 87193818Sjhb MTX_DEF | MTX_RECURSE); 87269583Swpaul RL_LOCK(sc); 87369583Swpaul 87440516Swpaul /* 87540516Swpaul * Handle power management nonsense. 87640516Swpaul */ 87740516Swpaul 87870167Swpaul if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) { 87970167Swpaul u_int32_t iobase, membase, irq; 88040516Swpaul 88170167Swpaul /* Save important PCI config data. */ 88270167Swpaul iobase = pci_read_config(dev, RL_PCI_LOIO, 4); 88370167Swpaul membase = pci_read_config(dev, RL_PCI_LOMEM, 4); 88470167Swpaul irq = pci_read_config(dev, RL_PCI_INTLINE, 4); 88540516Swpaul 88670167Swpaul /* Reset the power state. */ 88770167Swpaul printf("rl%d: chip is is in D%d power mode " 88870167Swpaul "-- setting to D0\n", unit, 88970167Swpaul pci_get_powerstate(dev)); 89040516Swpaul 89170167Swpaul pci_set_powerstate(dev, PCI_POWERSTATE_D0); 89240516Swpaul 89370167Swpaul /* Restore PCI config data. */ 89470167Swpaul pci_write_config(dev, RL_PCI_LOIO, iobase, 4); 89570167Swpaul pci_write_config(dev, RL_PCI_LOMEM, membase, 4); 89670167Swpaul pci_write_config(dev, RL_PCI_INTLINE, irq, 4); 89740516Swpaul } 89840516Swpaul 89940516Swpaul /* 90040516Swpaul * Map control/status registers. 90140516Swpaul */ 90272813Swpaul pci_enable_busmaster(dev); 90379472Swpaul pci_enable_io(dev, SYS_RES_IOPORT); 90479472Swpaul pci_enable_io(dev, SYS_RES_MEMORY); 90561041Speter command = pci_read_config(dev, PCIR_COMMAND, 4); 90640516Swpaul 90740516Swpaul#ifdef RL_USEIOSPACE 90840516Swpaul if (!(command & PCIM_CMD_PORTEN)) { 90940516Swpaul printf("rl%d: failed to enable I/O ports!\n", unit); 91050703Swpaul error = ENXIO; 91140516Swpaul goto fail; 91240516Swpaul } 91340516Swpaul#else 91440516Swpaul if (!(command & PCIM_CMD_MEMEN)) { 91540516Swpaul printf("rl%d: failed to enable memory mapping!\n", unit); 91650703Swpaul error = ENXIO; 91740516Swpaul goto fail; 91840516Swpaul } 91950703Swpaul#endif 92040516Swpaul 92150703Swpaul rid = RL_RID; 92250703Swpaul sc->rl_res = bus_alloc_resource(dev, RL_RES, &rid, 92350703Swpaul 0, ~0, 1, RF_ACTIVE); 92450703Swpaul 92550703Swpaul if (sc->rl_res == NULL) { 92650703Swpaul printf ("rl%d: couldn't map ports/memory\n", unit); 92750703Swpaul error = ENXIO; 92840516Swpaul goto fail; 92940516Swpaul } 93040516Swpaul 93169127Sroger /* Detect the Realtek 8139B. For some reason, this chip is very 93269127Sroger * unstable when left to autoselect the media 93369127Sroger * The best workaround is to set the device to the required 93469127Sroger * media type or to set it to the 10 Meg speed. 93569127Sroger */ 93669127Sroger 93769127Sroger if ((rman_get_end(sc->rl_res)-rman_get_start(sc->rl_res))==0xff) { 93869127Sroger printf("rl%d: Realtek 8139B detected. Warning, this may be unstable in autoselect mode\n", unit); 93969127Sroger } 94069127Sroger 94150703Swpaul sc->rl_btag = rman_get_bustag(sc->rl_res); 94250703Swpaul sc->rl_bhandle = rman_get_bushandle(sc->rl_res); 94350703Swpaul 94450703Swpaul rid = 0; 94550703Swpaul sc->rl_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 94650703Swpaul RF_SHAREABLE | RF_ACTIVE); 94750703Swpaul 94850703Swpaul if (sc->rl_irq == NULL) { 94940516Swpaul printf("rl%d: couldn't map interrupt\n", unit); 95050703Swpaul bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res); 95150703Swpaul error = ENXIO; 95240516Swpaul goto fail; 95340516Swpaul } 95440516Swpaul 95550703Swpaul error = bus_setup_intr(dev, sc->rl_irq, INTR_TYPE_NET, 95650703Swpaul rl_intr, sc, &sc->rl_intrhand); 95750703Swpaul 95850703Swpaul if (error) { 95968215Swpaul bus_release_resource(dev, SYS_RES_IRQ, 0, sc->rl_irq); 96050703Swpaul bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res); 96150703Swpaul printf("rl%d: couldn't set up irq\n", unit); 96250703Swpaul goto fail; 96350703Swpaul } 96450703Swpaul 96550703Swpaul callout_handle_init(&sc->rl_stat_ch); 96650703Swpaul 96740516Swpaul /* Reset the adapter. */ 96840516Swpaul rl_reset(sc); 96967931Swpaul sc->rl_eecmd_read = RL_EECMD_READ_6BIT; 97067931Swpaul rl_read_eeprom(sc, (caddr_t)&rl_did, 0, 1, 0); 97168215Swpaul if (rl_did != 0x8129) 97267931Swpaul sc->rl_eecmd_read = RL_EECMD_READ_8BIT; 97340516Swpaul 97440516Swpaul /* 97540516Swpaul * Get station address from the EEPROM. 97640516Swpaul */ 97740516Swpaul rl_read_eeprom(sc, (caddr_t)&eaddr, RL_EE_EADDR, 3, 0); 97840516Swpaul 97940516Swpaul /* 98040516Swpaul * A RealTek chip was detected. Inform the world. 98140516Swpaul */ 98240516Swpaul printf("rl%d: Ethernet address: %6D\n", unit, eaddr, ":"); 98340516Swpaul 98440516Swpaul sc->rl_unit = unit; 98540516Swpaul bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); 98640516Swpaul 98740516Swpaul /* 98840516Swpaul * Now read the exact device type from the EEPROM to find 98940516Swpaul * out if it's an 8129 or 8139. 99040516Swpaul */ 99140516Swpaul rl_read_eeprom(sc, (caddr_t)&rl_did, RL_EE_PCI_DID, 1, 0); 99240516Swpaul 99344238Swpaul if (rl_did == RT_DEVICEID_8139 || rl_did == ACCTON_DEVICEID_5030 || 99467771Swpaul rl_did == DELTA_DEVICEID_8139 || rl_did == ADDTRON_DEVICEID_8139 || 99596112Sjhb rl_did == RT_DEVICEID_8138 || rl_did == DLINK_DEVICEID_530TXPLUS || 996103020Siwasaki rl_did == DLINK_DEVICEID_690TXD || rl_did == COREGA_DEVICEID_CBTXD) 99740516Swpaul sc->rl_type = RL_8139; 99840516Swpaul else if (rl_did == RT_DEVICEID_8129) 99940516Swpaul sc->rl_type = RL_8129; 100040516Swpaul else { 100140516Swpaul printf("rl%d: unknown device ID: %x\n", unit, rl_did); 100250703Swpaul bus_teardown_intr(dev, sc->rl_irq, sc->rl_intrhand); 100368215Swpaul bus_release_resource(dev, SYS_RES_IRQ, 0, sc->rl_irq); 100450703Swpaul bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res); 100550703Swpaul error = ENXIO; 100640516Swpaul goto fail; 100740516Swpaul } 100840516Swpaul 100981713Swpaul /* 101081713Swpaul * Allocate the parent bus DMA tag appropriate for PCI. 101181713Swpaul */ 101281713Swpaul#define RL_NSEG_NEW 32 1013104324Sphk error = bus_dma_tag_create(NULL, /* parent */ 101481713Swpaul 1, 0, /* alignment, boundary */ 101581713Swpaul BUS_SPACE_MAXADDR_32BIT,/* lowaddr */ 101681713Swpaul BUS_SPACE_MAXADDR, /* highaddr */ 101781713Swpaul NULL, NULL, /* filter, filterarg */ 101881713Swpaul MAXBSIZE, RL_NSEG_NEW, /* maxsize, nsegments */ 101981713Swpaul BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */ 102081713Swpaul BUS_DMA_ALLOCNOW, /* flags */ 102181713Swpaul &sc->rl_parent_tag); 102240516Swpaul 102381713Swpaul /* 102481713Swpaul * Now allocate a tag for the DMA descriptor lists. 102581713Swpaul * All of our lists are allocated as a contiguous block 102681713Swpaul * of memory. 102781713Swpaul */ 102881713Swpaul error = bus_dma_tag_create(sc->rl_parent_tag, /* parent */ 102981713Swpaul 1, 0, /* alignment, boundary */ 103081713Swpaul BUS_SPACE_MAXADDR, /* lowaddr */ 103181713Swpaul BUS_SPACE_MAXADDR, /* highaddr */ 103281713Swpaul NULL, NULL, /* filter, filterarg */ 103381713Swpaul RL_RXBUFLEN + 1518, 1, /* maxsize,nsegments */ 103481713Swpaul BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */ 103581713Swpaul 0, /* flags */ 103681713Swpaul &sc->rl_tag); 103781713Swpaul 103881713Swpaul /* 103981713Swpaul * Now allocate a chunk of DMA-able memory based on the 104081713Swpaul * tag we just created. 104181713Swpaul */ 104281713Swpaul error = bus_dmamem_alloc(sc->rl_tag, 104381713Swpaul (void **)&sc->rl_cdata.rl_rx_buf, BUS_DMA_NOWAIT, 104481713Swpaul &sc->rl_cdata.rl_rx_dmamap); 104581713Swpaul 104640516Swpaul if (sc->rl_cdata.rl_rx_buf == NULL) { 104740516Swpaul printf("rl%d: no memory for list buffers!\n", unit); 104850703Swpaul bus_teardown_intr(dev, sc->rl_irq, sc->rl_intrhand); 104968215Swpaul bus_release_resource(dev, SYS_RES_IRQ, 0, sc->rl_irq); 105050703Swpaul bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res); 105181713Swpaul bus_dma_tag_destroy(sc->rl_tag); 105250703Swpaul error = ENXIO; 105340516Swpaul goto fail; 105440516Swpaul } 105540516Swpaul 105648028Swpaul /* Leave a few bytes before the start of the RX ring buffer. */ 105748028Swpaul sc->rl_cdata.rl_rx_buf_ptr = sc->rl_cdata.rl_rx_buf; 105848028Swpaul sc->rl_cdata.rl_rx_buf += sizeof(u_int64_t); 105948028Swpaul 106050703Swpaul /* Do MII setup */ 106150703Swpaul if (mii_phy_probe(dev, &sc->rl_miibus, 106250703Swpaul rl_ifmedia_upd, rl_ifmedia_sts)) { 106350703Swpaul printf("rl%d: MII without any phy!\n", sc->rl_unit); 106450703Swpaul bus_teardown_intr(dev, sc->rl_irq, sc->rl_intrhand); 106568215Swpaul bus_release_resource(dev, SYS_RES_IRQ, 0, sc->rl_irq); 106650703Swpaul bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res); 106781713Swpaul bus_dmamem_free(sc->rl_tag, 106881713Swpaul sc->rl_cdata.rl_rx_buf, sc->rl_cdata.rl_rx_dmamap); 106981713Swpaul bus_dma_tag_destroy(sc->rl_tag); 107050703Swpaul error = ENXIO; 107150703Swpaul goto fail; 107250703Swpaul } 107350703Swpaul 107440516Swpaul ifp = &sc->arpcom.ac_if; 107540516Swpaul ifp->if_softc = sc; 107640516Swpaul ifp->if_unit = unit; 107740516Swpaul ifp->if_name = "rl"; 107840516Swpaul ifp->if_mtu = ETHERMTU; 107940516Swpaul ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 108040516Swpaul ifp->if_ioctl = rl_ioctl; 108140516Swpaul ifp->if_output = ether_output; 108240516Swpaul ifp->if_start = rl_start; 108340516Swpaul ifp->if_watchdog = rl_watchdog; 108440516Swpaul ifp->if_init = rl_init; 108540516Swpaul ifp->if_baudrate = 10000000; 108645633Swpaul ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 108740516Swpaul 108840516Swpaul /* 108963090Sarchie * Call MI attach routine. 109040516Swpaul */ 109163090Sarchie ether_ifattach(ifp, ETHER_BPF_SUPPORTED); 109267087Swpaul RL_UNLOCK(sc); 109367087Swpaul return(0); 109440516Swpaul 109540516Swpaulfail: 109667087Swpaul RL_UNLOCK(sc); 109767087Swpaul mtx_destroy(&sc->rl_mtx); 109850703Swpaul return(error); 109940516Swpaul} 110040516Swpaul 1101102335Salfredstatic int 1102102335Salfredrl_detach(dev) 110350703Swpaul device_t dev; 110450703Swpaul{ 110550703Swpaul struct rl_softc *sc; 110650703Swpaul struct ifnet *ifp; 110750703Swpaul 110850703Swpaul sc = device_get_softc(dev); 110967087Swpaul RL_LOCK(sc); 111050703Swpaul ifp = &sc->arpcom.ac_if; 111150703Swpaul 111263090Sarchie ether_ifdetach(ifp, ETHER_BPF_SUPPORTED); 111350703Swpaul rl_stop(sc); 111450703Swpaul 111550703Swpaul bus_generic_detach(dev); 111650703Swpaul device_delete_child(dev, sc->rl_miibus); 111750703Swpaul 111850703Swpaul bus_teardown_intr(dev, sc->rl_irq, sc->rl_intrhand); 111968215Swpaul bus_release_resource(dev, SYS_RES_IRQ, 0, sc->rl_irq); 112050703Swpaul bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res); 112150703Swpaul 112281713Swpaul bus_dmamap_unload(sc->rl_tag, sc->rl_cdata.rl_rx_dmamap); 112381713Swpaul bus_dmamem_free(sc->rl_tag, sc->rl_cdata.rl_rx_buf, 112481713Swpaul sc->rl_cdata.rl_rx_dmamap); 112581713Swpaul bus_dma_tag_destroy(sc->rl_tag); 112681713Swpaul bus_dma_tag_destroy(sc->rl_parent_tag); 112750703Swpaul 112867087Swpaul RL_UNLOCK(sc); 112967087Swpaul mtx_destroy(&sc->rl_mtx); 113050703Swpaul 113150703Swpaul return(0); 113250703Swpaul} 113350703Swpaul 113440516Swpaul/* 113540516Swpaul * Initialize the transmit descriptors. 113640516Swpaul */ 1137102335Salfredstatic int 1138102335Salfredrl_list_tx_init(sc) 113940516Swpaul struct rl_softc *sc; 114040516Swpaul{ 114140516Swpaul struct rl_chain_data *cd; 114240516Swpaul int i; 114340516Swpaul 114440516Swpaul cd = &sc->rl_cdata; 114540516Swpaul for (i = 0; i < RL_TX_LIST_CNT; i++) { 114645633Swpaul cd->rl_tx_chain[i] = NULL; 114748028Swpaul CSR_WRITE_4(sc, 114848028Swpaul RL_TXADDR0 + (i * sizeof(u_int32_t)), 0x0000000); 114940516Swpaul } 115040516Swpaul 115145633Swpaul sc->rl_cdata.cur_tx = 0; 115245633Swpaul sc->rl_cdata.last_tx = 0; 115340516Swpaul 115440516Swpaul return(0); 115540516Swpaul} 115640516Swpaul 115740516Swpaul/* 115840516Swpaul * A frame has been uploaded: pass the resulting mbuf chain up to 115940516Swpaul * the higher level protocols. 116040516Swpaul * 116140516Swpaul * You know there's something wrong with a PCI bus-master chip design 116240516Swpaul * when you have to use m_devget(). 116340516Swpaul * 116440516Swpaul * The receive operation is badly documented in the datasheet, so I'll 116540516Swpaul * attempt to document it here. The driver provides a buffer area and 116640516Swpaul * places its base address in the RX buffer start address register. 116740516Swpaul * The chip then begins copying frames into the RX buffer. Each frame 116872645Sasmodai * is preceded by a 32-bit RX status word which specifies the length 116940516Swpaul * of the frame and certain other status bits. Each frame (starting with 117040516Swpaul * the status word) is also 32-bit aligned. The frame length is in the 117140516Swpaul * first 16 bits of the status word; the lower 15 bits correspond with 117240516Swpaul * the 'rx status register' mentioned in the datasheet. 117348028Swpaul * 117448028Swpaul * Note: to make the Alpha happy, the frame payload needs to be aligned 117578508Sbmilekic * on a 32-bit boundary. To achieve this, we pass RL_ETHER_ALIGN (2 bytes) 117678508Sbmilekic * as the offset argument to m_devget(). 117740516Swpaul */ 1178102335Salfredstatic void 1179102335Salfredrl_rxeof(sc) 118040516Swpaul struct rl_softc *sc; 118140516Swpaul{ 118240516Swpaul struct ether_header *eh; 118340516Swpaul struct mbuf *m; 118440516Swpaul struct ifnet *ifp; 118540516Swpaul int total_len = 0; 118640516Swpaul u_int32_t rxstat; 118740516Swpaul caddr_t rxbufpos; 118840516Swpaul int wrap = 0; 118940516Swpaul u_int16_t cur_rx; 119040516Swpaul u_int16_t limit; 119140516Swpaul u_int16_t rx_bytes = 0, max_bytes; 119240516Swpaul 119340516Swpaul ifp = &sc->arpcom.ac_if; 119440516Swpaul 119581713Swpaul bus_dmamap_sync(sc->rl_tag, sc->rl_cdata.rl_rx_dmamap, 119681713Swpaul BUS_DMASYNC_POSTWRITE); 119781713Swpaul 119840516Swpaul cur_rx = (CSR_READ_2(sc, RL_CURRXADDR) + 16) % RL_RXBUFLEN; 119940516Swpaul 120040516Swpaul /* Do not try to read past this point. */ 120140516Swpaul limit = CSR_READ_2(sc, RL_CURRXBUF) % RL_RXBUFLEN; 120240516Swpaul 120340516Swpaul if (limit < cur_rx) 120440516Swpaul max_bytes = (RL_RXBUFLEN - cur_rx) + limit; 120540516Swpaul else 120640516Swpaul max_bytes = limit - cur_rx; 120740516Swpaul 120842738Swpaul while((CSR_READ_1(sc, RL_COMMAND) & RL_CMD_EMPTY_RXBUF) == 0) { 120994883Sluigi#ifdef DEVICE_POLLING 1210102052Ssobomax if (ifp->if_flags & IFF_POLLING) { 121194883Sluigi if (sc->rxcycles <= 0) 121294883Sluigi break; 121394883Sluigi sc->rxcycles--; 121494883Sluigi } 121594883Sluigi#endif /* DEVICE_POLLING */ 121640516Swpaul rxbufpos = sc->rl_cdata.rl_rx_buf + cur_rx; 121740516Swpaul rxstat = *(u_int32_t *)rxbufpos; 121840516Swpaul 121940516Swpaul /* 122040516Swpaul * Here's a totally undocumented fact for you. When the 122140516Swpaul * RealTek chip is in the process of copying a packet into 122240516Swpaul * RAM for you, the length will be 0xfff0. If you spot a 122340516Swpaul * packet header with this value, you need to stop. The 122440516Swpaul * datasheet makes absolutely no mention of this and 122540516Swpaul * RealTek should be shot for this. 122640516Swpaul */ 122740516Swpaul if ((u_int16_t)(rxstat >> 16) == RL_RXSTAT_UNFINISHED) 122840516Swpaul break; 122940516Swpaul 123040516Swpaul if (!(rxstat & RL_RXSTAT_RXOK)) { 123140516Swpaul ifp->if_ierrors++; 123250703Swpaul rl_init(sc); 123350703Swpaul return; 123440516Swpaul } 123540516Swpaul 123640516Swpaul /* No errors; receive the packet. */ 123740516Swpaul total_len = rxstat >> 16; 123840516Swpaul rx_bytes += total_len + 4; 123940516Swpaul 124040516Swpaul /* 124142051Swpaul * XXX The RealTek chip includes the CRC with every 124242051Swpaul * received frame, and there's no way to turn this 124342051Swpaul * behavior off (at least, I can't find anything in 124442051Swpaul * the manual that explains how to do it) so we have 124542051Swpaul * to trim off the CRC manually. 124642051Swpaul */ 124742051Swpaul total_len -= ETHER_CRC_LEN; 124842051Swpaul 124942051Swpaul /* 125040516Swpaul * Avoid trying to read more bytes than we know 125140516Swpaul * the chip has prepared for us. 125240516Swpaul */ 125340516Swpaul if (rx_bytes > max_bytes) 125440516Swpaul break; 125540516Swpaul 125640516Swpaul rxbufpos = sc->rl_cdata.rl_rx_buf + 125740516Swpaul ((cur_rx + sizeof(u_int32_t)) % RL_RXBUFLEN); 125840516Swpaul 125940516Swpaul if (rxbufpos == (sc->rl_cdata.rl_rx_buf + RL_RXBUFLEN)) 126040516Swpaul rxbufpos = sc->rl_cdata.rl_rx_buf; 126140516Swpaul 126240516Swpaul wrap = (sc->rl_cdata.rl_rx_buf + RL_RXBUFLEN) - rxbufpos; 126340516Swpaul 126440516Swpaul if (total_len > wrap) { 126578508Sbmilekic m = m_devget(rxbufpos, total_len, RL_ETHER_ALIGN, ifp, 126678508Sbmilekic NULL); 126740516Swpaul if (m == NULL) { 126840516Swpaul ifp->if_ierrors++; 126952426Swpaul } else { 127040516Swpaul m_copyback(m, wrap, total_len - wrap, 127140516Swpaul sc->rl_cdata.rl_rx_buf); 127248028Swpaul } 127342051Swpaul cur_rx = (total_len - wrap + ETHER_CRC_LEN); 127440516Swpaul } else { 127578508Sbmilekic m = m_devget(rxbufpos, total_len, RL_ETHER_ALIGN, ifp, 127678508Sbmilekic NULL); 127740516Swpaul if (m == NULL) { 127840516Swpaul ifp->if_ierrors++; 127978508Sbmilekic } 128042051Swpaul cur_rx += total_len + 4 + ETHER_CRC_LEN; 128140516Swpaul } 128240516Swpaul 128340516Swpaul /* 128440516Swpaul * Round up to 32-bit boundary. 128540516Swpaul */ 128640516Swpaul cur_rx = (cur_rx + 3) & ~3; 128740516Swpaul CSR_WRITE_2(sc, RL_CURRXADDR, cur_rx - 16); 128840516Swpaul 128940516Swpaul if (m == NULL) 129040516Swpaul continue; 129140516Swpaul 129240516Swpaul eh = mtod(m, struct ether_header *); 129340516Swpaul ifp->if_ipackets++; 129440516Swpaul 129540516Swpaul /* Remove header from mbuf and pass it on. */ 129640516Swpaul m_adj(m, sizeof(struct ether_header)); 129740516Swpaul ether_input(ifp, eh, m); 129840516Swpaul } 129940516Swpaul 130040516Swpaul return; 130140516Swpaul} 130240516Swpaul 130340516Swpaul/* 130440516Swpaul * A frame was downloaded to the chip. It's safe for us to clean up 130540516Swpaul * the list buffers. 130640516Swpaul */ 1307102335Salfredstatic void 1308102335Salfredrl_txeof(sc) 130940516Swpaul struct rl_softc *sc; 131040516Swpaul{ 131140516Swpaul struct ifnet *ifp; 131240516Swpaul u_int32_t txstat; 131340516Swpaul 131440516Swpaul ifp = &sc->arpcom.ac_if; 131540516Swpaul 131640516Swpaul /* 131740516Swpaul * Go through our tx list and free mbufs for those 131840516Swpaul * frames that have been uploaded. 131940516Swpaul */ 132045633Swpaul do { 132145633Swpaul txstat = CSR_READ_4(sc, RL_LAST_TXSTAT(sc)); 132245633Swpaul if (!(txstat & (RL_TXSTAT_TX_OK| 132345633Swpaul RL_TXSTAT_TX_UNDERRUN|RL_TXSTAT_TXABRT))) 132440516Swpaul break; 132540516Swpaul 132645633Swpaul ifp->if_collisions += (txstat & RL_TXSTAT_COLLCNT) >> 24; 132740516Swpaul 132845633Swpaul if (RL_LAST_TXMBUF(sc) != NULL) { 132981713Swpaul bus_dmamap_unload(sc->rl_tag, RL_LAST_DMAMAP(sc)); 133081713Swpaul bus_dmamap_destroy(sc->rl_tag, RL_LAST_DMAMAP(sc)); 133145633Swpaul m_freem(RL_LAST_TXMBUF(sc)); 133245633Swpaul RL_LAST_TXMBUF(sc) = NULL; 133345633Swpaul } 133445633Swpaul if (txstat & RL_TXSTAT_TX_OK) 133545633Swpaul ifp->if_opackets++; 133645633Swpaul else { 133752426Swpaul int oldthresh; 133845633Swpaul ifp->if_oerrors++; 133945633Swpaul if ((txstat & RL_TXSTAT_TXABRT) || 134045633Swpaul (txstat & RL_TXSTAT_OUTOFWIN)) 134145633Swpaul CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG); 134252426Swpaul oldthresh = sc->rl_txthresh; 134352426Swpaul /* error recovery */ 134452426Swpaul rl_reset(sc); 134552426Swpaul rl_init(sc); 134652426Swpaul /* 134752426Swpaul * If there was a transmit underrun, 134852426Swpaul * bump the TX threshold. 134952426Swpaul */ 135052426Swpaul if (txstat & RL_TXSTAT_TX_UNDERRUN) 135152426Swpaul sc->rl_txthresh = oldthresh + 32; 135252426Swpaul return; 135345633Swpaul } 135445633Swpaul RL_INC(sc->rl_cdata.last_tx); 135545633Swpaul ifp->if_flags &= ~IFF_OACTIVE; 135645633Swpaul } while (sc->rl_cdata.last_tx != sc->rl_cdata.cur_tx); 135740516Swpaul 135899165Sluigi ifp->if_timer = 135999165Sluigi (sc->rl_cdata.last_tx == sc->rl_cdata.cur_tx) ? 0 : 5; 136099165Sluigi 136150703Swpaul return; 136250703Swpaul} 136340516Swpaul 1364102335Salfredstatic void 1365102335Salfredrl_tick(xsc) 136650703Swpaul void *xsc; 136750703Swpaul{ 136850703Swpaul struct rl_softc *sc; 136950703Swpaul struct mii_data *mii; 137050703Swpaul 137150703Swpaul sc = xsc; 137267087Swpaul RL_LOCK(sc); 137350703Swpaul mii = device_get_softc(sc->rl_miibus); 137450703Swpaul 137550703Swpaul mii_tick(mii); 137650703Swpaul 137750703Swpaul sc->rl_stat_ch = timeout(rl_tick, sc, hz); 137867087Swpaul RL_UNLOCK(sc); 137950703Swpaul 138040516Swpaul return; 138140516Swpaul} 138240516Swpaul 138394883Sluigi#ifdef DEVICE_POLLING 138494883Sluigistatic void 138594883Sluigirl_poll (struct ifnet *ifp, enum poll_cmd cmd, int count) 138694883Sluigi{ 138794883Sluigi struct rl_softc *sc = ifp->if_softc; 138894883Sluigi 138994883Sluigi RL_LOCK(sc); 139094883Sluigi if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */ 139194883Sluigi CSR_WRITE_2(sc, RL_IMR, RL_INTRS); 139294883Sluigi goto done; 139394883Sluigi } 139494883Sluigi 139594883Sluigi sc->rxcycles = count; 139694883Sluigi rl_rxeof(sc); 139794883Sluigi rl_txeof(sc); 139894883Sluigi if (ifp->if_snd.ifq_head != NULL) 139994883Sluigi rl_start(ifp); 140094883Sluigi 140194883Sluigi if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */ 140294883Sluigi u_int16_t status; 140394883Sluigi 140494883Sluigi status = CSR_READ_2(sc, RL_ISR); 1405100957Sjhb if (status == 0xffff) 1406100957Sjhb goto done; 140794883Sluigi if (status) 140894883Sluigi CSR_WRITE_2(sc, RL_ISR, status); 140994883Sluigi 141094883Sluigi /* 141194883Sluigi * XXX check behaviour on receiver stalls. 141294883Sluigi */ 141394883Sluigi 141494883Sluigi if (status & RL_ISR_SYSTEM_ERR) { 141594883Sluigi rl_reset(sc); 141694883Sluigi rl_init(sc); 141794883Sluigi } 141894883Sluigi } 141994883Sluigidone: 142094883Sluigi RL_UNLOCK(sc); 142194883Sluigi} 142294883Sluigi#endif /* DEVICE_POLLING */ 142394883Sluigi 1424102335Salfredstatic void 1425102335Salfredrl_intr(arg) 142640516Swpaul void *arg; 142740516Swpaul{ 142840516Swpaul struct rl_softc *sc; 142940516Swpaul struct ifnet *ifp; 143040516Swpaul u_int16_t status; 143140516Swpaul 143240516Swpaul sc = arg; 143386822Siwasaki 143486822Siwasaki if (sc->suspended) { 143586822Siwasaki return; 143686822Siwasaki } 143786822Siwasaki 143867087Swpaul RL_LOCK(sc); 143940516Swpaul ifp = &sc->arpcom.ac_if; 144040516Swpaul 144194883Sluigi#ifdef DEVICE_POLLING 1442102052Ssobomax if (ifp->if_flags & IFF_POLLING) 144394883Sluigi goto done; 144494883Sluigi if (ether_poll_register(rl_poll, ifp)) { /* ok, disable interrupts */ 144594883Sluigi CSR_WRITE_2(sc, RL_IMR, 0x0000); 144694883Sluigi rl_poll(ifp, 0, 1); 144794883Sluigi goto done; 144894883Sluigi } 144994883Sluigi#endif /* DEVICE_POLLING */ 145040516Swpaul 145140516Swpaul for (;;) { 145240516Swpaul 145340516Swpaul status = CSR_READ_2(sc, RL_ISR); 1454100957Sjhb /* If the card has gone away the read returns 0xffff. */ 1455100957Sjhb if (status == 0xffff) 1456100957Sjhb break; 145740516Swpaul if (status) 145840516Swpaul CSR_WRITE_2(sc, RL_ISR, status); 145940516Swpaul 146040516Swpaul if ((status & RL_INTRS) == 0) 146140516Swpaul break; 146240516Swpaul 146340516Swpaul if (status & RL_ISR_RX_OK) 146440516Swpaul rl_rxeof(sc); 146540516Swpaul 146640516Swpaul if (status & RL_ISR_RX_ERR) 146740516Swpaul rl_rxeof(sc); 146840516Swpaul 146945633Swpaul if ((status & RL_ISR_TX_OK) || (status & RL_ISR_TX_ERR)) 147040516Swpaul rl_txeof(sc); 147140516Swpaul 147240516Swpaul if (status & RL_ISR_SYSTEM_ERR) { 147340516Swpaul rl_reset(sc); 147440516Swpaul rl_init(sc); 147540516Swpaul } 147640516Swpaul 147740516Swpaul } 147840516Swpaul 147952426Swpaul if (ifp->if_snd.ifq_head != NULL) 148040516Swpaul rl_start(ifp); 148140516Swpaul 148294883Sluigi#ifdef DEVICE_POLLING 148394883Sluigidone: 148494883Sluigi#endif 148567087Swpaul RL_UNLOCK(sc); 148667087Swpaul 148740516Swpaul return; 148840516Swpaul} 148940516Swpaul 149040516Swpaul/* 149140516Swpaul * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data 149240516Swpaul * pointers to the fragment pointers. 149340516Swpaul */ 1494102335Salfredstatic int 1495102335Salfredrl_encap(sc, m_head) 149640516Swpaul struct rl_softc *sc; 149740516Swpaul struct mbuf *m_head; 149840516Swpaul{ 149941243Swpaul struct mbuf *m_new = NULL; 150040516Swpaul 150140516Swpaul /* 150245633Swpaul * The RealTek is brain damaged and wants longword-aligned 150345633Swpaul * TX buffers, plus we can only have one fragment buffer 150445633Swpaul * per packet. We have to copy pretty much all the time. 150540516Swpaul */ 150640516Swpaul 150741243Swpaul MGETHDR(m_new, M_DONTWAIT, MT_DATA); 150887846Sluigi if (m_new == NULL) 150941243Swpaul return(1); 151041243Swpaul if (m_head->m_pkthdr.len > MHLEN) { 151141243Swpaul MCLGET(m_new, M_DONTWAIT); 151241243Swpaul if (!(m_new->m_flags & M_EXT)) { 151341243Swpaul m_freem(m_new); 151440516Swpaul return(1); 151540516Swpaul } 151640516Swpaul } 151752426Swpaul m_copydata(m_head, 0, m_head->m_pkthdr.len, mtod(m_new, caddr_t)); 151841243Swpaul m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len; 151941243Swpaul m_freem(m_head); 152041243Swpaul m_head = m_new; 152140516Swpaul 152240516Swpaul /* Pad frames to at least 60 bytes. */ 152341243Swpaul if (m_head->m_pkthdr.len < RL_MIN_FRAMELEN) { 152455058Swpaul /* 152555058Swpaul * Make security concious people happy: zero out the 152655058Swpaul * bytes in the pad area, since we don't know what 152755058Swpaul * this mbuf cluster buffer's previous user might 152855058Swpaul * have left in it. 152955058Swpaul */ 153055058Swpaul bzero(mtod(m_head, char *) + m_head->m_pkthdr.len, 153155058Swpaul RL_MIN_FRAMELEN - m_head->m_pkthdr.len); 153240516Swpaul m_head->m_pkthdr.len += 153352426Swpaul (RL_MIN_FRAMELEN - m_head->m_pkthdr.len); 153441243Swpaul m_head->m_len = m_head->m_pkthdr.len; 153541243Swpaul } 153640516Swpaul 153745633Swpaul RL_CUR_TXMBUF(sc) = m_head; 153840516Swpaul 153940516Swpaul return(0); 154040516Swpaul} 154140516Swpaul 154240516Swpaul/* 154340516Swpaul * Main transmit routine. 154440516Swpaul */ 154540516Swpaul 1546102335Salfredstatic void 1547102335Salfredrl_start(ifp) 154840516Swpaul struct ifnet *ifp; 154940516Swpaul{ 155040516Swpaul struct rl_softc *sc; 155140516Swpaul struct mbuf *m_head = NULL; 155240516Swpaul 155340516Swpaul sc = ifp->if_softc; 155467087Swpaul RL_LOCK(sc); 155540516Swpaul 155645633Swpaul while(RL_CUR_TXMBUF(sc) == NULL) { 155740516Swpaul IF_DEQUEUE(&ifp->if_snd, m_head); 155840516Swpaul if (m_head == NULL) 155940516Swpaul break; 156040516Swpaul 156158801Swpaul if (rl_encap(sc, m_head)) { 156258801Swpaul IF_PREPEND(&ifp->if_snd, m_head); 156358801Swpaul ifp->if_flags |= IFF_OACTIVE; 156458801Swpaul break; 156558801Swpaul } 156640516Swpaul 156740516Swpaul /* 156840516Swpaul * If there's a BPF listener, bounce a copy of this frame 156940516Swpaul * to him. 157040516Swpaul */ 157140516Swpaul if (ifp->if_bpf) 157245633Swpaul bpf_mtap(ifp, RL_CUR_TXMBUF(sc)); 157351583Swpaul 157440516Swpaul /* 157540516Swpaul * Transmit the frame. 157640516Swpaul */ 157781713Swpaul bus_dmamap_create(sc->rl_tag, 0, &RL_CUR_DMAMAP(sc)); 157881713Swpaul bus_dmamap_load(sc->rl_tag, RL_CUR_DMAMAP(sc), 157981713Swpaul mtod(RL_CUR_TXMBUF(sc), void *), 158081713Swpaul RL_CUR_TXMBUF(sc)->m_pkthdr.len, rl_dma_map_txbuf, sc, 0); 158181713Swpaul bus_dmamap_sync(sc->rl_tag, RL_CUR_DMAMAP(sc), 158281713Swpaul BUS_DMASYNC_PREREAD); 158345633Swpaul CSR_WRITE_4(sc, RL_CUR_TXSTAT(sc), 158452426Swpaul RL_TXTHRESH(sc->rl_txthresh) | 158552426Swpaul RL_CUR_TXMBUF(sc)->m_pkthdr.len); 158645633Swpaul 158745633Swpaul RL_INC(sc->rl_cdata.cur_tx); 158840516Swpaul } 158940516Swpaul 159040516Swpaul /* 159145633Swpaul * We broke out of the loop because all our TX slots are 159245633Swpaul * full. Mark the NIC as busy until it drains some of the 159345633Swpaul * packets from the queue. 159445633Swpaul */ 159545633Swpaul if (RL_CUR_TXMBUF(sc) != NULL) 159645633Swpaul ifp->if_flags |= IFF_OACTIVE; 159745633Swpaul 159845633Swpaul /* 159940516Swpaul * Set a timeout in case the chip goes out to lunch. 160040516Swpaul */ 160140516Swpaul ifp->if_timer = 5; 160267087Swpaul RL_UNLOCK(sc); 160340516Swpaul 160440516Swpaul return; 160540516Swpaul} 160640516Swpaul 1607102335Salfredstatic void 1608102335Salfredrl_init(xsc) 160940516Swpaul void *xsc; 161040516Swpaul{ 161140516Swpaul struct rl_softc *sc = xsc; 161240516Swpaul struct ifnet *ifp = &sc->arpcom.ac_if; 161350703Swpaul struct mii_data *mii; 161467087Swpaul int i; 161540516Swpaul u_int32_t rxcfg = 0; 161640516Swpaul 161767087Swpaul RL_LOCK(sc); 161850703Swpaul mii = device_get_softc(sc->rl_miibus); 161940516Swpaul 162040516Swpaul /* 162140516Swpaul * Cancel pending I/O and free all RX/TX buffers. 162240516Swpaul */ 162340516Swpaul rl_stop(sc); 162440516Swpaul 162540516Swpaul /* Init our MAC address */ 162640516Swpaul for (i = 0; i < ETHER_ADDR_LEN; i++) { 162740516Swpaul CSR_WRITE_1(sc, RL_IDR0 + i, sc->arpcom.ac_enaddr[i]); 162840516Swpaul } 162940516Swpaul 163040516Swpaul /* Init the RX buffer pointer register. */ 163181713Swpaul bus_dmamap_load(sc->rl_tag, sc->rl_cdata.rl_rx_dmamap, 163281713Swpaul sc->rl_cdata.rl_rx_buf, RL_RXBUFLEN, rl_dma_map_rxbuf, sc, 0); 163381713Swpaul bus_dmamap_sync(sc->rl_tag, sc->rl_cdata.rl_rx_dmamap, 163481713Swpaul BUS_DMASYNC_PREWRITE); 163540516Swpaul 163640516Swpaul /* Init TX descriptors. */ 163740516Swpaul rl_list_tx_init(sc); 163840516Swpaul 163940516Swpaul /* 164040516Swpaul * Enable transmit and receive. 164140516Swpaul */ 164240516Swpaul CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB); 164340516Swpaul 164440516Swpaul /* 164545633Swpaul * Set the initial TX and RX configuration. 164640516Swpaul */ 164745633Swpaul CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG); 164840516Swpaul CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG); 164940516Swpaul 165040516Swpaul /* Set the individual bit to receive frames for this host only. */ 165140516Swpaul rxcfg = CSR_READ_4(sc, RL_RXCFG); 165240516Swpaul rxcfg |= RL_RXCFG_RX_INDIV; 165340516Swpaul 165440516Swpaul /* If we want promiscuous mode, set the allframes bit. */ 165540516Swpaul if (ifp->if_flags & IFF_PROMISC) { 165640516Swpaul rxcfg |= RL_RXCFG_RX_ALLPHYS; 165740516Swpaul CSR_WRITE_4(sc, RL_RXCFG, rxcfg); 165840516Swpaul } else { 165940516Swpaul rxcfg &= ~RL_RXCFG_RX_ALLPHYS; 166040516Swpaul CSR_WRITE_4(sc, RL_RXCFG, rxcfg); 166140516Swpaul } 166240516Swpaul 166340516Swpaul /* 166440516Swpaul * Set capture broadcast bit to capture broadcast frames. 166540516Swpaul */ 166640516Swpaul if (ifp->if_flags & IFF_BROADCAST) { 166740516Swpaul rxcfg |= RL_RXCFG_RX_BROAD; 166840516Swpaul CSR_WRITE_4(sc, RL_RXCFG, rxcfg); 166940516Swpaul } else { 167040516Swpaul rxcfg &= ~RL_RXCFG_RX_BROAD; 167140516Swpaul CSR_WRITE_4(sc, RL_RXCFG, rxcfg); 167240516Swpaul } 167340516Swpaul 167440516Swpaul /* 167540516Swpaul * Program the multicast filter, if necessary. 167640516Swpaul */ 167740516Swpaul rl_setmulti(sc); 167840516Swpaul 167994883Sluigi#ifdef DEVICE_POLLING 168040516Swpaul /* 168194883Sluigi * Disable interrupts if we are polling. 168294883Sluigi */ 1683102052Ssobomax if (ifp->if_flags & IFF_POLLING) 168494883Sluigi CSR_WRITE_2(sc, RL_IMR, 0); 168594883Sluigi else /* otherwise ... */ 168694883Sluigi#endif /* DEVICE_POLLING */ 168794883Sluigi /* 168840516Swpaul * Enable interrupts. 168940516Swpaul */ 169040516Swpaul CSR_WRITE_2(sc, RL_IMR, RL_INTRS); 169140516Swpaul 169252426Swpaul /* Set initial TX threshold */ 169352426Swpaul sc->rl_txthresh = RL_TX_THRESH_INIT; 169452426Swpaul 169540516Swpaul /* Start RX/TX process. */ 169640516Swpaul CSR_WRITE_4(sc, RL_MISSEDPKT, 0); 169740516Swpaul 169840516Swpaul /* Enable receiver and transmitter. */ 169940516Swpaul CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB); 170040516Swpaul 170150703Swpaul mii_mediachg(mii); 170240516Swpaul 170340516Swpaul CSR_WRITE_1(sc, RL_CFG1, RL_CFG1_DRVLOAD|RL_CFG1_FULLDUPLEX); 170440516Swpaul 170540516Swpaul ifp->if_flags |= IFF_RUNNING; 170640516Swpaul ifp->if_flags &= ~IFF_OACTIVE; 170740516Swpaul 170850703Swpaul sc->rl_stat_ch = timeout(rl_tick, sc, hz); 170967087Swpaul RL_UNLOCK(sc); 171050703Swpaul 171140516Swpaul return; 171240516Swpaul} 171340516Swpaul 171440516Swpaul/* 171540516Swpaul * Set media options. 171640516Swpaul */ 1717102335Salfredstatic int 1718102335Salfredrl_ifmedia_upd(ifp) 171940516Swpaul struct ifnet *ifp; 172040516Swpaul{ 172140516Swpaul struct rl_softc *sc; 172250703Swpaul struct mii_data *mii; 172340516Swpaul 172440516Swpaul sc = ifp->if_softc; 172550703Swpaul mii = device_get_softc(sc->rl_miibus); 172650703Swpaul mii_mediachg(mii); 172740516Swpaul 172840516Swpaul return(0); 172940516Swpaul} 173040516Swpaul 173140516Swpaul/* 173240516Swpaul * Report current media status. 173340516Swpaul */ 1734102335Salfredstatic void 1735102335Salfredrl_ifmedia_sts(ifp, ifmr) 173640516Swpaul struct ifnet *ifp; 173740516Swpaul struct ifmediareq *ifmr; 173840516Swpaul{ 173940516Swpaul struct rl_softc *sc; 174050703Swpaul struct mii_data *mii; 174140516Swpaul 174240516Swpaul sc = ifp->if_softc; 174350703Swpaul mii = device_get_softc(sc->rl_miibus); 174440516Swpaul 174550703Swpaul mii_pollstat(mii); 174650703Swpaul ifmr->ifm_active = mii->mii_media_active; 174750703Swpaul ifmr->ifm_status = mii->mii_media_status; 174840516Swpaul 174940516Swpaul return; 175040516Swpaul} 175140516Swpaul 1752102335Salfredstatic int 1753102335Salfredrl_ioctl(ifp, command, data) 175440516Swpaul struct ifnet *ifp; 175540516Swpaul u_long command; 175640516Swpaul caddr_t data; 175740516Swpaul{ 175840516Swpaul struct rl_softc *sc = ifp->if_softc; 175940516Swpaul struct ifreq *ifr = (struct ifreq *) data; 176050703Swpaul struct mii_data *mii; 176167087Swpaul int error = 0; 176240516Swpaul 176367087Swpaul RL_LOCK(sc); 176440516Swpaul 176540516Swpaul switch(command) { 176640516Swpaul case SIOCSIFADDR: 176740516Swpaul case SIOCGIFADDR: 176840516Swpaul case SIOCSIFMTU: 176940516Swpaul error = ether_ioctl(ifp, command, data); 177040516Swpaul break; 177140516Swpaul case SIOCSIFFLAGS: 177240516Swpaul if (ifp->if_flags & IFF_UP) { 177340516Swpaul rl_init(sc); 177440516Swpaul } else { 177540516Swpaul if (ifp->if_flags & IFF_RUNNING) 177640516Swpaul rl_stop(sc); 177740516Swpaul } 177840516Swpaul error = 0; 177940516Swpaul break; 178040516Swpaul case SIOCADDMULTI: 178140516Swpaul case SIOCDELMULTI: 178240516Swpaul rl_setmulti(sc); 178340516Swpaul error = 0; 178440516Swpaul break; 178540516Swpaul case SIOCGIFMEDIA: 178640516Swpaul case SIOCSIFMEDIA: 178750703Swpaul mii = device_get_softc(sc->rl_miibus); 178850703Swpaul error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 178940516Swpaul break; 179040516Swpaul default: 179140516Swpaul error = EINVAL; 179240516Swpaul break; 179340516Swpaul } 179440516Swpaul 179567087Swpaul RL_UNLOCK(sc); 179640516Swpaul 179740516Swpaul return(error); 179840516Swpaul} 179940516Swpaul 1800102335Salfredstatic void 1801102335Salfredrl_watchdog(ifp) 180240516Swpaul struct ifnet *ifp; 180340516Swpaul{ 180440516Swpaul struct rl_softc *sc; 180540516Swpaul 180640516Swpaul sc = ifp->if_softc; 180767087Swpaul RL_LOCK(sc); 180840516Swpaul printf("rl%d: watchdog timeout\n", sc->rl_unit); 180940516Swpaul ifp->if_oerrors++; 181050703Swpaul 181140516Swpaul rl_txeof(sc); 181240516Swpaul rl_rxeof(sc); 181340516Swpaul rl_init(sc); 181467087Swpaul RL_UNLOCK(sc); 181540516Swpaul 181640516Swpaul return; 181740516Swpaul} 181840516Swpaul 181940516Swpaul/* 182040516Swpaul * Stop the adapter and free any mbufs allocated to the 182140516Swpaul * RX and TX lists. 182240516Swpaul */ 1823102335Salfredstatic void 1824102335Salfredrl_stop(sc) 182540516Swpaul struct rl_softc *sc; 182640516Swpaul{ 182740516Swpaul register int i; 182840516Swpaul struct ifnet *ifp; 182940516Swpaul 183067087Swpaul RL_LOCK(sc); 183140516Swpaul ifp = &sc->arpcom.ac_if; 183240516Swpaul ifp->if_timer = 0; 183340516Swpaul 183450703Swpaul untimeout(rl_tick, sc, sc->rl_stat_ch); 183594883Sluigi ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 183694883Sluigi#ifdef DEVICE_POLLING 183794883Sluigi ether_poll_deregister(ifp); 183894883Sluigi#endif /* DEVICE_POLLING */ 183950703Swpaul 184040516Swpaul CSR_WRITE_1(sc, RL_COMMAND, 0x00); 184140516Swpaul CSR_WRITE_2(sc, RL_IMR, 0x0000); 184281713Swpaul bus_dmamap_unload(sc->rl_tag, sc->rl_cdata.rl_rx_dmamap); 184340516Swpaul 184440516Swpaul /* 184540516Swpaul * Free the TX list buffers. 184640516Swpaul */ 184740516Swpaul for (i = 0; i < RL_TX_LIST_CNT; i++) { 184845633Swpaul if (sc->rl_cdata.rl_tx_chain[i] != NULL) { 184981713Swpaul bus_dmamap_unload(sc->rl_tag, 185081713Swpaul sc->rl_cdata.rl_tx_dmamap[i]); 185181713Swpaul bus_dmamap_destroy(sc->rl_tag, 185281713Swpaul sc->rl_cdata.rl_tx_dmamap[i]); 185345633Swpaul m_freem(sc->rl_cdata.rl_tx_chain[i]); 185445633Swpaul sc->rl_cdata.rl_tx_chain[i] = NULL; 185545633Swpaul CSR_WRITE_4(sc, RL_TXADDR0 + i, 0x0000000); 185640516Swpaul } 185740516Swpaul } 185840516Swpaul 185967087Swpaul RL_UNLOCK(sc); 186040516Swpaul return; 186140516Swpaul} 186240516Swpaul 186340516Swpaul/* 186486822Siwasaki * Device suspend routine. Stop the interface and save some PCI 186586822Siwasaki * settings in case the BIOS doesn't restore them properly on 186686822Siwasaki * resume. 186786822Siwasaki */ 1868102335Salfredstatic int 1869102335Salfredrl_suspend(dev) 187086822Siwasaki device_t dev; 187186822Siwasaki{ 187286822Siwasaki register int i; 187386822Siwasaki struct rl_softc *sc; 187486822Siwasaki 187586822Siwasaki sc = device_get_softc(dev); 187686822Siwasaki 187786822Siwasaki rl_stop(sc); 187886822Siwasaki 187986822Siwasaki for (i = 0; i < 5; i++) 188086822Siwasaki sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4); 188186822Siwasaki sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4); 188286822Siwasaki sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1); 188386822Siwasaki sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1); 188486822Siwasaki sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1); 188586822Siwasaki 188686822Siwasaki sc->suspended = 1; 188786822Siwasaki 188886822Siwasaki return (0); 188986822Siwasaki} 189086822Siwasaki 189186822Siwasaki/* 189286822Siwasaki * Device resume routine. Restore some PCI settings in case the BIOS 189386822Siwasaki * doesn't, re-enable busmastering, and restart the interface if 189486822Siwasaki * appropriate. 189586822Siwasaki */ 1896102335Salfredstatic int 1897102335Salfredrl_resume(dev) 189886822Siwasaki device_t dev; 189986822Siwasaki{ 190086822Siwasaki register int i; 190186822Siwasaki struct rl_softc *sc; 190286822Siwasaki struct ifnet *ifp; 190386822Siwasaki 190486822Siwasaki sc = device_get_softc(dev); 190586822Siwasaki ifp = &sc->arpcom.ac_if; 190686822Siwasaki 190786822Siwasaki /* better way to do this? */ 190886822Siwasaki for (i = 0; i < 5; i++) 190986822Siwasaki pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4); 191086822Siwasaki pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4); 191186822Siwasaki pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1); 191286822Siwasaki pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1); 191386822Siwasaki pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1); 191486822Siwasaki 191586822Siwasaki /* reenable busmastering */ 191686822Siwasaki pci_enable_busmaster(dev); 191786822Siwasaki pci_enable_io(dev, RL_RES); 191886822Siwasaki 191986822Siwasaki /* reinitialize interface if necessary */ 192086822Siwasaki if (ifp->if_flags & IFF_UP) 192186822Siwasaki rl_init(sc); 192286822Siwasaki 192386822Siwasaki sc->suspended = 0; 192486822Siwasaki 192586822Siwasaki return (0); 192686822Siwasaki} 192786822Siwasaki 192886822Siwasaki/* 192940516Swpaul * Stop all chip I/O so that the kernel's probe routines don't 193040516Swpaul * get confused by errant DMAs when rebooting. 193140516Swpaul */ 1932102335Salfredstatic void 1933102335Salfredrl_shutdown(dev) 193450703Swpaul device_t dev; 193540516Swpaul{ 193650703Swpaul struct rl_softc *sc; 193740516Swpaul 193850703Swpaul sc = device_get_softc(dev); 193950703Swpaul 194040516Swpaul rl_stop(sc); 194140516Swpaul 194240516Swpaul return; 194340516Swpaul} 1944