if_rl.c revision 118978
140516Swpaul/* 2117388Swpaul * Copyright (c) 1997, 1998-2003 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 */ 3240516Swpaul 3340516Swpaul/* 34117388Swpaul * RealTek 8129/8139/8139C+/8169 PCI NIC driver 3540516Swpaul * 36117388Swpaul * Supports several extremely cheap PCI 10/100 and 10/100/1000 adapters 37117388Swpaul * based on RealTek chipsets. Datasheets can be obtained from 3840516Swpaul * www.realtek.com.tw. 3940516Swpaul * 40117388Swpaul * Written by Bill Paul <wpaul@windriver.com> 41117388Swpaul * Senior Networking Software Engineer 42117388Swpaul * Wind River Systems 4340516Swpaul */ 4440516Swpaul 4540516Swpaul/* 4640516Swpaul * The RealTek 8139 PCI NIC redefines the meaning of 'low end.' This is 4740516Swpaul * probably the worst PCI ethernet controller ever made, with the possible 4840516Swpaul * exception of the FEAST chip made by SMC. The 8139 supports bus-master 4940516Swpaul * DMA, but it has a terrible interface that nullifies any performance 5040516Swpaul * gains that bus-master DMA usually offers. 5140516Swpaul * 5240516Swpaul * For transmission, the chip offers a series of four TX descriptor 5340516Swpaul * registers. Each transmit frame must be in a contiguous buffer, aligned 5441569Swpaul * on a longword (32-bit) boundary. This means we almost always have to 5540516Swpaul * do mbuf copies in order to transmit a frame, except in the unlikely 5640516Swpaul * case where a) the packet fits into a single mbuf, and b) the packet 5740516Swpaul * is 32-bit aligned within the mbuf's data area. The presence of only 5840516Swpaul * four descriptor registers means that we can never have more than four 5940516Swpaul * packets queued for transmission at any one time. 6040516Swpaul * 6140516Swpaul * Reception is not much better. The driver has to allocate a single large 6240516Swpaul * buffer area (up to 64K in size) into which the chip will DMA received 6340516Swpaul * frames. Because we don't know where within this region received packets 6440516Swpaul * will begin or end, we have no choice but to copy data from the buffer 6540516Swpaul * area into mbufs in order to pass the packets up to the higher protocol 6640516Swpaul * levels. 6740516Swpaul * 6840516Swpaul * It's impossible given this rotten design to really achieve decent 6940516Swpaul * performance at 100Mbps, unless you happen to have a 400Mhz PII or 7040516Swpaul * some equally overmuscled CPU to drive it. 7140516Swpaul * 7240516Swpaul * On the bright side, the 8139 does have a built-in PHY, although 7340516Swpaul * rather than using an MDIO serial interface like most other NICs, the 7440516Swpaul * PHY registers are directly accessible through the 8139's register 7540516Swpaul * space. The 8139 supports autonegotiation, as well as a 64-bit multicast 7640516Swpaul * filter. 7740516Swpaul * 7840516Swpaul * The 8129 chip is an older version of the 8139 that uses an external PHY 7940516Swpaul * chip. The 8129 has a serial MDIO interface for accessing the MII where 8040516Swpaul * the 8139 lets you directly access the on-board PHY registers. We need 8140516Swpaul * to select which interface to use depending on the chip type. 82117388Swpaul * 83118712Swpaul * Fast forward a few years. RealTek now has a new chip called the 84117388Swpaul * 8139C+ which at long last implements descriptor-based DMA. Not 85118712Swpaul * only that, it supports RX and TX TCP/IP checksum offload, VLAN 86117388Swpaul * tagging and insertion, TCP large send and 64-bit addressing. 87117388Swpaul * Better still, it allows arbitrary byte alignments for RX and 88117388Swpaul * TX buffers, meaning no copying is necessary on any architecture. 89117388Swpaul * There are a few limitations however: the RX and TX descriptor 90117388Swpaul * rings must be aligned on 256 byte boundaries, they must be in 91117388Swpaul * contiguous RAM, and each ring can have a maximum of 64 descriptors. 92117388Swpaul * There are two TX descriptor queues: one normal priority and one 93117388Swpaul * high. Descriptor ring addresses and DMA buffer addresses are 94117388Swpaul * 64 bits wide. The 8139C+ is also backwards compatible with the 95117388Swpaul * 8139, so the chip will still function with older drivers: C+ 96117388Swpaul * mode has to be enabled by setting the appropriate bits in the C+ 97117388Swpaul * command register. The PHY access mechanism appears to be unchanged. 98117388Swpaul * 99118712Swpaul * The 8169 is a 10/100/1000 ethernet MAC. It has almost the same 100118712Swpaul * programming API as the C+ mode of the 8139C+, with a couple of 101118712Swpaul * minor changes and additions: TX start register and timer interrupt 102118712Swpaul * register are located at different offsets, and there are additional 103118712Swpaul * registers for GMII PHY status and control, as well as TBI-mode 104118712Swpaul * status and control. There is also a maximum RX packet size 105118712Swpaul * register to allow the chip to receive jumbo frames. The 8169 106118712Swpaul * can only be programmed in C+ mode: the old 8139 programming 107117388Swpaul * method isn't supported with this chip. Also, RealTek has a LOM 108117388Swpaul * (LAN On Motherboard) gigabit MAC chip called the RTL8110S which 109118712Swpaul * I believe to be register compatible with the 8169. Unlike the 110118712Swpaul * 8139C+, the 8169 can have up to 1024 descriptors per DMA ring. 111118712Swpaul * The reference 8169 board design uses a Marvell 88E1000 'Alaska' 112118712Swpaul * copper PHY. 113117388Swpaul * 114118712Swpaul * The 8169S and 8110S are newer versions of the 8169. Available 115118712Swpaul * in both 32-bit and 64-bit forms, these devices have built-in 116118712Swpaul * copper 10/100/1000 PHYs. The 8110S is a lan-on-motherboard chip 117118712Swpaul * that is pin-for-pin compatible with the 8100. Unfortunately, 118118712Swpaul * RealTek has not released programming manuals for the 8169S and 119118712Swpaul * 8110S yet. The datasheet for the original 8169 provides most 120117388Swpaul * of the information, but you must refer to RealTek's 8169 Linux 121118712Swpaul * driver to fill in the gaps. Mostly, it appears that the built-in 122118712Swpaul * PHY requires some special initialization. The original 8169 123118712Swpaul * datasheet and the 8139C+ datasheet can be obtained from 124118712Swpaul * http://www.freebsd.org/~wpaul/RealTek. 125117388Swpaul * 126117388Swpaul * This driver now supports both the old 8139 and new 8139C+ 127118712Swpaul * programming models. We detect the 8139C+ by looking for the 128118712Swpaul * corresponding hardware rev bits, and we detect the 8169 by its 129117388Swpaul * PCI ID. Two new NIC type codes, RL_8139CPLUS and RL_8169 have 130117388Swpaul * been added to distinguish the chips at runtime. Separate RX and 131117388Swpaul * TX handling routines have been added to handle C+ mode, which 132117388Swpaul * are selected via function pointers that are initialized during 133117388Swpaul * the driver attach phase. 13440516Swpaul */ 13540516Swpaul 136116192Sobrien#include <sys/cdefs.h> 137116192Sobrien__FBSDID("$FreeBSD: head/sys/pci/if_rl.c 118978 2003-08-15 22:47:55Z wpaul $"); 138116192Sobrien 13940516Swpaul#include <sys/param.h> 140108729Sjake#include <sys/endian.h> 14140516Swpaul#include <sys/systm.h> 14240516Swpaul#include <sys/sockio.h> 14340516Swpaul#include <sys/mbuf.h> 14440516Swpaul#include <sys/malloc.h> 14540516Swpaul#include <sys/kernel.h> 14640516Swpaul#include <sys/socket.h> 14740516Swpaul 14840516Swpaul#include <net/if.h> 14940516Swpaul#include <net/if_arp.h> 15040516Swpaul#include <net/ethernet.h> 15140516Swpaul#include <net/if_dl.h> 15240516Swpaul#include <net/if_media.h> 153117388Swpaul#include <net/if_vlan_var.h> 15440516Swpaul 15540516Swpaul#include <net/bpf.h> 15640516Swpaul 15741569Swpaul#include <machine/bus_pio.h> 15841569Swpaul#include <machine/bus_memio.h> 15941569Swpaul#include <machine/bus.h> 16050703Swpaul#include <machine/resource.h> 16150703Swpaul#include <sys/bus.h> 16250703Swpaul#include <sys/rman.h> 16340516Swpaul 16450703Swpaul#include <dev/mii/mii.h> 16550703Swpaul#include <dev/mii/miivar.h> 16650703Swpaul 16740516Swpaul#include <pci/pcireg.h> 16840516Swpaul#include <pci/pcivar.h> 16940516Swpaul 170113506SmdoddMODULE_DEPEND(rl, pci, 1, 1, 1); 171113506SmdoddMODULE_DEPEND(rl, ether, 1, 1, 1); 17259758SpeterMODULE_DEPEND(rl, miibus, 1, 1, 1); 17359758Speter 17451089Speter/* "controller miibus0" required. See GENERIC if you get errors here. */ 17550703Swpaul#include "miibus_if.h" 17650703Swpaul 17740516Swpaul/* 17840516Swpaul * Default to using PIO access for this driver. On SMP systems, 17940516Swpaul * there appear to be problems with memory mapped mode: it looks like 18040516Swpaul * doing too many memory mapped access back to back in rapid succession 18140516Swpaul * can hang the bus. I'm inclined to blame this on crummy design/construction 18240516Swpaul * on the part of RealTek. Memory mapped mode does appear to work on 18340516Swpaul * uniprocessor systems though. 18440516Swpaul */ 18540516Swpaul#define RL_USEIOSPACE 18640516Swpaul 18740516Swpaul#include <pci/if_rlreg.h> 18840516Swpaul 189117388Swpaul#define RL_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP) 190117388Swpaul 19140516Swpaul/* 19240516Swpaul * Various supported device vendors/types and their names. 19340516Swpaul */ 19440516Swpaulstatic struct rl_type rl_devs[] = { 195117388Swpaul { RT_VENDORID, RT_DEVICEID_8129, RL_8129, 19640516Swpaul "RealTek 8129 10/100BaseTX" }, 197117388Swpaul { RT_VENDORID, RT_DEVICEID_8139, RL_8139, 19840516Swpaul "RealTek 8139 10/100BaseTX" }, 199118586Swpaul { RT_VENDORID, RT_DEVICEID_8169, RL_8169, 200118586Swpaul "RealTek 8169 10/100/1000BaseTX" }, 201117388Swpaul { RT_VENDORID, RT_DEVICEID_8138, RL_8139, 20267771Swpaul "RealTek 8139 10/100BaseTX CardBus" }, 203118978Swpaul { RT_VENDORID, RT_DEVICEID_8100, RL_8139, 204118978Swpaul "RealTek 8100 10/100BaseTX" }, 205117388Swpaul { ACCTON_VENDORID, ACCTON_DEVICEID_5030, RL_8139, 20641243Swpaul "Accton MPX 5030/5038 10/100BaseTX" }, 207117388Swpaul { DELTA_VENDORID, DELTA_DEVICEID_8139, RL_8139, 20844238Swpaul "Delta Electronics 8139 10/100BaseTX" }, 209117388Swpaul { ADDTRON_VENDORID, ADDTRON_DEVICEID_8139, RL_8139, 21044238Swpaul "Addtron Technolgy 8139 10/100BaseTX" }, 211117388Swpaul { DLINK_VENDORID, DLINK_DEVICEID_530TXPLUS, RL_8139, 21272813Swpaul "D-Link DFE-530TX+ 10/100BaseTX" }, 213117388Swpaul { DLINK_VENDORID, DLINK_DEVICEID_690TXD, RL_8139, 21496112Sjhb "D-Link DFE-690TXD 10/100BaseTX" }, 215117388Swpaul { NORTEL_VENDORID, ACCTON_DEVICEID_5030, RL_8139, 21694400Swpaul "Nortel Networks 10/100BaseTX" }, 217117388Swpaul { COREGA_VENDORID, COREGA_DEVICEID_FETHERCBTXD, RL_8139, 218103020Siwasaki "Corega FEther CB-TXD" }, 219117388Swpaul { COREGA_VENDORID, COREGA_DEVICEID_FETHERIICBTXD, RL_8139, 220109095Ssanpei "Corega FEtherII CB-TXD" }, 221117388Swpaul /* XXX what type of realtek is PEPPERCON_DEVICEID_ROLF ? */ 222117388Swpaul { PEPPERCON_VENDORID, PEPPERCON_DEVICEID_ROLF, RL_8139, 223111381Sdan "Peppercon AG ROL-F" }, 224117388Swpaul { PLANEX_VENDORID, PLANEX_DEVICEID_FNW3800TX, RL_8139, 225112379Ssanpei "Planex FNW-3800-TX" }, 226117388Swpaul { CP_VENDORID, RT_DEVICEID_8139, RL_8139, 227117388Swpaul "Compaq HNE-300" }, 228117388Swpaul { LEVEL1_VENDORID, LEVEL1_DEVICEID_FPC0106TX, RL_8139, 229117388Swpaul "LevelOne FPC-0106TX" }, 230117388Swpaul { EDIMAX_VENDORID, EDIMAX_DEVICEID_EP4103DL, RL_8139, 231117388Swpaul "Edimax EP-4103DL CardBus" }, 232117388Swpaul { 0, 0, 0, NULL } 233117388Swpaul}; 234117388Swpaul 235117388Swpaulstatic struct rl_hwrev rl_hwrevs[] = { 236117388Swpaul { RL_HWREV_8139, RL_8139, "" }, 237117388Swpaul { RL_HWREV_8139A, RL_8139, "A" }, 238117388Swpaul { RL_HWREV_8139AG, RL_8139, "A-G" }, 239117388Swpaul { RL_HWREV_8139B, RL_8139, "B" }, 240117388Swpaul { RL_HWREV_8130, RL_8139, "8130" }, 241117388Swpaul { RL_HWREV_8139C, RL_8139, "C" }, 242118586Swpaul { RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C" }, 243117388Swpaul { RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+"}, 244118586Swpaul { RL_HWREV_8169, RL_8169, "8169"}, 245118586Swpaul { RL_HWREV_8110, RL_8169, "8169S/8110S"}, 246118586Swpaul { RL_HWREV_8100, RL_8139, "8100"}, 247118586Swpaul { RL_HWREV_8101, RL_8139, "8101"}, 24840516Swpaul { 0, 0, NULL } 24940516Swpaul}; 25040516Swpaul 25192739Salfredstatic int rl_probe (device_t); 25292739Salfredstatic int rl_attach (device_t); 25392739Salfredstatic int rl_detach (device_t); 25440516Swpaul 255117388Swpaulstatic int rl_encap (struct rl_softc *, struct mbuf *); 256117388Swpaulstatic int rl_encapcplus (struct rl_softc *, struct mbuf *, int *); 25740516Swpaul 258117388Swpaulstatic void rl_dma_map_addr (void *, bus_dma_segment_t *, int, int); 259117388Swpaulstatic void rl_dma_map_desc (void *, bus_dma_segment_t *, int, 260117388Swpaul bus_size_t, int); 261117388Swpaulstatic int rl_allocmem (device_t, struct rl_softc *); 262117388Swpaulstatic int rl_allocmemcplus (device_t, struct rl_softc *); 263117388Swpaulstatic int rl_newbuf (struct rl_softc *, int, struct mbuf *); 264117388Swpaulstatic int rl_rx_list_init (struct rl_softc *); 265117388Swpaulstatic int rl_tx_list_init (struct rl_softc *); 26692739Salfredstatic void rl_rxeof (struct rl_softc *); 267117388Swpaulstatic void rl_rxeofcplus (struct rl_softc *); 26892739Salfredstatic void rl_txeof (struct rl_softc *); 269117388Swpaulstatic void rl_txeofcplus (struct rl_softc *); 27092739Salfredstatic void rl_intr (void *); 271117388Swpaulstatic void rl_intrcplus (void *); 27292739Salfredstatic void rl_tick (void *); 27392739Salfredstatic void rl_start (struct ifnet *); 274117388Swpaulstatic void rl_startcplus (struct ifnet *); 27592739Salfredstatic int rl_ioctl (struct ifnet *, u_long, caddr_t); 27692739Salfredstatic void rl_init (void *); 27792739Salfredstatic void rl_stop (struct rl_softc *); 27892739Salfredstatic void rl_watchdog (struct ifnet *); 27992739Salfredstatic int rl_suspend (device_t); 28092739Salfredstatic int rl_resume (device_t); 28192739Salfredstatic void rl_shutdown (device_t); 28292739Salfredstatic int rl_ifmedia_upd (struct ifnet *); 28392739Salfredstatic void rl_ifmedia_sts (struct ifnet *, struct ifmediareq *); 28440516Swpaul 28592739Salfredstatic void rl_eeprom_putbyte (struct rl_softc *, int); 28692739Salfredstatic void rl_eeprom_getword (struct rl_softc *, int, u_int16_t *); 28792739Salfredstatic void rl_read_eeprom (struct rl_softc *, caddr_t, int, int, int); 28892739Salfredstatic void rl_mii_sync (struct rl_softc *); 28992739Salfredstatic void rl_mii_send (struct rl_softc *, u_int32_t, int); 29092739Salfredstatic int rl_mii_readreg (struct rl_softc *, struct rl_mii_frame *); 29192739Salfredstatic int rl_mii_writereg (struct rl_softc *, struct rl_mii_frame *); 292118586Swpaulstatic int rl_gmii_readreg (device_t, int, int); 293118586Swpaulstatic int rl_gmii_writereg (device_t, int, int, int); 29440516Swpaul 29592739Salfredstatic int rl_miibus_readreg (device_t, int, int); 29692739Salfredstatic int rl_miibus_writereg (device_t, int, int, int); 29792739Salfredstatic void rl_miibus_statchg (device_t); 29840516Swpaul 29992739Salfredstatic u_int8_t rl_calchash (caddr_t); 30092739Salfredstatic void rl_setmulti (struct rl_softc *); 30192739Salfredstatic void rl_reset (struct rl_softc *); 30292739Salfredstatic int rl_list_tx_init (struct rl_softc *); 30340516Swpaul 30492739Salfredstatic void rl_dma_map_rxbuf (void *, bus_dma_segment_t *, int, int); 30592739Salfredstatic void rl_dma_map_txbuf (void *, bus_dma_segment_t *, int, int); 30681713Swpaul 30750703Swpaul#ifdef RL_USEIOSPACE 30850703Swpaul#define RL_RES SYS_RES_IOPORT 30950703Swpaul#define RL_RID RL_PCI_LOIO 31050703Swpaul#else 31150703Swpaul#define RL_RES SYS_RES_MEMORY 31250703Swpaul#define RL_RID RL_PCI_LOMEM 31350703Swpaul#endif 31450703Swpaul 31550703Swpaulstatic device_method_t rl_methods[] = { 31650703Swpaul /* Device interface */ 31750703Swpaul DEVMETHOD(device_probe, rl_probe), 31850703Swpaul DEVMETHOD(device_attach, rl_attach), 31950703Swpaul DEVMETHOD(device_detach, rl_detach), 32086822Siwasaki DEVMETHOD(device_suspend, rl_suspend), 32186822Siwasaki DEVMETHOD(device_resume, rl_resume), 32250703Swpaul DEVMETHOD(device_shutdown, rl_shutdown), 32350703Swpaul 32450703Swpaul /* bus interface */ 32550703Swpaul DEVMETHOD(bus_print_child, bus_generic_print_child), 32650703Swpaul DEVMETHOD(bus_driver_added, bus_generic_driver_added), 32750703Swpaul 32850703Swpaul /* MII interface */ 32950703Swpaul DEVMETHOD(miibus_readreg, rl_miibus_readreg), 33050703Swpaul DEVMETHOD(miibus_writereg, rl_miibus_writereg), 33150703Swpaul DEVMETHOD(miibus_statchg, rl_miibus_statchg), 33250703Swpaul 33350703Swpaul { 0, 0 } 33450703Swpaul}; 33550703Swpaul 33650703Swpaulstatic driver_t rl_driver = { 33751455Swpaul "rl", 33850703Swpaul rl_methods, 33950703Swpaul sizeof(struct rl_softc) 34050703Swpaul}; 34150703Swpaul 34250703Swpaulstatic devclass_t rl_devclass; 34350703Swpaul 344113506SmdoddDRIVER_MODULE(rl, pci, rl_driver, rl_devclass, 0, 0); 345113506SmdoddDRIVER_MODULE(rl, cardbus, rl_driver, rl_devclass, 0, 0); 34651473SwpaulDRIVER_MODULE(miibus, rl, miibus_driver, miibus_devclass, 0, 0); 34750703Swpaul 34840516Swpaul#define EE_SET(x) \ 34940516Swpaul CSR_WRITE_1(sc, RL_EECMD, \ 35040516Swpaul CSR_READ_1(sc, RL_EECMD) | x) 35140516Swpaul 35240516Swpaul#define EE_CLR(x) \ 35340516Swpaul CSR_WRITE_1(sc, RL_EECMD, \ 35440516Swpaul CSR_READ_1(sc, RL_EECMD) & ~x) 35540516Swpaul 35681713Swpaulstatic void 35781713Swpaulrl_dma_map_rxbuf(arg, segs, nseg, error) 35881713Swpaul void *arg; 35981713Swpaul bus_dma_segment_t *segs; 36081713Swpaul int nseg, error; 36181713Swpaul{ 36281713Swpaul struct rl_softc *sc; 36381713Swpaul 36481713Swpaul sc = arg; 36581713Swpaul CSR_WRITE_4(sc, RL_RXADDR, segs->ds_addr & 0xFFFFFFFF); 36681713Swpaul 36781713Swpaul return; 36881713Swpaul} 36981713Swpaul 37081713Swpaulstatic void 37181713Swpaulrl_dma_map_txbuf(arg, segs, nseg, error) 37281713Swpaul void *arg; 37381713Swpaul bus_dma_segment_t *segs; 37481713Swpaul int nseg, error; 37581713Swpaul{ 37681713Swpaul struct rl_softc *sc; 37781713Swpaul 37881713Swpaul sc = arg; 37981713Swpaul CSR_WRITE_4(sc, RL_CUR_TXADDR(sc), segs->ds_addr & 0xFFFFFFFF); 38081713Swpaul 38181713Swpaul return; 38281713Swpaul} 38381713Swpaul 38440516Swpaul/* 38540516Swpaul * Send a read command and address to the EEPROM, check for ACK. 38640516Swpaul */ 387102335Salfredstatic void 388102335Salfredrl_eeprom_putbyte(sc, addr) 38940516Swpaul struct rl_softc *sc; 39041656Swpaul int addr; 39140516Swpaul{ 39240516Swpaul register int d, i; 39340516Swpaul 39467931Swpaul d = addr | sc->rl_eecmd_read; 39540516Swpaul 39640516Swpaul /* 39755170Sbillf * Feed in each bit and strobe the clock. 39840516Swpaul */ 39940516Swpaul for (i = 0x400; i; i >>= 1) { 40040516Swpaul if (d & i) { 40140516Swpaul EE_SET(RL_EE_DATAIN); 40240516Swpaul } else { 40340516Swpaul EE_CLR(RL_EE_DATAIN); 40440516Swpaul } 40540516Swpaul DELAY(100); 40640516Swpaul EE_SET(RL_EE_CLK); 40740516Swpaul DELAY(150); 40840516Swpaul EE_CLR(RL_EE_CLK); 40940516Swpaul DELAY(100); 41040516Swpaul } 41140516Swpaul 41240516Swpaul return; 41340516Swpaul} 41440516Swpaul 41540516Swpaul/* 41640516Swpaul * Read a word of data stored in the EEPROM at address 'addr.' 41740516Swpaul */ 418102335Salfredstatic void 419102335Salfredrl_eeprom_getword(sc, addr, dest) 42040516Swpaul struct rl_softc *sc; 42141656Swpaul int addr; 42240516Swpaul u_int16_t *dest; 42340516Swpaul{ 42440516Swpaul register int i; 42540516Swpaul u_int16_t word = 0; 42640516Swpaul 42740516Swpaul /* Enter EEPROM access mode. */ 42840516Swpaul CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL); 42940516Swpaul 43040516Swpaul /* 43140516Swpaul * Send address of word we want to read. 43240516Swpaul */ 43340516Swpaul rl_eeprom_putbyte(sc, addr); 43440516Swpaul 43540516Swpaul CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL); 43640516Swpaul 43740516Swpaul /* 43840516Swpaul * Start reading bits from EEPROM. 43940516Swpaul */ 44040516Swpaul for (i = 0x8000; i; i >>= 1) { 44140516Swpaul EE_SET(RL_EE_CLK); 44240516Swpaul DELAY(100); 44340516Swpaul if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT) 44440516Swpaul word |= i; 44540516Swpaul EE_CLR(RL_EE_CLK); 44640516Swpaul DELAY(100); 44740516Swpaul } 44840516Swpaul 44940516Swpaul /* Turn off EEPROM access mode. */ 45040516Swpaul CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 45140516Swpaul 45240516Swpaul *dest = word; 45340516Swpaul 45440516Swpaul return; 45540516Swpaul} 45640516Swpaul 45740516Swpaul/* 45840516Swpaul * Read a sequence of words from the EEPROM. 45940516Swpaul */ 460102335Salfredstatic void 461102335Salfredrl_read_eeprom(sc, dest, off, cnt, swap) 46240516Swpaul struct rl_softc *sc; 46340516Swpaul caddr_t dest; 46440516Swpaul int off; 46540516Swpaul int cnt; 46640516Swpaul int swap; 46740516Swpaul{ 46840516Swpaul int i; 46940516Swpaul u_int16_t word = 0, *ptr; 47040516Swpaul 47140516Swpaul for (i = 0; i < cnt; i++) { 47240516Swpaul rl_eeprom_getword(sc, off + i, &word); 47340516Swpaul ptr = (u_int16_t *)(dest + (i * 2)); 47440516Swpaul if (swap) 47540516Swpaul *ptr = ntohs(word); 47640516Swpaul else 47740516Swpaul *ptr = word; 47840516Swpaul } 47940516Swpaul 48040516Swpaul return; 48140516Swpaul} 48240516Swpaul 48340516Swpaul 48440516Swpaul/* 48540516Swpaul * MII access routines are provided for the 8129, which 48640516Swpaul * doesn't have a built-in PHY. For the 8139, we fake things 48740516Swpaul * up by diverting rl_phy_readreg()/rl_phy_writereg() to the 48840516Swpaul * direct access PHY registers. 48940516Swpaul */ 49040516Swpaul#define MII_SET(x) \ 49140516Swpaul CSR_WRITE_1(sc, RL_MII, \ 492105221Sphk CSR_READ_1(sc, RL_MII) | (x)) 49340516Swpaul 49440516Swpaul#define MII_CLR(x) \ 49540516Swpaul CSR_WRITE_1(sc, RL_MII, \ 496105221Sphk CSR_READ_1(sc, RL_MII) & ~(x)) 49740516Swpaul 49840516Swpaul/* 49940516Swpaul * Sync the PHYs by setting data bit and strobing the clock 32 times. 50040516Swpaul */ 501102335Salfredstatic void 502102335Salfredrl_mii_sync(sc) 50340516Swpaul struct rl_softc *sc; 50440516Swpaul{ 50540516Swpaul register int i; 50640516Swpaul 50740516Swpaul MII_SET(RL_MII_DIR|RL_MII_DATAOUT); 50840516Swpaul 50940516Swpaul for (i = 0; i < 32; i++) { 51040516Swpaul MII_SET(RL_MII_CLK); 51140516Swpaul DELAY(1); 51240516Swpaul MII_CLR(RL_MII_CLK); 51340516Swpaul DELAY(1); 51440516Swpaul } 51540516Swpaul 51640516Swpaul return; 51740516Swpaul} 51840516Swpaul 51940516Swpaul/* 52040516Swpaul * Clock a series of bits through the MII. 52140516Swpaul */ 522102335Salfredstatic void 523102335Salfredrl_mii_send(sc, bits, cnt) 52440516Swpaul struct rl_softc *sc; 52540516Swpaul u_int32_t bits; 52640516Swpaul int cnt; 52740516Swpaul{ 52840516Swpaul int i; 52940516Swpaul 53040516Swpaul MII_CLR(RL_MII_CLK); 53140516Swpaul 53240516Swpaul for (i = (0x1 << (cnt - 1)); i; i >>= 1) { 533109109Sdes if (bits & i) { 53440516Swpaul MII_SET(RL_MII_DATAOUT); 535109109Sdes } else { 53640516Swpaul MII_CLR(RL_MII_DATAOUT); 537109109Sdes } 53840516Swpaul DELAY(1); 53940516Swpaul MII_CLR(RL_MII_CLK); 54040516Swpaul DELAY(1); 54140516Swpaul MII_SET(RL_MII_CLK); 54240516Swpaul } 54340516Swpaul} 54440516Swpaul 54540516Swpaul/* 54640516Swpaul * Read an PHY register through the MII. 54740516Swpaul */ 548102335Salfredstatic int 549102335Salfredrl_mii_readreg(sc, frame) 55040516Swpaul struct rl_softc *sc; 55140516Swpaul struct rl_mii_frame *frame; 552109109Sdes 55340516Swpaul{ 55467087Swpaul int i, ack; 55540516Swpaul 55667087Swpaul RL_LOCK(sc); 55740516Swpaul 55840516Swpaul /* 55940516Swpaul * Set up frame for RX. 56040516Swpaul */ 56140516Swpaul frame->mii_stdelim = RL_MII_STARTDELIM; 56240516Swpaul frame->mii_opcode = RL_MII_READOP; 56340516Swpaul frame->mii_turnaround = 0; 56440516Swpaul frame->mii_data = 0; 565109109Sdes 56640516Swpaul CSR_WRITE_2(sc, RL_MII, 0); 56740516Swpaul 56840516Swpaul /* 569109109Sdes * Turn on data xmit. 57040516Swpaul */ 57140516Swpaul MII_SET(RL_MII_DIR); 57240516Swpaul 57340516Swpaul rl_mii_sync(sc); 57440516Swpaul 57540516Swpaul /* 57640516Swpaul * Send command/address info. 57740516Swpaul */ 57840516Swpaul rl_mii_send(sc, frame->mii_stdelim, 2); 57940516Swpaul rl_mii_send(sc, frame->mii_opcode, 2); 58040516Swpaul rl_mii_send(sc, frame->mii_phyaddr, 5); 58140516Swpaul rl_mii_send(sc, frame->mii_regaddr, 5); 58240516Swpaul 58340516Swpaul /* Idle bit */ 58440516Swpaul MII_CLR((RL_MII_CLK|RL_MII_DATAOUT)); 58540516Swpaul DELAY(1); 58640516Swpaul MII_SET(RL_MII_CLK); 58740516Swpaul DELAY(1); 58840516Swpaul 58940516Swpaul /* Turn off xmit. */ 59040516Swpaul MII_CLR(RL_MII_DIR); 59140516Swpaul 59240516Swpaul /* Check for ack */ 59340516Swpaul MII_CLR(RL_MII_CLK); 59440516Swpaul DELAY(1); 595109058Smbr ack = CSR_READ_2(sc, RL_MII) & RL_MII_DATAIN; 59640516Swpaul MII_SET(RL_MII_CLK); 59740516Swpaul DELAY(1); 59840516Swpaul 59940516Swpaul /* 60040516Swpaul * Now try reading data bits. If the ack failed, we still 60140516Swpaul * need to clock through 16 cycles to keep the PHY(s) in sync. 60240516Swpaul */ 60340516Swpaul if (ack) { 60440516Swpaul for(i = 0; i < 16; i++) { 60540516Swpaul MII_CLR(RL_MII_CLK); 60640516Swpaul DELAY(1); 60740516Swpaul MII_SET(RL_MII_CLK); 60840516Swpaul DELAY(1); 60940516Swpaul } 61040516Swpaul goto fail; 61140516Swpaul } 61240516Swpaul 61340516Swpaul for (i = 0x8000; i; i >>= 1) { 61440516Swpaul MII_CLR(RL_MII_CLK); 61540516Swpaul DELAY(1); 61640516Swpaul if (!ack) { 61740516Swpaul if (CSR_READ_2(sc, RL_MII) & RL_MII_DATAIN) 61840516Swpaul frame->mii_data |= i; 61940516Swpaul DELAY(1); 62040516Swpaul } 62140516Swpaul MII_SET(RL_MII_CLK); 62240516Swpaul DELAY(1); 62340516Swpaul } 62440516Swpaul 62540516Swpaulfail: 62640516Swpaul 62740516Swpaul MII_CLR(RL_MII_CLK); 62840516Swpaul DELAY(1); 62940516Swpaul MII_SET(RL_MII_CLK); 63040516Swpaul DELAY(1); 63140516Swpaul 63267087Swpaul RL_UNLOCK(sc); 63340516Swpaul 63440516Swpaul if (ack) 63540516Swpaul return(1); 63640516Swpaul return(0); 63740516Swpaul} 63840516Swpaul 63940516Swpaul/* 64040516Swpaul * Write to a PHY register through the MII. 64140516Swpaul */ 642102335Salfredstatic int 643102335Salfredrl_mii_writereg(sc, frame) 64440516Swpaul struct rl_softc *sc; 64540516Swpaul struct rl_mii_frame *frame; 646109109Sdes 64740516Swpaul{ 64867087Swpaul RL_LOCK(sc); 64940516Swpaul 65040516Swpaul /* 65140516Swpaul * Set up frame for TX. 65240516Swpaul */ 65340516Swpaul 65440516Swpaul frame->mii_stdelim = RL_MII_STARTDELIM; 65540516Swpaul frame->mii_opcode = RL_MII_WRITEOP; 65640516Swpaul frame->mii_turnaround = RL_MII_TURNAROUND; 657109109Sdes 65840516Swpaul /* 659109109Sdes * Turn on data output. 66040516Swpaul */ 66140516Swpaul MII_SET(RL_MII_DIR); 66240516Swpaul 66340516Swpaul rl_mii_sync(sc); 66440516Swpaul 66540516Swpaul rl_mii_send(sc, frame->mii_stdelim, 2); 66640516Swpaul rl_mii_send(sc, frame->mii_opcode, 2); 66740516Swpaul rl_mii_send(sc, frame->mii_phyaddr, 5); 66840516Swpaul rl_mii_send(sc, frame->mii_regaddr, 5); 66940516Swpaul rl_mii_send(sc, frame->mii_turnaround, 2); 67040516Swpaul rl_mii_send(sc, frame->mii_data, 16); 67140516Swpaul 67240516Swpaul /* Idle bit. */ 67340516Swpaul MII_SET(RL_MII_CLK); 67440516Swpaul DELAY(1); 67540516Swpaul MII_CLR(RL_MII_CLK); 67640516Swpaul DELAY(1); 67740516Swpaul 67840516Swpaul /* 67940516Swpaul * Turn off xmit. 68040516Swpaul */ 68140516Swpaul MII_CLR(RL_MII_DIR); 68240516Swpaul 68367087Swpaul RL_UNLOCK(sc); 68440516Swpaul 68540516Swpaul return(0); 68640516Swpaul} 68740516Swpaul 688102335Salfredstatic int 689118586Swpaulrl_gmii_readreg(dev, phy, reg) 690118586Swpaul device_t dev; 691118586Swpaul int phy, reg; 692118586Swpaul{ 693118586Swpaul struct rl_softc *sc; 694118586Swpaul u_int32_t rval; 695118586Swpaul int i; 696118586Swpaul 697118586Swpaul if (phy != 1) 698118586Swpaul return(0); 699118586Swpaul 700118586Swpaul sc = device_get_softc(dev); 701118586Swpaul 702118586Swpaul CSR_WRITE_4(sc, RL_PHYAR, reg << 16); 703118586Swpaul DELAY(1000); 704118586Swpaul 705118586Swpaul for (i = 0; i < RL_TIMEOUT; i++) { 706118586Swpaul rval = CSR_READ_4(sc, RL_PHYAR); 707118586Swpaul if (rval & RL_PHYAR_BUSY) 708118586Swpaul break; 709118586Swpaul DELAY(100); 710118586Swpaul } 711118586Swpaul 712118586Swpaul if (i == RL_TIMEOUT) { 713118586Swpaul printf ("rl%d: PHY read failed\n", sc->rl_unit); 714118586Swpaul return (0); 715118586Swpaul } 716118586Swpaul 717118586Swpaul return (rval & RL_PHYAR_PHYDATA); 718118586Swpaul} 719118586Swpaul 720118586Swpaulstatic int 721118586Swpaulrl_gmii_writereg(dev, phy, reg, data) 722118586Swpaul device_t dev; 723118586Swpaul int phy, reg, data; 724118586Swpaul{ 725118586Swpaul struct rl_softc *sc; 726118586Swpaul u_int32_t rval; 727118586Swpaul int i; 728118586Swpaul 729118586Swpaul if (phy > 0) 730118586Swpaul return(0); 731118586Swpaul 732118586Swpaul sc = device_get_softc(dev); 733118586Swpaul 734118586Swpaul CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) | 735118586Swpaul (data | RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY); 736118586Swpaul DELAY(1000); 737118586Swpaul 738118586Swpaul for (i = 0; i < RL_TIMEOUT; i++) { 739118586Swpaul rval = CSR_READ_4(sc, RL_PHYAR); 740118586Swpaul if (!(rval & RL_PHYAR_BUSY)) 741118586Swpaul break; 742118586Swpaul DELAY(100); 743118586Swpaul } 744118586Swpaul 745118586Swpaul if (i == RL_TIMEOUT) { 746118586Swpaul printf ("rl%d: PHY write failed\n", sc->rl_unit); 747118586Swpaul return (0); 748118586Swpaul } 749118586Swpaul 750118586Swpaul return (0); 751118586Swpaul} 752118586Swpaul 753118586Swpaulstatic int 754102335Salfredrl_miibus_readreg(dev, phy, reg) 75550703Swpaul device_t dev; 75650703Swpaul int phy, reg; 75750703Swpaul{ 75840516Swpaul struct rl_softc *sc; 75940516Swpaul struct rl_mii_frame frame; 76040516Swpaul u_int16_t rval = 0; 76140516Swpaul u_int16_t rl8139_reg = 0; 76240516Swpaul 76350703Swpaul sc = device_get_softc(dev); 76467087Swpaul RL_LOCK(sc); 76550703Swpaul 766118586Swpaul if (sc->rl_type == RL_8169) { 767118586Swpaul rval = rl_gmii_readreg(dev, phy, reg); 768118586Swpaul RL_UNLOCK(sc); 769118586Swpaul return (rval); 770118586Swpaul } 771118586Swpaul 772117388Swpaul if (sc->rl_type == RL_8139 || sc->rl_type == RL_8139CPLUS) { 77350703Swpaul /* Pretend the internal PHY is only at address 0 */ 77467087Swpaul if (phy) { 77567087Swpaul RL_UNLOCK(sc); 77650703Swpaul return(0); 77767087Swpaul } 77840516Swpaul switch(reg) { 77950703Swpaul case MII_BMCR: 78040516Swpaul rl8139_reg = RL_BMCR; 78140516Swpaul break; 78250703Swpaul case MII_BMSR: 78340516Swpaul rl8139_reg = RL_BMSR; 78440516Swpaul break; 78550703Swpaul case MII_ANAR: 78640516Swpaul rl8139_reg = RL_ANAR; 78740516Swpaul break; 78850703Swpaul case MII_ANER: 78950703Swpaul rl8139_reg = RL_ANER; 79050703Swpaul break; 79150703Swpaul case MII_ANLPAR: 79240516Swpaul rl8139_reg = RL_LPAR; 79340516Swpaul break; 79450703Swpaul case MII_PHYIDR1: 79550703Swpaul case MII_PHYIDR2: 79667087Swpaul RL_UNLOCK(sc); 79750703Swpaul return(0); 79894149Swpaul /* 79994149Swpaul * Allow the rlphy driver to read the media status 80094149Swpaul * register. If we have a link partner which does not 80194149Swpaul * support NWAY, this is the register which will tell 80294149Swpaul * us the results of parallel detection. 80394149Swpaul */ 80494149Swpaul case RL_MEDIASTAT: 80594149Swpaul rval = CSR_READ_1(sc, RL_MEDIASTAT); 80694149Swpaul RL_UNLOCK(sc); 80794149Swpaul return(rval); 80840516Swpaul default: 80940516Swpaul printf("rl%d: bad phy register\n", sc->rl_unit); 81067087Swpaul RL_UNLOCK(sc); 81140516Swpaul return(0); 81240516Swpaul } 81340516Swpaul rval = CSR_READ_2(sc, rl8139_reg); 81467087Swpaul RL_UNLOCK(sc); 81540516Swpaul return(rval); 81640516Swpaul } 81740516Swpaul 81840516Swpaul bzero((char *)&frame, sizeof(frame)); 81940516Swpaul 82050703Swpaul frame.mii_phyaddr = phy; 82140516Swpaul frame.mii_regaddr = reg; 82240516Swpaul rl_mii_readreg(sc, &frame); 82367087Swpaul RL_UNLOCK(sc); 82440516Swpaul 82540516Swpaul return(frame.mii_data); 82640516Swpaul} 82740516Swpaul 828102335Salfredstatic int 829102335Salfredrl_miibus_writereg(dev, phy, reg, data) 83050703Swpaul device_t dev; 83150703Swpaul int phy, reg, data; 83250703Swpaul{ 83340516Swpaul struct rl_softc *sc; 83440516Swpaul struct rl_mii_frame frame; 83540516Swpaul u_int16_t rl8139_reg = 0; 836118586Swpaul int rval = 0; 83740516Swpaul 83850703Swpaul sc = device_get_softc(dev); 83967087Swpaul RL_LOCK(sc); 84050703Swpaul 841118586Swpaul if (sc->rl_type == RL_8169) { 842118586Swpaul rval = rl_gmii_writereg(dev, phy, reg, data); 843118586Swpaul RL_UNLOCK(sc); 844118586Swpaul return (rval); 845118586Swpaul } 846118586Swpaul 847117388Swpaul if (sc->rl_type == RL_8139 || sc->rl_type == RL_8139CPLUS) { 84850703Swpaul /* Pretend the internal PHY is only at address 0 */ 84967087Swpaul if (phy) { 85067087Swpaul RL_UNLOCK(sc); 85150703Swpaul return(0); 85267087Swpaul } 85340516Swpaul switch(reg) { 85450703Swpaul case MII_BMCR: 85540516Swpaul rl8139_reg = RL_BMCR; 85640516Swpaul break; 85750703Swpaul case MII_BMSR: 85840516Swpaul rl8139_reg = RL_BMSR; 85940516Swpaul break; 86050703Swpaul case MII_ANAR: 86140516Swpaul rl8139_reg = RL_ANAR; 86240516Swpaul break; 86350703Swpaul case MII_ANER: 86450703Swpaul rl8139_reg = RL_ANER; 86550703Swpaul break; 86650703Swpaul case MII_ANLPAR: 86740516Swpaul rl8139_reg = RL_LPAR; 86840516Swpaul break; 86950703Swpaul case MII_PHYIDR1: 87050703Swpaul case MII_PHYIDR2: 87167087Swpaul RL_UNLOCK(sc); 87250703Swpaul return(0); 87350703Swpaul break; 87440516Swpaul default: 87540516Swpaul printf("rl%d: bad phy register\n", sc->rl_unit); 87667087Swpaul RL_UNLOCK(sc); 87750703Swpaul return(0); 87840516Swpaul } 87940516Swpaul CSR_WRITE_2(sc, rl8139_reg, data); 88067087Swpaul RL_UNLOCK(sc); 88150703Swpaul return(0); 88240516Swpaul } 88340516Swpaul 88440516Swpaul bzero((char *)&frame, sizeof(frame)); 88540516Swpaul 88650703Swpaul frame.mii_phyaddr = phy; 88740516Swpaul frame.mii_regaddr = reg; 88840516Swpaul frame.mii_data = data; 88940516Swpaul 89040516Swpaul rl_mii_writereg(sc, &frame); 89140516Swpaul 89267087Swpaul RL_UNLOCK(sc); 89350703Swpaul return(0); 89450703Swpaul} 89550703Swpaul 896102335Salfredstatic void 897102335Salfredrl_miibus_statchg(dev) 89850703Swpaul device_t dev; 89950703Swpaul{ 90040516Swpaul return; 90140516Swpaul} 90240516Swpaul 90340516Swpaul/* 90443062Swpaul * Calculate CRC of a multicast group address, return the upper 6 bits. 90540516Swpaul */ 906102335Salfredstatic u_int8_t 907102335Salfredrl_calchash(addr) 90841656Swpaul caddr_t addr; 90940516Swpaul{ 91040516Swpaul u_int32_t crc, carry; 91140516Swpaul int i, j; 91240516Swpaul u_int8_t c; 91340516Swpaul 91440516Swpaul /* Compute CRC for the address value. */ 91540516Swpaul crc = 0xFFFFFFFF; /* initial value */ 91640516Swpaul 91740516Swpaul for (i = 0; i < 6; i++) { 91840516Swpaul c = *(addr + i); 91940516Swpaul for (j = 0; j < 8; j++) { 92040516Swpaul carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01); 92140516Swpaul crc <<= 1; 92240516Swpaul c >>= 1; 92340516Swpaul if (carry) 92440516Swpaul crc = (crc ^ 0x04c11db6) | carry; 92540516Swpaul } 92640516Swpaul } 92740516Swpaul 92840516Swpaul /* return the filter bit position */ 92943062Swpaul return(crc >> 26); 93040516Swpaul} 93140516Swpaul 93240516Swpaul/* 93340516Swpaul * Program the 64-bit multicast hash filter. 93440516Swpaul */ 935102335Salfredstatic void 936102335Salfredrl_setmulti(sc) 93740516Swpaul struct rl_softc *sc; 93840516Swpaul{ 93940516Swpaul struct ifnet *ifp; 94040516Swpaul int h = 0; 94140516Swpaul u_int32_t hashes[2] = { 0, 0 }; 94240516Swpaul struct ifmultiaddr *ifma; 94340516Swpaul u_int32_t rxfilt; 94440516Swpaul int mcnt = 0; 94540516Swpaul 94640516Swpaul ifp = &sc->arpcom.ac_if; 94740516Swpaul 94840516Swpaul rxfilt = CSR_READ_4(sc, RL_RXCFG); 94940516Swpaul 95043062Swpaul if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 95140516Swpaul rxfilt |= RL_RXCFG_RX_MULTI; 95240516Swpaul CSR_WRITE_4(sc, RL_RXCFG, rxfilt); 95340516Swpaul CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF); 95440516Swpaul CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF); 95540516Swpaul return; 95640516Swpaul } 95740516Swpaul 95840516Swpaul /* first, zot all the existing hash bits */ 95940516Swpaul CSR_WRITE_4(sc, RL_MAR0, 0); 96040516Swpaul CSR_WRITE_4(sc, RL_MAR4, 0); 96140516Swpaul 96240516Swpaul /* now program new ones */ 96372084Sphk TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 96440516Swpaul if (ifma->ifma_addr->sa_family != AF_LINK) 96540516Swpaul continue; 96640516Swpaul h = rl_calchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr)); 96740516Swpaul if (h < 32) 96840516Swpaul hashes[0] |= (1 << h); 96940516Swpaul else 97040516Swpaul hashes[1] |= (1 << (h - 32)); 97140516Swpaul mcnt++; 97240516Swpaul } 97340516Swpaul 97440516Swpaul if (mcnt) 97540516Swpaul rxfilt |= RL_RXCFG_RX_MULTI; 97640516Swpaul else 97740516Swpaul rxfilt &= ~RL_RXCFG_RX_MULTI; 97840516Swpaul 97940516Swpaul CSR_WRITE_4(sc, RL_RXCFG, rxfilt); 98040516Swpaul CSR_WRITE_4(sc, RL_MAR0, hashes[0]); 98140516Swpaul CSR_WRITE_4(sc, RL_MAR4, hashes[1]); 98240516Swpaul 98340516Swpaul return; 98440516Swpaul} 98540516Swpaul 986102335Salfredstatic void 987102335Salfredrl_reset(sc) 98840516Swpaul struct rl_softc *sc; 98940516Swpaul{ 99040516Swpaul register int i; 99140516Swpaul 99240516Swpaul CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET); 99340516Swpaul 99440516Swpaul for (i = 0; i < RL_TIMEOUT; i++) { 99540516Swpaul DELAY(10); 99640516Swpaul if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET)) 99740516Swpaul break; 99840516Swpaul } 99940516Swpaul if (i == RL_TIMEOUT) 100040516Swpaul printf("rl%d: reset never completed!\n", sc->rl_unit); 100140516Swpaul 1002118586Swpaul CSR_WRITE_1(sc, 0x82, 1); 1003118586Swpaul 1004109109Sdes return; 100540516Swpaul} 100640516Swpaul 100740516Swpaul/* 100840516Swpaul * Probe for a RealTek 8129/8139 chip. Check the PCI vendor and device 100940516Swpaul * IDs against our list and return a device name if we find a match. 101040516Swpaul */ 1011102335Salfredstatic int 1012102335Salfredrl_probe(dev) 101350703Swpaul device_t dev; 101440516Swpaul{ 101540516Swpaul struct rl_type *t; 1016117388Swpaul struct rl_softc *sc; 1017117388Swpaul struct rl_hwrev *hw_rev; 1018117388Swpaul int rid; 1019117388Swpaul u_int32_t hwrev; 1020117388Swpaul char desc[64]; 102140516Swpaul 102240516Swpaul t = rl_devs; 1023117388Swpaul sc = device_get_softc(dev); 102440516Swpaul 102540516Swpaul while(t->rl_name != NULL) { 102650703Swpaul if ((pci_get_vendor(dev) == t->rl_vid) && 102750703Swpaul (pci_get_device(dev) == t->rl_did)) { 1028117388Swpaul 1029117388Swpaul /* 1030117388Swpaul * Temporarily map the I/O space 1031117388Swpaul * so we can read the chip ID register. 1032117388Swpaul */ 1033117388Swpaul rid = RL_RID; 1034117388Swpaul sc->rl_res = bus_alloc_resource(dev, RL_RES, &rid, 1035117388Swpaul 0, ~0, 1, RF_ACTIVE); 1036117388Swpaul if (sc->rl_res == NULL) { 1037117388Swpaul device_printf(dev, 1038117388Swpaul "couldn't map ports/memory\n"); 1039117388Swpaul return(ENXIO); 1040117388Swpaul } 1041117388Swpaul sc->rl_btag = rman_get_bustag(sc->rl_res); 1042117388Swpaul sc->rl_bhandle = rman_get_bushandle(sc->rl_res); 1043117388Swpaul mtx_init(&sc->rl_mtx, 1044117388Swpaul device_get_nameunit(dev), 1045117388Swpaul MTX_NETWORK_LOCK, MTX_DEF); 1046117388Swpaul RL_LOCK(sc); 1047117388Swpaul if (t->rl_basetype == RL_8139) { 1048117388Swpaul hwrev = CSR_READ_4(sc, RL_TXCFG) & 1049117388Swpaul RL_TXCFG_HWREV; 1050117388Swpaul hw_rev = rl_hwrevs; 1051117388Swpaul while (hw_rev->rl_desc != NULL) { 1052117388Swpaul if (hw_rev->rl_rev == hwrev) { 1053117388Swpaul sprintf(desc, "%s, rev. %s", 1054117388Swpaul t->rl_name, 1055117388Swpaul hw_rev->rl_desc); 1056117388Swpaul sc->rl_type = hw_rev->rl_type; 1057117388Swpaul break; 1058117388Swpaul } 1059117388Swpaul hw_rev++; 1060117388Swpaul } 1061117388Swpaul if (hw_rev->rl_desc == NULL) 1062117388Swpaul sprintf(desc, "%s, rev. %s", 1063117388Swpaul t->rl_name, "unknown"); 1064118586Swpaul } else 1065118586Swpaul sprintf(desc, "%s", t->rl_name); 1066117388Swpaul bus_release_resource(dev, RL_RES, 1067117388Swpaul RL_RID, sc->rl_res); 1068117388Swpaul RL_UNLOCK(sc); 1069117388Swpaul mtx_destroy(&sc->rl_mtx); 1070117388Swpaul device_set_desc_copy(dev, desc); 107150703Swpaul return(0); 107240516Swpaul } 107340516Swpaul t++; 107440516Swpaul } 107540516Swpaul 107650703Swpaul return(ENXIO); 107740516Swpaul} 107840516Swpaul 107940516Swpaul/* 1080117388Swpaul * This routine takes the segment list provided as the result of 1081117388Swpaul * a bus_dma_map_load() operation and assigns the addresses/lengths 1082117388Swpaul * to RealTek DMA descriptors. This can be called either by the RX 1083117388Swpaul * code or the TX code. In the RX case, we'll probably wind up mapping 1084117388Swpaul * at most one segment. For the TX case, there could be any number of 1085117388Swpaul * segments since TX packets may span multiple mbufs. In either case, 1086117388Swpaul * if the number of segments is larger than the rl_maxsegs limit 1087117388Swpaul * specified by the caller, we abort the mapping operation. Sadly, 1088117388Swpaul * whoever designed the buffer mapping API did not provide a way to 1089117388Swpaul * return an error from here, so we have to fake it a bit. 1090117388Swpaul */ 1091117388Swpaul 1092117388Swpaulstatic void 1093117388Swpaulrl_dma_map_desc(arg, segs, nseg, mapsize, error) 1094117388Swpaul void *arg; 1095117388Swpaul bus_dma_segment_t *segs; 1096117388Swpaul int nseg; 1097117388Swpaul bus_size_t mapsize; 1098117388Swpaul int error; 1099117388Swpaul{ 1100117388Swpaul struct rl_dmaload_arg *ctx; 1101117388Swpaul struct rl_desc *d = NULL; 1102117388Swpaul int i = 0, idx; 1103117388Swpaul 1104117388Swpaul if (error) 1105117388Swpaul return; 1106117388Swpaul 1107117388Swpaul ctx = arg; 1108117388Swpaul 1109117388Swpaul /* Signal error to caller if there's too many segments */ 1110117388Swpaul if (nseg > ctx->rl_maxsegs) { 1111117388Swpaul ctx->rl_maxsegs = 0; 1112117388Swpaul return; 1113117388Swpaul } 1114117388Swpaul 1115117388Swpaul /* 1116117388Swpaul * Map the segment array into descriptors. Note that we set the 1117117388Swpaul * start-of-frame and end-of-frame markers for either TX or RX, but 1118117388Swpaul * they really only have meaning in the TX case. (In the RX case, 1119117388Swpaul * it's the chip that tells us where packets begin and end.) 1120117388Swpaul * We also keep track of the end of the ring and set the 1121117388Swpaul * end-of-ring bits as needed, and we set the ownership bits 1122117388Swpaul * in all except the very first descriptor. (The caller will 1123117388Swpaul * set this descriptor later when it start transmission or 1124117388Swpaul * reception.) 1125117388Swpaul */ 1126117388Swpaul idx = ctx->rl_idx; 1127117388Swpaul while(1) { 1128117388Swpaul u_int32_t cmdstat; 1129117388Swpaul d = &ctx->rl_ring[idx]; 1130117388Swpaul if (le32toh(d->rl_cmdstat) & RL_RDESC_STAT_OWN) { 1131117388Swpaul ctx->rl_maxsegs = 0; 1132117388Swpaul return; 1133117388Swpaul } 1134117388Swpaul cmdstat = segs[i].ds_len; 1135118712Swpaul d->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr)); 1136118712Swpaul d->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr)); 1137117388Swpaul if (i == 0) 1138117388Swpaul cmdstat |= RL_TDESC_CMD_SOF; 1139117388Swpaul else 1140117388Swpaul cmdstat |= RL_TDESC_CMD_OWN; 1141118586Swpaul if (idx == (RL_RX_DESC_CNT - 1)) 1142117388Swpaul cmdstat |= RL_TDESC_CMD_EOR; 1143118889Swpaul d->rl_cmdstat = htole32(cmdstat | ctx->rl_flags); 1144117388Swpaul i++; 1145117388Swpaul if (i == nseg) 1146117388Swpaul break; 1147117388Swpaul RL_DESC_INC(idx); 1148117388Swpaul } 1149117388Swpaul 1150117388Swpaul d->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF); 1151117388Swpaul ctx->rl_maxsegs = nseg; 1152117388Swpaul ctx->rl_idx = idx; 1153117388Swpaul 1154117388Swpaul return; 1155117388Swpaul} 1156117388Swpaul 1157117388Swpaul/* 1158117388Swpaul * Map a single buffer address. 1159117388Swpaul */ 1160117388Swpaul 1161117388Swpaulstatic void 1162117388Swpaulrl_dma_map_addr(arg, segs, nseg, error) 1163117388Swpaul void *arg; 1164117388Swpaul bus_dma_segment_t *segs; 1165117388Swpaul int nseg; 1166117388Swpaul int error; 1167117388Swpaul{ 1168117388Swpaul u_int32_t *addr; 1169117388Swpaul 1170117388Swpaul if (error) 1171117388Swpaul return; 1172117388Swpaul 1173117388Swpaul KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 1174117388Swpaul addr = arg; 1175117388Swpaul *addr = segs->ds_addr; 1176117388Swpaul 1177117388Swpaul return; 1178117388Swpaul} 1179117388Swpaul 1180117388Swpaulstatic int 1181117388Swpaulrl_allocmem(dev, sc) 1182117388Swpaul device_t dev; 1183117388Swpaul struct rl_softc *sc; 1184117388Swpaul{ 1185117388Swpaul int error; 1186117388Swpaul 1187117388Swpaul /* 1188117388Swpaul * Now allocate a tag for the DMA descriptor lists. 1189117388Swpaul * All of our lists are allocated as a contiguous block 1190117388Swpaul * of memory. 1191117388Swpaul */ 1192117388Swpaul error = bus_dma_tag_create(sc->rl_parent_tag, /* parent */ 1193117388Swpaul 1, 0, /* alignment, boundary */ 1194117388Swpaul BUS_SPACE_MAXADDR, /* lowaddr */ 1195117388Swpaul BUS_SPACE_MAXADDR, /* highaddr */ 1196117388Swpaul NULL, NULL, /* filter, filterarg */ 1197117388Swpaul RL_RXBUFLEN + 1518, 1, /* maxsize,nsegments */ 1198117388Swpaul BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */ 1199117388Swpaul 0, /* flags */ 1200117388Swpaul NULL, NULL, /* lockfunc, lockarg */ 1201117388Swpaul &sc->rl_tag); 1202117388Swpaul if (error) 1203117388Swpaul return(error); 1204117388Swpaul 1205117388Swpaul /* 1206117388Swpaul * Now allocate a chunk of DMA-able memory based on the 1207117388Swpaul * tag we just created. 1208117388Swpaul */ 1209117388Swpaul error = bus_dmamem_alloc(sc->rl_tag, 1210117388Swpaul (void **)&sc->rl_cdata.rl_rx_buf, BUS_DMA_NOWAIT, 1211117388Swpaul &sc->rl_cdata.rl_rx_dmamap); 1212117388Swpaul 1213117388Swpaul if (error) { 1214117388Swpaul printf("rl%d: no memory for list buffers!\n", sc->rl_unit); 1215117388Swpaul bus_dma_tag_destroy(sc->rl_tag); 1216117388Swpaul sc->rl_tag = NULL; 1217117388Swpaul return(error); 1218117388Swpaul } 1219117388Swpaul 1220117388Swpaul /* Leave a few bytes before the start of the RX ring buffer. */ 1221117388Swpaul sc->rl_cdata.rl_rx_buf_ptr = sc->rl_cdata.rl_rx_buf; 1222117388Swpaul sc->rl_cdata.rl_rx_buf += sizeof(u_int64_t); 1223117388Swpaul 1224117388Swpaul return(0); 1225117388Swpaul} 1226117388Swpaul 1227117388Swpaulstatic int 1228117388Swpaulrl_allocmemcplus(dev, sc) 1229117388Swpaul device_t dev; 1230117388Swpaul struct rl_softc *sc; 1231117388Swpaul{ 1232117388Swpaul int error; 1233117388Swpaul int nseg; 1234117388Swpaul int i; 1235117388Swpaul 1236117388Swpaul /* 1237117388Swpaul * Allocate map for RX mbufs. 1238117388Swpaul */ 1239117388Swpaul nseg = 32; 1240117388Swpaul error = bus_dma_tag_create(sc->rl_parent_tag, ETHER_ALIGN, 0, 1241117388Swpaul BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 1242117388Swpaul NULL, MCLBYTES * nseg, nseg, MCLBYTES, 0, NULL, NULL, 1243117388Swpaul &sc->rl_ldata.rl_mtag); 1244117388Swpaul if (error) { 1245117388Swpaul device_printf(dev, "could not allocate dma tag\n"); 1246117388Swpaul return (ENOMEM); 1247117388Swpaul } 1248117388Swpaul 1249117388Swpaul /* 1250117388Swpaul * Allocate map for TX descriptor list. 1251117388Swpaul */ 1252117388Swpaul error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN, 1253117388Swpaul 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 1254117388Swpaul NULL, RL_TX_LIST_SZ, 1, RL_TX_LIST_SZ, 0, NULL, NULL, 1255117388Swpaul &sc->rl_ldata.rl_tx_list_tag); 1256117388Swpaul if (error) { 1257117388Swpaul device_printf(dev, "could not allocate dma tag\n"); 1258117388Swpaul return (ENOMEM); 1259117388Swpaul } 1260117388Swpaul 1261117388Swpaul /* Allocate DMA'able memory for the TX ring */ 1262117388Swpaul 1263117388Swpaul error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag, 1264118089Smux (void **)&sc->rl_ldata.rl_tx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO, 1265117388Swpaul &sc->rl_ldata.rl_tx_list_map); 1266117388Swpaul if (error) 1267117388Swpaul return (ENOMEM); 1268117388Swpaul 1269117388Swpaul /* Load the map for the TX ring. */ 1270117388Swpaul 1271117388Swpaul error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag, 1272117388Swpaul sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list, 1273117388Swpaul RL_TX_LIST_SZ, rl_dma_map_addr, 1274117388Swpaul &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT); 1275117388Swpaul 1276117388Swpaul /* Create DMA maps for TX buffers */ 1277117388Swpaul 1278117388Swpaul for (i = 0; i < RL_TX_DESC_CNT; i++) { 1279117388Swpaul error = bus_dmamap_create(sc->rl_ldata.rl_mtag, 0, 1280117388Swpaul &sc->rl_ldata.rl_tx_dmamap[i]); 1281117388Swpaul if (error) { 1282117388Swpaul device_printf(dev, "can't create DMA map for TX\n"); 1283117388Swpaul return(ENOMEM); 1284117388Swpaul } 1285117388Swpaul } 1286117388Swpaul 1287117388Swpaul /* 1288117388Swpaul * Allocate map for RX descriptor list. 1289117388Swpaul */ 1290117388Swpaul error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN, 1291117388Swpaul 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 1292117388Swpaul NULL, RL_TX_LIST_SZ, 1, RL_TX_LIST_SZ, 0, NULL, NULL, 1293117388Swpaul &sc->rl_ldata.rl_rx_list_tag); 1294117388Swpaul if (error) { 1295117388Swpaul device_printf(dev, "could not allocate dma tag\n"); 1296117388Swpaul return (ENOMEM); 1297117388Swpaul } 1298117388Swpaul 1299117388Swpaul /* Allocate DMA'able memory for the RX ring */ 1300117388Swpaul 1301117388Swpaul error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag, 1302118089Smux (void **)&sc->rl_ldata.rl_rx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO, 1303117388Swpaul &sc->rl_ldata.rl_rx_list_map); 1304117388Swpaul if (error) 1305117388Swpaul return (ENOMEM); 1306117388Swpaul 1307117388Swpaul /* Load the map for the RX ring. */ 1308117388Swpaul 1309117388Swpaul error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag, 1310117388Swpaul sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list, 1311117388Swpaul RL_TX_LIST_SZ, rl_dma_map_addr, 1312117388Swpaul &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT); 1313117388Swpaul 1314117388Swpaul /* Create DMA maps for RX buffers */ 1315117388Swpaul 1316117388Swpaul for (i = 0; i < RL_RX_DESC_CNT; i++) { 1317117388Swpaul error = bus_dmamap_create(sc->rl_ldata.rl_mtag, 0, 1318117388Swpaul &sc->rl_ldata.rl_rx_dmamap[i]); 1319117388Swpaul if (error) { 1320117388Swpaul device_printf(dev, "can't create DMA map for RX\n"); 1321117388Swpaul return(ENOMEM); 1322117388Swpaul } 1323117388Swpaul } 1324117388Swpaul 1325117388Swpaul return(0); 1326117388Swpaul} 1327117388Swpaul 1328117388Swpaul/* 132940516Swpaul * Attach the interface. Allocate softc structures, do ifmedia 133040516Swpaul * setup and ethernet/BPF attach. 133140516Swpaul */ 1332102335Salfredstatic int 1333102335Salfredrl_attach(dev) 133450703Swpaul device_t dev; 133540516Swpaul{ 133640516Swpaul u_char eaddr[ETHER_ADDR_LEN]; 1337108729Sjake u_int16_t as[3]; 133840516Swpaul struct rl_softc *sc; 133940516Swpaul struct ifnet *ifp; 1340117388Swpaul struct rl_type *t; 1341117388Swpaul struct rl_hwrev *hw_rev; 1342117388Swpaul int hwrev; 134340516Swpaul u_int16_t rl_did = 0; 1344108729Sjake int unit, error = 0, rid, i; 134540516Swpaul 134650703Swpaul sc = device_get_softc(dev); 134750703Swpaul unit = device_get_unit(dev); 134840516Swpaul 134993818Sjhb mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 135093818Sjhb MTX_DEF | MTX_RECURSE); 1351117208Simp#ifndef BURN_BRIDGES 135240516Swpaul /* 135340516Swpaul * Handle power management nonsense. 135440516Swpaul */ 135540516Swpaul 135670167Swpaul if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) { 135770167Swpaul u_int32_t iobase, membase, irq; 135840516Swpaul 135970167Swpaul /* Save important PCI config data. */ 136070167Swpaul iobase = pci_read_config(dev, RL_PCI_LOIO, 4); 136170167Swpaul membase = pci_read_config(dev, RL_PCI_LOMEM, 4); 136270167Swpaul irq = pci_read_config(dev, RL_PCI_INTLINE, 4); 136340516Swpaul 136470167Swpaul /* Reset the power state. */ 136570167Swpaul printf("rl%d: chip is is in D%d power mode " 136670167Swpaul "-- setting to D0\n", unit, 136770167Swpaul pci_get_powerstate(dev)); 136840516Swpaul 136970167Swpaul pci_set_powerstate(dev, PCI_POWERSTATE_D0); 137040516Swpaul 137170167Swpaul /* Restore PCI config data. */ 137270167Swpaul pci_write_config(dev, RL_PCI_LOIO, iobase, 4); 137370167Swpaul pci_write_config(dev, RL_PCI_LOMEM, membase, 4); 137470167Swpaul pci_write_config(dev, RL_PCI_INTLINE, irq, 4); 137540516Swpaul } 1376117208Simp#endif 137740516Swpaul /* 137840516Swpaul * Map control/status registers. 137940516Swpaul */ 138072813Swpaul pci_enable_busmaster(dev); 138140516Swpaul 1382109109Sdes rid = RL_RID; 138350703Swpaul sc->rl_res = bus_alloc_resource(dev, RL_RES, &rid, 138450703Swpaul 0, ~0, 1, RF_ACTIVE); 138550703Swpaul 138650703Swpaul if (sc->rl_res == NULL) { 138750703Swpaul printf ("rl%d: couldn't map ports/memory\n", unit); 138850703Swpaul error = ENXIO; 138940516Swpaul goto fail; 139040516Swpaul } 139140516Swpaul 1392117388Swpaul#ifdef notdef 139369127Sroger /* Detect the Realtek 8139B. For some reason, this chip is very 139469127Sroger * unstable when left to autoselect the media 139569127Sroger * The best workaround is to set the device to the required 139669127Sroger * media type or to set it to the 10 Meg speed. 139769127Sroger */ 139869127Sroger 139969127Sroger if ((rman_get_end(sc->rl_res)-rman_get_start(sc->rl_res))==0xff) { 1400117388Swpaul printf("rl%d: Realtek 8139B detected. Warning," 1401117388Swpaul " this may be unstable in autoselect mode\n", unit); 140269127Sroger } 1403117388Swpaul#endif 140469127Sroger 140550703Swpaul sc->rl_btag = rman_get_bustag(sc->rl_res); 140650703Swpaul sc->rl_bhandle = rman_get_bushandle(sc->rl_res); 140750703Swpaul 1408112872Snjl /* Allocate interrupt */ 140950703Swpaul rid = 0; 141050703Swpaul sc->rl_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 141150703Swpaul RF_SHAREABLE | RF_ACTIVE); 141250703Swpaul 141350703Swpaul if (sc->rl_irq == NULL) { 141440516Swpaul printf("rl%d: couldn't map interrupt\n", unit); 141550703Swpaul error = ENXIO; 141640516Swpaul goto fail; 141740516Swpaul } 141840516Swpaul 141940516Swpaul /* Reset the adapter. */ 142040516Swpaul rl_reset(sc); 142167931Swpaul sc->rl_eecmd_read = RL_EECMD_READ_6BIT; 142267931Swpaul rl_read_eeprom(sc, (caddr_t)&rl_did, 0, 1, 0); 142368215Swpaul if (rl_did != 0x8129) 142467931Swpaul sc->rl_eecmd_read = RL_EECMD_READ_8BIT; 142540516Swpaul 142640516Swpaul /* 142740516Swpaul * Get station address from the EEPROM. 142840516Swpaul */ 1429108729Sjake rl_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3, 0); 1430108729Sjake for (i = 0; i < 3; i++) { 1431108729Sjake eaddr[(i * 2) + 0] = as[i] & 0xff; 1432108729Sjake eaddr[(i * 2) + 1] = as[i] >> 8; 1433108729Sjake } 143440516Swpaul 143540516Swpaul /* 143640516Swpaul * A RealTek chip was detected. Inform the world. 143740516Swpaul */ 143840516Swpaul printf("rl%d: Ethernet address: %6D\n", unit, eaddr, ":"); 143940516Swpaul 144040516Swpaul sc->rl_unit = unit; 144140516Swpaul bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); 144240516Swpaul 144340516Swpaul /* 144440516Swpaul * Now read the exact device type from the EEPROM to find 144540516Swpaul * out if it's an 8129 or 8139. 144640516Swpaul */ 144740516Swpaul rl_read_eeprom(sc, (caddr_t)&rl_did, RL_EE_PCI_DID, 1, 0); 144840516Swpaul 1449117388Swpaul t = rl_devs; 1450117388Swpaul while(t->rl_name != NULL) { 1451117388Swpaul if (rl_did == t->rl_did) { 1452117388Swpaul sc->rl_type = t->rl_basetype; 1453117388Swpaul break; 1454117388Swpaul } 1455117388Swpaul t++; 1456117388Swpaul } 1457117388Swpaul if (t->rl_name == NULL) { 145840516Swpaul printf("rl%d: unknown device ID: %x\n", unit, rl_did); 145950703Swpaul error = ENXIO; 146040516Swpaul goto fail; 146140516Swpaul } 1462117388Swpaul if (sc->rl_type == RL_8139) { 1463117388Swpaul hw_rev = rl_hwrevs; 1464117388Swpaul hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV; 1465117388Swpaul while (hw_rev->rl_desc != NULL) { 1466117388Swpaul if (hw_rev->rl_rev == hwrev) { 1467117388Swpaul sc->rl_type = hw_rev->rl_type; 1468117388Swpaul break; 1469117388Swpaul } 1470117388Swpaul hw_rev++; 1471117388Swpaul } 1472117388Swpaul if (hw_rev->rl_desc == NULL) { 1473117388Swpaul printf("rl%d: unknown hwrev: %x\n", unit, hwrev); 1474117388Swpaul } 1475117388Swpaul } else if (rl_did == RT_DEVICEID_8129) { 1476117388Swpaul sc->rl_type = RL_8129; 1477117388Swpaul } else if (rl_did == RT_DEVICEID_8169) { 1478117388Swpaul sc->rl_type = RL_8169; 1479117388Swpaul } 148040516Swpaul 148181713Swpaul /* 148281713Swpaul * Allocate the parent bus DMA tag appropriate for PCI. 148381713Swpaul */ 148481713Swpaul#define RL_NSEG_NEW 32 1485109109Sdes error = bus_dma_tag_create(NULL, /* parent */ 148681713Swpaul 1, 0, /* alignment, boundary */ 148781713Swpaul BUS_SPACE_MAXADDR_32BIT,/* lowaddr */ 148881713Swpaul BUS_SPACE_MAXADDR, /* highaddr */ 148981713Swpaul NULL, NULL, /* filter, filterarg */ 149081713Swpaul MAXBSIZE, RL_NSEG_NEW, /* maxsize, nsegments */ 1491109109Sdes BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */ 149281713Swpaul BUS_DMA_ALLOCNOW, /* flags */ 1493117126Sscottl NULL, NULL, /* lockfunc, lockarg */ 149481713Swpaul &sc->rl_parent_tag); 1495112872Snjl if (error) 1496112872Snjl goto fail; 149740516Swpaul 149881713Swpaul /* 1499117388Swpaul * If this is an 8139C+ or 8169 chip, we have to allocate 1500117388Swpaul * our busdma tags/memory differently. We need to allocate 1501117388Swpaul * a chunk of DMA'able memory for the RX and TX descriptor 1502117388Swpaul * lists. 150381713Swpaul */ 1504117388Swpaul if (sc->rl_type == RL_8139CPLUS || sc->rl_type == RL_8169) 1505117388Swpaul error = rl_allocmemcplus(dev, sc); 1506117388Swpaul else 1507117388Swpaul error = rl_allocmem(dev, sc); 1508117388Swpaul 1509112872Snjl if (error) 1510112872Snjl goto fail; 151181713Swpaul 151250703Swpaul /* Do MII setup */ 151350703Swpaul if (mii_phy_probe(dev, &sc->rl_miibus, 151450703Swpaul rl_ifmedia_upd, rl_ifmedia_sts)) { 151550703Swpaul printf("rl%d: MII without any phy!\n", sc->rl_unit); 151650703Swpaul error = ENXIO; 151750703Swpaul goto fail; 151850703Swpaul } 151950703Swpaul 152040516Swpaul ifp = &sc->arpcom.ac_if; 152140516Swpaul ifp->if_softc = sc; 152240516Swpaul ifp->if_unit = unit; 152340516Swpaul ifp->if_name = "rl"; 152440516Swpaul ifp->if_mtu = ETHERMTU; 152540516Swpaul ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 152640516Swpaul ifp->if_ioctl = rl_ioctl; 152740516Swpaul ifp->if_output = ether_output; 1528117388Swpaul ifp->if_capabilities = IFCAP_VLAN_MTU; 1529117388Swpaul if (RL_ISCPLUS(sc)) { 1530117388Swpaul ifp->if_start = rl_startcplus; 1531117388Swpaul ifp->if_hwassist = RL_CSUM_FEATURES; 1532117388Swpaul ifp->if_capabilities |= IFCAP_HWCSUM|IFCAP_VLAN_HWTAGGING; 1533117388Swpaul } else 1534117388Swpaul ifp->if_start = rl_start; 153540516Swpaul ifp->if_watchdog = rl_watchdog; 153640516Swpaul ifp->if_init = rl_init; 153740516Swpaul ifp->if_baudrate = 10000000; 1538117388Swpaul ifp->if_snd.ifq_maxlen = RL_IFQ_MAXLEN; 1539117388Swpaul ifp->if_capenable = ifp->if_capabilities; 154040516Swpaul 1541112872Snjl callout_handle_init(&sc->rl_stat_ch); 1542112872Snjl 154340516Swpaul /* 154463090Sarchie * Call MI attach routine. 154540516Swpaul */ 1546106936Ssam ether_ifattach(ifp, eaddr); 1547106157Simp 1548113609Snjl /* Hook interrupt last to avoid having to lock softc */ 1549106157Simp error = bus_setup_intr(dev, sc->rl_irq, INTR_TYPE_NET, 1550117388Swpaul RL_ISCPLUS(sc) ? rl_intrcplus : rl_intr, sc, &sc->rl_intrhand); 1551106157Simp 1552106157Simp if (error) { 1553106157Simp printf("rl%d: couldn't set up irq\n", unit); 1554113609Snjl ether_ifdetach(ifp); 1555106157Simp goto fail; 1556106157Simp } 1557106157Simp 155840516Swpaulfail: 1559112872Snjl if (error) 1560112872Snjl rl_detach(dev); 1561112872Snjl 1562110601Snjl return (error); 156340516Swpaul} 156440516Swpaul 1565113609Snjl/* 1566113609Snjl * Shutdown hardware and free up resources. This can be called any 1567113609Snjl * time after the mutex has been initialized. It is called in both 1568113609Snjl * the error case in attach and the normal detach case so it needs 1569113609Snjl * to be careful about only freeing resources that have actually been 1570113609Snjl * allocated. 1571113609Snjl */ 1572102335Salfredstatic int 1573102335Salfredrl_detach(dev) 157450703Swpaul device_t dev; 157550703Swpaul{ 157650703Swpaul struct rl_softc *sc; 157750703Swpaul struct ifnet *ifp; 1578117388Swpaul int i; 157950703Swpaul 158050703Swpaul sc = device_get_softc(dev); 1581112880Sjhb KASSERT(mtx_initialized(&sc->rl_mtx), ("rl mutex not initialized")); 158267087Swpaul RL_LOCK(sc); 158350703Swpaul ifp = &sc->arpcom.ac_if; 158450703Swpaul 1585113609Snjl /* These should only be active if attach succeeded */ 1586113812Simp if (device_is_attached(dev)) { 1587113609Snjl rl_stop(sc); 1588112872Snjl ether_ifdetach(ifp); 1589113609Snjl } 1590113609Snjl if (sc->rl_miibus) 1591112872Snjl device_delete_child(dev, sc->rl_miibus); 1592113609Snjl bus_generic_detach(dev); 159350703Swpaul 1594112872Snjl if (sc->rl_intrhand) 1595112872Snjl bus_teardown_intr(dev, sc->rl_irq, sc->rl_intrhand); 1596112872Snjl if (sc->rl_irq) 1597112872Snjl bus_release_resource(dev, SYS_RES_IRQ, 0, sc->rl_irq); 1598112872Snjl if (sc->rl_res) 1599112872Snjl bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res); 160050703Swpaul 1601117388Swpaul if (RL_ISCPLUS(sc)) { 1602117388Swpaul 1603117388Swpaul /* Unload and free the RX DMA ring memory and map */ 1604117388Swpaul 1605117388Swpaul if (sc->rl_ldata.rl_rx_list_tag) { 1606117388Swpaul bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag, 1607117388Swpaul sc->rl_ldata.rl_rx_list_map); 1608117388Swpaul bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag, 1609117388Swpaul sc->rl_ldata.rl_rx_list, 1610117388Swpaul sc->rl_ldata.rl_rx_list_map); 1611117388Swpaul bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag); 1612117388Swpaul } 1613117388Swpaul 1614117388Swpaul /* Unload and free the TX DMA ring memory and map */ 1615117388Swpaul 1616117388Swpaul if (sc->rl_ldata.rl_tx_list_tag) { 1617117388Swpaul bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag, 1618117388Swpaul sc->rl_ldata.rl_tx_list_map); 1619117388Swpaul bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag, 1620117388Swpaul sc->rl_ldata.rl_tx_list, 1621117388Swpaul sc->rl_ldata.rl_tx_list_map); 1622117388Swpaul bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag); 1623117388Swpaul } 1624117388Swpaul 1625117388Swpaul /* Destroy all the RX and TX buffer maps */ 1626117388Swpaul 1627117388Swpaul if (sc->rl_ldata.rl_mtag) { 1628117388Swpaul for (i = 0; i < RL_TX_DESC_CNT; i++) 1629117388Swpaul bus_dmamap_destroy(sc->rl_ldata.rl_mtag, 1630117388Swpaul sc->rl_ldata.rl_tx_dmamap[i]); 1631117388Swpaul for (i = 0; i < RL_RX_DESC_CNT; i++) 1632117388Swpaul bus_dmamap_destroy(sc->rl_ldata.rl_mtag, 1633117388Swpaul sc->rl_ldata.rl_rx_dmamap[i]); 1634117388Swpaul bus_dma_tag_destroy(sc->rl_ldata.rl_mtag); 1635117388Swpaul } 1636117388Swpaul 1637117388Swpaul /* Unload and free the stats buffer and map */ 1638117388Swpaul 1639117388Swpaul if (sc->rl_ldata.rl_stag) { 1640117388Swpaul bus_dmamap_unload(sc->rl_ldata.rl_stag, 1641117388Swpaul sc->rl_ldata.rl_rx_list_map); 1642117388Swpaul bus_dmamem_free(sc->rl_ldata.rl_stag, 1643117388Swpaul sc->rl_ldata.rl_stats, 1644117388Swpaul sc->rl_ldata.rl_smap); 1645117388Swpaul bus_dma_tag_destroy(sc->rl_ldata.rl_stag); 1646117388Swpaul } 1647117388Swpaul 1648117388Swpaul } else { 1649117388Swpaul if (sc->rl_tag) { 1650117388Swpaul bus_dmamap_unload(sc->rl_tag, 1651117388Swpaul sc->rl_cdata.rl_rx_dmamap); 1652117388Swpaul bus_dmamem_free(sc->rl_tag, sc->rl_cdata.rl_rx_buf, 1653117388Swpaul sc->rl_cdata.rl_rx_dmamap); 1654117388Swpaul bus_dma_tag_destroy(sc->rl_tag); 1655117388Swpaul } 1656112872Snjl } 1657117388Swpaul 1658112872Snjl if (sc->rl_parent_tag) 1659112872Snjl bus_dma_tag_destroy(sc->rl_parent_tag); 166050703Swpaul 166167087Swpaul RL_UNLOCK(sc); 166267087Swpaul mtx_destroy(&sc->rl_mtx); 166350703Swpaul 166450703Swpaul return(0); 166550703Swpaul} 166650703Swpaul 166740516Swpaul/* 166840516Swpaul * Initialize the transmit descriptors. 166940516Swpaul */ 1670102335Salfredstatic int 1671102335Salfredrl_list_tx_init(sc) 167240516Swpaul struct rl_softc *sc; 167340516Swpaul{ 167440516Swpaul struct rl_chain_data *cd; 167540516Swpaul int i; 167640516Swpaul 167740516Swpaul cd = &sc->rl_cdata; 167840516Swpaul for (i = 0; i < RL_TX_LIST_CNT; i++) { 167945633Swpaul cd->rl_tx_chain[i] = NULL; 168048028Swpaul CSR_WRITE_4(sc, 168148028Swpaul RL_TXADDR0 + (i * sizeof(u_int32_t)), 0x0000000); 168240516Swpaul } 168340516Swpaul 168445633Swpaul sc->rl_cdata.cur_tx = 0; 168545633Swpaul sc->rl_cdata.last_tx = 0; 168640516Swpaul 168740516Swpaul return(0); 168840516Swpaul} 168940516Swpaul 1690117388Swpaulstatic int 1691117388Swpaulrl_newbuf (sc, idx, m) 1692117388Swpaul struct rl_softc *sc; 1693117388Swpaul int idx; 1694117388Swpaul struct mbuf *m; 1695117388Swpaul{ 1696117388Swpaul struct rl_dmaload_arg arg; 1697117388Swpaul struct mbuf *n = NULL; 1698117388Swpaul int error; 1699117388Swpaul 1700117388Swpaul if (m == NULL) { 1701117388Swpaul n = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1702117388Swpaul if (n == NULL) 1703117388Swpaul return(ENOBUFS); 1704117388Swpaul m = n; 1705117388Swpaul } else 1706117388Swpaul m->m_data = m->m_ext.ext_buf; 1707117388Swpaul 1708117388Swpaul /* 1709117388Swpaul * Initialize mbuf length fields and fixup 1710117388Swpaul * alignment so that the frame payload is 1711117388Swpaul * longword aligned. 1712117388Swpaul */ 1713117388Swpaul m->m_len = m->m_pkthdr.len = 1536; 1714117388Swpaul m_adj(m, ETHER_ALIGN); 1715117388Swpaul 1716117388Swpaul arg.sc = sc; 1717117388Swpaul arg.rl_idx = idx; 1718117388Swpaul arg.rl_maxsegs = 1; 1719118889Swpaul arg.rl_flags = 0; 1720117388Swpaul arg.rl_ring = sc->rl_ldata.rl_rx_list; 1721117388Swpaul 1722117388Swpaul error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, 1723117388Swpaul sc->rl_ldata.rl_rx_dmamap[idx], m, rl_dma_map_desc, 1724117388Swpaul &arg, BUS_DMA_NOWAIT); 1725117388Swpaul if (error || arg.rl_maxsegs != 1) { 1726117388Swpaul if (n != NULL) 1727117388Swpaul m_freem(n); 1728117388Swpaul return (ENOMEM); 1729117388Swpaul } 1730117388Swpaul 1731117388Swpaul sc->rl_ldata.rl_rx_list[idx].rl_cmdstat |= htole32(RL_RDESC_CMD_OWN); 1732117388Swpaul sc->rl_ldata.rl_rx_mbuf[idx] = m; 1733117388Swpaul 1734117388Swpaul bus_dmamap_sync(sc->rl_ldata.rl_mtag, 1735117388Swpaul sc->rl_ldata.rl_rx_dmamap[idx], 1736117388Swpaul BUS_DMASYNC_PREREAD); 1737117388Swpaul 1738117388Swpaul return(0); 1739117388Swpaul} 1740117388Swpaul 1741117388Swpaulstatic int 1742117388Swpaulrl_tx_list_init(sc) 1743117388Swpaul struct rl_softc *sc; 1744117388Swpaul{ 1745117388Swpaul bzero ((char *)sc->rl_ldata.rl_tx_list, RL_TX_LIST_SZ); 1746117388Swpaul bzero ((char *)&sc->rl_ldata.rl_tx_mbuf, 1747117388Swpaul (RL_TX_DESC_CNT * sizeof(struct mbuf *))); 1748117388Swpaul 1749117388Swpaul bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 1750117388Swpaul sc->rl_ldata.rl_tx_list_map, BUS_DMASYNC_PREWRITE); 1751117388Swpaul sc->rl_ldata.rl_tx_prodidx = 0; 1752117388Swpaul sc->rl_ldata.rl_tx_considx = 0; 1753117388Swpaul sc->rl_ldata.rl_tx_free = RL_TX_DESC_CNT; 1754117388Swpaul 1755117388Swpaul return(0); 1756117388Swpaul} 1757117388Swpaul 1758117388Swpaulstatic int 1759117388Swpaulrl_rx_list_init(sc) 1760117388Swpaul struct rl_softc *sc; 1761117388Swpaul{ 1762117388Swpaul int i; 1763117388Swpaul 1764117388Swpaul bzero ((char *)sc->rl_ldata.rl_rx_list, RL_RX_LIST_SZ); 1765117388Swpaul bzero ((char *)&sc->rl_ldata.rl_rx_mbuf, 1766117388Swpaul (RL_RX_DESC_CNT * sizeof(struct mbuf *))); 1767117388Swpaul 1768117388Swpaul for (i = 0; i < RL_RX_DESC_CNT; i++) { 1769117388Swpaul if (rl_newbuf(sc, i, NULL) == ENOBUFS) 1770117388Swpaul return(ENOBUFS); 1771117388Swpaul } 1772117388Swpaul 1773117388Swpaul /* Flush the RX descriptors */ 1774117388Swpaul 1775117388Swpaul bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 1776117388Swpaul sc->rl_ldata.rl_rx_list_map, 1777117388Swpaul BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 1778117388Swpaul 1779117388Swpaul sc->rl_ldata.rl_rx_prodidx = 0; 1780117388Swpaul 1781117388Swpaul return(0); 1782117388Swpaul} 1783117388Swpaul 178440516Swpaul/* 1785117388Swpaul * RX handler for C+. This is pretty much like any other 1786117388Swpaul * descriptor-based RX handler. 1787117388Swpaul */ 1788117388Swpaulstatic void 1789117388Swpaulrl_rxeofcplus(sc) 1790117388Swpaul struct rl_softc *sc; 1791117388Swpaul{ 1792117388Swpaul struct mbuf *m; 1793117388Swpaul struct ifnet *ifp; 1794117388Swpaul int i, total_len; 1795117388Swpaul struct rl_desc *cur_rx; 1796117388Swpaul u_int32_t rxstat, rxvlan; 1797117388Swpaul 1798117388Swpaul ifp = &sc->arpcom.ac_if; 1799117388Swpaul i = sc->rl_ldata.rl_rx_prodidx; 1800117388Swpaul 1801117388Swpaul /* Invalidate the descriptor memory */ 1802117388Swpaul 1803117748Swpaul bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 1804117388Swpaul sc->rl_ldata.rl_rx_list_map, 1805117388Swpaul BUS_DMASYNC_POSTREAD); 1806117388Swpaul 1807117388Swpaul while (!RL_OWN(&sc->rl_ldata.rl_rx_list[i])) { 1808117388Swpaul 1809117388Swpaul cur_rx = &sc->rl_ldata.rl_rx_list[i]; 1810117388Swpaul m = sc->rl_ldata.rl_rx_mbuf[i]; 1811117388Swpaul total_len = RL_RXBYTES(cur_rx) - ETHER_CRC_LEN; 1812117388Swpaul rxstat = le32toh(cur_rx->rl_cmdstat); 1813117388Swpaul rxvlan = le32toh(cur_rx->rl_vlanctl); 1814117388Swpaul 1815117388Swpaul /* Invalidate the RX mbuf and unload its map */ 1816117388Swpaul 1817117388Swpaul bus_dmamap_sync(sc->rl_ldata.rl_mtag, 1818117388Swpaul sc->rl_ldata.rl_rx_dmamap[i], 1819117388Swpaul BUS_DMASYNC_POSTREAD); 1820117388Swpaul bus_dmamap_unload(sc->rl_ldata.rl_mtag, 1821117388Swpaul sc->rl_ldata.rl_rx_dmamap[i]); 1822117388Swpaul 1823118714Swpaul /* 1824118714Swpaul * NOTE: For some reason that I can't comprehend, 1825118714Swpaul * the RealTek engineers decided not to implement 1826118714Swpaul * the 'frame alignment error' bit in the 8169's 1827118714Swpaul * status word. Unfortunately, rather than simply 1828118714Swpaul * mark the bit as 'reserved,' they took it away 1829118714Swpaul * completely and shifted the other status bits 1830118714Swpaul * over one slot. The OWN, EOR, FS and LS bits are 1831118714Swpaul * still in the same places, as is the frame length 1832118714Swpaul * field. We have already extracted the frame length 1833118714Swpaul * and checked the OWN bit, so to work around this 1834118714Swpaul * problem, we shift the status bits one space to 1835118714Swpaul * the right so that we can evaluate everything else 1836118714Swpaul * correctly. 1837118714Swpaul */ 1838118714Swpaul if (sc->rl_type == RL_8169) 1839118714Swpaul rxstat >>= 1; 1840118714Swpaul 1841117388Swpaul if (rxstat & RL_RDESC_STAT_RXERRSUM) { 1842117388Swpaul ifp->if_ierrors++; 1843117388Swpaul rl_newbuf(sc, i, m); 1844117388Swpaul RL_DESC_INC(i); 1845117388Swpaul continue; 1846117388Swpaul } 1847117388Swpaul 1848117388Swpaul /* 1849117388Swpaul * If allocating a replacement mbuf fails, 1850117388Swpaul * reload the current one. 1851117388Swpaul */ 1852117388Swpaul 1853117388Swpaul if (rl_newbuf(sc, i, NULL)) { 1854117388Swpaul ifp->if_ierrors++; 1855117388Swpaul rl_newbuf(sc, i, m); 1856117388Swpaul RL_DESC_INC(i); 1857117388Swpaul continue; 1858117388Swpaul } 1859117388Swpaul 1860117388Swpaul RL_DESC_INC(i); 1861117388Swpaul 1862117388Swpaul ifp->if_ipackets++; 1863117388Swpaul m->m_pkthdr.len = m->m_len = total_len; 1864117388Swpaul m->m_pkthdr.rcvif = ifp; 1865117388Swpaul 1866118712Swpaul /* Do RX checksumming if enabled */ 1867117388Swpaul 1868118712Swpaul if (ifp->if_capenable & IFCAP_RXCSUM) { 1869118712Swpaul 1870118712Swpaul /* Check IP header checksum */ 1871118712Swpaul if (rxstat & RL_RDESC_STAT_PROTOID) 1872118712Swpaul m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED; 1873118712Swpaul if (!(rxstat & RL_RDESC_STAT_IPSUMBAD)) 1874118712Swpaul m->m_pkthdr.csum_flags |= CSUM_IP_VALID; 1875118712Swpaul 1876118712Swpaul /* Check TCP/UDP checksum */ 1877118712Swpaul if ((RL_TCPPKT(rxstat) && 1878118712Swpaul !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) || 1879118712Swpaul (RL_UDPPKT(rxstat) && 1880118712Swpaul !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) { 1881118712Swpaul m->m_pkthdr.csum_flags |= 1882118712Swpaul CSUM_DATA_VALID|CSUM_PSEUDO_HDR; 1883118712Swpaul m->m_pkthdr.csum_data = 0xffff; 1884118712Swpaul } 1885117388Swpaul } 1886117388Swpaul 1887117388Swpaul if (rxvlan & RL_RDESC_VLANCTL_TAG) 1888117388Swpaul VLAN_INPUT_TAG(ifp, m, 1889117388Swpaul ntohs((rxvlan & RL_RDESC_VLANCTL_DATA)), continue); 1890117388Swpaul (*ifp->if_input)(ifp, m); 1891117388Swpaul } 1892117388Swpaul 1893117388Swpaul /* Flush the RX DMA ring */ 1894117388Swpaul 1895117388Swpaul bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 1896117388Swpaul sc->rl_ldata.rl_rx_list_map, 1897117388Swpaul BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 1898117388Swpaul 1899117388Swpaul sc->rl_ldata.rl_rx_prodidx = i; 1900117388Swpaul 1901117388Swpaul return; 1902117388Swpaul} 1903117388Swpaul 1904117388Swpaul/* 190540516Swpaul * A frame has been uploaded: pass the resulting mbuf chain up to 190640516Swpaul * the higher level protocols. 190740516Swpaul * 190840516Swpaul * You know there's something wrong with a PCI bus-master chip design 190940516Swpaul * when you have to use m_devget(). 191040516Swpaul * 191140516Swpaul * The receive operation is badly documented in the datasheet, so I'll 191240516Swpaul * attempt to document it here. The driver provides a buffer area and 191340516Swpaul * places its base address in the RX buffer start address register. 191440516Swpaul * The chip then begins copying frames into the RX buffer. Each frame 191572645Sasmodai * is preceded by a 32-bit RX status word which specifies the length 191640516Swpaul * of the frame and certain other status bits. Each frame (starting with 191740516Swpaul * the status word) is also 32-bit aligned. The frame length is in the 191840516Swpaul * first 16 bits of the status word; the lower 15 bits correspond with 191940516Swpaul * the 'rx status register' mentioned in the datasheet. 192048028Swpaul * 192148028Swpaul * Note: to make the Alpha happy, the frame payload needs to be aligned 192278508Sbmilekic * on a 32-bit boundary. To achieve this, we pass RL_ETHER_ALIGN (2 bytes) 1923109109Sdes * as the offset argument to m_devget(). 192440516Swpaul */ 1925102335Salfredstatic void 1926102335Salfredrl_rxeof(sc) 192740516Swpaul struct rl_softc *sc; 192840516Swpaul{ 1929109109Sdes struct mbuf *m; 1930109109Sdes struct ifnet *ifp; 193140516Swpaul int total_len = 0; 193240516Swpaul u_int32_t rxstat; 193340516Swpaul caddr_t rxbufpos; 193440516Swpaul int wrap = 0; 193540516Swpaul u_int16_t cur_rx; 193640516Swpaul u_int16_t limit; 193740516Swpaul u_int16_t rx_bytes = 0, max_bytes; 193840516Swpaul 193940516Swpaul ifp = &sc->arpcom.ac_if; 194040516Swpaul 194181713Swpaul bus_dmamap_sync(sc->rl_tag, sc->rl_cdata.rl_rx_dmamap, 1942108729Sjake BUS_DMASYNC_POSTREAD); 194381713Swpaul 194440516Swpaul cur_rx = (CSR_READ_2(sc, RL_CURRXADDR) + 16) % RL_RXBUFLEN; 194540516Swpaul 194640516Swpaul /* Do not try to read past this point. */ 194740516Swpaul limit = CSR_READ_2(sc, RL_CURRXBUF) % RL_RXBUFLEN; 194840516Swpaul 194940516Swpaul if (limit < cur_rx) 195040516Swpaul max_bytes = (RL_RXBUFLEN - cur_rx) + limit; 195140516Swpaul else 195240516Swpaul max_bytes = limit - cur_rx; 195340516Swpaul 195442738Swpaul while((CSR_READ_1(sc, RL_COMMAND) & RL_CMD_EMPTY_RXBUF) == 0) { 195594883Sluigi#ifdef DEVICE_POLLING 1956102052Ssobomax if (ifp->if_flags & IFF_POLLING) { 195794883Sluigi if (sc->rxcycles <= 0) 195894883Sluigi break; 195994883Sluigi sc->rxcycles--; 196094883Sluigi } 196194883Sluigi#endif /* DEVICE_POLLING */ 196240516Swpaul rxbufpos = sc->rl_cdata.rl_rx_buf + cur_rx; 1963108729Sjake rxstat = le32toh(*(u_int32_t *)rxbufpos); 196440516Swpaul 196540516Swpaul /* 196640516Swpaul * Here's a totally undocumented fact for you. When the 196740516Swpaul * RealTek chip is in the process of copying a packet into 196840516Swpaul * RAM for you, the length will be 0xfff0. If you spot a 196940516Swpaul * packet header with this value, you need to stop. The 197040516Swpaul * datasheet makes absolutely no mention of this and 197140516Swpaul * RealTek should be shot for this. 197240516Swpaul */ 197340516Swpaul if ((u_int16_t)(rxstat >> 16) == RL_RXSTAT_UNFINISHED) 197440516Swpaul break; 1975109109Sdes 197640516Swpaul if (!(rxstat & RL_RXSTAT_RXOK)) { 197740516Swpaul ifp->if_ierrors++; 197850703Swpaul rl_init(sc); 197950703Swpaul return; 198040516Swpaul } 198140516Swpaul 1982109109Sdes /* No errors; receive the packet. */ 198340516Swpaul total_len = rxstat >> 16; 198440516Swpaul rx_bytes += total_len + 4; 198540516Swpaul 198640516Swpaul /* 198742051Swpaul * XXX The RealTek chip includes the CRC with every 198842051Swpaul * received frame, and there's no way to turn this 198942051Swpaul * behavior off (at least, I can't find anything in 1990109109Sdes * the manual that explains how to do it) so we have 199142051Swpaul * to trim off the CRC manually. 199242051Swpaul */ 199342051Swpaul total_len -= ETHER_CRC_LEN; 199442051Swpaul 199542051Swpaul /* 199640516Swpaul * Avoid trying to read more bytes than we know 199740516Swpaul * the chip has prepared for us. 199840516Swpaul */ 199940516Swpaul if (rx_bytes > max_bytes) 200040516Swpaul break; 200140516Swpaul 200240516Swpaul rxbufpos = sc->rl_cdata.rl_rx_buf + 200340516Swpaul ((cur_rx + sizeof(u_int32_t)) % RL_RXBUFLEN); 200440516Swpaul 200540516Swpaul if (rxbufpos == (sc->rl_cdata.rl_rx_buf + RL_RXBUFLEN)) 200640516Swpaul rxbufpos = sc->rl_cdata.rl_rx_buf; 200740516Swpaul 200840516Swpaul wrap = (sc->rl_cdata.rl_rx_buf + RL_RXBUFLEN) - rxbufpos; 200940516Swpaul 201040516Swpaul if (total_len > wrap) { 201178508Sbmilekic m = m_devget(rxbufpos, total_len, RL_ETHER_ALIGN, ifp, 201278508Sbmilekic NULL); 201340516Swpaul if (m == NULL) { 201440516Swpaul ifp->if_ierrors++; 201552426Swpaul } else { 201640516Swpaul m_copyback(m, wrap, total_len - wrap, 201740516Swpaul sc->rl_cdata.rl_rx_buf); 201848028Swpaul } 201942051Swpaul cur_rx = (total_len - wrap + ETHER_CRC_LEN); 202040516Swpaul } else { 202178508Sbmilekic m = m_devget(rxbufpos, total_len, RL_ETHER_ALIGN, ifp, 202278508Sbmilekic NULL); 202340516Swpaul if (m == NULL) { 202440516Swpaul ifp->if_ierrors++; 202578508Sbmilekic } 202642051Swpaul cur_rx += total_len + 4 + ETHER_CRC_LEN; 202740516Swpaul } 202840516Swpaul 202940516Swpaul /* 203040516Swpaul * Round up to 32-bit boundary. 203140516Swpaul */ 203240516Swpaul cur_rx = (cur_rx + 3) & ~3; 203340516Swpaul CSR_WRITE_2(sc, RL_CURRXADDR, cur_rx - 16); 203440516Swpaul 203540516Swpaul if (m == NULL) 203640516Swpaul continue; 203740516Swpaul 203840516Swpaul ifp->if_ipackets++; 2039106936Ssam (*ifp->if_input)(ifp, m); 204040516Swpaul } 204140516Swpaul 204240516Swpaul return; 204340516Swpaul} 204440516Swpaul 2045117388Swpaulstatic void 2046117388Swpaulrl_txeofcplus(sc) 2047117388Swpaul struct rl_softc *sc; 2048117388Swpaul{ 2049117388Swpaul struct ifnet *ifp; 2050117388Swpaul u_int32_t txstat; 2051117388Swpaul int idx; 2052117388Swpaul 2053117388Swpaul ifp = &sc->arpcom.ac_if; 2054117388Swpaul idx = sc->rl_ldata.rl_tx_considx; 2055117388Swpaul 2056117388Swpaul /* Invalidate the TX descriptor list */ 2057117388Swpaul 2058117748Swpaul bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 2059117388Swpaul sc->rl_ldata.rl_tx_list_map, 2060117388Swpaul BUS_DMASYNC_POSTREAD); 2061117388Swpaul 2062117388Swpaul while (idx != sc->rl_ldata.rl_tx_prodidx) { 2063117388Swpaul 2064117388Swpaul txstat = le32toh(sc->rl_ldata.rl_tx_list[idx].rl_cmdstat); 2065117388Swpaul if (txstat & RL_TDESC_CMD_OWN) 2066117388Swpaul break; 2067117388Swpaul 2068117388Swpaul /* 2069117388Swpaul * We only stash mbufs in the last descriptor 2070117388Swpaul * in a fragment chain, which also happens to 2071117388Swpaul * be the only place where the TX status bits 2072117388Swpaul * are valid. 2073117388Swpaul */ 2074117388Swpaul 2075117388Swpaul if (txstat & RL_TDESC_CMD_EOF) { 2076117388Swpaul m_freem(sc->rl_ldata.rl_tx_mbuf[idx]); 2077117388Swpaul sc->rl_ldata.rl_tx_mbuf[idx] = NULL; 2078117388Swpaul bus_dmamap_unload(sc->rl_ldata.rl_mtag, 2079117388Swpaul sc->rl_ldata.rl_tx_dmamap[idx]); 2080117388Swpaul if (txstat & (RL_TDESC_STAT_EXCESSCOL| 2081117388Swpaul RL_TDESC_STAT_COLCNT)) 2082117388Swpaul ifp->if_collisions++; 2083117388Swpaul if (txstat & RL_TDESC_STAT_TXERRSUM) 2084117388Swpaul ifp->if_oerrors++; 2085117388Swpaul else 2086117388Swpaul ifp->if_opackets++; 2087117388Swpaul } 2088117388Swpaul sc->rl_ldata.rl_tx_free++; 2089117388Swpaul RL_DESC_INC(idx); 2090117388Swpaul } 2091117388Swpaul 2092117388Swpaul /* No changes made to the TX ring, so no flush needed */ 2093117388Swpaul 2094117388Swpaul if (idx != sc->rl_ldata.rl_tx_considx) { 2095117388Swpaul sc->rl_ldata.rl_tx_considx = idx; 2096117388Swpaul ifp->if_flags &= ~IFF_OACTIVE; 2097117388Swpaul ifp->if_timer = 0; 2098117388Swpaul } 2099117388Swpaul 2100117388Swpaul return; 2101117388Swpaul} 2102117388Swpaul 210340516Swpaul/* 210440516Swpaul * A frame was downloaded to the chip. It's safe for us to clean up 210540516Swpaul * the list buffers. 210640516Swpaul */ 2107102335Salfredstatic void 2108102335Salfredrl_txeof(sc) 210940516Swpaul struct rl_softc *sc; 211040516Swpaul{ 211140516Swpaul struct ifnet *ifp; 211240516Swpaul u_int32_t txstat; 211340516Swpaul 211440516Swpaul ifp = &sc->arpcom.ac_if; 211540516Swpaul 211640516Swpaul /* 211740516Swpaul * Go through our tx list and free mbufs for those 211840516Swpaul * frames that have been uploaded. 211940516Swpaul */ 212045633Swpaul do { 212145633Swpaul txstat = CSR_READ_4(sc, RL_LAST_TXSTAT(sc)); 212245633Swpaul if (!(txstat & (RL_TXSTAT_TX_OK| 212345633Swpaul RL_TXSTAT_TX_UNDERRUN|RL_TXSTAT_TXABRT))) 212440516Swpaul break; 212540516Swpaul 212645633Swpaul ifp->if_collisions += (txstat & RL_TXSTAT_COLLCNT) >> 24; 212740516Swpaul 212845633Swpaul if (RL_LAST_TXMBUF(sc) != NULL) { 212981713Swpaul bus_dmamap_unload(sc->rl_tag, RL_LAST_DMAMAP(sc)); 213081713Swpaul bus_dmamap_destroy(sc->rl_tag, RL_LAST_DMAMAP(sc)); 213145633Swpaul m_freem(RL_LAST_TXMBUF(sc)); 213245633Swpaul RL_LAST_TXMBUF(sc) = NULL; 213345633Swpaul } 213445633Swpaul if (txstat & RL_TXSTAT_TX_OK) 213545633Swpaul ifp->if_opackets++; 213645633Swpaul else { 213752426Swpaul int oldthresh; 213845633Swpaul ifp->if_oerrors++; 213945633Swpaul if ((txstat & RL_TXSTAT_TXABRT) || 214045633Swpaul (txstat & RL_TXSTAT_OUTOFWIN)) 214145633Swpaul CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG); 214252426Swpaul oldthresh = sc->rl_txthresh; 214352426Swpaul /* error recovery */ 214452426Swpaul rl_reset(sc); 214552426Swpaul rl_init(sc); 214652426Swpaul /* 214752426Swpaul * If there was a transmit underrun, 214852426Swpaul * bump the TX threshold. 214952426Swpaul */ 215052426Swpaul if (txstat & RL_TXSTAT_TX_UNDERRUN) 215152426Swpaul sc->rl_txthresh = oldthresh + 32; 215252426Swpaul return; 215345633Swpaul } 215445633Swpaul RL_INC(sc->rl_cdata.last_tx); 215545633Swpaul ifp->if_flags &= ~IFF_OACTIVE; 215645633Swpaul } while (sc->rl_cdata.last_tx != sc->rl_cdata.cur_tx); 215740516Swpaul 215899165Sluigi ifp->if_timer = 215999165Sluigi (sc->rl_cdata.last_tx == sc->rl_cdata.cur_tx) ? 0 : 5; 216099165Sluigi 216150703Swpaul return; 216250703Swpaul} 216340516Swpaul 2164102335Salfredstatic void 2165102335Salfredrl_tick(xsc) 216650703Swpaul void *xsc; 216750703Swpaul{ 216850703Swpaul struct rl_softc *sc; 216950703Swpaul struct mii_data *mii; 217050703Swpaul 217150703Swpaul sc = xsc; 217267087Swpaul RL_LOCK(sc); 217350703Swpaul mii = device_get_softc(sc->rl_miibus); 217450703Swpaul 217550703Swpaul mii_tick(mii); 217650703Swpaul 217750703Swpaul sc->rl_stat_ch = timeout(rl_tick, sc, hz); 217867087Swpaul RL_UNLOCK(sc); 217950703Swpaul 218040516Swpaul return; 218140516Swpaul} 218240516Swpaul 218394883Sluigi#ifdef DEVICE_POLLING 218494883Sluigistatic void 218594883Sluigirl_poll (struct ifnet *ifp, enum poll_cmd cmd, int count) 218694883Sluigi{ 218794883Sluigi struct rl_softc *sc = ifp->if_softc; 218894883Sluigi 218994883Sluigi RL_LOCK(sc); 219094883Sluigi if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */ 2191117388Swpaul if (RL_ISCPLUS(sc)) 2192117388Swpaul CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS); 2193117388Swpaul else 2194117388Swpaul CSR_WRITE_2(sc, RL_IMR, RL_INTRS); 219594883Sluigi goto done; 219694883Sluigi } 219794883Sluigi 219894883Sluigi sc->rxcycles = count; 2199117388Swpaul if (RL_ISCPLUS(sc)) { 2200117388Swpaul rl_rxeofcplus(sc); 2201117388Swpaul rl_txeofcplus(sc); 2202117388Swpaul } else { 2203117388Swpaul rl_rxeof(sc); 2204117388Swpaul rl_txeof(sc); 2205117388Swpaul } 2206117388Swpaul 220794883Sluigi if (ifp->if_snd.ifq_head != NULL) 2208117388Swpaul (*ifp->if_start)(ifp); 220994883Sluigi 221094883Sluigi if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */ 221194883Sluigi u_int16_t status; 221294883Sluigi 221394883Sluigi status = CSR_READ_2(sc, RL_ISR); 2214100957Sjhb if (status == 0xffff) 2215100957Sjhb goto done; 221694883Sluigi if (status) 221794883Sluigi CSR_WRITE_2(sc, RL_ISR, status); 221894883Sluigi 221994883Sluigi /* 222094883Sluigi * XXX check behaviour on receiver stalls. 222194883Sluigi */ 222294883Sluigi 222394883Sluigi if (status & RL_ISR_SYSTEM_ERR) { 222494883Sluigi rl_reset(sc); 222594883Sluigi rl_init(sc); 222694883Sluigi } 222794883Sluigi } 222894883Sluigidone: 222994883Sluigi RL_UNLOCK(sc); 223094883Sluigi} 223194883Sluigi#endif /* DEVICE_POLLING */ 223294883Sluigi 2233102335Salfredstatic void 2234117388Swpaulrl_intrcplus(arg) 2235117388Swpaul void *arg; 2236117388Swpaul{ 2237117388Swpaul struct rl_softc *sc; 2238117388Swpaul struct ifnet *ifp; 2239117388Swpaul u_int16_t status; 2240117388Swpaul 2241117388Swpaul sc = arg; 2242117388Swpaul 2243117388Swpaul if (sc->suspended) { 2244117388Swpaul return; 2245117388Swpaul } 2246117388Swpaul 2247117388Swpaul RL_LOCK(sc); 2248117388Swpaul ifp = &sc->arpcom.ac_if; 2249117388Swpaul 2250117388Swpaul#ifdef DEVICE_POLLING 2251117388Swpaul if (ifp->if_flags & IFF_POLLING) 2252117388Swpaul goto done; 2253117388Swpaul if (ether_poll_register(rl_poll, ifp)) { /* ok, disable interrupts */ 2254117388Swpaul CSR_WRITE_2(sc, RL_IMR, 0x0000); 2255117388Swpaul rl_poll(ifp, 0, 1); 2256117388Swpaul goto done; 2257117388Swpaul } 2258117388Swpaul#endif /* DEVICE_POLLING */ 2259117388Swpaul 2260117388Swpaul for (;;) { 2261117388Swpaul 2262117388Swpaul status = CSR_READ_2(sc, RL_ISR); 2263117388Swpaul /* If the card has gone away the read returns 0xffff. */ 2264117388Swpaul if (status == 0xffff) 2265117388Swpaul break; 2266117388Swpaul if (status) 2267117388Swpaul CSR_WRITE_2(sc, RL_ISR, status); 2268117388Swpaul 2269117388Swpaul if ((status & RL_INTRS_CPLUS) == 0) 2270117388Swpaul break; 2271117388Swpaul 2272117388Swpaul if (status & RL_ISR_RX_OK) 2273117388Swpaul rl_rxeofcplus(sc); 2274117388Swpaul 2275117388Swpaul if (status & RL_ISR_RX_ERR) 2276117388Swpaul rl_rxeofcplus(sc); 2277117388Swpaul 2278117388Swpaul if ((status & RL_ISR_TIMEOUT_EXPIRED) || 2279117388Swpaul (status & RL_ISR_TX_ERR) || 2280117388Swpaul (status & RL_ISR_TX_DESC_UNAVAIL)) 2281117388Swpaul rl_txeofcplus(sc); 2282117388Swpaul 2283117388Swpaul if (status & RL_ISR_SYSTEM_ERR) { 2284117388Swpaul rl_reset(sc); 2285117388Swpaul rl_init(sc); 2286117388Swpaul } 2287117388Swpaul 2288117388Swpaul } 2289117388Swpaul 2290117388Swpaul if (ifp->if_snd.ifq_head != NULL) 2291117388Swpaul (*ifp->if_start)(ifp); 2292117388Swpaul 2293117388Swpaul#ifdef DEVICE_POLLING 2294117388Swpauldone: 2295117388Swpaul#endif 2296117388Swpaul RL_UNLOCK(sc); 2297117388Swpaul 2298117388Swpaul return; 2299117388Swpaul} 2300117388Swpaul 2301117388Swpaulstatic void 2302102335Salfredrl_intr(arg) 230340516Swpaul void *arg; 230440516Swpaul{ 230540516Swpaul struct rl_softc *sc; 230640516Swpaul struct ifnet *ifp; 230740516Swpaul u_int16_t status; 230840516Swpaul 230940516Swpaul sc = arg; 231086822Siwasaki 231186822Siwasaki if (sc->suspended) { 231286822Siwasaki return; 231386822Siwasaki } 231486822Siwasaki 231567087Swpaul RL_LOCK(sc); 231640516Swpaul ifp = &sc->arpcom.ac_if; 231740516Swpaul 231894883Sluigi#ifdef DEVICE_POLLING 2319102052Ssobomax if (ifp->if_flags & IFF_POLLING) 232094883Sluigi goto done; 232194883Sluigi if (ether_poll_register(rl_poll, ifp)) { /* ok, disable interrupts */ 232294883Sluigi CSR_WRITE_2(sc, RL_IMR, 0x0000); 232394883Sluigi rl_poll(ifp, 0, 1); 232494883Sluigi goto done; 232594883Sluigi } 232694883Sluigi#endif /* DEVICE_POLLING */ 232740516Swpaul 232840516Swpaul for (;;) { 232940516Swpaul 233040516Swpaul status = CSR_READ_2(sc, RL_ISR); 2331100957Sjhb /* If the card has gone away the read returns 0xffff. */ 2332100957Sjhb if (status == 0xffff) 2333100957Sjhb break; 233440516Swpaul if (status) 233540516Swpaul CSR_WRITE_2(sc, RL_ISR, status); 233640516Swpaul 233740516Swpaul if ((status & RL_INTRS) == 0) 233840516Swpaul break; 233940516Swpaul 234040516Swpaul if (status & RL_ISR_RX_OK) 234140516Swpaul rl_rxeof(sc); 234240516Swpaul 234340516Swpaul if (status & RL_ISR_RX_ERR) 234440516Swpaul rl_rxeof(sc); 234540516Swpaul 234645633Swpaul if ((status & RL_ISR_TX_OK) || (status & RL_ISR_TX_ERR)) 234740516Swpaul rl_txeof(sc); 234840516Swpaul 234940516Swpaul if (status & RL_ISR_SYSTEM_ERR) { 235040516Swpaul rl_reset(sc); 235140516Swpaul rl_init(sc); 235240516Swpaul } 235340516Swpaul 235440516Swpaul } 235540516Swpaul 235652426Swpaul if (ifp->if_snd.ifq_head != NULL) 2357117388Swpaul (*ifp->if_start)(ifp); 235840516Swpaul 235994883Sluigi#ifdef DEVICE_POLLING 236094883Sluigidone: 236194883Sluigi#endif 236267087Swpaul RL_UNLOCK(sc); 236367087Swpaul 236440516Swpaul return; 236540516Swpaul} 236640516Swpaul 2367117388Swpaulstatic int 2368117388Swpaulrl_encapcplus(sc, m_head, idx) 2369117388Swpaul struct rl_softc *sc; 2370117388Swpaul struct mbuf *m_head; 2371117388Swpaul int *idx; 2372117388Swpaul{ 2373117388Swpaul struct mbuf *m_new = NULL; 2374117388Swpaul struct rl_dmaload_arg arg; 2375117388Swpaul bus_dmamap_t map; 2376117388Swpaul int error; 2377117388Swpaul struct m_tag *mtag; 2378117388Swpaul 2379117388Swpaul if (sc->rl_ldata.rl_tx_free < 4) 2380117388Swpaul return(EFBIG); 2381117388Swpaul 2382118889Swpaul /* 2383118889Swpaul * Set up checksum offload. Note: checksum offload bits must 2384118889Swpaul * appear in all descriptors of a multi-descriptor transmit 2385118889Swpaul * attempt. (This is according to testing done with an 8169 2386118889Swpaul * chip. I'm not sure if this is a requirement or a bug.) 2387118889Swpaul */ 2388118889Swpaul 2389118889Swpaul arg.rl_flags = 0; 2390118889Swpaul 2391118889Swpaul if (m_head->m_pkthdr.csum_flags & CSUM_IP) 2392118889Swpaul arg.rl_flags |= RL_TDESC_CMD_IPCSUM; 2393118889Swpaul if (m_head->m_pkthdr.csum_flags & CSUM_TCP) 2394118889Swpaul arg.rl_flags |= RL_TDESC_CMD_TCPCSUM; 2395118889Swpaul if (m_head->m_pkthdr.csum_flags & CSUM_UDP) 2396118889Swpaul arg.rl_flags |= RL_TDESC_CMD_UDPCSUM; 2397118889Swpaul 2398117388Swpaul arg.sc = sc; 2399117388Swpaul arg.rl_idx = *idx; 2400117388Swpaul arg.rl_maxsegs = sc->rl_ldata.rl_tx_free; 2401117388Swpaul arg.rl_ring = sc->rl_ldata.rl_tx_list; 2402117388Swpaul 2403117388Swpaul map = sc->rl_ldata.rl_tx_dmamap[*idx]; 2404117388Swpaul error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, map, 2405117388Swpaul m_head, rl_dma_map_desc, &arg, BUS_DMA_NOWAIT); 2406117388Swpaul 2407117388Swpaul if (error && error != EFBIG) { 2408117388Swpaul printf("rl%d: can't map mbuf (error %d)\n", sc->rl_unit, error); 2409117388Swpaul return(ENOBUFS); 2410117388Swpaul } 2411117388Swpaul 2412117388Swpaul /* Too many segments to map, coalesce into a single mbuf */ 2413117388Swpaul 2414117388Swpaul if (error || arg.rl_maxsegs == 0) { 2415117388Swpaul m_new = m_defrag(m_head, M_DONTWAIT); 2416117388Swpaul if (m_new == NULL) 2417117388Swpaul return(1); 2418117388Swpaul else 2419117388Swpaul m_head = m_new; 2420117388Swpaul 2421117388Swpaul arg.sc = sc; 2422117388Swpaul arg.rl_idx = *idx; 2423117388Swpaul arg.rl_maxsegs = sc->rl_ldata.rl_tx_free; 2424117388Swpaul arg.rl_ring = sc->rl_ldata.rl_tx_list; 2425117388Swpaul 2426117388Swpaul error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, map, 2427117388Swpaul m_head, rl_dma_map_desc, &arg, BUS_DMA_NOWAIT); 2428117388Swpaul if (error) { 2429117388Swpaul printf("rl%d: can't map mbuf (error %d)\n", 2430117388Swpaul sc->rl_unit, error); 2431117388Swpaul return(EFBIG); 2432117388Swpaul } 2433117388Swpaul } 2434117388Swpaul 2435117388Swpaul /* 2436117388Swpaul * Insure that the map for this transmission 2437117388Swpaul * is placed at the array index of the last descriptor 2438117388Swpaul * in this chain. 2439117388Swpaul */ 2440117388Swpaul sc->rl_ldata.rl_tx_dmamap[*idx] = 2441117388Swpaul sc->rl_ldata.rl_tx_dmamap[arg.rl_idx]; 2442117388Swpaul sc->rl_ldata.rl_tx_dmamap[arg.rl_idx] = map; 2443117388Swpaul 2444117388Swpaul sc->rl_ldata.rl_tx_mbuf[arg.rl_idx] = m_head; 2445117388Swpaul sc->rl_ldata.rl_tx_free -= arg.rl_maxsegs; 2446117388Swpaul 2447117388Swpaul /* 2448117388Swpaul * Set up hardware VLAN tagging. Note: vlan tag info must 2449117388Swpaul * appear in the first descriptor of a multi-descriptor 2450117388Swpaul * transmission attempt. 2451117388Swpaul */ 2452117388Swpaul 2453117388Swpaul mtag = VLAN_OUTPUT_TAG(&sc->arpcom.ac_if, m_head); 2454117388Swpaul if (mtag != NULL) 2455117388Swpaul sc->rl_ldata.rl_tx_list[*idx].rl_vlanctl = 2456117388Swpaul htole32(htons(VLAN_TAG_VALUE(mtag)) | RL_TDESC_VLANCTL_TAG); 2457117388Swpaul 2458117388Swpaul /* Transfer ownership of packet to the chip. */ 2459117388Swpaul 2460118889Swpaul sc->rl_ldata.rl_tx_list[arg.rl_idx].rl_cmdstat |= 2461118889Swpaul htole32(RL_TDESC_CMD_OWN); 2462117388Swpaul if (*idx != arg.rl_idx) 2463118889Swpaul sc->rl_ldata.rl_tx_list[*idx].rl_cmdstat |= 2464118889Swpaul htole32(RL_TDESC_CMD_OWN); 2465117388Swpaul 2466117388Swpaul RL_DESC_INC(arg.rl_idx); 2467117388Swpaul *idx = arg.rl_idx; 2468117388Swpaul 2469117388Swpaul return(0); 2470117388Swpaul} 2471117388Swpaul 247240516Swpaul/* 2473117388Swpaul * Main transmit routine for C+ and gigE NICs. 2474117388Swpaul */ 2475117388Swpaul 2476117388Swpaulstatic void 2477117388Swpaulrl_startcplus(ifp) 2478117388Swpaul struct ifnet *ifp; 2479117388Swpaul{ 2480117388Swpaul struct rl_softc *sc; 2481117388Swpaul struct mbuf *m_head = NULL; 2482117388Swpaul int idx; 2483117388Swpaul 2484117388Swpaul sc = ifp->if_softc; 2485117388Swpaul RL_LOCK(sc); 2486117388Swpaul 2487117388Swpaul idx = sc->rl_ldata.rl_tx_prodidx; 2488117388Swpaul 2489117388Swpaul while (sc->rl_ldata.rl_tx_mbuf[idx] == NULL) { 2490117388Swpaul IF_DEQUEUE(&ifp->if_snd, m_head); 2491117388Swpaul if (m_head == NULL) 2492117388Swpaul break; 2493117388Swpaul 2494117388Swpaul if (rl_encapcplus(sc, m_head, &idx)) { 2495117388Swpaul IF_PREPEND(&ifp->if_snd, m_head); 2496117388Swpaul ifp->if_flags |= IFF_OACTIVE; 2497117388Swpaul break; 2498117388Swpaul } 2499117388Swpaul 2500117388Swpaul /* 2501117388Swpaul * If there's a BPF listener, bounce a copy of this frame 2502117388Swpaul * to him. 2503117388Swpaul */ 2504117388Swpaul BPF_MTAP(ifp, m_head); 2505117388Swpaul } 2506117388Swpaul 2507117388Swpaul /* Flush the TX descriptors */ 2508117388Swpaul 2509117748Swpaul bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 2510117388Swpaul sc->rl_ldata.rl_tx_list_map, 2511117388Swpaul BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 2512117388Swpaul 2513117388Swpaul sc->rl_ldata.rl_tx_prodidx = idx; 2514117388Swpaul 2515117388Swpaul /* 2516117388Swpaul * RealTek put the TX poll request register in a different 2517117388Swpaul * location on the 8169 gigE chip. I don't know why. 2518117388Swpaul */ 2519117388Swpaul 2520117388Swpaul if (sc->rl_type == RL_8169) 2521117388Swpaul CSR_WRITE_2(sc, RL_GTXSTART, RL_TXSTART_START); 2522117388Swpaul else 2523117388Swpaul CSR_WRITE_2(sc, RL_TXSTART, RL_TXSTART_START); 2524117388Swpaul 2525117388Swpaul /* 2526117388Swpaul * Use the countdown timer for interrupt moderation. 2527117388Swpaul * 'TX done' interrupts are disabled. Instead, we reset the 2528117388Swpaul * countdown timer, which will begin counting until it hits 2529117388Swpaul * the value in the TIMERINT register, and then trigger an 2530117388Swpaul * interrupt. Each time we write to the TIMERCNT register, 2531117388Swpaul * the timer count is reset to 0. 2532117388Swpaul */ 2533117388Swpaul CSR_WRITE_4(sc, RL_TIMERCNT, 1); 2534117388Swpaul 2535117388Swpaul RL_UNLOCK(sc); 2536117388Swpaul 2537117388Swpaul /* 2538117388Swpaul * Set a timeout in case the chip goes out to lunch. 2539117388Swpaul */ 2540117388Swpaul ifp->if_timer = 5; 2541117388Swpaul 2542117388Swpaul return; 2543117388Swpaul} 2544117388Swpaul 2545117388Swpaul/* 254640516Swpaul * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data 254740516Swpaul * pointers to the fragment pointers. 254840516Swpaul */ 2549102335Salfredstatic int 2550102335Salfredrl_encap(sc, m_head) 255140516Swpaul struct rl_softc *sc; 255240516Swpaul struct mbuf *m_head; 255340516Swpaul{ 255441243Swpaul struct mbuf *m_new = NULL; 255540516Swpaul 255640516Swpaul /* 255745633Swpaul * The RealTek is brain damaged and wants longword-aligned 255845633Swpaul * TX buffers, plus we can only have one fragment buffer 255945633Swpaul * per packet. We have to copy pretty much all the time. 256040516Swpaul */ 2561112839Ssilby m_new = m_defrag(m_head, M_DONTWAIT); 256240516Swpaul 2563113496Ssilby if (m_new == NULL) { 2564113496Ssilby m_freem(m_head); 256541243Swpaul return(1); 2566113496Ssilby } 256741243Swpaul m_head = m_new; 256840516Swpaul 256940516Swpaul /* Pad frames to at least 60 bytes. */ 257041243Swpaul if (m_head->m_pkthdr.len < RL_MIN_FRAMELEN) { 257155058Swpaul /* 257255058Swpaul * Make security concious people happy: zero out the 257355058Swpaul * bytes in the pad area, since we don't know what 257455058Swpaul * this mbuf cluster buffer's previous user might 257555058Swpaul * have left in it. 2576109109Sdes */ 257755058Swpaul bzero(mtod(m_head, char *) + m_head->m_pkthdr.len, 257855058Swpaul RL_MIN_FRAMELEN - m_head->m_pkthdr.len); 257940516Swpaul m_head->m_pkthdr.len += 258052426Swpaul (RL_MIN_FRAMELEN - m_head->m_pkthdr.len); 258141243Swpaul m_head->m_len = m_head->m_pkthdr.len; 258241243Swpaul } 258340516Swpaul 258445633Swpaul RL_CUR_TXMBUF(sc) = m_head; 258540516Swpaul 258640516Swpaul return(0); 258740516Swpaul} 258840516Swpaul 258940516Swpaul/* 259040516Swpaul * Main transmit routine. 259140516Swpaul */ 259240516Swpaul 2593102335Salfredstatic void 2594102335Salfredrl_start(ifp) 259540516Swpaul struct ifnet *ifp; 259640516Swpaul{ 259740516Swpaul struct rl_softc *sc; 259840516Swpaul struct mbuf *m_head = NULL; 259940516Swpaul 260040516Swpaul sc = ifp->if_softc; 260167087Swpaul RL_LOCK(sc); 260240516Swpaul 260345633Swpaul while(RL_CUR_TXMBUF(sc) == NULL) { 260440516Swpaul IF_DEQUEUE(&ifp->if_snd, m_head); 260540516Swpaul if (m_head == NULL) 260640516Swpaul break; 260740516Swpaul 260858801Swpaul if (rl_encap(sc, m_head)) { 260958801Swpaul break; 261058801Swpaul } 261140516Swpaul 261240516Swpaul /* 261340516Swpaul * If there's a BPF listener, bounce a copy of this frame 261440516Swpaul * to him. 261540516Swpaul */ 2616106936Ssam BPF_MTAP(ifp, RL_CUR_TXMBUF(sc)); 261751583Swpaul 261840516Swpaul /* 261940516Swpaul * Transmit the frame. 2620109109Sdes */ 262181713Swpaul bus_dmamap_create(sc->rl_tag, 0, &RL_CUR_DMAMAP(sc)); 262281713Swpaul bus_dmamap_load(sc->rl_tag, RL_CUR_DMAMAP(sc), 262381713Swpaul mtod(RL_CUR_TXMBUF(sc), void *), 2624117388Swpaul RL_CUR_TXMBUF(sc)->m_pkthdr.len, rl_dma_map_txbuf, 2625117388Swpaul sc, BUS_DMA_NOWAIT); 262681713Swpaul bus_dmamap_sync(sc->rl_tag, RL_CUR_DMAMAP(sc), 262781713Swpaul BUS_DMASYNC_PREREAD); 262845633Swpaul CSR_WRITE_4(sc, RL_CUR_TXSTAT(sc), 262952426Swpaul RL_TXTHRESH(sc->rl_txthresh) | 263052426Swpaul RL_CUR_TXMBUF(sc)->m_pkthdr.len); 263145633Swpaul 263245633Swpaul RL_INC(sc->rl_cdata.cur_tx); 2633113237Ssilby 2634113237Ssilby /* 2635113237Ssilby * Set a timeout in case the chip goes out to lunch. 2636113237Ssilby */ 2637113237Ssilby ifp->if_timer = 5; 263840516Swpaul } 263940516Swpaul 264040516Swpaul /* 264145633Swpaul * We broke out of the loop because all our TX slots are 264245633Swpaul * full. Mark the NIC as busy until it drains some of the 264345633Swpaul * packets from the queue. 264445633Swpaul */ 264545633Swpaul if (RL_CUR_TXMBUF(sc) != NULL) 264645633Swpaul ifp->if_flags |= IFF_OACTIVE; 264745633Swpaul 264867087Swpaul RL_UNLOCK(sc); 264940516Swpaul 265040516Swpaul return; 265140516Swpaul} 265240516Swpaul 2653102335Salfredstatic void 2654102335Salfredrl_init(xsc) 265540516Swpaul void *xsc; 265640516Swpaul{ 265740516Swpaul struct rl_softc *sc = xsc; 265840516Swpaul struct ifnet *ifp = &sc->arpcom.ac_if; 265950703Swpaul struct mii_data *mii; 266040516Swpaul u_int32_t rxcfg = 0; 266140516Swpaul 266267087Swpaul RL_LOCK(sc); 266350703Swpaul mii = device_get_softc(sc->rl_miibus); 266440516Swpaul 266540516Swpaul /* 266640516Swpaul * Cancel pending I/O and free all RX/TX buffers. 266740516Swpaul */ 266840516Swpaul rl_stop(sc); 266940516Swpaul 2670117029Swpaul /* 2671117029Swpaul * Init our MAC address. Even though the chipset 2672117029Swpaul * documentation doesn't mention it, we need to enter "Config 2673117029Swpaul * register write enable" mode to modify the ID registers. 2674117029Swpaul */ 2675117029Swpaul CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG); 2676117029Swpaul CSR_WRITE_4(sc, RL_IDR0, *(u_int32_t *)(&sc->arpcom.ac_enaddr[0])); 2677117029Swpaul CSR_WRITE_4(sc, RL_IDR4, *(u_int32_t *)(&sc->arpcom.ac_enaddr[4])); 2678117029Swpaul CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 267940516Swpaul 2680117388Swpaul /* 2681117388Swpaul * For C+ mode, initialize the RX descriptors and mbufs. 2682117388Swpaul */ 2683117388Swpaul if (RL_ISCPLUS(sc)) { 2684117388Swpaul rl_rx_list_init(sc); 2685117388Swpaul rl_tx_list_init(sc); 2686117388Swpaul } else { 268740516Swpaul 2688117388Swpaul /* Init the RX buffer pointer register. */ 2689117388Swpaul bus_dmamap_load(sc->rl_tag, sc->rl_cdata.rl_rx_dmamap, 2690117388Swpaul sc->rl_cdata.rl_rx_buf, RL_RXBUFLEN, 2691117388Swpaul rl_dma_map_rxbuf, sc, BUS_DMA_NOWAIT); 2692117388Swpaul bus_dmamap_sync(sc->rl_tag, sc->rl_cdata.rl_rx_dmamap, 2693117388Swpaul BUS_DMASYNC_PREWRITE); 269440516Swpaul 2695117388Swpaul /* Init TX descriptors. */ 2696117388Swpaul rl_list_tx_init(sc); 2697117388Swpaul } 2698117388Swpaul 269940516Swpaul /* 270040516Swpaul * Enable transmit and receive. 270140516Swpaul */ 270240516Swpaul CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB); 270340516Swpaul 270440516Swpaul /* 270545633Swpaul * Set the initial TX and RX configuration. 270640516Swpaul */ 270745633Swpaul CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG); 270840516Swpaul CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG); 270940516Swpaul 271040516Swpaul /* Set the individual bit to receive frames for this host only. */ 271140516Swpaul rxcfg = CSR_READ_4(sc, RL_RXCFG); 271240516Swpaul rxcfg |= RL_RXCFG_RX_INDIV; 271340516Swpaul 271440516Swpaul /* If we want promiscuous mode, set the allframes bit. */ 271540516Swpaul if (ifp->if_flags & IFF_PROMISC) { 271640516Swpaul rxcfg |= RL_RXCFG_RX_ALLPHYS; 271740516Swpaul CSR_WRITE_4(sc, RL_RXCFG, rxcfg); 271840516Swpaul } else { 271940516Swpaul rxcfg &= ~RL_RXCFG_RX_ALLPHYS; 272040516Swpaul CSR_WRITE_4(sc, RL_RXCFG, rxcfg); 272140516Swpaul } 272240516Swpaul 272340516Swpaul /* 272440516Swpaul * Set capture broadcast bit to capture broadcast frames. 272540516Swpaul */ 272640516Swpaul if (ifp->if_flags & IFF_BROADCAST) { 272740516Swpaul rxcfg |= RL_RXCFG_RX_BROAD; 272840516Swpaul CSR_WRITE_4(sc, RL_RXCFG, rxcfg); 272940516Swpaul } else { 273040516Swpaul rxcfg &= ~RL_RXCFG_RX_BROAD; 273140516Swpaul CSR_WRITE_4(sc, RL_RXCFG, rxcfg); 273240516Swpaul } 273340516Swpaul 273440516Swpaul /* 273540516Swpaul * Program the multicast filter, if necessary. 273640516Swpaul */ 273740516Swpaul rl_setmulti(sc); 273840516Swpaul 273994883Sluigi#ifdef DEVICE_POLLING 274040516Swpaul /* 274194883Sluigi * Disable interrupts if we are polling. 274294883Sluigi */ 2743102052Ssobomax if (ifp->if_flags & IFF_POLLING) 274494883Sluigi CSR_WRITE_2(sc, RL_IMR, 0); 274594883Sluigi else /* otherwise ... */ 274694883Sluigi#endif /* DEVICE_POLLING */ 274794883Sluigi /* 274840516Swpaul * Enable interrupts. 274940516Swpaul */ 2750117388Swpaul if (RL_ISCPLUS(sc)) 2751117388Swpaul CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS); 2752117388Swpaul else 2753117388Swpaul CSR_WRITE_2(sc, RL_IMR, RL_INTRS); 275440516Swpaul 275552426Swpaul /* Set initial TX threshold */ 275652426Swpaul sc->rl_txthresh = RL_TX_THRESH_INIT; 275752426Swpaul 275840516Swpaul /* Start RX/TX process. */ 275940516Swpaul CSR_WRITE_4(sc, RL_MISSEDPKT, 0); 2760117388Swpaul#ifdef notdef 276140516Swpaul /* Enable receiver and transmitter. */ 276240516Swpaul CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB); 2763117388Swpaul#endif 2764117388Swpaul /* 2765117388Swpaul * If this is a C+ capable chip, enable C+ RX and TX mode, 2766117388Swpaul * and load the addresses of the RX and TX lists into the chip. 2767117388Swpaul */ 2768117388Swpaul if (RL_ISCPLUS(sc)) { 2769117388Swpaul CSR_WRITE_2(sc, RL_CPLUS_CMD, RL_CPLUSCMD_RXENB| 2770117388Swpaul RL_CPLUSCMD_TXENB|RL_CPLUSCMD_PCI_MRW| 2771117388Swpaul RL_CPLUSCMD_VLANSTRIP| 2772117388Swpaul (ifp->if_capenable & IFCAP_RXCSUM ? 2773117388Swpaul RL_CPLUSCMD_RXCSUM_ENB : 0)); 277440516Swpaul 2775118712Swpaul CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI, 2776118712Swpaul RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr)); 2777117388Swpaul CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO, 2778118712Swpaul RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr)); 2779117388Swpaul 2780118712Swpaul CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI, 2781118712Swpaul RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr)); 2782117388Swpaul CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO, 2783118712Swpaul RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr)); 2784117388Swpaul 2785117388Swpaul CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, RL_EARLYTXTHRESH_CNT); 2786117388Swpaul 2787117388Swpaul /* 2788117388Swpaul * Initialize the timer interrupt register so that 2789117388Swpaul * a timer interrupt will be generated once the timer 2790117388Swpaul * reaches a certain number of ticks. The timer is 2791117388Swpaul * reloaded on each transmit. This gives us TX interrupt 2792117388Swpaul * moderation, which dramatically improves TX frame rate. 2793117388Swpaul */ 2794117388Swpaul 2795118586Swpaul if (sc->rl_type == RL_8169) 2796118712Swpaul CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800); 2797118586Swpaul else 2798118586Swpaul CSR_WRITE_4(sc, RL_TIMERINT, 0x400); 2799117388Swpaul 2800117388Swpaul /* 2801117388Swpaul * For 8169 gigE NICs, set the max allowed RX packet 2802117388Swpaul * size so we can receive jumbo frames. 2803117388Swpaul */ 2804117388Swpaul if (sc->rl_type == RL_8169) 2805117388Swpaul CSR_WRITE_2(sc, RL_MAXRXPKTLEN, RL_PKTSZ(16384)); 2806117388Swpaul 2807117388Swpaul } 2808117388Swpaul 280950703Swpaul mii_mediachg(mii); 281040516Swpaul 281140516Swpaul CSR_WRITE_1(sc, RL_CFG1, RL_CFG1_DRVLOAD|RL_CFG1_FULLDUPLEX); 281240516Swpaul 281340516Swpaul ifp->if_flags |= IFF_RUNNING; 281440516Swpaul ifp->if_flags &= ~IFF_OACTIVE; 281540516Swpaul 281650703Swpaul sc->rl_stat_ch = timeout(rl_tick, sc, hz); 281767087Swpaul RL_UNLOCK(sc); 281850703Swpaul 281940516Swpaul return; 282040516Swpaul} 282140516Swpaul 282240516Swpaul/* 282340516Swpaul * Set media options. 282440516Swpaul */ 2825102335Salfredstatic int 2826102335Salfredrl_ifmedia_upd(ifp) 282740516Swpaul struct ifnet *ifp; 282840516Swpaul{ 282940516Swpaul struct rl_softc *sc; 283050703Swpaul struct mii_data *mii; 283140516Swpaul 283240516Swpaul sc = ifp->if_softc; 283350703Swpaul mii = device_get_softc(sc->rl_miibus); 283450703Swpaul mii_mediachg(mii); 283540516Swpaul 283640516Swpaul return(0); 283740516Swpaul} 283840516Swpaul 283940516Swpaul/* 284040516Swpaul * Report current media status. 284140516Swpaul */ 2842102335Salfredstatic void 2843102335Salfredrl_ifmedia_sts(ifp, ifmr) 284440516Swpaul struct ifnet *ifp; 284540516Swpaul struct ifmediareq *ifmr; 284640516Swpaul{ 284740516Swpaul struct rl_softc *sc; 284850703Swpaul struct mii_data *mii; 284940516Swpaul 285040516Swpaul sc = ifp->if_softc; 285150703Swpaul mii = device_get_softc(sc->rl_miibus); 285240516Swpaul 285350703Swpaul mii_pollstat(mii); 285450703Swpaul ifmr->ifm_active = mii->mii_media_active; 285550703Swpaul ifmr->ifm_status = mii->mii_media_status; 285640516Swpaul 285740516Swpaul return; 285840516Swpaul} 285940516Swpaul 2860102335Salfredstatic int 2861102335Salfredrl_ioctl(ifp, command, data) 286240516Swpaul struct ifnet *ifp; 286340516Swpaul u_long command; 286440516Swpaul caddr_t data; 286540516Swpaul{ 286640516Swpaul struct rl_softc *sc = ifp->if_softc; 286740516Swpaul struct ifreq *ifr = (struct ifreq *) data; 286850703Swpaul struct mii_data *mii; 286967087Swpaul int error = 0; 287040516Swpaul 287167087Swpaul RL_LOCK(sc); 287240516Swpaul 287340516Swpaul switch(command) { 287440516Swpaul case SIOCSIFFLAGS: 287540516Swpaul if (ifp->if_flags & IFF_UP) { 287640516Swpaul rl_init(sc); 287740516Swpaul } else { 287840516Swpaul if (ifp->if_flags & IFF_RUNNING) 287940516Swpaul rl_stop(sc); 288040516Swpaul } 288140516Swpaul error = 0; 288240516Swpaul break; 288340516Swpaul case SIOCADDMULTI: 288440516Swpaul case SIOCDELMULTI: 288540516Swpaul rl_setmulti(sc); 288640516Swpaul error = 0; 288740516Swpaul break; 288840516Swpaul case SIOCGIFMEDIA: 288940516Swpaul case SIOCSIFMEDIA: 289050703Swpaul mii = device_get_softc(sc->rl_miibus); 289150703Swpaul error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 289240516Swpaul break; 2893117388Swpaul case SIOCSIFCAP: 2894117388Swpaul ifp->if_capenable = ifr->ifr_reqcap; 2895117388Swpaul if (ifp->if_capenable & IFCAP_TXCSUM) 2896117388Swpaul ifp->if_hwassist = RL_CSUM_FEATURES; 2897117388Swpaul else 2898117388Swpaul ifp->if_hwassist = 0; 2899117388Swpaul if (ifp->if_flags & IFF_RUNNING) 2900117388Swpaul rl_init(sc); 2901117388Swpaul break; 290240516Swpaul default: 2903106936Ssam error = ether_ioctl(ifp, command, data); 290440516Swpaul break; 290540516Swpaul } 290640516Swpaul 290767087Swpaul RL_UNLOCK(sc); 290840516Swpaul 290940516Swpaul return(error); 291040516Swpaul} 291140516Swpaul 2912102335Salfredstatic void 2913102335Salfredrl_watchdog(ifp) 291440516Swpaul struct ifnet *ifp; 291540516Swpaul{ 291640516Swpaul struct rl_softc *sc; 291740516Swpaul 291840516Swpaul sc = ifp->if_softc; 291967087Swpaul RL_LOCK(sc); 292040516Swpaul printf("rl%d: watchdog timeout\n", sc->rl_unit); 292140516Swpaul ifp->if_oerrors++; 292250703Swpaul 2923117388Swpaul if (RL_ISCPLUS(sc)) { 2924117388Swpaul rl_txeofcplus(sc); 2925117388Swpaul rl_rxeofcplus(sc); 2926117388Swpaul } else { 2927117388Swpaul rl_txeof(sc); 2928117388Swpaul rl_rxeof(sc); 2929117388Swpaul } 2930117388Swpaul 293140516Swpaul rl_init(sc); 2932117388Swpaul 293367087Swpaul RL_UNLOCK(sc); 293440516Swpaul 293540516Swpaul return; 293640516Swpaul} 293740516Swpaul 293840516Swpaul/* 293940516Swpaul * Stop the adapter and free any mbufs allocated to the 294040516Swpaul * RX and TX lists. 294140516Swpaul */ 2942102335Salfredstatic void 2943102335Salfredrl_stop(sc) 294440516Swpaul struct rl_softc *sc; 294540516Swpaul{ 294640516Swpaul register int i; 294740516Swpaul struct ifnet *ifp; 294840516Swpaul 294967087Swpaul RL_LOCK(sc); 295040516Swpaul ifp = &sc->arpcom.ac_if; 295140516Swpaul ifp->if_timer = 0; 295240516Swpaul 295350703Swpaul untimeout(rl_tick, sc, sc->rl_stat_ch); 295494883Sluigi ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 295594883Sluigi#ifdef DEVICE_POLLING 295694883Sluigi ether_poll_deregister(ifp); 295794883Sluigi#endif /* DEVICE_POLLING */ 295850703Swpaul 295940516Swpaul CSR_WRITE_1(sc, RL_COMMAND, 0x00); 296040516Swpaul CSR_WRITE_2(sc, RL_IMR, 0x0000); 296140516Swpaul 2962117388Swpaul if (RL_ISCPLUS(sc)) { 2963117388Swpaul 2964117388Swpaul /* Free the TX list buffers. */ 2965117388Swpaul 2966117388Swpaul for (i = 0; i < RL_TX_DESC_CNT; i++) { 2967117388Swpaul if (sc->rl_ldata.rl_tx_mbuf[i] != NULL) { 2968117388Swpaul bus_dmamap_unload(sc->rl_ldata.rl_mtag, 2969117388Swpaul sc->rl_ldata.rl_tx_dmamap[i]); 2970117388Swpaul m_freem(sc->rl_ldata.rl_tx_mbuf[i]); 2971117388Swpaul sc->rl_ldata.rl_tx_mbuf[i] = NULL; 2972117388Swpaul } 297340516Swpaul } 2974117388Swpaul 2975117388Swpaul /* Free the RX list buffers. */ 2976117388Swpaul 2977117388Swpaul for (i = 0; i < RL_RX_DESC_CNT; i++) { 2978117388Swpaul if (sc->rl_ldata.rl_rx_mbuf[i] != NULL) { 2979117388Swpaul bus_dmamap_unload(sc->rl_ldata.rl_mtag, 2980117388Swpaul sc->rl_ldata.rl_rx_dmamap[i]); 2981117388Swpaul m_freem(sc->rl_ldata.rl_rx_mbuf[i]); 2982117388Swpaul sc->rl_ldata.rl_rx_mbuf[i] = NULL; 2983117388Swpaul } 2984117388Swpaul } 2985117388Swpaul 2986117388Swpaul } else { 2987117388Swpaul 2988117388Swpaul bus_dmamap_unload(sc->rl_tag, sc->rl_cdata.rl_rx_dmamap); 2989117388Swpaul 2990117388Swpaul /* 2991117388Swpaul * Free the TX list buffers. 2992117388Swpaul */ 2993117388Swpaul for (i = 0; i < RL_TX_LIST_CNT; i++) { 2994117388Swpaul if (sc->rl_cdata.rl_tx_chain[i] != NULL) { 2995117388Swpaul bus_dmamap_unload(sc->rl_tag, 2996117388Swpaul sc->rl_cdata.rl_tx_dmamap[i]); 2997117388Swpaul bus_dmamap_destroy(sc->rl_tag, 2998117388Swpaul sc->rl_cdata.rl_tx_dmamap[i]); 2999117388Swpaul m_freem(sc->rl_cdata.rl_tx_chain[i]); 3000117388Swpaul sc->rl_cdata.rl_tx_chain[i] = NULL; 3001117388Swpaul CSR_WRITE_4(sc, RL_TXADDR0 + i, 0x0000000); 3002117388Swpaul } 3003117388Swpaul } 300440516Swpaul } 300540516Swpaul 300667087Swpaul RL_UNLOCK(sc); 300740516Swpaul return; 300840516Swpaul} 300940516Swpaul 301040516Swpaul/* 301186822Siwasaki * Device suspend routine. Stop the interface and save some PCI 301286822Siwasaki * settings in case the BIOS doesn't restore them properly on 301386822Siwasaki * resume. 301486822Siwasaki */ 3015102335Salfredstatic int 3016102335Salfredrl_suspend(dev) 301786822Siwasaki device_t dev; 301886822Siwasaki{ 301986822Siwasaki register int i; 302086822Siwasaki struct rl_softc *sc; 302186822Siwasaki 302286822Siwasaki sc = device_get_softc(dev); 302386822Siwasaki 302486822Siwasaki rl_stop(sc); 302586822Siwasaki 302686822Siwasaki for (i = 0; i < 5; i++) 302786822Siwasaki sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4); 302886822Siwasaki sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4); 302986822Siwasaki sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1); 303086822Siwasaki sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1); 303186822Siwasaki sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1); 303286822Siwasaki 303386822Siwasaki sc->suspended = 1; 303486822Siwasaki 303586822Siwasaki return (0); 303686822Siwasaki} 303786822Siwasaki 303886822Siwasaki/* 303986822Siwasaki * Device resume routine. Restore some PCI settings in case the BIOS 304086822Siwasaki * doesn't, re-enable busmastering, and restart the interface if 304186822Siwasaki * appropriate. 304286822Siwasaki */ 3043102335Salfredstatic int 3044102335Salfredrl_resume(dev) 304586822Siwasaki device_t dev; 304686822Siwasaki{ 304786822Siwasaki register int i; 304886822Siwasaki struct rl_softc *sc; 304986822Siwasaki struct ifnet *ifp; 305086822Siwasaki 305186822Siwasaki sc = device_get_softc(dev); 305286822Siwasaki ifp = &sc->arpcom.ac_if; 305386822Siwasaki 305486822Siwasaki /* better way to do this? */ 305586822Siwasaki for (i = 0; i < 5; i++) 305686822Siwasaki pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4); 305786822Siwasaki pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4); 305886822Siwasaki pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1); 305986822Siwasaki pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1); 306086822Siwasaki pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1); 306186822Siwasaki 306286822Siwasaki /* reenable busmastering */ 306386822Siwasaki pci_enable_busmaster(dev); 306486822Siwasaki pci_enable_io(dev, RL_RES); 306586822Siwasaki 3066109109Sdes /* reinitialize interface if necessary */ 3067109109Sdes if (ifp->if_flags & IFF_UP) 3068109109Sdes rl_init(sc); 306986822Siwasaki 307086822Siwasaki sc->suspended = 0; 307186822Siwasaki 307286822Siwasaki return (0); 307386822Siwasaki} 307486822Siwasaki 307586822Siwasaki/* 307640516Swpaul * Stop all chip I/O so that the kernel's probe routines don't 307740516Swpaul * get confused by errant DMAs when rebooting. 307840516Swpaul */ 3079102335Salfredstatic void 3080102335Salfredrl_shutdown(dev) 308150703Swpaul device_t dev; 308240516Swpaul{ 308350703Swpaul struct rl_softc *sc; 308440516Swpaul 308550703Swpaul sc = device_get_softc(dev); 308650703Swpaul 308740516Swpaul rl_stop(sc); 308840516Swpaul 308940516Swpaul return; 309040516Swpaul} 3091