1098ca2bdSWarner Losh /*- 2a94100faSBill Paul * Copyright (c) 1997, 1998-2003 3a94100faSBill Paul * Bill Paul <wpaul@windriver.com>. All rights reserved. 4a94100faSBill Paul * 5a94100faSBill Paul * Redistribution and use in source and binary forms, with or without 6a94100faSBill Paul * modification, are permitted provided that the following conditions 7a94100faSBill Paul * are met: 8a94100faSBill Paul * 1. Redistributions of source code must retain the above copyright 9a94100faSBill Paul * notice, this list of conditions and the following disclaimer. 10a94100faSBill Paul * 2. Redistributions in binary form must reproduce the above copyright 11a94100faSBill Paul * notice, this list of conditions and the following disclaimer in the 12a94100faSBill Paul * documentation and/or other materials provided with the distribution. 13a94100faSBill Paul * 3. All advertising materials mentioning features or use of this software 14a94100faSBill Paul * must display the following acknowledgement: 15a94100faSBill Paul * This product includes software developed by Bill Paul. 16a94100faSBill Paul * 4. Neither the name of the author nor the names of any co-contributors 17a94100faSBill Paul * may be used to endorse or promote products derived from this software 18a94100faSBill Paul * without specific prior written permission. 19a94100faSBill Paul * 20a94100faSBill Paul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21a94100faSBill Paul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22a94100faSBill Paul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23a94100faSBill Paul * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 24a94100faSBill Paul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25a94100faSBill Paul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26a94100faSBill Paul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27a94100faSBill Paul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28a94100faSBill Paul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29a94100faSBill Paul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30a94100faSBill Paul * THE POSSIBILITY OF SUCH DAMAGE. 31a94100faSBill Paul */ 32a94100faSBill Paul 334dc52c32SDavid E. O'Brien #include <sys/cdefs.h> 344dc52c32SDavid E. O'Brien __FBSDID("$FreeBSD$"); 354dc52c32SDavid E. O'Brien 36a94100faSBill Paul /* 37ed510fb0SBill Paul * RealTek 8139C+/8169/8169S/8110S/8168/8111/8101E PCI NIC driver 38a94100faSBill Paul * 39a94100faSBill Paul * Written by Bill Paul <wpaul@windriver.com> 40a94100faSBill Paul * Senior Networking Software Engineer 41a94100faSBill Paul * Wind River Systems 42a94100faSBill Paul */ 43a94100faSBill Paul 44a94100faSBill Paul /* 45a94100faSBill Paul * This driver is designed to support RealTek's next generation of 46a94100faSBill Paul * 10/100 and 10/100/1000 PCI ethernet controllers. There are currently 47ed510fb0SBill Paul * seven devices in this family: the RTL8139C+, the RTL8169, the RTL8169S, 48ed510fb0SBill Paul * RTL8110S, the RTL8168, the RTL8111 and the RTL8101E. 49a94100faSBill Paul * 50a94100faSBill Paul * The 8139C+ is a 10/100 ethernet chip. It is backwards compatible 51a94100faSBill Paul * with the older 8139 family, however it also supports a special 52a94100faSBill Paul * C+ mode of operation that provides several new performance enhancing 53a94100faSBill Paul * features. These include: 54a94100faSBill Paul * 55a94100faSBill Paul * o Descriptor based DMA mechanism. Each descriptor represents 56a94100faSBill Paul * a single packet fragment. Data buffers may be aligned on 57a94100faSBill Paul * any byte boundary. 58a94100faSBill Paul * 59a94100faSBill Paul * o 64-bit DMA 60a94100faSBill Paul * 61a94100faSBill Paul * o TCP/IP checksum offload for both RX and TX 62a94100faSBill Paul * 63a94100faSBill Paul * o High and normal priority transmit DMA rings 64a94100faSBill Paul * 65a94100faSBill Paul * o VLAN tag insertion and extraction 66a94100faSBill Paul * 67a94100faSBill Paul * o TCP large send (segmentation offload) 68a94100faSBill Paul * 69a94100faSBill Paul * Like the 8139, the 8139C+ also has a built-in 10/100 PHY. The C+ 70a94100faSBill Paul * programming API is fairly straightforward. The RX filtering, EEPROM 71a94100faSBill Paul * access and PHY access is the same as it is on the older 8139 series 72a94100faSBill Paul * chips. 73a94100faSBill Paul * 74a94100faSBill Paul * The 8169 is a 64-bit 10/100/1000 gigabit ethernet MAC. It has almost the 75a94100faSBill Paul * same programming API and feature set as the 8139C+ with the following 76a94100faSBill Paul * differences and additions: 77a94100faSBill Paul * 78a94100faSBill Paul * o 1000Mbps mode 79a94100faSBill Paul * 80a94100faSBill Paul * o Jumbo frames 81a94100faSBill Paul * 82a94100faSBill Paul * o GMII and TBI ports/registers for interfacing with copper 83a94100faSBill Paul * or fiber PHYs 84a94100faSBill Paul * 85a94100faSBill Paul * o RX and TX DMA rings can have up to 1024 descriptors 86a94100faSBill Paul * (the 8139C+ allows a maximum of 64) 87a94100faSBill Paul * 88a94100faSBill Paul * o Slight differences in register layout from the 8139C+ 89a94100faSBill Paul * 90a94100faSBill Paul * The TX start and timer interrupt registers are at different locations 91a94100faSBill Paul * on the 8169 than they are on the 8139C+. Also, the status word in the 92a94100faSBill Paul * RX descriptor has a slightly different bit layout. The 8169 does not 93a94100faSBill Paul * have a built-in PHY. Most reference boards use a Marvell 88E1000 'Alaska' 94a94100faSBill Paul * copper gigE PHY. 95a94100faSBill Paul * 96a94100faSBill Paul * The 8169S/8110S 10/100/1000 devices have built-in copper gigE PHYs 97a94100faSBill Paul * (the 'S' stands for 'single-chip'). These devices have the same 98a94100faSBill Paul * programming API as the older 8169, but also have some vendor-specific 99a94100faSBill Paul * registers for the on-board PHY. The 8110S is a LAN-on-motherboard 100a94100faSBill Paul * part designed to be pin-compatible with the RealTek 8100 10/100 chip. 101a94100faSBill Paul * 102a94100faSBill Paul * This driver takes advantage of the RX and TX checksum offload and 103a94100faSBill Paul * VLAN tag insertion/extraction features. It also implements TX 104a94100faSBill Paul * interrupt moderation using the timer interrupt registers, which 105a94100faSBill Paul * significantly reduces TX interrupt load. There is also support 106a94100faSBill Paul * for jumbo frames, however the 8169/8169S/8110S can not transmit 10722a11c96SJohn-Mark Gurney * jumbo frames larger than 7440, so the max MTU possible with this 10822a11c96SJohn-Mark Gurney * driver is 7422 bytes. 109a94100faSBill Paul */ 110a94100faSBill Paul 111f0796cd2SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS 112f0796cd2SGleb Smirnoff #include "opt_device_polling.h" 113f0796cd2SGleb Smirnoff #endif 114f0796cd2SGleb Smirnoff 115a94100faSBill Paul #include <sys/param.h> 116a94100faSBill Paul #include <sys/endian.h> 117a94100faSBill Paul #include <sys/systm.h> 118a94100faSBill Paul #include <sys/sockio.h> 119a94100faSBill Paul #include <sys/mbuf.h> 120a94100faSBill Paul #include <sys/malloc.h> 121fe12f24bSPoul-Henning Kamp #include <sys/module.h> 122a94100faSBill Paul #include <sys/kernel.h> 123a94100faSBill Paul #include <sys/socket.h> 124ed510fb0SBill Paul #include <sys/lock.h> 125ed510fb0SBill Paul #include <sys/mutex.h> 1260534aae0SPyun YongHyeon #include <sys/sysctl.h> 127ed510fb0SBill Paul #include <sys/taskqueue.h> 128a94100faSBill Paul 129a94100faSBill Paul #include <net/if.h> 13076039bc8SGleb Smirnoff #include <net/if_var.h> 131a94100faSBill Paul #include <net/if_arp.h> 132a94100faSBill Paul #include <net/ethernet.h> 133a94100faSBill Paul #include <net/if_dl.h> 134a94100faSBill Paul #include <net/if_media.h> 135fc74a9f9SBrooks Davis #include <net/if_types.h> 136a94100faSBill Paul #include <net/if_vlan_var.h> 137a94100faSBill Paul 138a94100faSBill Paul #include <net/bpf.h> 139a94100faSBill Paul 140a94100faSBill Paul #include <machine/bus.h> 141a94100faSBill Paul #include <machine/resource.h> 142a94100faSBill Paul #include <sys/bus.h> 143a94100faSBill Paul #include <sys/rman.h> 144a94100faSBill Paul 145a94100faSBill Paul #include <dev/mii/mii.h> 146a94100faSBill Paul #include <dev/mii/miivar.h> 147a94100faSBill Paul 148a94100faSBill Paul #include <dev/pci/pcireg.h> 149a94100faSBill Paul #include <dev/pci/pcivar.h> 150a94100faSBill Paul 151b2d3d26fSGleb Smirnoff #include <dev/rl/if_rlreg.h> 152d65abd66SPyun YongHyeon 153a94100faSBill Paul MODULE_DEPEND(re, pci, 1, 1, 1); 154a94100faSBill Paul MODULE_DEPEND(re, ether, 1, 1, 1); 155a94100faSBill Paul MODULE_DEPEND(re, miibus, 1, 1, 1); 156a94100faSBill Paul 157298bfdf3SWarner Losh /* "device miibus" required. See GENERIC if you get errors here. */ 158a94100faSBill Paul #include "miibus_if.h" 159a94100faSBill Paul 1605774c5ffSPyun YongHyeon /* Tunables. */ 161502be0f7SPyun YongHyeon static int intr_filter = 0; 162502be0f7SPyun YongHyeon TUNABLE_INT("hw.re.intr_filter", &intr_filter); 163c2d2e19cSPyun YongHyeon static int msi_disable = 0; 1645774c5ffSPyun YongHyeon TUNABLE_INT("hw.re.msi_disable", &msi_disable); 1654a58fd45SPyun YongHyeon static int msix_disable = 0; 1664a58fd45SPyun YongHyeon TUNABLE_INT("hw.re.msix_disable", &msix_disable); 1672c21710bSPyun YongHyeon static int prefer_iomap = 0; 1682c21710bSPyun YongHyeon TUNABLE_INT("hw.re.prefer_iomap", &prefer_iomap); 1695774c5ffSPyun YongHyeon 170a94100faSBill Paul #define RE_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP) 171a94100faSBill Paul 172a94100faSBill Paul /* 173a94100faSBill Paul * Various supported device vendors/types and their names. 174a94100faSBill Paul */ 17529658c96SDimitry Andric static const struct rl_type re_devs[] = { 1769dfcacbeSPyun YongHyeon { DLINK_VENDORID, DLINK_DEVICEID_528T, 0, 17732aa5f0eSAnton Berezin "D-Link DGE-528(T) Gigabit Ethernet Adapter" }, 178caa19d50SPyun YongHyeon { DLINK_VENDORID, DLINK_DEVICEID_530T_REVC, 0, 179caa19d50SPyun YongHyeon "D-Link DGE-530(T) Gigabit Ethernet Adapter" }, 1809dfcacbeSPyun YongHyeon { RT_VENDORID, RT_DEVICEID_8139, 0, 181a94100faSBill Paul "RealTek 8139C+ 10/100BaseTX" }, 1829dfcacbeSPyun YongHyeon { RT_VENDORID, RT_DEVICEID_8101E, 0, 18354899a96SPyun YongHyeon "RealTek 810xE PCIe 10/100baseTX" }, 1849dfcacbeSPyun YongHyeon { RT_VENDORID, RT_DEVICEID_8168, 0, 185ab9f923eSPyun YongHyeon "RealTek 8168/8111 B/C/CP/D/DP/E/F/G PCIe Gigabit Ethernet" }, 186*938e9a89SKevin Lo { NCUBE_VENDORID, RT_DEVICEID_8168, 0, 187*938e9a89SKevin Lo "TP-Link TG-3468 v2 (RTL8168) Gigabit Ethernet" }, 1889dfcacbeSPyun YongHyeon { RT_VENDORID, RT_DEVICEID_8169, 0, 189715922d7SPyun YongHyeon "RealTek 8169/8169S/8169SB(L)/8110S/8110SB(L) Gigabit Ethernet" }, 1909dfcacbeSPyun YongHyeon { RT_VENDORID, RT_DEVICEID_8169SC, 0, 1912ee2c3b4SRemko Lodder "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" }, 1929dfcacbeSPyun YongHyeon { COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, 0, 193ea263191SMIHIRA Sanpei Yoshiro "Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" }, 1949dfcacbeSPyun YongHyeon { LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, 0, 19526390635SJohn Baldwin "Linksys EG1032 (RTL8169S) Gigabit Ethernet" }, 1969dfcacbeSPyun YongHyeon { USR_VENDORID, USR_DEVICEID_997902, 0, 197dfdb409eSPyun YongHyeon "US Robotics 997902 (RTL8169S) Gigabit Ethernet" } 198a94100faSBill Paul }; 199a94100faSBill Paul 20029658c96SDimitry Andric static const struct rl_hwrev re_hwrevs[] = { 20181eee0ebSPyun YongHyeon { RL_HWREV_8139, RL_8139, "", RL_MTU }, 20281eee0ebSPyun YongHyeon { RL_HWREV_8139A, RL_8139, "A", RL_MTU }, 20381eee0ebSPyun YongHyeon { RL_HWREV_8139AG, RL_8139, "A-G", RL_MTU }, 20481eee0ebSPyun YongHyeon { RL_HWREV_8139B, RL_8139, "B", RL_MTU }, 20581eee0ebSPyun YongHyeon { RL_HWREV_8130, RL_8139, "8130", RL_MTU }, 20681eee0ebSPyun YongHyeon { RL_HWREV_8139C, RL_8139, "C", RL_MTU }, 20781eee0ebSPyun YongHyeon { RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C", RL_MTU }, 20881eee0ebSPyun YongHyeon { RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+", RL_MTU }, 209ef278cb4SPyun YongHyeon { RL_HWREV_8168B_SPIN1, RL_8169, "8168", RL_JUMBO_MTU }, 21081eee0ebSPyun YongHyeon { RL_HWREV_8169, RL_8169, "8169", RL_JUMBO_MTU }, 21181eee0ebSPyun YongHyeon { RL_HWREV_8169S, RL_8169, "8169S", RL_JUMBO_MTU }, 21281eee0ebSPyun YongHyeon { RL_HWREV_8110S, RL_8169, "8110S", RL_JUMBO_MTU }, 21381eee0ebSPyun YongHyeon { RL_HWREV_8169_8110SB, RL_8169, "8169SB/8110SB", RL_JUMBO_MTU }, 21481eee0ebSPyun YongHyeon { RL_HWREV_8169_8110SC, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU }, 21581eee0ebSPyun YongHyeon { RL_HWREV_8169_8110SBL, RL_8169, "8169SBL/8110SBL", RL_JUMBO_MTU }, 21681eee0ebSPyun YongHyeon { RL_HWREV_8169_8110SCE, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU }, 21781eee0ebSPyun YongHyeon { RL_HWREV_8100, RL_8139, "8100", RL_MTU }, 21881eee0ebSPyun YongHyeon { RL_HWREV_8101, RL_8139, "8101", RL_MTU }, 21981eee0ebSPyun YongHyeon { RL_HWREV_8100E, RL_8169, "8100E", RL_MTU }, 22081eee0ebSPyun YongHyeon { RL_HWREV_8101E, RL_8169, "8101E", RL_MTU }, 22181eee0ebSPyun YongHyeon { RL_HWREV_8102E, RL_8169, "8102E", RL_MTU }, 22281eee0ebSPyun YongHyeon { RL_HWREV_8102EL, RL_8169, "8102EL", RL_MTU }, 22381eee0ebSPyun YongHyeon { RL_HWREV_8102EL_SPIN1, RL_8169, "8102EL", RL_MTU }, 22481eee0ebSPyun YongHyeon { RL_HWREV_8103E, RL_8169, "8103E", RL_MTU }, 22539e69201SPyun YongHyeon { RL_HWREV_8401E, RL_8169, "8401E", RL_MTU }, 226a9e3362aSPyun YongHyeon { RL_HWREV_8402, RL_8169, "8402", RL_MTU }, 22754899a96SPyun YongHyeon { RL_HWREV_8105E, RL_8169, "8105E", RL_MTU }, 2286b0a8e04SPyun YongHyeon { RL_HWREV_8105E_SPIN1, RL_8169, "8105E", RL_MTU }, 229214c71f6SPyun YongHyeon { RL_HWREV_8106E, RL_8169, "8106E", RL_MTU }, 230ef278cb4SPyun YongHyeon { RL_HWREV_8168B_SPIN2, RL_8169, "8168", RL_JUMBO_MTU }, 231ef278cb4SPyun YongHyeon { RL_HWREV_8168B_SPIN3, RL_8169, "8168", RL_JUMBO_MTU }, 23281eee0ebSPyun YongHyeon { RL_HWREV_8168C, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K }, 23381eee0ebSPyun YongHyeon { RL_HWREV_8168C_SPIN2, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K }, 23481eee0ebSPyun YongHyeon { RL_HWREV_8168CP, RL_8169, "8168CP/8111CP", RL_JUMBO_MTU_6K }, 23581eee0ebSPyun YongHyeon { RL_HWREV_8168D, RL_8169, "8168D/8111D", RL_JUMBO_MTU_9K }, 23681eee0ebSPyun YongHyeon { RL_HWREV_8168DP, RL_8169, "8168DP/8111DP", RL_JUMBO_MTU_9K }, 23781eee0ebSPyun YongHyeon { RL_HWREV_8168E, RL_8169, "8168E/8111E", RL_JUMBO_MTU_9K}, 23881eee0ebSPyun YongHyeon { RL_HWREV_8168E_VL, RL_8169, "8168E/8111E-VL", RL_JUMBO_MTU_6K}, 239c3767eabSPyun YongHyeon { RL_HWREV_8168EP, RL_8169, "8168EP/8111EP", RL_JUMBO_MTU_9K}, 240d467ffaaSPyun YongHyeon { RL_HWREV_8168F, RL_8169, "8168F/8111F", RL_JUMBO_MTU_9K}, 241ab9f923eSPyun YongHyeon { RL_HWREV_8168G, RL_8169, "8168G/8111G", RL_JUMBO_MTU_9K}, 242ab9f923eSPyun YongHyeon { RL_HWREV_8168GU, RL_8169, "8168GU/8111GU", RL_JUMBO_MTU_9K}, 24396b2c26aSMarius Strobl { RL_HWREV_8168H, RL_8169, "8168H/8111H", RL_JUMBO_MTU_9K}, 244d56f7f52SPyun YongHyeon { RL_HWREV_8411, RL_8169, "8411", RL_JUMBO_MTU_9K}, 245ab9f923eSPyun YongHyeon { RL_HWREV_8411B, RL_8169, "8411B", RL_JUMBO_MTU_9K}, 24681eee0ebSPyun YongHyeon { 0, 0, NULL, 0 } 247a94100faSBill Paul }; 248a94100faSBill Paul 249a94100faSBill Paul static int re_probe (device_t); 250a94100faSBill Paul static int re_attach (device_t); 251a94100faSBill Paul static int re_detach (device_t); 252a94100faSBill Paul 253d65abd66SPyun YongHyeon static int re_encap (struct rl_softc *, struct mbuf **); 254a94100faSBill Paul 255a94100faSBill Paul static void re_dma_map_addr (void *, bus_dma_segment_t *, int, int); 256a94100faSBill Paul static int re_allocmem (device_t, struct rl_softc *); 257d65abd66SPyun YongHyeon static __inline void re_discard_rxbuf 258d65abd66SPyun YongHyeon (struct rl_softc *, int); 259d65abd66SPyun YongHyeon static int re_newbuf (struct rl_softc *, int); 26081eee0ebSPyun YongHyeon static int re_jumbo_newbuf (struct rl_softc *, int); 261a94100faSBill Paul static int re_rx_list_init (struct rl_softc *); 26281eee0ebSPyun YongHyeon static int re_jrx_list_init (struct rl_softc *); 263a94100faSBill Paul static int re_tx_list_init (struct rl_softc *); 26422a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX 26522a11c96SJohn-Mark Gurney static __inline void re_fixup_rx 26622a11c96SJohn-Mark Gurney (struct mbuf *); 26722a11c96SJohn-Mark Gurney #endif 2681abcdbd1SAttilio Rao static int re_rxeof (struct rl_softc *, int *); 269a94100faSBill Paul static void re_txeof (struct rl_softc *); 27097b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING 2711abcdbd1SAttilio Rao static int re_poll (struct ifnet *, enum poll_cmd, int); 2721abcdbd1SAttilio Rao static int re_poll_locked (struct ifnet *, enum poll_cmd, int); 27397b9d4baSJohn-Mark Gurney #endif 274ef544f63SPaolo Pisati static int re_intr (void *); 275502be0f7SPyun YongHyeon static void re_intr_msi (void *); 276a94100faSBill Paul static void re_tick (void *); 277ed510fb0SBill Paul static void re_int_task (void *, int); 278a94100faSBill Paul static void re_start (struct ifnet *); 279d180a66fSPyun YongHyeon static void re_start_locked (struct ifnet *); 280a94100faSBill Paul static int re_ioctl (struct ifnet *, u_long, caddr_t); 281a94100faSBill Paul static void re_init (void *); 28297b9d4baSJohn-Mark Gurney static void re_init_locked (struct rl_softc *); 283a94100faSBill Paul static void re_stop (struct rl_softc *); 2841d545c7aSMarius Strobl static void re_watchdog (struct rl_softc *); 285a94100faSBill Paul static int re_suspend (device_t); 286a94100faSBill Paul static int re_resume (device_t); 2876a087a87SPyun YongHyeon static int re_shutdown (device_t); 288a94100faSBill Paul static int re_ifmedia_upd (struct ifnet *); 289a94100faSBill Paul static void re_ifmedia_sts (struct ifnet *, struct ifmediareq *); 290a94100faSBill Paul 291a94100faSBill Paul static void re_eeprom_putbyte (struct rl_softc *, int); 292a94100faSBill Paul static void re_eeprom_getword (struct rl_softc *, int, u_int16_t *); 293ed510fb0SBill Paul static void re_read_eeprom (struct rl_softc *, caddr_t, int, int); 294a94100faSBill Paul static int re_gmii_readreg (device_t, int, int); 295a94100faSBill Paul static int re_gmii_writereg (device_t, int, int, int); 296a94100faSBill Paul 297a94100faSBill Paul static int re_miibus_readreg (device_t, int, int); 298a94100faSBill Paul static int re_miibus_writereg (device_t, int, int, int); 299a94100faSBill Paul static void re_miibus_statchg (device_t); 300a94100faSBill Paul 30181eee0ebSPyun YongHyeon static void re_set_jumbo (struct rl_softc *, int); 302ff191365SJung-uk Kim static void re_set_rxmode (struct rl_softc *); 303a94100faSBill Paul static void re_reset (struct rl_softc *); 3047467bd53SPyun YongHyeon static void re_setwol (struct rl_softc *); 3057467bd53SPyun YongHyeon static void re_clrwol (struct rl_softc *); 3066830588dSPyun YongHyeon static void re_set_linkspeed (struct rl_softc *); 307a94100faSBill Paul 308579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP /* see ixgbe.c for details */ 309579a6e3cSLuigi Rizzo #include <dev/netmap/if_re_netmap.h> 310847bf383SLuigi Rizzo MODULE_DEPEND(re, netmap, 1, 1, 1); 311579a6e3cSLuigi Rizzo #endif /* !DEV_NETMAP */ 312579a6e3cSLuigi Rizzo 313ed510fb0SBill Paul #ifdef RE_DIAG 314a94100faSBill Paul static int re_diag (struct rl_softc *); 315ed510fb0SBill Paul #endif 316a94100faSBill Paul 3170534aae0SPyun YongHyeon static void re_add_sysctls (struct rl_softc *); 3180534aae0SPyun YongHyeon static int re_sysctl_stats (SYSCTL_HANDLER_ARGS); 319502be0f7SPyun YongHyeon static int sysctl_int_range (SYSCTL_HANDLER_ARGS, int, int); 320502be0f7SPyun YongHyeon static int sysctl_hw_re_int_mod (SYSCTL_HANDLER_ARGS); 3210534aae0SPyun YongHyeon 322a94100faSBill Paul static device_method_t re_methods[] = { 323a94100faSBill Paul /* Device interface */ 324a94100faSBill Paul DEVMETHOD(device_probe, re_probe), 325a94100faSBill Paul DEVMETHOD(device_attach, re_attach), 326a94100faSBill Paul DEVMETHOD(device_detach, re_detach), 327a94100faSBill Paul DEVMETHOD(device_suspend, re_suspend), 328a94100faSBill Paul DEVMETHOD(device_resume, re_resume), 329a94100faSBill Paul DEVMETHOD(device_shutdown, re_shutdown), 330a94100faSBill Paul 331a94100faSBill Paul /* MII interface */ 332a94100faSBill Paul DEVMETHOD(miibus_readreg, re_miibus_readreg), 333a94100faSBill Paul DEVMETHOD(miibus_writereg, re_miibus_writereg), 334a94100faSBill Paul DEVMETHOD(miibus_statchg, re_miibus_statchg), 335a94100faSBill Paul 3364b7ec270SMarius Strobl DEVMETHOD_END 337a94100faSBill Paul }; 338a94100faSBill Paul 339a94100faSBill Paul static driver_t re_driver = { 340a94100faSBill Paul "re", 341a94100faSBill Paul re_methods, 342a94100faSBill Paul sizeof(struct rl_softc) 343a94100faSBill Paul }; 344a94100faSBill Paul 345a94100faSBill Paul static devclass_t re_devclass; 346a94100faSBill Paul 347a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0); 348a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0); 349a94100faSBill Paul 350a94100faSBill Paul #define EE_SET(x) \ 351a94100faSBill Paul CSR_WRITE_1(sc, RL_EECMD, \ 352a94100faSBill Paul CSR_READ_1(sc, RL_EECMD) | x) 353a94100faSBill Paul 354a94100faSBill Paul #define EE_CLR(x) \ 355a94100faSBill Paul CSR_WRITE_1(sc, RL_EECMD, \ 356a94100faSBill Paul CSR_READ_1(sc, RL_EECMD) & ~x) 357a94100faSBill Paul 358a94100faSBill Paul /* 359a94100faSBill Paul * Send a read command and address to the EEPROM, check for ACK. 360a94100faSBill Paul */ 361a94100faSBill Paul static void 3627b5ffebfSPyun YongHyeon re_eeprom_putbyte(struct rl_softc *sc, int addr) 363a94100faSBill Paul { 3640ce0868aSPyun YongHyeon int d, i; 365a94100faSBill Paul 366ed510fb0SBill Paul d = addr | (RL_9346_READ << sc->rl_eewidth); 367a94100faSBill Paul 368a94100faSBill Paul /* 369a94100faSBill Paul * Feed in each bit and strobe the clock. 370a94100faSBill Paul */ 371ed510fb0SBill Paul 372ed510fb0SBill Paul for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) { 373a94100faSBill Paul if (d & i) { 374a94100faSBill Paul EE_SET(RL_EE_DATAIN); 375a94100faSBill Paul } else { 376a94100faSBill Paul EE_CLR(RL_EE_DATAIN); 377a94100faSBill Paul } 378a94100faSBill Paul DELAY(100); 379a94100faSBill Paul EE_SET(RL_EE_CLK); 380a94100faSBill Paul DELAY(150); 381a94100faSBill Paul EE_CLR(RL_EE_CLK); 382a94100faSBill Paul DELAY(100); 383a94100faSBill Paul } 384a94100faSBill Paul } 385a94100faSBill Paul 386a94100faSBill Paul /* 387a94100faSBill Paul * Read a word of data stored in the EEPROM at address 'addr.' 388a94100faSBill Paul */ 389a94100faSBill Paul static void 3907b5ffebfSPyun YongHyeon re_eeprom_getword(struct rl_softc *sc, int addr, u_int16_t *dest) 391a94100faSBill Paul { 3920ce0868aSPyun YongHyeon int i; 393a94100faSBill Paul u_int16_t word = 0; 394a94100faSBill Paul 395a94100faSBill Paul /* 396a94100faSBill Paul * Send address of word we want to read. 397a94100faSBill Paul */ 398a94100faSBill Paul re_eeprom_putbyte(sc, addr); 399a94100faSBill Paul 400a94100faSBill Paul /* 401a94100faSBill Paul * Start reading bits from EEPROM. 402a94100faSBill Paul */ 403a94100faSBill Paul for (i = 0x8000; i; i >>= 1) { 404a94100faSBill Paul EE_SET(RL_EE_CLK); 405a94100faSBill Paul DELAY(100); 406a94100faSBill Paul if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT) 407a94100faSBill Paul word |= i; 408a94100faSBill Paul EE_CLR(RL_EE_CLK); 409a94100faSBill Paul DELAY(100); 410a94100faSBill Paul } 411a94100faSBill Paul 412a94100faSBill Paul *dest = word; 413a94100faSBill Paul } 414a94100faSBill Paul 415a94100faSBill Paul /* 416a94100faSBill Paul * Read a sequence of words from the EEPROM. 417a94100faSBill Paul */ 418a94100faSBill Paul static void 4197b5ffebfSPyun YongHyeon re_read_eeprom(struct rl_softc *sc, caddr_t dest, int off, int cnt) 420a94100faSBill Paul { 421a94100faSBill Paul int i; 422a94100faSBill Paul u_int16_t word = 0, *ptr; 423a94100faSBill Paul 424ed510fb0SBill Paul CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM); 425ed510fb0SBill Paul 426ed510fb0SBill Paul DELAY(100); 427ed510fb0SBill Paul 428a94100faSBill Paul for (i = 0; i < cnt; i++) { 429ed510fb0SBill Paul CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL); 430a94100faSBill Paul re_eeprom_getword(sc, off + i, &word); 431ed510fb0SBill Paul CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL); 432a94100faSBill Paul ptr = (u_int16_t *)(dest + (i * 2)); 433be099007SPyun YongHyeon *ptr = word; 434a94100faSBill Paul } 435ed510fb0SBill Paul 436ed510fb0SBill Paul CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM); 437a94100faSBill Paul } 438a94100faSBill Paul 439a94100faSBill Paul static int 4407b5ffebfSPyun YongHyeon re_gmii_readreg(device_t dev, int phy, int reg) 441a94100faSBill Paul { 442a94100faSBill Paul struct rl_softc *sc; 443a94100faSBill Paul u_int32_t rval; 444a94100faSBill Paul int i; 445a94100faSBill Paul 446a94100faSBill Paul sc = device_get_softc(dev); 447a94100faSBill Paul 4489bac70b8SBill Paul /* Let the rgephy driver read the GMEDIASTAT register */ 4499bac70b8SBill Paul 4509bac70b8SBill Paul if (reg == RL_GMEDIASTAT) { 4519bac70b8SBill Paul rval = CSR_READ_1(sc, RL_GMEDIASTAT); 4529bac70b8SBill Paul return (rval); 4539bac70b8SBill Paul } 4549bac70b8SBill Paul 455a94100faSBill Paul CSR_WRITE_4(sc, RL_PHYAR, reg << 16); 456a94100faSBill Paul 45796b774f4SPyun YongHyeon for (i = 0; i < RL_PHY_TIMEOUT; i++) { 458a94100faSBill Paul rval = CSR_READ_4(sc, RL_PHYAR); 459a94100faSBill Paul if (rval & RL_PHYAR_BUSY) 460a94100faSBill Paul break; 4612bc085c6SPyun YongHyeon DELAY(25); 462a94100faSBill Paul } 463a94100faSBill Paul 46496b774f4SPyun YongHyeon if (i == RL_PHY_TIMEOUT) { 4656b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "PHY read failed\n"); 466a94100faSBill Paul return (0); 467a94100faSBill Paul } 468a94100faSBill Paul 4692bc085c6SPyun YongHyeon /* 4702bc085c6SPyun YongHyeon * Controller requires a 20us delay to process next MDIO request. 4712bc085c6SPyun YongHyeon */ 4722bc085c6SPyun YongHyeon DELAY(20); 4732bc085c6SPyun YongHyeon 474a94100faSBill Paul return (rval & RL_PHYAR_PHYDATA); 475a94100faSBill Paul } 476a94100faSBill Paul 477a94100faSBill Paul static int 4787b5ffebfSPyun YongHyeon re_gmii_writereg(device_t dev, int phy, int reg, int data) 479a94100faSBill Paul { 480a94100faSBill Paul struct rl_softc *sc; 481a94100faSBill Paul u_int32_t rval; 482a94100faSBill Paul int i; 483a94100faSBill Paul 484a94100faSBill Paul sc = device_get_softc(dev); 485a94100faSBill Paul 486a94100faSBill Paul CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) | 4879bac70b8SBill Paul (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY); 488a94100faSBill Paul 48996b774f4SPyun YongHyeon for (i = 0; i < RL_PHY_TIMEOUT; i++) { 490a94100faSBill Paul rval = CSR_READ_4(sc, RL_PHYAR); 491a94100faSBill Paul if (!(rval & RL_PHYAR_BUSY)) 492a94100faSBill Paul break; 4932bc085c6SPyun YongHyeon DELAY(25); 494a94100faSBill Paul } 495a94100faSBill Paul 49696b774f4SPyun YongHyeon if (i == RL_PHY_TIMEOUT) { 4976b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "PHY write failed\n"); 498a94100faSBill Paul return (0); 499a94100faSBill Paul } 500a94100faSBill Paul 5012bc085c6SPyun YongHyeon /* 5022bc085c6SPyun YongHyeon * Controller requires a 20us delay to process next MDIO request. 5032bc085c6SPyun YongHyeon */ 5042bc085c6SPyun YongHyeon DELAY(20); 5052bc085c6SPyun YongHyeon 506a94100faSBill Paul return (0); 507a94100faSBill Paul } 508a94100faSBill Paul 509a94100faSBill Paul static int 5107b5ffebfSPyun YongHyeon re_miibus_readreg(device_t dev, int phy, int reg) 511a94100faSBill Paul { 512a94100faSBill Paul struct rl_softc *sc; 513a94100faSBill Paul u_int16_t rval = 0; 514a94100faSBill Paul u_int16_t re8139_reg = 0; 515a94100faSBill Paul 516a94100faSBill Paul sc = device_get_softc(dev); 517a94100faSBill Paul 518a94100faSBill Paul if (sc->rl_type == RL_8169) { 519a94100faSBill Paul rval = re_gmii_readreg(dev, phy, reg); 520a94100faSBill Paul return (rval); 521a94100faSBill Paul } 522a94100faSBill Paul 523a94100faSBill Paul switch (reg) { 524a94100faSBill Paul case MII_BMCR: 525a94100faSBill Paul re8139_reg = RL_BMCR; 526a94100faSBill Paul break; 527a94100faSBill Paul case MII_BMSR: 528a94100faSBill Paul re8139_reg = RL_BMSR; 529a94100faSBill Paul break; 530a94100faSBill Paul case MII_ANAR: 531a94100faSBill Paul re8139_reg = RL_ANAR; 532a94100faSBill Paul break; 533a94100faSBill Paul case MII_ANER: 534a94100faSBill Paul re8139_reg = RL_ANER; 535a94100faSBill Paul break; 536a94100faSBill Paul case MII_ANLPAR: 537a94100faSBill Paul re8139_reg = RL_LPAR; 538a94100faSBill Paul break; 539a94100faSBill Paul case MII_PHYIDR1: 540a94100faSBill Paul case MII_PHYIDR2: 541a94100faSBill Paul return (0); 542a94100faSBill Paul /* 543a94100faSBill Paul * Allow the rlphy driver to read the media status 544a94100faSBill Paul * register. If we have a link partner which does not 545a94100faSBill Paul * support NWAY, this is the register which will tell 546a94100faSBill Paul * us the results of parallel detection. 547a94100faSBill Paul */ 548a94100faSBill Paul case RL_MEDIASTAT: 549a94100faSBill Paul rval = CSR_READ_1(sc, RL_MEDIASTAT); 550a94100faSBill Paul return (rval); 551a94100faSBill Paul default: 5526b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "bad phy register\n"); 553a94100faSBill Paul return (0); 554a94100faSBill Paul } 555a94100faSBill Paul rval = CSR_READ_2(sc, re8139_reg); 556baa12772SPyun YongHyeon if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) { 557baa12772SPyun YongHyeon /* 8139C+ has different bit layout. */ 558baa12772SPyun YongHyeon rval &= ~(BMCR_LOOP | BMCR_ISO); 559baa12772SPyun YongHyeon } 560a94100faSBill Paul return (rval); 561a94100faSBill Paul } 562a94100faSBill Paul 563a94100faSBill Paul static int 5647b5ffebfSPyun YongHyeon re_miibus_writereg(device_t dev, int phy, int reg, int data) 565a94100faSBill Paul { 566a94100faSBill Paul struct rl_softc *sc; 567a94100faSBill Paul u_int16_t re8139_reg = 0; 568a94100faSBill Paul int rval = 0; 569a94100faSBill Paul 570a94100faSBill Paul sc = device_get_softc(dev); 571a94100faSBill Paul 572a94100faSBill Paul if (sc->rl_type == RL_8169) { 573a94100faSBill Paul rval = re_gmii_writereg(dev, phy, reg, data); 574a94100faSBill Paul return (rval); 575a94100faSBill Paul } 576a94100faSBill Paul 577a94100faSBill Paul switch (reg) { 578a94100faSBill Paul case MII_BMCR: 579a94100faSBill Paul re8139_reg = RL_BMCR; 580baa12772SPyun YongHyeon if (sc->rl_type == RL_8139CPLUS) { 581baa12772SPyun YongHyeon /* 8139C+ has different bit layout. */ 582baa12772SPyun YongHyeon data &= ~(BMCR_LOOP | BMCR_ISO); 583baa12772SPyun YongHyeon } 584a94100faSBill Paul break; 585a94100faSBill Paul case MII_BMSR: 586a94100faSBill Paul re8139_reg = RL_BMSR; 587a94100faSBill Paul break; 588a94100faSBill Paul case MII_ANAR: 589a94100faSBill Paul re8139_reg = RL_ANAR; 590a94100faSBill Paul break; 591a94100faSBill Paul case MII_ANER: 592a94100faSBill Paul re8139_reg = RL_ANER; 593a94100faSBill Paul break; 594a94100faSBill Paul case MII_ANLPAR: 595a94100faSBill Paul re8139_reg = RL_LPAR; 596a94100faSBill Paul break; 597a94100faSBill Paul case MII_PHYIDR1: 598a94100faSBill Paul case MII_PHYIDR2: 599a94100faSBill Paul return (0); 600a94100faSBill Paul break; 601a94100faSBill Paul default: 6026b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "bad phy register\n"); 603a94100faSBill Paul return (0); 604a94100faSBill Paul } 605a94100faSBill Paul CSR_WRITE_2(sc, re8139_reg, data); 606a94100faSBill Paul return (0); 607a94100faSBill Paul } 608a94100faSBill Paul 609a94100faSBill Paul static void 6107b5ffebfSPyun YongHyeon re_miibus_statchg(device_t dev) 611a94100faSBill Paul { 612130b6dfbSPyun YongHyeon struct rl_softc *sc; 613130b6dfbSPyun YongHyeon struct ifnet *ifp; 614130b6dfbSPyun YongHyeon struct mii_data *mii; 615a11e2f18SBruce M Simpson 616130b6dfbSPyun YongHyeon sc = device_get_softc(dev); 617130b6dfbSPyun YongHyeon mii = device_get_softc(sc->rl_miibus); 618130b6dfbSPyun YongHyeon ifp = sc->rl_ifp; 619130b6dfbSPyun YongHyeon if (mii == NULL || ifp == NULL || 620130b6dfbSPyun YongHyeon (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 621130b6dfbSPyun YongHyeon return; 622130b6dfbSPyun YongHyeon 623130b6dfbSPyun YongHyeon sc->rl_flags &= ~RL_FLAG_LINK; 624130b6dfbSPyun YongHyeon if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) == 625130b6dfbSPyun YongHyeon (IFM_ACTIVE | IFM_AVALID)) { 626130b6dfbSPyun YongHyeon switch (IFM_SUBTYPE(mii->mii_media_active)) { 627130b6dfbSPyun YongHyeon case IFM_10_T: 628130b6dfbSPyun YongHyeon case IFM_100_TX: 629130b6dfbSPyun YongHyeon sc->rl_flags |= RL_FLAG_LINK; 630130b6dfbSPyun YongHyeon break; 631130b6dfbSPyun YongHyeon case IFM_1000_T: 632130b6dfbSPyun YongHyeon if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0) 633130b6dfbSPyun YongHyeon break; 634130b6dfbSPyun YongHyeon sc->rl_flags |= RL_FLAG_LINK; 635130b6dfbSPyun YongHyeon break; 636130b6dfbSPyun YongHyeon default: 637130b6dfbSPyun YongHyeon break; 638130b6dfbSPyun YongHyeon } 639130b6dfbSPyun YongHyeon } 640130b6dfbSPyun YongHyeon /* 64114013280SMarius Strobl * RealTek controllers do not provide any interface to the RX/TX 64214013280SMarius Strobl * MACs for resolved speed, duplex and flow-control parameters. 643130b6dfbSPyun YongHyeon */ 644a94100faSBill Paul } 645a94100faSBill Paul 646a94100faSBill Paul /* 647ff191365SJung-uk Kim * Set the RX configuration and 64-bit multicast hash filter. 648a94100faSBill Paul */ 649a94100faSBill Paul static void 650ff191365SJung-uk Kim re_set_rxmode(struct rl_softc *sc) 651a94100faSBill Paul { 652a94100faSBill Paul struct ifnet *ifp; 653a94100faSBill Paul struct ifmultiaddr *ifma; 654ff191365SJung-uk Kim uint32_t hashes[2] = { 0, 0 }; 655ff191365SJung-uk Kim uint32_t h, rxfilt; 656a94100faSBill Paul 65797b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 65897b9d4baSJohn-Mark Gurney 659fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 660a94100faSBill Paul 661ff191365SJung-uk Kim rxfilt = RL_RXCFG_CONFIG | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_BROAD; 662f1a5f291SMarius Strobl if ((sc->rl_flags & RL_FLAG_EARLYOFF) != 0) 663f1a5f291SMarius Strobl rxfilt |= RL_RXCFG_EARLYOFF; 66414013280SMarius Strobl else if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0) 665f1a5f291SMarius Strobl rxfilt |= RL_RXCFG_EARLYOFFV2; 666a94100faSBill Paul 667ff191365SJung-uk Kim if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) { 6687c103000SPyun YongHyeon if (ifp->if_flags & IFF_PROMISC) 6697c103000SPyun YongHyeon rxfilt |= RL_RXCFG_RX_ALLPHYS; 670a0637caaSPyun YongHyeon /* 671a0637caaSPyun YongHyeon * Unlike other hardwares, we have to explicitly set 672a0637caaSPyun YongHyeon * RL_RXCFG_RX_MULTI to receive multicast frames in 673a0637caaSPyun YongHyeon * promiscuous mode. 674a0637caaSPyun YongHyeon */ 675a94100faSBill Paul rxfilt |= RL_RXCFG_RX_MULTI; 676ff191365SJung-uk Kim hashes[0] = hashes[1] = 0xffffffff; 677ff191365SJung-uk Kim goto done; 678a94100faSBill Paul } 679a94100faSBill Paul 680eb956cd0SRobert Watson if_maddr_rlock(ifp); 681a94100faSBill Paul TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 682a94100faSBill Paul if (ifma->ifma_addr->sa_family != AF_LINK) 683a94100faSBill Paul continue; 6840e939c0cSChristian Weisgerber h = ether_crc32_be(LLADDR((struct sockaddr_dl *) 6850e939c0cSChristian Weisgerber ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 686a94100faSBill Paul if (h < 32) 687a94100faSBill Paul hashes[0] |= (1 << h); 688a94100faSBill Paul else 689a94100faSBill Paul hashes[1] |= (1 << (h - 32)); 690a94100faSBill Paul } 691eb956cd0SRobert Watson if_maddr_runlock(ifp); 692a94100faSBill Paul 693ff191365SJung-uk Kim if (hashes[0] != 0 || hashes[1] != 0) { 694bb7dfefbSBill Paul /* 695ff191365SJung-uk Kim * For some unfathomable reason, RealTek decided to 696ff191365SJung-uk Kim * reverse the order of the multicast hash registers 697ff191365SJung-uk Kim * in the PCI Express parts. This means we have to 698ff191365SJung-uk Kim * write the hash pattern in reverse order for those 699ff191365SJung-uk Kim * devices. 700bb7dfefbSBill Paul */ 701aaab4fbeSJung-uk Kim if ((sc->rl_flags & RL_FLAG_PCIE) != 0) { 702ff191365SJung-uk Kim h = bswap32(hashes[0]); 703ff191365SJung-uk Kim hashes[0] = bswap32(hashes[1]); 704ff191365SJung-uk Kim hashes[1] = h; 705ff191365SJung-uk Kim } 706ff191365SJung-uk Kim rxfilt |= RL_RXCFG_RX_MULTI; 707ff191365SJung-uk Kim } 708ff191365SJung-uk Kim 709b8333e45SPyun YongHyeon if (sc->rl_hwrev->rl_rev == RL_HWREV_8168F) { 710b8333e45SPyun YongHyeon /* Disable multicast filtering due to silicon bug. */ 711b8333e45SPyun YongHyeon hashes[0] = 0xffffffff; 712b8333e45SPyun YongHyeon hashes[1] = 0xffffffff; 713b8333e45SPyun YongHyeon } 714b8333e45SPyun YongHyeon 715ff191365SJung-uk Kim done: 716a94100faSBill Paul CSR_WRITE_4(sc, RL_MAR0, hashes[0]); 717a94100faSBill Paul CSR_WRITE_4(sc, RL_MAR4, hashes[1]); 718ff191365SJung-uk Kim CSR_WRITE_4(sc, RL_RXCFG, rxfilt); 719bb7dfefbSBill Paul } 720a94100faSBill Paul 721a94100faSBill Paul static void 7227b5ffebfSPyun YongHyeon re_reset(struct rl_softc *sc) 723a94100faSBill Paul { 7240ce0868aSPyun YongHyeon int i; 725a94100faSBill Paul 72697b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 72797b9d4baSJohn-Mark Gurney 728a94100faSBill Paul CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET); 729a94100faSBill Paul 730a94100faSBill Paul for (i = 0; i < RL_TIMEOUT; i++) { 731a94100faSBill Paul DELAY(10); 732a94100faSBill Paul if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET)) 733a94100faSBill Paul break; 734a94100faSBill Paul } 735a94100faSBill Paul if (i == RL_TIMEOUT) 7366b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "reset never completed!\n"); 737a94100faSBill Paul 738566ca8caSJung-uk Kim if ((sc->rl_flags & RL_FLAG_MACRESET) != 0) 739a94100faSBill Paul CSR_WRITE_1(sc, 0x82, 1); 74081eee0ebSPyun YongHyeon if (sc->rl_hwrev->rl_rev == RL_HWREV_8169S) 741566ca8caSJung-uk Kim re_gmii_writereg(sc->rl_dev, 1, 0x0b, 0); 742a94100faSBill Paul } 743a94100faSBill Paul 744ed510fb0SBill Paul #ifdef RE_DIAG 745ed510fb0SBill Paul 746a94100faSBill Paul /* 747a94100faSBill Paul * The following routine is designed to test for a defect on some 748a94100faSBill Paul * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64# 749a94100faSBill Paul * lines connected to the bus, however for a 32-bit only card, they 750a94100faSBill Paul * should be pulled high. The result of this defect is that the 751a94100faSBill Paul * NIC will not work right if you plug it into a 64-bit slot: DMA 752a94100faSBill Paul * operations will be done with 64-bit transfers, which will fail 753a94100faSBill Paul * because the 64-bit data lines aren't connected. 754a94100faSBill Paul * 755a94100faSBill Paul * There's no way to work around this (short of talking a soldering 756a94100faSBill Paul * iron to the board), however we can detect it. The method we use 757a94100faSBill Paul * here is to put the NIC into digital loopback mode, set the receiver 758a94100faSBill Paul * to promiscuous mode, and then try to send a frame. We then compare 759a94100faSBill Paul * the frame data we sent to what was received. If the data matches, 760a94100faSBill Paul * then the NIC is working correctly, otherwise we know the user has 761a94100faSBill Paul * a defective NIC which has been mistakenly plugged into a 64-bit PCI 762a94100faSBill Paul * slot. In the latter case, there's no way the NIC can work correctly, 763a94100faSBill Paul * so we print out a message on the console and abort the device attach. 764a94100faSBill Paul */ 765a94100faSBill Paul 766a94100faSBill Paul static int 7677b5ffebfSPyun YongHyeon re_diag(struct rl_softc *sc) 768a94100faSBill Paul { 769fc74a9f9SBrooks Davis struct ifnet *ifp = sc->rl_ifp; 770a94100faSBill Paul struct mbuf *m0; 771a94100faSBill Paul struct ether_header *eh; 772a94100faSBill Paul struct rl_desc *cur_rx; 773a94100faSBill Paul u_int16_t status; 774a94100faSBill Paul u_int32_t rxstat; 775ed510fb0SBill Paul int total_len, i, error = 0, phyaddr; 776a94100faSBill Paul u_int8_t dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' }; 777a94100faSBill Paul u_int8_t src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' }; 778a94100faSBill Paul 779a94100faSBill Paul /* Allocate a single mbuf */ 780c6499eccSGleb Smirnoff MGETHDR(m0, M_NOWAIT, MT_DATA); 781a94100faSBill Paul if (m0 == NULL) 782a94100faSBill Paul return (ENOBUFS); 783a94100faSBill Paul 78497b9d4baSJohn-Mark Gurney RL_LOCK(sc); 78597b9d4baSJohn-Mark Gurney 786a94100faSBill Paul /* 787a94100faSBill Paul * Initialize the NIC in test mode. This sets the chip up 788a94100faSBill Paul * so that it can send and receive frames, but performs the 789a94100faSBill Paul * following special functions: 790a94100faSBill Paul * - Puts receiver in promiscuous mode 791a94100faSBill Paul * - Enables digital loopback mode 792a94100faSBill Paul * - Leaves interrupts turned off 793a94100faSBill Paul */ 794a94100faSBill Paul 795a94100faSBill Paul ifp->if_flags |= IFF_PROMISC; 796a94100faSBill Paul sc->rl_testmode = 1; 7978476c243SPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 79897b9d4baSJohn-Mark Gurney re_init_locked(sc); 799351a76f9SPyun YongHyeon sc->rl_flags |= RL_FLAG_LINK; 800ed510fb0SBill Paul if (sc->rl_type == RL_8169) 801ed510fb0SBill Paul phyaddr = 1; 802ed510fb0SBill Paul else 803ed510fb0SBill Paul phyaddr = 0; 804ed510fb0SBill Paul 805ed510fb0SBill Paul re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET); 806ed510fb0SBill Paul for (i = 0; i < RL_TIMEOUT; i++) { 807ed510fb0SBill Paul status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR); 808ed510fb0SBill Paul if (!(status & BMCR_RESET)) 809ed510fb0SBill Paul break; 810ed510fb0SBill Paul } 811ed510fb0SBill Paul 812ed510fb0SBill Paul re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP); 813ed510fb0SBill Paul CSR_WRITE_2(sc, RL_ISR, RL_INTRS); 814ed510fb0SBill Paul 815804af9a1SBill Paul DELAY(100000); 816a94100faSBill Paul 817a94100faSBill Paul /* Put some data in the mbuf */ 818a94100faSBill Paul 819a94100faSBill Paul eh = mtod(m0, struct ether_header *); 820a94100faSBill Paul bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN); 821a94100faSBill Paul bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN); 822a94100faSBill Paul eh->ether_type = htons(ETHERTYPE_IP); 823a94100faSBill Paul m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN; 824a94100faSBill Paul 8257cae6651SBill Paul /* 8267cae6651SBill Paul * Queue the packet, start transmission. 8277cae6651SBill Paul * Note: IF_HANDOFF() ultimately calls re_start() for us. 8287cae6651SBill Paul */ 829a94100faSBill Paul 830abc8ff44SBill Paul CSR_WRITE_2(sc, RL_ISR, 0xFFFF); 83197b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 83252732175SMax Laier /* XXX: re_diag must not be called when in ALTQ mode */ 8337cae6651SBill Paul IF_HANDOFF(&ifp->if_snd, m0, ifp); 83497b9d4baSJohn-Mark Gurney RL_LOCK(sc); 835a94100faSBill Paul m0 = NULL; 836a94100faSBill Paul 837a94100faSBill Paul /* Wait for it to propagate through the chip */ 838a94100faSBill Paul 839abc8ff44SBill Paul DELAY(100000); 840a94100faSBill Paul for (i = 0; i < RL_TIMEOUT; i++) { 841a94100faSBill Paul status = CSR_READ_2(sc, RL_ISR); 842ed510fb0SBill Paul CSR_WRITE_2(sc, RL_ISR, status); 843abc8ff44SBill Paul if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) == 844abc8ff44SBill Paul (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) 845a94100faSBill Paul break; 846a94100faSBill Paul DELAY(10); 847a94100faSBill Paul } 848a94100faSBill Paul 849a94100faSBill Paul if (i == RL_TIMEOUT) { 8506b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, 8516b9f5c94SGleb Smirnoff "diagnostic failed, failed to receive packet in" 8526b9f5c94SGleb Smirnoff " loopback mode\n"); 853a94100faSBill Paul error = EIO; 854a94100faSBill Paul goto done; 855a94100faSBill Paul } 856a94100faSBill Paul 857a94100faSBill Paul /* 858a94100faSBill Paul * The packet should have been dumped into the first 859a94100faSBill Paul * entry in the RX DMA ring. Grab it from there. 860a94100faSBill Paul */ 861a94100faSBill Paul 862a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 863a94100faSBill Paul sc->rl_ldata.rl_rx_list_map, 864a94100faSBill Paul BUS_DMASYNC_POSTREAD); 865d65abd66SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, 866d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc[0].rx_dmamap, 867d65abd66SPyun YongHyeon BUS_DMASYNC_POSTREAD); 868d65abd66SPyun YongHyeon bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, 869d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc[0].rx_dmamap); 870a94100faSBill Paul 871d65abd66SPyun YongHyeon m0 = sc->rl_ldata.rl_rx_desc[0].rx_m; 872d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc[0].rx_m = NULL; 873a94100faSBill Paul eh = mtod(m0, struct ether_header *); 874a94100faSBill Paul 875a94100faSBill Paul cur_rx = &sc->rl_ldata.rl_rx_list[0]; 876a94100faSBill Paul total_len = RL_RXBYTES(cur_rx); 877a94100faSBill Paul rxstat = le32toh(cur_rx->rl_cmdstat); 878a94100faSBill Paul 879a94100faSBill Paul if (total_len != ETHER_MIN_LEN) { 8806b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, 8816b9f5c94SGleb Smirnoff "diagnostic failed, received short packet\n"); 882a94100faSBill Paul error = EIO; 883a94100faSBill Paul goto done; 884a94100faSBill Paul } 885a94100faSBill Paul 886a94100faSBill Paul /* Test that the received packet data matches what we sent. */ 887a94100faSBill Paul 888a94100faSBill Paul if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) || 889a94100faSBill Paul bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) || 890a94100faSBill Paul ntohs(eh->ether_type) != ETHERTYPE_IP) { 8916b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n"); 8926b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n", 893a94100faSBill Paul dst, ":", src, ":", ETHERTYPE_IP); 8946b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n", 895a94100faSBill Paul eh->ether_dhost, ":", eh->ether_shost, ":", 896a94100faSBill Paul ntohs(eh->ether_type)); 8976b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "You may have a defective 32-bit " 8986b9f5c94SGleb Smirnoff "NIC plugged into a 64-bit PCI slot.\n"); 8996b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "Please re-install the NIC in a " 9006b9f5c94SGleb Smirnoff "32-bit slot for proper operation.\n"); 9016b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "Read the re(4) man page for more " 9026b9f5c94SGleb Smirnoff "details.\n"); 903a94100faSBill Paul error = EIO; 904a94100faSBill Paul } 905a94100faSBill Paul 906a94100faSBill Paul done: 907a94100faSBill Paul /* Turn interface off, release resources */ 908a94100faSBill Paul 909a94100faSBill Paul sc->rl_testmode = 0; 910351a76f9SPyun YongHyeon sc->rl_flags &= ~RL_FLAG_LINK; 911a94100faSBill Paul ifp->if_flags &= ~IFF_PROMISC; 912a94100faSBill Paul re_stop(sc); 913a94100faSBill Paul if (m0 != NULL) 914a94100faSBill Paul m_freem(m0); 915a94100faSBill Paul 91697b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 91797b9d4baSJohn-Mark Gurney 918a94100faSBill Paul return (error); 919a94100faSBill Paul } 920a94100faSBill Paul 921ed510fb0SBill Paul #endif 922ed510fb0SBill Paul 923a94100faSBill Paul /* 924a94100faSBill Paul * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device 925a94100faSBill Paul * IDs against our list and return a device name if we find a match. 926a94100faSBill Paul */ 927a94100faSBill Paul static int 9287b5ffebfSPyun YongHyeon re_probe(device_t dev) 929a94100faSBill Paul { 930b3030306SMarius Strobl const struct rl_type *t; 931dfdb409eSPyun YongHyeon uint16_t devid, vendor; 932dfdb409eSPyun YongHyeon uint16_t revid, sdevid; 933dfdb409eSPyun YongHyeon int i; 934a94100faSBill Paul 935dfdb409eSPyun YongHyeon vendor = pci_get_vendor(dev); 936dfdb409eSPyun YongHyeon devid = pci_get_device(dev); 937dfdb409eSPyun YongHyeon revid = pci_get_revid(dev); 938dfdb409eSPyun YongHyeon sdevid = pci_get_subdevice(dev); 939a94100faSBill Paul 940dfdb409eSPyun YongHyeon if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) { 941dfdb409eSPyun YongHyeon if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) { 94226390635SJohn Baldwin /* 94326390635SJohn Baldwin * Only attach to rev. 3 of the Linksys EG1032 adapter. 944dfdb409eSPyun YongHyeon * Rev. 2 is supported by sk(4). 94526390635SJohn Baldwin */ 946a94100faSBill Paul return (ENXIO); 947a94100faSBill Paul } 948dfdb409eSPyun YongHyeon } 949dfdb409eSPyun YongHyeon 950dfdb409eSPyun YongHyeon if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) { 951dfdb409eSPyun YongHyeon if (revid != 0x20) { 952dfdb409eSPyun YongHyeon /* 8139, let rl(4) take care of this device. */ 953dfdb409eSPyun YongHyeon return (ENXIO); 954dfdb409eSPyun YongHyeon } 955dfdb409eSPyun YongHyeon } 956dfdb409eSPyun YongHyeon 957dfdb409eSPyun YongHyeon t = re_devs; 95873a1170aSPedro F. Giffuni for (i = 0; i < nitems(re_devs); i++, t++) { 959dfdb409eSPyun YongHyeon if (vendor == t->rl_vid && devid == t->rl_did) { 960a94100faSBill Paul device_set_desc(dev, t->rl_name); 961d2b677bbSWarner Losh return (BUS_PROBE_DEFAULT); 962a94100faSBill Paul } 963a94100faSBill Paul } 964a94100faSBill Paul 965a94100faSBill Paul return (ENXIO); 966a94100faSBill Paul } 967a94100faSBill Paul 968a94100faSBill Paul /* 969a94100faSBill Paul * Map a single buffer address. 970a94100faSBill Paul */ 971a94100faSBill Paul 972a94100faSBill Paul static void 9737b5ffebfSPyun YongHyeon re_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error) 974a94100faSBill Paul { 9758fd99e38SPyun YongHyeon bus_addr_t *addr; 976a94100faSBill Paul 977a94100faSBill Paul if (error) 978a94100faSBill Paul return; 979a94100faSBill Paul 980a94100faSBill Paul KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 981a94100faSBill Paul addr = arg; 982a94100faSBill Paul *addr = segs->ds_addr; 983a94100faSBill Paul } 984a94100faSBill Paul 985a94100faSBill Paul static int 9867b5ffebfSPyun YongHyeon re_allocmem(device_t dev, struct rl_softc *sc) 987a94100faSBill Paul { 98866366ca4SPyun YongHyeon bus_addr_t lowaddr; 989d65abd66SPyun YongHyeon bus_size_t rx_list_size, tx_list_size; 990a94100faSBill Paul int error; 991a94100faSBill Paul int i; 992a94100faSBill Paul 993d65abd66SPyun YongHyeon rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc); 994d65abd66SPyun YongHyeon tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc); 995d65abd66SPyun YongHyeon 996d65abd66SPyun YongHyeon /* 997d65abd66SPyun YongHyeon * Allocate the parent bus DMA tag appropriate for PCI. 998ce628393SPyun YongHyeon * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD 999ce628393SPyun YongHyeon * register should be set. However some RealTek chips are known 1000ce628393SPyun YongHyeon * to be buggy on DAC handling, therefore disable DAC by limiting 1001ce628393SPyun YongHyeon * DMA address space to 32bit. PCIe variants of RealTek chips 100266366ca4SPyun YongHyeon * may not have the limitation. 1003d65abd66SPyun YongHyeon */ 100466366ca4SPyun YongHyeon lowaddr = BUS_SPACE_MAXADDR; 100566366ca4SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_PCIE) == 0) 100666366ca4SPyun YongHyeon lowaddr = BUS_SPACE_MAXADDR_32BIT; 1007d65abd66SPyun YongHyeon error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0, 100866366ca4SPyun YongHyeon lowaddr, BUS_SPACE_MAXADDR, NULL, NULL, 1009d65abd66SPyun YongHyeon BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0, 1010d65abd66SPyun YongHyeon NULL, NULL, &sc->rl_parent_tag); 1011d65abd66SPyun YongHyeon if (error) { 1012d65abd66SPyun YongHyeon device_printf(dev, "could not allocate parent DMA tag\n"); 1013d65abd66SPyun YongHyeon return (error); 1014d65abd66SPyun YongHyeon } 1015d65abd66SPyun YongHyeon 1016d65abd66SPyun YongHyeon /* 1017d65abd66SPyun YongHyeon * Allocate map for TX mbufs. 1018d65abd66SPyun YongHyeon */ 1019d65abd66SPyun YongHyeon error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0, 1020d65abd66SPyun YongHyeon BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, 1021d65abd66SPyun YongHyeon NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0, 1022d65abd66SPyun YongHyeon NULL, NULL, &sc->rl_ldata.rl_tx_mtag); 1023d65abd66SPyun YongHyeon if (error) { 1024d65abd66SPyun YongHyeon device_printf(dev, "could not allocate TX DMA tag\n"); 1025d65abd66SPyun YongHyeon return (error); 1026d65abd66SPyun YongHyeon } 1027d65abd66SPyun YongHyeon 1028a94100faSBill Paul /* 1029a94100faSBill Paul * Allocate map for RX mbufs. 1030a94100faSBill Paul */ 1031d65abd66SPyun YongHyeon 103281eee0ebSPyun YongHyeon if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) { 103381eee0ebSPyun YongHyeon error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 103481eee0ebSPyun YongHyeon 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 103581eee0ebSPyun YongHyeon MJUM9BYTES, 1, MJUM9BYTES, 0, NULL, NULL, 103681eee0ebSPyun YongHyeon &sc->rl_ldata.rl_jrx_mtag); 103781eee0ebSPyun YongHyeon if (error) { 103881eee0ebSPyun YongHyeon device_printf(dev, 103981eee0ebSPyun YongHyeon "could not allocate jumbo RX DMA tag\n"); 104081eee0ebSPyun YongHyeon return (error); 104181eee0ebSPyun YongHyeon } 104281eee0ebSPyun YongHyeon } 1043d65abd66SPyun YongHyeon error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0, 1044d65abd66SPyun YongHyeon BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 1045d65abd66SPyun YongHyeon MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag); 1046a94100faSBill Paul if (error) { 1047d65abd66SPyun YongHyeon device_printf(dev, "could not allocate RX DMA tag\n"); 1048d65abd66SPyun YongHyeon return (error); 1049a94100faSBill Paul } 1050a94100faSBill Paul 1051a94100faSBill Paul /* 1052a94100faSBill Paul * Allocate map for TX descriptor list. 1053a94100faSBill Paul */ 1054a94100faSBill Paul error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN, 1055a94100faSBill Paul 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 1056d65abd66SPyun YongHyeon NULL, tx_list_size, 1, tx_list_size, 0, 1057a94100faSBill Paul NULL, NULL, &sc->rl_ldata.rl_tx_list_tag); 1058a94100faSBill Paul if (error) { 1059d65abd66SPyun YongHyeon device_printf(dev, "could not allocate TX DMA ring tag\n"); 1060d65abd66SPyun YongHyeon return (error); 1061a94100faSBill Paul } 1062a94100faSBill Paul 1063a94100faSBill Paul /* Allocate DMA'able memory for the TX ring */ 1064a94100faSBill Paul 1065a94100faSBill Paul error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag, 1066d65abd66SPyun YongHyeon (void **)&sc->rl_ldata.rl_tx_list, 1067d65abd66SPyun YongHyeon BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, 1068a94100faSBill Paul &sc->rl_ldata.rl_tx_list_map); 1069d65abd66SPyun YongHyeon if (error) { 1070d65abd66SPyun YongHyeon device_printf(dev, "could not allocate TX DMA ring\n"); 1071d65abd66SPyun YongHyeon return (error); 1072d65abd66SPyun YongHyeon } 1073a94100faSBill Paul 1074a94100faSBill Paul /* Load the map for the TX ring. */ 1075a94100faSBill Paul 1076d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_list_addr = 0; 1077a94100faSBill Paul error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag, 1078a94100faSBill Paul sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list, 1079d65abd66SPyun YongHyeon tx_list_size, re_dma_map_addr, 1080a94100faSBill Paul &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT); 1081d65abd66SPyun YongHyeon if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) { 1082d65abd66SPyun YongHyeon device_printf(dev, "could not load TX DMA ring\n"); 1083d65abd66SPyun YongHyeon return (ENOMEM); 1084d65abd66SPyun YongHyeon } 1085a94100faSBill Paul 1086a94100faSBill Paul /* Create DMA maps for TX buffers */ 1087a94100faSBill Paul 1088d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) { 1089d65abd66SPyun YongHyeon error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0, 1090d65abd66SPyun YongHyeon &sc->rl_ldata.rl_tx_desc[i].tx_dmamap); 1091a94100faSBill Paul if (error) { 1092d65abd66SPyun YongHyeon device_printf(dev, "could not create DMA map for TX\n"); 1093d65abd66SPyun YongHyeon return (error); 1094a94100faSBill Paul } 1095a94100faSBill Paul } 1096a94100faSBill Paul 1097a94100faSBill Paul /* 1098a94100faSBill Paul * Allocate map for RX descriptor list. 1099a94100faSBill Paul */ 1100a94100faSBill Paul error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN, 1101a94100faSBill Paul 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 1102d65abd66SPyun YongHyeon NULL, rx_list_size, 1, rx_list_size, 0, 1103a94100faSBill Paul NULL, NULL, &sc->rl_ldata.rl_rx_list_tag); 1104a94100faSBill Paul if (error) { 1105d65abd66SPyun YongHyeon device_printf(dev, "could not create RX DMA ring tag\n"); 1106d65abd66SPyun YongHyeon return (error); 1107a94100faSBill Paul } 1108a94100faSBill Paul 1109a94100faSBill Paul /* Allocate DMA'able memory for the RX ring */ 1110a94100faSBill Paul 1111a94100faSBill Paul error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag, 1112d65abd66SPyun YongHyeon (void **)&sc->rl_ldata.rl_rx_list, 1113d65abd66SPyun YongHyeon BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, 1114a94100faSBill Paul &sc->rl_ldata.rl_rx_list_map); 1115d65abd66SPyun YongHyeon if (error) { 1116d65abd66SPyun YongHyeon device_printf(dev, "could not allocate RX DMA ring\n"); 1117d65abd66SPyun YongHyeon return (error); 1118d65abd66SPyun YongHyeon } 1119a94100faSBill Paul 1120a94100faSBill Paul /* Load the map for the RX ring. */ 1121a94100faSBill Paul 1122d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_list_addr = 0; 1123a94100faSBill Paul error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag, 1124a94100faSBill Paul sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list, 1125d65abd66SPyun YongHyeon rx_list_size, re_dma_map_addr, 1126a94100faSBill Paul &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT); 1127d65abd66SPyun YongHyeon if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) { 1128d65abd66SPyun YongHyeon device_printf(dev, "could not load RX DMA ring\n"); 1129d65abd66SPyun YongHyeon return (ENOMEM); 1130d65abd66SPyun YongHyeon } 1131a94100faSBill Paul 1132a94100faSBill Paul /* Create DMA maps for RX buffers */ 1133a94100faSBill Paul 113481eee0ebSPyun YongHyeon if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) { 113581eee0ebSPyun YongHyeon error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0, 113681eee0ebSPyun YongHyeon &sc->rl_ldata.rl_jrx_sparemap); 113781eee0ebSPyun YongHyeon if (error) { 113881eee0ebSPyun YongHyeon device_printf(dev, 113981eee0ebSPyun YongHyeon "could not create spare DMA map for jumbo RX\n"); 114081eee0ebSPyun YongHyeon return (error); 114181eee0ebSPyun YongHyeon } 114281eee0ebSPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 114381eee0ebSPyun YongHyeon error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0, 114481eee0ebSPyun YongHyeon &sc->rl_ldata.rl_jrx_desc[i].rx_dmamap); 114581eee0ebSPyun YongHyeon if (error) { 114681eee0ebSPyun YongHyeon device_printf(dev, 114781eee0ebSPyun YongHyeon "could not create DMA map for jumbo RX\n"); 114881eee0ebSPyun YongHyeon return (error); 114981eee0ebSPyun YongHyeon } 115081eee0ebSPyun YongHyeon } 115181eee0ebSPyun YongHyeon } 1152d65abd66SPyun YongHyeon error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0, 1153d65abd66SPyun YongHyeon &sc->rl_ldata.rl_rx_sparemap); 1154a94100faSBill Paul if (error) { 1155d65abd66SPyun YongHyeon device_printf(dev, "could not create spare DMA map for RX\n"); 1156d65abd66SPyun YongHyeon return (error); 1157d65abd66SPyun YongHyeon } 1158d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 1159d65abd66SPyun YongHyeon error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0, 1160d65abd66SPyun YongHyeon &sc->rl_ldata.rl_rx_desc[i].rx_dmamap); 1161d65abd66SPyun YongHyeon if (error) { 1162d65abd66SPyun YongHyeon device_printf(dev, "could not create DMA map for RX\n"); 1163d65abd66SPyun YongHyeon return (error); 1164a94100faSBill Paul } 1165a94100faSBill Paul } 1166a94100faSBill Paul 11670534aae0SPyun YongHyeon /* Create DMA map for statistics. */ 11680534aae0SPyun YongHyeon error = bus_dma_tag_create(sc->rl_parent_tag, RL_DUMP_ALIGN, 0, 11690534aae0SPyun YongHyeon BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 11700534aae0SPyun YongHyeon sizeof(struct rl_stats), 1, sizeof(struct rl_stats), 0, NULL, NULL, 11710534aae0SPyun YongHyeon &sc->rl_ldata.rl_stag); 11720534aae0SPyun YongHyeon if (error) { 11730534aae0SPyun YongHyeon device_printf(dev, "could not create statistics DMA tag\n"); 11740534aae0SPyun YongHyeon return (error); 11750534aae0SPyun YongHyeon } 11760534aae0SPyun YongHyeon /* Allocate DMA'able memory for statistics. */ 11770534aae0SPyun YongHyeon error = bus_dmamem_alloc(sc->rl_ldata.rl_stag, 11780534aae0SPyun YongHyeon (void **)&sc->rl_ldata.rl_stats, 11790534aae0SPyun YongHyeon BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, 11800534aae0SPyun YongHyeon &sc->rl_ldata.rl_smap); 11810534aae0SPyun YongHyeon if (error) { 11820534aae0SPyun YongHyeon device_printf(dev, 11830534aae0SPyun YongHyeon "could not allocate statistics DMA memory\n"); 11840534aae0SPyun YongHyeon return (error); 11850534aae0SPyun YongHyeon } 11860534aae0SPyun YongHyeon /* Load the map for statistics. */ 11870534aae0SPyun YongHyeon sc->rl_ldata.rl_stats_addr = 0; 11880534aae0SPyun YongHyeon error = bus_dmamap_load(sc->rl_ldata.rl_stag, sc->rl_ldata.rl_smap, 11890534aae0SPyun YongHyeon sc->rl_ldata.rl_stats, sizeof(struct rl_stats), re_dma_map_addr, 11900534aae0SPyun YongHyeon &sc->rl_ldata.rl_stats_addr, BUS_DMA_NOWAIT); 11910534aae0SPyun YongHyeon if (error != 0 || sc->rl_ldata.rl_stats_addr == 0) { 11920534aae0SPyun YongHyeon device_printf(dev, "could not load statistics DMA memory\n"); 11930534aae0SPyun YongHyeon return (ENOMEM); 11940534aae0SPyun YongHyeon } 11950534aae0SPyun YongHyeon 1196a94100faSBill Paul return (0); 1197a94100faSBill Paul } 1198a94100faSBill Paul 1199a94100faSBill Paul /* 1200a94100faSBill Paul * Attach the interface. Allocate softc structures, do ifmedia 1201a94100faSBill Paul * setup and ethernet/BPF attach. 1202a94100faSBill Paul */ 1203a94100faSBill Paul static int 12047b5ffebfSPyun YongHyeon re_attach(device_t dev) 1205a94100faSBill Paul { 1206a94100faSBill Paul u_char eaddr[ETHER_ADDR_LEN]; 1207be099007SPyun YongHyeon u_int16_t as[ETHER_ADDR_LEN / 2]; 1208a94100faSBill Paul struct rl_softc *sc; 1209a94100faSBill Paul struct ifnet *ifp; 1210b3030306SMarius Strobl const struct rl_hwrev *hw_rev; 121114013280SMarius Strobl int capmask, error = 0, hwrev, i, msic, msixc, 121214013280SMarius Strobl phy, reg, rid; 1213017f1c8dSPyun YongHyeon u_int32_t cap, ctl; 1214ace7ed5dSPyun YongHyeon u_int16_t devid, re_did = 0; 121503ca7ae8SPyun YongHyeon uint8_t cfg; 1216a94100faSBill Paul 1217a94100faSBill Paul sc = device_get_softc(dev); 1218ed510fb0SBill Paul sc->rl_dev = dev; 1219a94100faSBill Paul 1220a94100faSBill Paul mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 122197b9d4baSJohn-Mark Gurney MTX_DEF); 1222d1754a9bSJohn Baldwin callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0); 1223d1754a9bSJohn Baldwin 1224a94100faSBill Paul /* 1225a94100faSBill Paul * Map control/status registers. 1226a94100faSBill Paul */ 1227a94100faSBill Paul pci_enable_busmaster(dev); 1228a94100faSBill Paul 1229ace7ed5dSPyun YongHyeon devid = pci_get_device(dev); 12302c21710bSPyun YongHyeon /* 12312c21710bSPyun YongHyeon * Prefer memory space register mapping over IO space. 12322c21710bSPyun YongHyeon * Because RTL8169SC does not seem to work when memory mapping 12332c21710bSPyun YongHyeon * is used always activate io mapping. 12342c21710bSPyun YongHyeon */ 12352c21710bSPyun YongHyeon if (devid == RT_DEVICEID_8169SC) 12362c21710bSPyun YongHyeon prefer_iomap = 1; 12372c21710bSPyun YongHyeon if (prefer_iomap == 0) { 1238ace7ed5dSPyun YongHyeon sc->rl_res_id = PCIR_BAR(1); 1239ace7ed5dSPyun YongHyeon sc->rl_res_type = SYS_RES_MEMORY; 1240ace7ed5dSPyun YongHyeon /* RTL8168/8101E seems to use different BARs. */ 1241ace7ed5dSPyun YongHyeon if (devid == RT_DEVICEID_8168 || devid == RT_DEVICEID_8101E) 1242ace7ed5dSPyun YongHyeon sc->rl_res_id = PCIR_BAR(2); 12432c21710bSPyun YongHyeon } else { 12442c21710bSPyun YongHyeon sc->rl_res_id = PCIR_BAR(0); 12452c21710bSPyun YongHyeon sc->rl_res_type = SYS_RES_IOPORT; 12462c21710bSPyun YongHyeon } 1247ace7ed5dSPyun YongHyeon sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type, 1248ace7ed5dSPyun YongHyeon &sc->rl_res_id, RF_ACTIVE); 12492c21710bSPyun YongHyeon if (sc->rl_res == NULL && prefer_iomap == 0) { 1250ace7ed5dSPyun YongHyeon sc->rl_res_id = PCIR_BAR(0); 1251ace7ed5dSPyun YongHyeon sc->rl_res_type = SYS_RES_IOPORT; 1252ace7ed5dSPyun YongHyeon sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type, 1253ace7ed5dSPyun YongHyeon &sc->rl_res_id, RF_ACTIVE); 12542c21710bSPyun YongHyeon } 1255ace7ed5dSPyun YongHyeon if (sc->rl_res == NULL) { 1256d1754a9bSJohn Baldwin device_printf(dev, "couldn't map ports/memory\n"); 1257a94100faSBill Paul error = ENXIO; 1258a94100faSBill Paul goto fail; 1259a94100faSBill Paul } 1260a94100faSBill Paul 1261a94100faSBill Paul sc->rl_btag = rman_get_bustag(sc->rl_res); 1262a94100faSBill Paul sc->rl_bhandle = rman_get_bushandle(sc->rl_res); 1263a94100faSBill Paul 12645774c5ffSPyun YongHyeon msic = pci_msi_count(dev); 12654a58fd45SPyun YongHyeon msixc = pci_msix_count(dev); 1266017f1c8dSPyun YongHyeon if (pci_find_cap(dev, PCIY_EXPRESS, ®) == 0) { 12674a58fd45SPyun YongHyeon sc->rl_flags |= RL_FLAG_PCIE; 1268017f1c8dSPyun YongHyeon sc->rl_expcap = reg; 1269017f1c8dSPyun YongHyeon } 12704a58fd45SPyun YongHyeon if (bootverbose) { 12715774c5ffSPyun YongHyeon device_printf(dev, "MSI count : %d\n", msic); 12724a58fd45SPyun YongHyeon device_printf(dev, "MSI-X count : %d\n", msixc); 12735774c5ffSPyun YongHyeon } 12744a58fd45SPyun YongHyeon if (msix_disable > 0) 12754a58fd45SPyun YongHyeon msixc = 0; 12764a58fd45SPyun YongHyeon if (msi_disable > 0) 12774a58fd45SPyun YongHyeon msic = 0; 12784a58fd45SPyun YongHyeon /* Prefer MSI-X to MSI. */ 12794a58fd45SPyun YongHyeon if (msixc > 0) { 1280f1a5f291SMarius Strobl msixc = RL_MSI_MESSAGES; 12814a58fd45SPyun YongHyeon rid = PCIR_BAR(4); 12824a58fd45SPyun YongHyeon sc->rl_res_pba = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 12834a58fd45SPyun YongHyeon &rid, RF_ACTIVE); 12844a58fd45SPyun YongHyeon if (sc->rl_res_pba == NULL) { 12854a58fd45SPyun YongHyeon device_printf(sc->rl_dev, 12864a58fd45SPyun YongHyeon "could not allocate MSI-X PBA resource\n"); 12874a58fd45SPyun YongHyeon } 12884a58fd45SPyun YongHyeon if (sc->rl_res_pba != NULL && 12894a58fd45SPyun YongHyeon pci_alloc_msix(dev, &msixc) == 0) { 1290f1a5f291SMarius Strobl if (msixc == RL_MSI_MESSAGES) { 12914a58fd45SPyun YongHyeon device_printf(dev, "Using %d MSI-X message\n", 12924a58fd45SPyun YongHyeon msixc); 12934a58fd45SPyun YongHyeon sc->rl_flags |= RL_FLAG_MSIX; 12944a58fd45SPyun YongHyeon } else 12954a58fd45SPyun YongHyeon pci_release_msi(dev); 12964a58fd45SPyun YongHyeon } 12974a58fd45SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_MSIX) == 0) { 12984a58fd45SPyun YongHyeon if (sc->rl_res_pba != NULL) 12994a58fd45SPyun YongHyeon bus_release_resource(dev, SYS_RES_MEMORY, rid, 13004a58fd45SPyun YongHyeon sc->rl_res_pba); 13014a58fd45SPyun YongHyeon sc->rl_res_pba = NULL; 13024a58fd45SPyun YongHyeon msixc = 0; 13034a58fd45SPyun YongHyeon } 13044a58fd45SPyun YongHyeon } 13054a58fd45SPyun YongHyeon /* Prefer MSI to INTx. */ 13064a58fd45SPyun YongHyeon if (msixc == 0 && msic > 0) { 1307f1a5f291SMarius Strobl msic = RL_MSI_MESSAGES; 13085774c5ffSPyun YongHyeon if (pci_alloc_msi(dev, &msic) == 0) { 13095774c5ffSPyun YongHyeon if (msic == RL_MSI_MESSAGES) { 13104a58fd45SPyun YongHyeon device_printf(dev, "Using %d MSI message\n", 13115774c5ffSPyun YongHyeon msic); 1312351a76f9SPyun YongHyeon sc->rl_flags |= RL_FLAG_MSI; 1313339a44fbSPyun YongHyeon /* Explicitly set MSI enable bit. */ 1314339a44fbSPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE); 1315339a44fbSPyun YongHyeon cfg = CSR_READ_1(sc, RL_CFG2); 1316339a44fbSPyun YongHyeon cfg |= RL_CFG2_MSI; 1317339a44fbSPyun YongHyeon CSR_WRITE_1(sc, RL_CFG2, cfg); 1318f98dd8cfSPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 13195774c5ffSPyun YongHyeon } else 13205774c5ffSPyun YongHyeon pci_release_msi(dev); 13215774c5ffSPyun YongHyeon } 13224a58fd45SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_MSI) == 0) 13234a58fd45SPyun YongHyeon msic = 0; 13245774c5ffSPyun YongHyeon } 1325a94100faSBill Paul 13265774c5ffSPyun YongHyeon /* Allocate interrupt */ 13274a58fd45SPyun YongHyeon if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0) { 13285774c5ffSPyun YongHyeon rid = 0; 13295774c5ffSPyun YongHyeon sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 13305774c5ffSPyun YongHyeon RF_SHAREABLE | RF_ACTIVE); 13315774c5ffSPyun YongHyeon if (sc->rl_irq[0] == NULL) { 13325774c5ffSPyun YongHyeon device_printf(dev, "couldn't allocate IRQ resources\n"); 1333a94100faSBill Paul error = ENXIO; 1334a94100faSBill Paul goto fail; 1335a94100faSBill Paul } 13365774c5ffSPyun YongHyeon } else { 13375774c5ffSPyun YongHyeon for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) { 13385774c5ffSPyun YongHyeon sc->rl_irq[i] = bus_alloc_resource_any(dev, 13395774c5ffSPyun YongHyeon SYS_RES_IRQ, &rid, RF_ACTIVE); 13405774c5ffSPyun YongHyeon if (sc->rl_irq[i] == NULL) { 13415774c5ffSPyun YongHyeon device_printf(dev, 13422df05392SSergey Kandaurov "couldn't allocate IRQ resources for " 13435774c5ffSPyun YongHyeon "message %d\n", rid); 13445774c5ffSPyun YongHyeon error = ENXIO; 13455774c5ffSPyun YongHyeon goto fail; 13465774c5ffSPyun YongHyeon } 13475774c5ffSPyun YongHyeon } 13485774c5ffSPyun YongHyeon } 1349a94100faSBill Paul 13504d2bf239SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_MSI) == 0) { 13514d2bf239SPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE); 13524d2bf239SPyun YongHyeon cfg = CSR_READ_1(sc, RL_CFG2); 13534d2bf239SPyun YongHyeon if ((cfg & RL_CFG2_MSI) != 0) { 13544d2bf239SPyun YongHyeon device_printf(dev, "turning off MSI enable bit.\n"); 13554d2bf239SPyun YongHyeon cfg &= ~RL_CFG2_MSI; 13564d2bf239SPyun YongHyeon CSR_WRITE_1(sc, RL_CFG2, cfg); 13574d2bf239SPyun YongHyeon } 13584d2bf239SPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 13594d2bf239SPyun YongHyeon } 13604d2bf239SPyun YongHyeon 1361017f1c8dSPyun YongHyeon /* Disable ASPM L0S/L1. */ 1362017f1c8dSPyun YongHyeon if (sc->rl_expcap != 0) { 1363017f1c8dSPyun YongHyeon cap = pci_read_config(dev, sc->rl_expcap + 1364389c8bd5SGavin Atkinson PCIER_LINK_CAP, 2); 1365389c8bd5SGavin Atkinson if ((cap & PCIEM_LINK_CAP_ASPM) != 0) { 1366017f1c8dSPyun YongHyeon ctl = pci_read_config(dev, sc->rl_expcap + 1367389c8bd5SGavin Atkinson PCIER_LINK_CTL, 2); 1368e935190aSGavin Atkinson if ((ctl & PCIEM_LINK_CTL_ASPMC) != 0) { 1369e935190aSGavin Atkinson ctl &= ~PCIEM_LINK_CTL_ASPMC; 1370017f1c8dSPyun YongHyeon pci_write_config(dev, sc->rl_expcap + 1371389c8bd5SGavin Atkinson PCIER_LINK_CTL, ctl, 2); 1372017f1c8dSPyun YongHyeon device_printf(dev, "ASPM disabled\n"); 1373017f1c8dSPyun YongHyeon } 1374017f1c8dSPyun YongHyeon } else 1375017f1c8dSPyun YongHyeon device_printf(dev, "no ASPM capability\n"); 1376017f1c8dSPyun YongHyeon } 1377017f1c8dSPyun YongHyeon 1378abc8ff44SBill Paul hw_rev = re_hwrevs; 1379a810fc83SPyun YongHyeon hwrev = CSR_READ_4(sc, RL_TXCFG); 1380566ca8caSJung-uk Kim switch (hwrev & 0x70000000) { 1381566ca8caSJung-uk Kim case 0x00000000: 1382566ca8caSJung-uk Kim case 0x10000000: 1383566ca8caSJung-uk Kim device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0xfc800000); 1384566ca8caSJung-uk Kim hwrev &= (RL_TXCFG_HWREV | 0x80000000); 1385566ca8caSJung-uk Kim break; 1386566ca8caSJung-uk Kim default: 1387a810fc83SPyun YongHyeon device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0x7c800000); 1388fd3ae0f5SPyun YongHyeon sc->rl_macrev = hwrev & 0x00700000; 1389a810fc83SPyun YongHyeon hwrev &= RL_TXCFG_HWREV; 1390566ca8caSJung-uk Kim break; 1391566ca8caSJung-uk Kim } 1392fd3ae0f5SPyun YongHyeon device_printf(dev, "MAC rev. 0x%08x\n", sc->rl_macrev); 1393abc8ff44SBill Paul while (hw_rev->rl_desc != NULL) { 1394abc8ff44SBill Paul if (hw_rev->rl_rev == hwrev) { 1395abc8ff44SBill Paul sc->rl_type = hw_rev->rl_type; 139681eee0ebSPyun YongHyeon sc->rl_hwrev = hw_rev; 1397abc8ff44SBill Paul break; 1398abc8ff44SBill Paul } 1399abc8ff44SBill Paul hw_rev++; 1400abc8ff44SBill Paul } 1401d65abd66SPyun YongHyeon if (hw_rev->rl_desc == NULL) { 1402a810fc83SPyun YongHyeon device_printf(dev, "Unknown H/W revision: 0x%08x\n", hwrev); 1403d65abd66SPyun YongHyeon error = ENXIO; 1404d65abd66SPyun YongHyeon goto fail; 1405d65abd66SPyun YongHyeon } 1406abc8ff44SBill Paul 1407351a76f9SPyun YongHyeon switch (hw_rev->rl_rev) { 1408351a76f9SPyun YongHyeon case RL_HWREV_8139CPLUS: 140981eee0ebSPyun YongHyeon sc->rl_flags |= RL_FLAG_FASTETHER | RL_FLAG_AUTOPAD; 1410351a76f9SPyun YongHyeon break; 1411351a76f9SPyun YongHyeon case RL_HWREV_8100E: 1412351a76f9SPyun YongHyeon case RL_HWREV_8101E: 141381eee0ebSPyun YongHyeon sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_FASTETHER; 1414351a76f9SPyun YongHyeon break; 1415b1d62f0fSPyun YongHyeon case RL_HWREV_8102E: 1416b1d62f0fSPyun YongHyeon case RL_HWREV_8102EL: 14173d22427cSTai-hwa Liang case RL_HWREV_8102EL_SPIN1: 141881eee0ebSPyun YongHyeon sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 | 141981eee0ebSPyun YongHyeon RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | 142081eee0ebSPyun YongHyeon RL_FLAG_AUTOPAD; 1421b1d62f0fSPyun YongHyeon break; 14228281a098SPyun YongHyeon case RL_HWREV_8103E: 142381eee0ebSPyun YongHyeon sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 | 142481eee0ebSPyun YongHyeon RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | 142581eee0ebSPyun YongHyeon RL_FLAG_AUTOPAD | RL_FLAG_MACSLEEP; 14268281a098SPyun YongHyeon break; 142739e69201SPyun YongHyeon case RL_HWREV_8401E: 142854899a96SPyun YongHyeon case RL_HWREV_8105E: 14296b0a8e04SPyun YongHyeon case RL_HWREV_8105E_SPIN1: 1430214c71f6SPyun YongHyeon case RL_HWREV_8106E: 143154899a96SPyun YongHyeon sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM | 143254899a96SPyun YongHyeon RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | 143354899a96SPyun YongHyeon RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD; 143454899a96SPyun YongHyeon break; 1435eef0e496SPyun YongHyeon case RL_HWREV_8402: 1436eef0e496SPyun YongHyeon sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM | 1437eef0e496SPyun YongHyeon RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | 1438eef0e496SPyun YongHyeon RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | 1439eef0e496SPyun YongHyeon RL_FLAG_CMDSTOP_WAIT_TXQ; 1440eef0e496SPyun YongHyeon break; 1441ef278cb4SPyun YongHyeon case RL_HWREV_8168B_SPIN1: 1442ef278cb4SPyun YongHyeon case RL_HWREV_8168B_SPIN2: 1443886ff602SPyun YongHyeon sc->rl_flags |= RL_FLAG_WOLRXENB; 1444886ff602SPyun YongHyeon /* FALLTHROUGH */ 1445ef278cb4SPyun YongHyeon case RL_HWREV_8168B_SPIN3: 1446aaab4fbeSJung-uk Kim sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_MACSTAT; 1447deb5c680SPyun YongHyeon break; 1448deb5c680SPyun YongHyeon case RL_HWREV_8168C_SPIN2: 144961f45a72SPyun YongHyeon sc->rl_flags |= RL_FLAG_MACSLEEP; 145061f45a72SPyun YongHyeon /* FALLTHROUGH */ 145161f45a72SPyun YongHyeon case RL_HWREV_8168C: 1452fd3ae0f5SPyun YongHyeon if (sc->rl_macrev == 0x00200000) 145361f45a72SPyun YongHyeon sc->rl_flags |= RL_FLAG_MACSLEEP; 145461f45a72SPyun YongHyeon /* FALLTHROUGH */ 1455deb5c680SPyun YongHyeon case RL_HWREV_8168CP: 1456aaab4fbeSJung-uk Kim sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | 1457f2e491c9SPyun YongHyeon RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP | 14586830588dSPyun YongHyeon RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 | RL_FLAG_WOL_MANLINK; 1459351a76f9SPyun YongHyeon break; 1460df2dc2b3SPyun YongHyeon case RL_HWREV_8168D: 1461df2dc2b3SPyun YongHyeon sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM | 1462df2dc2b3SPyun YongHyeon RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | 1463df2dc2b3SPyun YongHyeon RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 | 1464df2dc2b3SPyun YongHyeon RL_FLAG_WOL_MANLINK; 1465df2dc2b3SPyun YongHyeon break; 1466eef0e496SPyun YongHyeon case RL_HWREV_8168DP: 1467eef0e496SPyun YongHyeon sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | 1468eef0e496SPyun YongHyeon RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_AUTOPAD | 14696830588dSPyun YongHyeon RL_FLAG_JUMBOV2 | RL_FLAG_WAIT_TXPOLL | RL_FLAG_WOL_MANLINK; 1470eef0e496SPyun YongHyeon break; 1471d0c45156SPyun YongHyeon case RL_HWREV_8168E: 1472d0c45156SPyun YongHyeon sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM | 1473d0c45156SPyun YongHyeon RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | 14746830588dSPyun YongHyeon RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 | 14756830588dSPyun YongHyeon RL_FLAG_WOL_MANLINK; 1476d0c45156SPyun YongHyeon break; 1477f0431c5bSPyun YongHyeon case RL_HWREV_8168E_VL: 1478d467ffaaSPyun YongHyeon case RL_HWREV_8168F: 1479f1a5f291SMarius Strobl sc->rl_flags |= RL_FLAG_EARLYOFF; 1480f1a5f291SMarius Strobl /* FALLTHROUGH */ 1481d56f7f52SPyun YongHyeon case RL_HWREV_8411: 1482f0431c5bSPyun YongHyeon sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | 1483f0431c5bSPyun YongHyeon RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP | 1484eef0e496SPyun YongHyeon RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 | 14856830588dSPyun YongHyeon RL_FLAG_CMDSTOP_WAIT_TXQ | RL_FLAG_WOL_MANLINK; 1486f0431c5bSPyun YongHyeon break; 1487f1a5f291SMarius Strobl case RL_HWREV_8168EP: 1488f1a5f291SMarius Strobl case RL_HWREV_8168G: 1489f1a5f291SMarius Strobl case RL_HWREV_8411B: 1490f1a5f291SMarius Strobl sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | 1491f1a5f291SMarius Strobl RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP | 1492f1a5f291SMarius Strobl RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 | 1493f1a5f291SMarius Strobl RL_FLAG_CMDSTOP_WAIT_TXQ | RL_FLAG_WOL_MANLINK | 149414013280SMarius Strobl RL_FLAG_8168G_PLUS; 1495f1a5f291SMarius Strobl break; 1496ab9f923eSPyun YongHyeon case RL_HWREV_8168GU: 149714013280SMarius Strobl case RL_HWREV_8168H: 1498ab9f923eSPyun YongHyeon if (pci_get_device(dev) == RT_DEVICEID_8101E) { 149914013280SMarius Strobl /* RTL8106E(US), RTL8107E */ 1500ab9f923eSPyun YongHyeon sc->rl_flags |= RL_FLAG_FASTETHER; 1501ab9f923eSPyun YongHyeon } else 1502ab9f923eSPyun YongHyeon sc->rl_flags |= RL_FLAG_JUMBOV2 | RL_FLAG_WOL_MANLINK; 1503ab9f923eSPyun YongHyeon 1504ab9f923eSPyun YongHyeon sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | 1505ab9f923eSPyun YongHyeon RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP | 1506f1a5f291SMarius Strobl RL_FLAG_AUTOPAD | RL_FLAG_CMDSTOP_WAIT_TXQ | 150714013280SMarius Strobl RL_FLAG_8168G_PLUS; 1508ab9f923eSPyun YongHyeon break; 1509566ca8caSJung-uk Kim case RL_HWREV_8169_8110SB: 1510566ca8caSJung-uk Kim case RL_HWREV_8169_8110SBL: 1511566ca8caSJung-uk Kim case RL_HWREV_8169_8110SC: 1512566ca8caSJung-uk Kim case RL_HWREV_8169_8110SCE: 1513566ca8caSJung-uk Kim sc->rl_flags |= RL_FLAG_PHYWAKE; 1514566ca8caSJung-uk Kim /* FALLTHROUGH */ 15150596d7e6SPyun YongHyeon case RL_HWREV_8169: 15160596d7e6SPyun YongHyeon case RL_HWREV_8169S: 1517566ca8caSJung-uk Kim case RL_HWREV_8110S: 1518566ca8caSJung-uk Kim sc->rl_flags |= RL_FLAG_MACRESET; 1519351a76f9SPyun YongHyeon break; 1520351a76f9SPyun YongHyeon default: 1521351a76f9SPyun YongHyeon break; 1522351a76f9SPyun YongHyeon } 1523351a76f9SPyun YongHyeon 1524e7e7593cSPyun YongHyeon if (sc->rl_hwrev->rl_rev == RL_HWREV_8139CPLUS) { 1525e7e7593cSPyun YongHyeon sc->rl_cfg0 = RL_8139_CFG0; 1526e7e7593cSPyun YongHyeon sc->rl_cfg1 = RL_8139_CFG1; 1527e7e7593cSPyun YongHyeon sc->rl_cfg2 = 0; 1528e7e7593cSPyun YongHyeon sc->rl_cfg3 = RL_8139_CFG3; 1529e7e7593cSPyun YongHyeon sc->rl_cfg4 = RL_8139_CFG4; 1530e7e7593cSPyun YongHyeon sc->rl_cfg5 = RL_8139_CFG5; 1531e7e7593cSPyun YongHyeon } else { 1532e7e7593cSPyun YongHyeon sc->rl_cfg0 = RL_CFG0; 1533e7e7593cSPyun YongHyeon sc->rl_cfg1 = RL_CFG1; 1534e7e7593cSPyun YongHyeon sc->rl_cfg2 = RL_CFG2; 1535e7e7593cSPyun YongHyeon sc->rl_cfg3 = RL_CFG3; 1536e7e7593cSPyun YongHyeon sc->rl_cfg4 = RL_CFG4; 1537e7e7593cSPyun YongHyeon sc->rl_cfg5 = RL_CFG5; 1538e7e7593cSPyun YongHyeon } 1539e7e7593cSPyun YongHyeon 154093252626SPyun YongHyeon /* Reset the adapter. */ 154193252626SPyun YongHyeon RL_LOCK(sc); 154293252626SPyun YongHyeon re_reset(sc); 154393252626SPyun YongHyeon RL_UNLOCK(sc); 154493252626SPyun YongHyeon 1545deb5c680SPyun YongHyeon /* Enable PME. */ 1546deb5c680SPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE); 1547e7e7593cSPyun YongHyeon cfg = CSR_READ_1(sc, sc->rl_cfg1); 1548deb5c680SPyun YongHyeon cfg |= RL_CFG1_PME; 1549e7e7593cSPyun YongHyeon CSR_WRITE_1(sc, sc->rl_cfg1, cfg); 1550e7e7593cSPyun YongHyeon cfg = CSR_READ_1(sc, sc->rl_cfg5); 1551deb5c680SPyun YongHyeon cfg &= RL_CFG5_PME_STS; 1552e7e7593cSPyun YongHyeon CSR_WRITE_1(sc, sc->rl_cfg5, cfg); 1553deb5c680SPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 1554deb5c680SPyun YongHyeon 1555deb5c680SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_PAR) != 0) { 1556deb5c680SPyun YongHyeon /* 1557deb5c680SPyun YongHyeon * XXX Should have a better way to extract station 1558deb5c680SPyun YongHyeon * address from EEPROM. 1559deb5c680SPyun YongHyeon */ 1560deb5c680SPyun YongHyeon for (i = 0; i < ETHER_ADDR_LEN; i++) 1561deb5c680SPyun YongHyeon eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i); 1562deb5c680SPyun YongHyeon } else { 1563141f92e7SPyun YongHyeon sc->rl_eewidth = RL_9356_ADDR_LEN; 1564ed510fb0SBill Paul re_read_eeprom(sc, (caddr_t)&re_did, 0, 1); 1565a94100faSBill Paul if (re_did != 0x8129) 1566141f92e7SPyun YongHyeon sc->rl_eewidth = RL_9346_ADDR_LEN; 1567a94100faSBill Paul 1568a94100faSBill Paul /* 1569a94100faSBill Paul * Get station address from the EEPROM. 1570a94100faSBill Paul */ 1571ed510fb0SBill Paul re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3); 1572be099007SPyun YongHyeon for (i = 0; i < ETHER_ADDR_LEN / 2; i++) 1573be099007SPyun YongHyeon as[i] = le16toh(as[i]); 1574de8925a2SKevin Lo bcopy(as, eaddr, ETHER_ADDR_LEN); 1575deb5c680SPyun YongHyeon } 1576ed510fb0SBill Paul 1577ed510fb0SBill Paul if (sc->rl_type == RL_8169) { 1578d65abd66SPyun YongHyeon /* Set RX length mask and number of descriptors. */ 1579ed510fb0SBill Paul sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN; 1580ed510fb0SBill Paul sc->rl_txstart = RL_GTXSTART; 1581d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT; 1582d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT; 1583ed510fb0SBill Paul } else { 1584d65abd66SPyun YongHyeon /* Set RX length mask and number of descriptors. */ 1585ed510fb0SBill Paul sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN; 1586ed510fb0SBill Paul sc->rl_txstart = RL_TXSTART; 1587d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT; 1588d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT; 1589abc8ff44SBill Paul } 15909bac70b8SBill Paul 1591a94100faSBill Paul error = re_allocmem(dev, sc); 1592a94100faSBill Paul if (error) 1593a94100faSBill Paul goto fail; 15940534aae0SPyun YongHyeon re_add_sysctls(sc); 1595a94100faSBill Paul 1596cd036ec1SBrooks Davis ifp = sc->rl_ifp = if_alloc(IFT_ETHER); 1597cd036ec1SBrooks Davis if (ifp == NULL) { 1598d1754a9bSJohn Baldwin device_printf(dev, "can not if_alloc()\n"); 1599cd036ec1SBrooks Davis error = ENOSPC; 1600cd036ec1SBrooks Davis goto fail; 1601cd036ec1SBrooks Davis } 1602cd036ec1SBrooks Davis 160361f45a72SPyun YongHyeon /* Take controller out of deep sleep mode. */ 160461f45a72SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) { 160561f45a72SPyun YongHyeon if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80) 160661f45a72SPyun YongHyeon CSR_WRITE_1(sc, RL_GPIO, 160761f45a72SPyun YongHyeon CSR_READ_1(sc, RL_GPIO) | 0x01); 160861f45a72SPyun YongHyeon else 160961f45a72SPyun YongHyeon CSR_WRITE_1(sc, RL_GPIO, 161061f45a72SPyun YongHyeon CSR_READ_1(sc, RL_GPIO) & ~0x01); 161161f45a72SPyun YongHyeon } 161261f45a72SPyun YongHyeon 1613351a76f9SPyun YongHyeon /* Take PHY out of power down mode. */ 161439e69201SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0) { 1615d0c45156SPyun YongHyeon CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) | 0x80); 161639e69201SPyun YongHyeon if (hw_rev->rl_rev == RL_HWREV_8401E) 161739e69201SPyun YongHyeon CSR_WRITE_1(sc, 0xD1, CSR_READ_1(sc, 0xD1) & ~0x08); 161839e69201SPyun YongHyeon } 1619351a76f9SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_PHYWAKE) != 0) { 1620351a76f9SPyun YongHyeon re_gmii_writereg(dev, 1, 0x1f, 0); 1621351a76f9SPyun YongHyeon re_gmii_writereg(dev, 1, 0x0e, 0); 1622351a76f9SPyun YongHyeon } 1623351a76f9SPyun YongHyeon 1624a94100faSBill Paul ifp->if_softc = sc; 16259bf40edeSBrooks Davis if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 1626a94100faSBill Paul ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1627a94100faSBill Paul ifp->if_ioctl = re_ioctl; 1628a94100faSBill Paul ifp->if_start = re_start; 1629bc2a1002SPyun YongHyeon /* 1630bc2a1002SPyun YongHyeon * RTL8168/8111C generates wrong IP checksummed frame if the 163174a03446SPyun YongHyeon * packet has IP options so disable TX checksum offloading. 1632bc2a1002SPyun YongHyeon */ 1633bc2a1002SPyun YongHyeon if (sc->rl_hwrev->rl_rev == RL_HWREV_8168C || 16343c2a957dSPyun YongHyeon sc->rl_hwrev->rl_rev == RL_HWREV_8168C_SPIN2 || 163574a03446SPyun YongHyeon sc->rl_hwrev->rl_rev == RL_HWREV_8168CP) { 163674a03446SPyun YongHyeon ifp->if_hwassist = 0; 163774a03446SPyun YongHyeon ifp->if_capabilities = IFCAP_RXCSUM | IFCAP_TSO4; 163874a03446SPyun YongHyeon } else { 1639bc2a1002SPyun YongHyeon ifp->if_hwassist = CSUM_IP | CSUM_TCP | CSUM_UDP; 1640d6d7d923SPyun YongHyeon ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_TSO4; 164174a03446SPyun YongHyeon } 164274a03446SPyun YongHyeon ifp->if_hwassist |= CSUM_TSO; 1643498bd0d3SBill Paul ifp->if_capenable = ifp->if_capabilities; 1644a94100faSBill Paul ifp->if_init = re_init; 164552732175SMax Laier IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN); 164652732175SMax Laier ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN; 164752732175SMax Laier IFQ_SET_READY(&ifp->if_snd); 1648a94100faSBill Paul 1649ed510fb0SBill Paul TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc); 1650ed510fb0SBill Paul 1651fed3ed71SPyun YongHyeon #define RE_PHYAD_INTERNAL 0 1652fed3ed71SPyun YongHyeon 1653fed3ed71SPyun YongHyeon /* Do MII setup. */ 1654fed3ed71SPyun YongHyeon phy = RE_PHYAD_INTERNAL; 1655fed3ed71SPyun YongHyeon if (sc->rl_type == RL_8169) 1656fed3ed71SPyun YongHyeon phy = 1; 165714013280SMarius Strobl capmask = BMSR_DEFCAPMASK; 165814013280SMarius Strobl if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0) 165914013280SMarius Strobl capmask &= ~BMSR_EXTSTAT; 1660fed3ed71SPyun YongHyeon error = mii_attach(dev, &sc->rl_miibus, ifp, re_ifmedia_upd, 166114013280SMarius Strobl re_ifmedia_sts, capmask, phy, MII_OFFSET_ANY, MIIF_DOPAUSE); 1662fed3ed71SPyun YongHyeon if (error != 0) { 1663fed3ed71SPyun YongHyeon device_printf(dev, "attaching PHYs failed\n"); 1664fed3ed71SPyun YongHyeon goto fail; 1665fed3ed71SPyun YongHyeon } 1666fed3ed71SPyun YongHyeon 1667a94100faSBill Paul /* 1668a94100faSBill Paul * Call MI attach routine. 1669a94100faSBill Paul */ 1670a94100faSBill Paul ether_ifattach(ifp, eaddr); 1671a94100faSBill Paul 1672960fd5b3SPyun YongHyeon /* VLAN capability setup */ 1673960fd5b3SPyun YongHyeon ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING; 1674960fd5b3SPyun YongHyeon if (ifp->if_capabilities & IFCAP_HWCSUM) 1675960fd5b3SPyun YongHyeon ifp->if_capabilities |= IFCAP_VLAN_HWCSUM; 16767467bd53SPyun YongHyeon /* Enable WOL if PM is supported. */ 16773b0a4aefSJohn Baldwin if (pci_find_cap(sc->rl_dev, PCIY_PMG, ®) == 0) 16787467bd53SPyun YongHyeon ifp->if_capabilities |= IFCAP_WOL; 1679960fd5b3SPyun YongHyeon ifp->if_capenable = ifp->if_capabilities; 168044f7cbf5SPyun YongHyeon ifp->if_capenable &= ~(IFCAP_WOL_UCAST | IFCAP_WOL_MCAST); 1681a2a8420cSPyun YongHyeon /* 1682f9ad4da7SPyun YongHyeon * Don't enable TSO by default. It is known to generate 1683f9ad4da7SPyun YongHyeon * corrupted TCP segments(bad TCP options) under certain 16842df05392SSergey Kandaurov * circumstances. 1685a2a8420cSPyun YongHyeon */ 1686a2a8420cSPyun YongHyeon ifp->if_hwassist &= ~CSUM_TSO; 1687ecafbbb5SPyun YongHyeon ifp->if_capenable &= ~(IFCAP_TSO4 | IFCAP_VLAN_HWTSO); 1688960fd5b3SPyun YongHyeon #ifdef DEVICE_POLLING 1689960fd5b3SPyun YongHyeon ifp->if_capabilities |= IFCAP_POLLING; 1690960fd5b3SPyun YongHyeon #endif 1691960fd5b3SPyun YongHyeon /* 1692960fd5b3SPyun YongHyeon * Tell the upper layer(s) we support long frames. 1693960fd5b3SPyun YongHyeon * Must appear after the call to ether_ifattach() because 1694960fd5b3SPyun YongHyeon * ether_ifattach() sets ifi_hdrlen to the default value. 1695960fd5b3SPyun YongHyeon */ 16961bffa951SGleb Smirnoff ifp->if_hdrlen = sizeof(struct ether_vlan_header); 1697960fd5b3SPyun YongHyeon 1698579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP 1699579a6e3cSLuigi Rizzo re_netmap_attach(sc); 1700579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */ 1701e9f8886eSMarius Strobl 1702ed510fb0SBill Paul #ifdef RE_DIAG 1703ed510fb0SBill Paul /* 1704ed510fb0SBill Paul * Perform hardware diagnostic on the original RTL8169. 1705ed510fb0SBill Paul * Some 32-bit cards were incorrectly wired and would 1706ed510fb0SBill Paul * malfunction if plugged into a 64-bit slot. 1707ed510fb0SBill Paul */ 1708ed510fb0SBill Paul if (hwrev == RL_HWREV_8169) { 1709ed510fb0SBill Paul error = re_diag(sc); 1710a94100faSBill Paul if (error) { 1711ed510fb0SBill Paul device_printf(dev, 1712ed510fb0SBill Paul "attach aborted due to hardware diag failure\n"); 1713a94100faSBill Paul ether_ifdetach(ifp); 1714a94100faSBill Paul goto fail; 1715a94100faSBill Paul } 1716ed510fb0SBill Paul } 1717ed510fb0SBill Paul #endif 1718a94100faSBill Paul 1719502be0f7SPyun YongHyeon #ifdef RE_TX_MODERATION 1720502be0f7SPyun YongHyeon intr_filter = 1; 1721502be0f7SPyun YongHyeon #endif 1722a94100faSBill Paul /* Hook interrupt last to avoid having to lock softc */ 1723502be0f7SPyun YongHyeon if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0 && 1724502be0f7SPyun YongHyeon intr_filter == 0) { 1725502be0f7SPyun YongHyeon error = bus_setup_intr(dev, sc->rl_irq[0], 1726502be0f7SPyun YongHyeon INTR_TYPE_NET | INTR_MPSAFE, NULL, re_intr_msi, sc, 1727502be0f7SPyun YongHyeon &sc->rl_intrhand[0]); 1728502be0f7SPyun YongHyeon } else { 17295774c5ffSPyun YongHyeon error = bus_setup_intr(dev, sc->rl_irq[0], 17305774c5ffSPyun YongHyeon INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc, 17315774c5ffSPyun YongHyeon &sc->rl_intrhand[0]); 17325774c5ffSPyun YongHyeon } 1733a94100faSBill Paul if (error) { 1734d1754a9bSJohn Baldwin device_printf(dev, "couldn't set up irq\n"); 1735a94100faSBill Paul ether_ifdetach(ifp); 1736a94100faSBill Paul } 1737a94100faSBill Paul 1738a94100faSBill Paul fail: 1739a94100faSBill Paul if (error) 1740a94100faSBill Paul re_detach(dev); 1741a94100faSBill Paul 1742a94100faSBill Paul return (error); 1743a94100faSBill Paul } 1744a94100faSBill Paul 1745a94100faSBill Paul /* 1746a94100faSBill Paul * Shutdown hardware and free up resources. This can be called any 1747a94100faSBill Paul * time after the mutex has been initialized. It is called in both 1748a94100faSBill Paul * the error case in attach and the normal detach case so it needs 1749a94100faSBill Paul * to be careful about only freeing resources that have actually been 1750a94100faSBill Paul * allocated. 1751a94100faSBill Paul */ 1752a94100faSBill Paul static int 17537b5ffebfSPyun YongHyeon re_detach(device_t dev) 1754a94100faSBill Paul { 1755a94100faSBill Paul struct rl_softc *sc; 1756a94100faSBill Paul struct ifnet *ifp; 17575774c5ffSPyun YongHyeon int i, rid; 1758a94100faSBill Paul 1759a94100faSBill Paul sc = device_get_softc(dev); 1760fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 1761aedd16d9SJohn-Mark Gurney KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized")); 176297b9d4baSJohn-Mark Gurney 176381cf2eb6SPyun YongHyeon /* These should only be active if attach succeeded */ 176481cf2eb6SPyun YongHyeon if (device_is_attached(dev)) { 176540929967SGleb Smirnoff #ifdef DEVICE_POLLING 176640929967SGleb Smirnoff if (ifp->if_capenable & IFCAP_POLLING) 176740929967SGleb Smirnoff ether_poll_deregister(ifp); 176840929967SGleb Smirnoff #endif 176997b9d4baSJohn-Mark Gurney RL_LOCK(sc); 177097b9d4baSJohn-Mark Gurney #if 0 177197b9d4baSJohn-Mark Gurney sc->suspended = 1; 177297b9d4baSJohn-Mark Gurney #endif 1773a94100faSBill Paul re_stop(sc); 1774525e6a87SRuslan Ermilov RL_UNLOCK(sc); 1775d1754a9bSJohn Baldwin callout_drain(&sc->rl_stat_callout); 17763d4c1b57SJohn Baldwin taskqueue_drain(taskqueue_fast, &sc->rl_inttask); 1777a94100faSBill Paul /* 1778a94100faSBill Paul * Force off the IFF_UP flag here, in case someone 1779a94100faSBill Paul * still had a BPF descriptor attached to this 178097b9d4baSJohn-Mark Gurney * interface. If they do, ether_ifdetach() will cause 1781a94100faSBill Paul * the BPF code to try and clear the promisc mode 1782a94100faSBill Paul * flag, which will bubble down to re_ioctl(), 1783a94100faSBill Paul * which will try to call re_init() again. This will 1784a94100faSBill Paul * turn the NIC back on and restart the MII ticker, 1785a94100faSBill Paul * which will panic the system when the kernel tries 1786a94100faSBill Paul * to invoke the re_tick() function that isn't there 1787a94100faSBill Paul * anymore. 1788a94100faSBill Paul */ 1789a94100faSBill Paul ifp->if_flags &= ~IFF_UP; 1790525e6a87SRuslan Ermilov ether_ifdetach(ifp); 1791a94100faSBill Paul } 1792a94100faSBill Paul if (sc->rl_miibus) 1793a94100faSBill Paul device_delete_child(dev, sc->rl_miibus); 1794a94100faSBill Paul bus_generic_detach(dev); 1795a94100faSBill Paul 179697b9d4baSJohn-Mark Gurney /* 179797b9d4baSJohn-Mark Gurney * The rest is resource deallocation, so we should already be 179897b9d4baSJohn-Mark Gurney * stopped here. 179997b9d4baSJohn-Mark Gurney */ 180097b9d4baSJohn-Mark Gurney 1801502be0f7SPyun YongHyeon if (sc->rl_intrhand[0] != NULL) { 1802502be0f7SPyun YongHyeon bus_teardown_intr(dev, sc->rl_irq[0], sc->rl_intrhand[0]); 1803502be0f7SPyun YongHyeon sc->rl_intrhand[0] = NULL; 18045774c5ffSPyun YongHyeon } 180582242c11SKevin Lo if (ifp != NULL) { 180682242c11SKevin Lo #ifdef DEV_NETMAP 180782242c11SKevin Lo netmap_detach(ifp); 180882242c11SKevin Lo #endif /* DEV_NETMAP */ 1809ad4f426eSWarner Losh if_free(ifp); 181082242c11SKevin Lo } 1811502be0f7SPyun YongHyeon if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0) 1812502be0f7SPyun YongHyeon rid = 0; 1813502be0f7SPyun YongHyeon else 1814502be0f7SPyun YongHyeon rid = 1; 18155774c5ffSPyun YongHyeon if (sc->rl_irq[0] != NULL) { 1816502be0f7SPyun YongHyeon bus_release_resource(dev, SYS_RES_IRQ, rid, sc->rl_irq[0]); 18175774c5ffSPyun YongHyeon sc->rl_irq[0] = NULL; 18185774c5ffSPyun YongHyeon } 1819502be0f7SPyun YongHyeon if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0) 18205774c5ffSPyun YongHyeon pci_release_msi(dev); 18214a58fd45SPyun YongHyeon if (sc->rl_res_pba) { 18224a58fd45SPyun YongHyeon rid = PCIR_BAR(4); 18234a58fd45SPyun YongHyeon bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->rl_res_pba); 18244a58fd45SPyun YongHyeon } 1825a94100faSBill Paul if (sc->rl_res) 1826ace7ed5dSPyun YongHyeon bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id, 1827ace7ed5dSPyun YongHyeon sc->rl_res); 1828a94100faSBill Paul 1829a94100faSBill Paul /* Unload and free the RX DMA ring memory and map */ 1830a94100faSBill Paul 1831a94100faSBill Paul if (sc->rl_ldata.rl_rx_list_tag) { 1832068d8643SJohn Baldwin if (sc->rl_ldata.rl_rx_list_addr) 1833a94100faSBill Paul bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag, 1834a94100faSBill Paul sc->rl_ldata.rl_rx_list_map); 1835068d8643SJohn Baldwin if (sc->rl_ldata.rl_rx_list) 1836a94100faSBill Paul bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag, 1837a94100faSBill Paul sc->rl_ldata.rl_rx_list, 1838a94100faSBill Paul sc->rl_ldata.rl_rx_list_map); 1839a94100faSBill Paul bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag); 1840a94100faSBill Paul } 1841a94100faSBill Paul 1842a94100faSBill Paul /* Unload and free the TX DMA ring memory and map */ 1843a94100faSBill Paul 1844a94100faSBill Paul if (sc->rl_ldata.rl_tx_list_tag) { 1845068d8643SJohn Baldwin if (sc->rl_ldata.rl_tx_list_addr) 1846a94100faSBill Paul bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag, 1847a94100faSBill Paul sc->rl_ldata.rl_tx_list_map); 1848068d8643SJohn Baldwin if (sc->rl_ldata.rl_tx_list) 1849a94100faSBill Paul bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag, 1850a94100faSBill Paul sc->rl_ldata.rl_tx_list, 1851a94100faSBill Paul sc->rl_ldata.rl_tx_list_map); 1852a94100faSBill Paul bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag); 1853a94100faSBill Paul } 1854a94100faSBill Paul 1855a94100faSBill Paul /* Destroy all the RX and TX buffer maps */ 1856a94100faSBill Paul 1857d65abd66SPyun YongHyeon if (sc->rl_ldata.rl_tx_mtag) { 18589e18005dSPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) { 18599e18005dSPyun YongHyeon if (sc->rl_ldata.rl_tx_desc[i].tx_dmamap) 1860d65abd66SPyun YongHyeon bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag, 1861d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_desc[i].tx_dmamap); 18629e18005dSPyun YongHyeon } 1863d65abd66SPyun YongHyeon bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag); 1864d65abd66SPyun YongHyeon } 1865d65abd66SPyun YongHyeon if (sc->rl_ldata.rl_rx_mtag) { 18669e18005dSPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 18679e18005dSPyun YongHyeon if (sc->rl_ldata.rl_rx_desc[i].rx_dmamap) 1868d65abd66SPyun YongHyeon bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag, 1869d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc[i].rx_dmamap); 18709e18005dSPyun YongHyeon } 1871d65abd66SPyun YongHyeon if (sc->rl_ldata.rl_rx_sparemap) 1872d65abd66SPyun YongHyeon bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag, 1873d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_sparemap); 1874d65abd66SPyun YongHyeon bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag); 1875a94100faSBill Paul } 187681eee0ebSPyun YongHyeon if (sc->rl_ldata.rl_jrx_mtag) { 187781eee0ebSPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 187881eee0ebSPyun YongHyeon if (sc->rl_ldata.rl_jrx_desc[i].rx_dmamap) 187981eee0ebSPyun YongHyeon bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag, 188081eee0ebSPyun YongHyeon sc->rl_ldata.rl_jrx_desc[i].rx_dmamap); 188181eee0ebSPyun YongHyeon } 188281eee0ebSPyun YongHyeon if (sc->rl_ldata.rl_jrx_sparemap) 188381eee0ebSPyun YongHyeon bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag, 188481eee0ebSPyun YongHyeon sc->rl_ldata.rl_jrx_sparemap); 188581eee0ebSPyun YongHyeon bus_dma_tag_destroy(sc->rl_ldata.rl_jrx_mtag); 188681eee0ebSPyun YongHyeon } 1887a94100faSBill Paul /* Unload and free the stats buffer and map */ 1888a94100faSBill Paul 1889a94100faSBill Paul if (sc->rl_ldata.rl_stag) { 1890068d8643SJohn Baldwin if (sc->rl_ldata.rl_stats_addr) 1891a94100faSBill Paul bus_dmamap_unload(sc->rl_ldata.rl_stag, 1892a94100faSBill Paul sc->rl_ldata.rl_smap); 1893068d8643SJohn Baldwin if (sc->rl_ldata.rl_stats) 18940534aae0SPyun YongHyeon bus_dmamem_free(sc->rl_ldata.rl_stag, 18950534aae0SPyun YongHyeon sc->rl_ldata.rl_stats, sc->rl_ldata.rl_smap); 1896a94100faSBill Paul bus_dma_tag_destroy(sc->rl_ldata.rl_stag); 1897a94100faSBill Paul } 1898a94100faSBill Paul 1899a94100faSBill Paul if (sc->rl_parent_tag) 1900a94100faSBill Paul bus_dma_tag_destroy(sc->rl_parent_tag); 1901a94100faSBill Paul 1902a94100faSBill Paul mtx_destroy(&sc->rl_mtx); 1903a94100faSBill Paul 1904a94100faSBill Paul return (0); 1905a94100faSBill Paul } 1906a94100faSBill Paul 1907d65abd66SPyun YongHyeon static __inline void 19087b5ffebfSPyun YongHyeon re_discard_rxbuf(struct rl_softc *sc, int idx) 1909a94100faSBill Paul { 1910d65abd66SPyun YongHyeon struct rl_desc *desc; 1911d65abd66SPyun YongHyeon struct rl_rxdesc *rxd; 1912d65abd66SPyun YongHyeon uint32_t cmdstat; 1913a94100faSBill Paul 191481eee0ebSPyun YongHyeon if (sc->rl_ifp->if_mtu > RL_MTU && 191581eee0ebSPyun YongHyeon (sc->rl_flags & RL_FLAG_JUMBOV2) != 0) 191681eee0ebSPyun YongHyeon rxd = &sc->rl_ldata.rl_jrx_desc[idx]; 191781eee0ebSPyun YongHyeon else 1918d65abd66SPyun YongHyeon rxd = &sc->rl_ldata.rl_rx_desc[idx]; 1919d65abd66SPyun YongHyeon desc = &sc->rl_ldata.rl_rx_list[idx]; 1920d65abd66SPyun YongHyeon desc->rl_vlanctl = 0; 1921d65abd66SPyun YongHyeon cmdstat = rxd->rx_size; 1922d65abd66SPyun YongHyeon if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1) 1923d65abd66SPyun YongHyeon cmdstat |= RL_RDESC_CMD_EOR; 1924d65abd66SPyun YongHyeon desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN); 1925d65abd66SPyun YongHyeon } 1926d65abd66SPyun YongHyeon 1927d65abd66SPyun YongHyeon static int 19287b5ffebfSPyun YongHyeon re_newbuf(struct rl_softc *sc, int idx) 1929d65abd66SPyun YongHyeon { 1930d65abd66SPyun YongHyeon struct mbuf *m; 1931d65abd66SPyun YongHyeon struct rl_rxdesc *rxd; 1932d65abd66SPyun YongHyeon bus_dma_segment_t segs[1]; 1933d65abd66SPyun YongHyeon bus_dmamap_t map; 1934d65abd66SPyun YongHyeon struct rl_desc *desc; 1935d65abd66SPyun YongHyeon uint32_t cmdstat; 1936d65abd66SPyun YongHyeon int error, nsegs; 1937d65abd66SPyun YongHyeon 1938c6499eccSGleb Smirnoff m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1939d65abd66SPyun YongHyeon if (m == NULL) 1940a94100faSBill Paul return (ENOBUFS); 1941a94100faSBill Paul 1942a94100faSBill Paul m->m_len = m->m_pkthdr.len = MCLBYTES; 194322a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX 194422a11c96SJohn-Mark Gurney /* 194522a11c96SJohn-Mark Gurney * This is part of an evil trick to deal with non-x86 platforms. 194622a11c96SJohn-Mark Gurney * The RealTek chip requires RX buffers to be aligned on 64-bit 194722a11c96SJohn-Mark Gurney * boundaries, but that will hose non-x86 machines. To get around 194822a11c96SJohn-Mark Gurney * this, we leave some empty space at the start of each buffer 194922a11c96SJohn-Mark Gurney * and for non-x86 hosts, we copy the buffer back six bytes 195022a11c96SJohn-Mark Gurney * to achieve word alignment. This is slightly more efficient 195122a11c96SJohn-Mark Gurney * than allocating a new buffer, copying the contents, and 195222a11c96SJohn-Mark Gurney * discarding the old buffer. 195322a11c96SJohn-Mark Gurney */ 195422a11c96SJohn-Mark Gurney m_adj(m, RE_ETHER_ALIGN); 195522a11c96SJohn-Mark Gurney #endif 1956d65abd66SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag, 1957d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT); 1958d65abd66SPyun YongHyeon if (error != 0) { 1959d65abd66SPyun YongHyeon m_freem(m); 1960d65abd66SPyun YongHyeon return (ENOBUFS); 1961d65abd66SPyun YongHyeon } 1962d65abd66SPyun YongHyeon KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs)); 1963a94100faSBill Paul 1964d65abd66SPyun YongHyeon rxd = &sc->rl_ldata.rl_rx_desc[idx]; 1965d65abd66SPyun YongHyeon if (rxd->rx_m != NULL) { 1966d65abd66SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap, 1967d65abd66SPyun YongHyeon BUS_DMASYNC_POSTREAD); 1968d65abd66SPyun YongHyeon bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap); 1969a94100faSBill Paul } 1970a94100faSBill Paul 1971d65abd66SPyun YongHyeon rxd->rx_m = m; 1972d65abd66SPyun YongHyeon map = rxd->rx_dmamap; 1973d65abd66SPyun YongHyeon rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap; 1974d65abd66SPyun YongHyeon rxd->rx_size = segs[0].ds_len; 1975d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_sparemap = map; 1976d65abd66SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap, 1977a94100faSBill Paul BUS_DMASYNC_PREREAD); 1978a94100faSBill Paul 1979d65abd66SPyun YongHyeon desc = &sc->rl_ldata.rl_rx_list[idx]; 1980d65abd66SPyun YongHyeon desc->rl_vlanctl = 0; 1981d65abd66SPyun YongHyeon desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr)); 1982d65abd66SPyun YongHyeon desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr)); 1983d65abd66SPyun YongHyeon cmdstat = segs[0].ds_len; 1984d65abd66SPyun YongHyeon if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1) 1985d65abd66SPyun YongHyeon cmdstat |= RL_RDESC_CMD_EOR; 1986d65abd66SPyun YongHyeon desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN); 1987d65abd66SPyun YongHyeon 1988a94100faSBill Paul return (0); 1989a94100faSBill Paul } 1990a94100faSBill Paul 199181eee0ebSPyun YongHyeon static int 199281eee0ebSPyun YongHyeon re_jumbo_newbuf(struct rl_softc *sc, int idx) 199381eee0ebSPyun YongHyeon { 199481eee0ebSPyun YongHyeon struct mbuf *m; 199581eee0ebSPyun YongHyeon struct rl_rxdesc *rxd; 199681eee0ebSPyun YongHyeon bus_dma_segment_t segs[1]; 199781eee0ebSPyun YongHyeon bus_dmamap_t map; 199881eee0ebSPyun YongHyeon struct rl_desc *desc; 199981eee0ebSPyun YongHyeon uint32_t cmdstat; 200081eee0ebSPyun YongHyeon int error, nsegs; 200181eee0ebSPyun YongHyeon 2002c6499eccSGleb Smirnoff m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES); 200381eee0ebSPyun YongHyeon if (m == NULL) 200481eee0ebSPyun YongHyeon return (ENOBUFS); 200581eee0ebSPyun YongHyeon m->m_len = m->m_pkthdr.len = MJUM9BYTES; 200681eee0ebSPyun YongHyeon #ifdef RE_FIXUP_RX 200781eee0ebSPyun YongHyeon m_adj(m, RE_ETHER_ALIGN); 200881eee0ebSPyun YongHyeon #endif 200981eee0ebSPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_jrx_mtag, 201081eee0ebSPyun YongHyeon sc->rl_ldata.rl_jrx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT); 201181eee0ebSPyun YongHyeon if (error != 0) { 201281eee0ebSPyun YongHyeon m_freem(m); 201381eee0ebSPyun YongHyeon return (ENOBUFS); 201481eee0ebSPyun YongHyeon } 201581eee0ebSPyun YongHyeon KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs)); 201681eee0ebSPyun YongHyeon 201781eee0ebSPyun YongHyeon rxd = &sc->rl_ldata.rl_jrx_desc[idx]; 201881eee0ebSPyun YongHyeon if (rxd->rx_m != NULL) { 201981eee0ebSPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap, 202081eee0ebSPyun YongHyeon BUS_DMASYNC_POSTREAD); 202181eee0ebSPyun YongHyeon bus_dmamap_unload(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap); 202281eee0ebSPyun YongHyeon } 202381eee0ebSPyun YongHyeon 202481eee0ebSPyun YongHyeon rxd->rx_m = m; 202581eee0ebSPyun YongHyeon map = rxd->rx_dmamap; 202681eee0ebSPyun YongHyeon rxd->rx_dmamap = sc->rl_ldata.rl_jrx_sparemap; 202781eee0ebSPyun YongHyeon rxd->rx_size = segs[0].ds_len; 202881eee0ebSPyun YongHyeon sc->rl_ldata.rl_jrx_sparemap = map; 202981eee0ebSPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap, 203081eee0ebSPyun YongHyeon BUS_DMASYNC_PREREAD); 203181eee0ebSPyun YongHyeon 203281eee0ebSPyun YongHyeon desc = &sc->rl_ldata.rl_rx_list[idx]; 203381eee0ebSPyun YongHyeon desc->rl_vlanctl = 0; 203481eee0ebSPyun YongHyeon desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr)); 203581eee0ebSPyun YongHyeon desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr)); 203681eee0ebSPyun YongHyeon cmdstat = segs[0].ds_len; 203781eee0ebSPyun YongHyeon if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1) 203881eee0ebSPyun YongHyeon cmdstat |= RL_RDESC_CMD_EOR; 203981eee0ebSPyun YongHyeon desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN); 204081eee0ebSPyun YongHyeon 204181eee0ebSPyun YongHyeon return (0); 204281eee0ebSPyun YongHyeon } 204381eee0ebSPyun YongHyeon 204422a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX 204522a11c96SJohn-Mark Gurney static __inline void 20467b5ffebfSPyun YongHyeon re_fixup_rx(struct mbuf *m) 204722a11c96SJohn-Mark Gurney { 204822a11c96SJohn-Mark Gurney int i; 204922a11c96SJohn-Mark Gurney uint16_t *src, *dst; 205022a11c96SJohn-Mark Gurney 205122a11c96SJohn-Mark Gurney src = mtod(m, uint16_t *); 205222a11c96SJohn-Mark Gurney dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src; 205322a11c96SJohn-Mark Gurney 205422a11c96SJohn-Mark Gurney for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++) 205522a11c96SJohn-Mark Gurney *dst++ = *src++; 205622a11c96SJohn-Mark Gurney 205722a11c96SJohn-Mark Gurney m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN; 205822a11c96SJohn-Mark Gurney } 205922a11c96SJohn-Mark Gurney #endif 206022a11c96SJohn-Mark Gurney 2061a94100faSBill Paul static int 20627b5ffebfSPyun YongHyeon re_tx_list_init(struct rl_softc *sc) 2063a94100faSBill Paul { 2064d65abd66SPyun YongHyeon struct rl_desc *desc; 2065d65abd66SPyun YongHyeon int i; 206697b9d4baSJohn-Mark Gurney 206797b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 206897b9d4baSJohn-Mark Gurney 2069d65abd66SPyun YongHyeon bzero(sc->rl_ldata.rl_tx_list, 2070d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc)); 2071d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) 2072d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_desc[i].tx_m = NULL; 2073579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP 2074579a6e3cSLuigi Rizzo re_netmap_tx_init(sc); 2075579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */ 2076d65abd66SPyun YongHyeon /* Set EOR. */ 2077d65abd66SPyun YongHyeon desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1]; 2078d65abd66SPyun YongHyeon desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR); 2079a94100faSBill Paul 2080a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 2081d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_list_map, 2082d65abd66SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 2083d65abd66SPyun YongHyeon 2084a94100faSBill Paul sc->rl_ldata.rl_tx_prodidx = 0; 2085a94100faSBill Paul sc->rl_ldata.rl_tx_considx = 0; 2086d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt; 2087a94100faSBill Paul 2088a94100faSBill Paul return (0); 2089a94100faSBill Paul } 2090a94100faSBill Paul 2091a94100faSBill Paul static int 20927b5ffebfSPyun YongHyeon re_rx_list_init(struct rl_softc *sc) 2093a94100faSBill Paul { 2094d65abd66SPyun YongHyeon int error, i; 2095a94100faSBill Paul 2096d65abd66SPyun YongHyeon bzero(sc->rl_ldata.rl_rx_list, 2097d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc)); 2098d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 2099d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc[i].rx_m = NULL; 2100d65abd66SPyun YongHyeon if ((error = re_newbuf(sc, i)) != 0) 2101d65abd66SPyun YongHyeon return (error); 2102a94100faSBill Paul } 2103579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP 2104579a6e3cSLuigi Rizzo re_netmap_rx_init(sc); 2105579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */ 2106a94100faSBill Paul 2107a94100faSBill Paul /* Flush the RX descriptors */ 2108a94100faSBill Paul 2109a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 2110a94100faSBill Paul sc->rl_ldata.rl_rx_list_map, 2111a94100faSBill Paul BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 2112a94100faSBill Paul 2113a94100faSBill Paul sc->rl_ldata.rl_rx_prodidx = 0; 2114a94100faSBill Paul sc->rl_head = sc->rl_tail = NULL; 2115502be0f7SPyun YongHyeon sc->rl_int_rx_act = 0; 2116a94100faSBill Paul 2117a94100faSBill Paul return (0); 2118a94100faSBill Paul } 2119a94100faSBill Paul 212081eee0ebSPyun YongHyeon static int 212181eee0ebSPyun YongHyeon re_jrx_list_init(struct rl_softc *sc) 212281eee0ebSPyun YongHyeon { 212381eee0ebSPyun YongHyeon int error, i; 212481eee0ebSPyun YongHyeon 212581eee0ebSPyun YongHyeon bzero(sc->rl_ldata.rl_rx_list, 212681eee0ebSPyun YongHyeon sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc)); 212781eee0ebSPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 212881eee0ebSPyun YongHyeon sc->rl_ldata.rl_jrx_desc[i].rx_m = NULL; 212981eee0ebSPyun YongHyeon if ((error = re_jumbo_newbuf(sc, i)) != 0) 213081eee0ebSPyun YongHyeon return (error); 213181eee0ebSPyun YongHyeon } 213281eee0ebSPyun YongHyeon 213381eee0ebSPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 213481eee0ebSPyun YongHyeon sc->rl_ldata.rl_rx_list_map, 213581eee0ebSPyun YongHyeon BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD); 213681eee0ebSPyun YongHyeon 213781eee0ebSPyun YongHyeon sc->rl_ldata.rl_rx_prodidx = 0; 213881eee0ebSPyun YongHyeon sc->rl_head = sc->rl_tail = NULL; 2139502be0f7SPyun YongHyeon sc->rl_int_rx_act = 0; 214081eee0ebSPyun YongHyeon 214181eee0ebSPyun YongHyeon return (0); 214281eee0ebSPyun YongHyeon } 214381eee0ebSPyun YongHyeon 2144a94100faSBill Paul /* 2145a94100faSBill Paul * RX handler for C+ and 8169. For the gigE chips, we support 2146a94100faSBill Paul * the reception of jumbo frames that have been fragmented 2147a94100faSBill Paul * across multiple 2K mbuf cluster buffers. 2148a94100faSBill Paul */ 2149ed510fb0SBill Paul static int 21501abcdbd1SAttilio Rao re_rxeof(struct rl_softc *sc, int *rx_npktsp) 2151a94100faSBill Paul { 2152a94100faSBill Paul struct mbuf *m; 2153a94100faSBill Paul struct ifnet *ifp; 215481eee0ebSPyun YongHyeon int i, rxerr, total_len; 2155a94100faSBill Paul struct rl_desc *cur_rx; 2156a94100faSBill Paul u_int32_t rxstat, rxvlan; 215781eee0ebSPyun YongHyeon int jumbo, maxpkt = 16, rx_npkts = 0; 2158a94100faSBill Paul 21595120abbfSSam Leffler RL_LOCK_ASSERT(sc); 21605120abbfSSam Leffler 2161fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 2162579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP 2163ce3ee1e7SLuigi Rizzo if (netmap_rx_irq(ifp, 0, &rx_npkts)) 2164579a6e3cSLuigi Rizzo return 0; 2165579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */ 216681eee0ebSPyun YongHyeon if (ifp->if_mtu > RL_MTU && (sc->rl_flags & RL_FLAG_JUMBOV2) != 0) 216781eee0ebSPyun YongHyeon jumbo = 1; 216881eee0ebSPyun YongHyeon else 216981eee0ebSPyun YongHyeon jumbo = 0; 2170a94100faSBill Paul 2171a94100faSBill Paul /* Invalidate the descriptor memory */ 2172a94100faSBill Paul 2173a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 2174a94100faSBill Paul sc->rl_ldata.rl_rx_list_map, 2175d65abd66SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 2176a94100faSBill Paul 2177d65abd66SPyun YongHyeon for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0; 2178d65abd66SPyun YongHyeon i = RL_RX_DESC_NXT(sc, i)) { 21795b6d1d9dSPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 21805b6d1d9dSPyun YongHyeon break; 2181a94100faSBill Paul cur_rx = &sc->rl_ldata.rl_rx_list[i]; 2182a94100faSBill Paul rxstat = le32toh(cur_rx->rl_cmdstat); 2183d65abd66SPyun YongHyeon if ((rxstat & RL_RDESC_STAT_OWN) != 0) 2184d65abd66SPyun YongHyeon break; 2185d65abd66SPyun YongHyeon total_len = rxstat & sc->rl_rxlenmask; 2186a94100faSBill Paul rxvlan = le32toh(cur_rx->rl_vlanctl); 218781eee0ebSPyun YongHyeon if (jumbo != 0) 218881eee0ebSPyun YongHyeon m = sc->rl_ldata.rl_jrx_desc[i].rx_m; 218981eee0ebSPyun YongHyeon else 2190d65abd66SPyun YongHyeon m = sc->rl_ldata.rl_rx_desc[i].rx_m; 2191a94100faSBill Paul 219281eee0ebSPyun YongHyeon if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 && 219381eee0ebSPyun YongHyeon (rxstat & (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) != 219481eee0ebSPyun YongHyeon (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) { 219581eee0ebSPyun YongHyeon /* 219681eee0ebSPyun YongHyeon * RTL8168C or later controllers do not 219781eee0ebSPyun YongHyeon * support multi-fragment packet. 219881eee0ebSPyun YongHyeon */ 219981eee0ebSPyun YongHyeon re_discard_rxbuf(sc, i); 220081eee0ebSPyun YongHyeon continue; 220181eee0ebSPyun YongHyeon } else if ((rxstat & RL_RDESC_STAT_EOF) == 0) { 2202d65abd66SPyun YongHyeon if (re_newbuf(sc, i) != 0) { 2203d65abd66SPyun YongHyeon /* 2204d65abd66SPyun YongHyeon * If this is part of a multi-fragment packet, 2205d65abd66SPyun YongHyeon * discard all the pieces. 2206d65abd66SPyun YongHyeon */ 2207d65abd66SPyun YongHyeon if (sc->rl_head != NULL) { 2208d65abd66SPyun YongHyeon m_freem(sc->rl_head); 2209d65abd66SPyun YongHyeon sc->rl_head = sc->rl_tail = NULL; 2210d65abd66SPyun YongHyeon } 2211d65abd66SPyun YongHyeon re_discard_rxbuf(sc, i); 2212d65abd66SPyun YongHyeon continue; 2213d65abd66SPyun YongHyeon } 221422a11c96SJohn-Mark Gurney m->m_len = RE_RX_DESC_BUFLEN; 2215a94100faSBill Paul if (sc->rl_head == NULL) 2216a94100faSBill Paul sc->rl_head = sc->rl_tail = m; 2217a94100faSBill Paul else { 2218a94100faSBill Paul m->m_flags &= ~M_PKTHDR; 2219a94100faSBill Paul sc->rl_tail->m_next = m; 2220a94100faSBill Paul sc->rl_tail = m; 2221a94100faSBill Paul } 2222a94100faSBill Paul continue; 2223a94100faSBill Paul } 2224a94100faSBill Paul 2225a94100faSBill Paul /* 2226a94100faSBill Paul * NOTE: for the 8139C+, the frame length field 2227a94100faSBill Paul * is always 12 bits in size, but for the gigE chips, 2228a94100faSBill Paul * it is 13 bits (since the max RX frame length is 16K). 2229a94100faSBill Paul * Unfortunately, all 32 bits in the status word 2230a94100faSBill Paul * were already used, so to make room for the extra 2231a94100faSBill Paul * length bit, RealTek took out the 'frame alignment 2232a94100faSBill Paul * error' bit and shifted the other status bits 2233a94100faSBill Paul * over one slot. The OWN, EOR, FS and LS bits are 2234a94100faSBill Paul * still in the same places. We have already extracted 2235a94100faSBill Paul * the frame length and checked the OWN bit, so rather 2236a94100faSBill Paul * than using an alternate bit mapping, we shift the 2237a94100faSBill Paul * status bits one space to the right so we can evaluate 2238a94100faSBill Paul * them using the 8169 status as though it was in the 2239a94100faSBill Paul * same format as that of the 8139C+. 2240a94100faSBill Paul */ 2241a94100faSBill Paul if (sc->rl_type == RL_8169) 2242a94100faSBill Paul rxstat >>= 1; 2243a94100faSBill Paul 224422a11c96SJohn-Mark Gurney /* 224522a11c96SJohn-Mark Gurney * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be 224622a11c96SJohn-Mark Gurney * set, but if CRC is clear, it will still be a valid frame. 224722a11c96SJohn-Mark Gurney */ 224881eee0ebSPyun YongHyeon if ((rxstat & RL_RDESC_STAT_RXERRSUM) != 0) { 224981eee0ebSPyun YongHyeon rxerr = 1; 225081eee0ebSPyun YongHyeon if ((sc->rl_flags & RL_FLAG_JUMBOV2) == 0 && 225181eee0ebSPyun YongHyeon total_len > 8191 && 225281eee0ebSPyun YongHyeon (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT) 225381eee0ebSPyun YongHyeon rxerr = 0; 225481eee0ebSPyun YongHyeon if (rxerr != 0) { 2255c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 2256a94100faSBill Paul /* 2257a94100faSBill Paul * If this is part of a multi-fragment packet, 2258a94100faSBill Paul * discard all the pieces. 2259a94100faSBill Paul */ 2260a94100faSBill Paul if (sc->rl_head != NULL) { 2261a94100faSBill Paul m_freem(sc->rl_head); 2262a94100faSBill Paul sc->rl_head = sc->rl_tail = NULL; 2263a94100faSBill Paul } 2264d65abd66SPyun YongHyeon re_discard_rxbuf(sc, i); 2265a94100faSBill Paul continue; 2266a94100faSBill Paul } 226781eee0ebSPyun YongHyeon } 2268a94100faSBill Paul 2269a94100faSBill Paul /* 2270a94100faSBill Paul * If allocating a replacement mbuf fails, 2271a94100faSBill Paul * reload the current one. 2272a94100faSBill Paul */ 227381eee0ebSPyun YongHyeon if (jumbo != 0) 227481eee0ebSPyun YongHyeon rxerr = re_jumbo_newbuf(sc, i); 227581eee0ebSPyun YongHyeon else 227681eee0ebSPyun YongHyeon rxerr = re_newbuf(sc, i); 227781eee0ebSPyun YongHyeon if (rxerr != 0) { 2278c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1); 2279a94100faSBill Paul if (sc->rl_head != NULL) { 2280a94100faSBill Paul m_freem(sc->rl_head); 2281a94100faSBill Paul sc->rl_head = sc->rl_tail = NULL; 2282a94100faSBill Paul } 2283d65abd66SPyun YongHyeon re_discard_rxbuf(sc, i); 2284a94100faSBill Paul continue; 2285a94100faSBill Paul } 2286a94100faSBill Paul 2287a94100faSBill Paul if (sc->rl_head != NULL) { 228881eee0ebSPyun YongHyeon if (jumbo != 0) 228981eee0ebSPyun YongHyeon m->m_len = total_len; 229081eee0ebSPyun YongHyeon else { 229122a11c96SJohn-Mark Gurney m->m_len = total_len % RE_RX_DESC_BUFLEN; 229222a11c96SJohn-Mark Gurney if (m->m_len == 0) 229322a11c96SJohn-Mark Gurney m->m_len = RE_RX_DESC_BUFLEN; 229481eee0ebSPyun YongHyeon } 2295a94100faSBill Paul /* 2296a94100faSBill Paul * Special case: if there's 4 bytes or less 2297a94100faSBill Paul * in this buffer, the mbuf can be discarded: 2298a94100faSBill Paul * the last 4 bytes is the CRC, which we don't 2299a94100faSBill Paul * care about anyway. 2300a94100faSBill Paul */ 2301a94100faSBill Paul if (m->m_len <= ETHER_CRC_LEN) { 2302a94100faSBill Paul sc->rl_tail->m_len -= 2303a94100faSBill Paul (ETHER_CRC_LEN - m->m_len); 2304a94100faSBill Paul m_freem(m); 2305a94100faSBill Paul } else { 2306a94100faSBill Paul m->m_len -= ETHER_CRC_LEN; 2307a94100faSBill Paul m->m_flags &= ~M_PKTHDR; 2308a94100faSBill Paul sc->rl_tail->m_next = m; 2309a94100faSBill Paul } 2310a94100faSBill Paul m = sc->rl_head; 2311a94100faSBill Paul sc->rl_head = sc->rl_tail = NULL; 2312a94100faSBill Paul m->m_pkthdr.len = total_len - ETHER_CRC_LEN; 2313a94100faSBill Paul } else 2314a94100faSBill Paul m->m_pkthdr.len = m->m_len = 2315a94100faSBill Paul (total_len - ETHER_CRC_LEN); 2316a94100faSBill Paul 231722a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX 231822a11c96SJohn-Mark Gurney re_fixup_rx(m); 231922a11c96SJohn-Mark Gurney #endif 2320c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 2321a94100faSBill Paul m->m_pkthdr.rcvif = ifp; 2322a94100faSBill Paul 2323a94100faSBill Paul /* Do RX checksumming if enabled */ 2324a94100faSBill Paul 2325a94100faSBill Paul if (ifp->if_capenable & IFCAP_RXCSUM) { 2326deb5c680SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) { 2327a94100faSBill Paul /* Check IP header checksum */ 2328a94100faSBill Paul if (rxstat & RL_RDESC_STAT_PROTOID) 2329deb5c680SPyun YongHyeon m->m_pkthdr.csum_flags |= 2330deb5c680SPyun YongHyeon CSUM_IP_CHECKED; 2331a94100faSBill Paul if (!(rxstat & RL_RDESC_STAT_IPSUMBAD)) 2332deb5c680SPyun YongHyeon m->m_pkthdr.csum_flags |= 2333deb5c680SPyun YongHyeon CSUM_IP_VALID; 2334a94100faSBill Paul 2335a94100faSBill Paul /* Check TCP/UDP checksum */ 2336a94100faSBill Paul if ((RL_TCPPKT(rxstat) && 2337a94100faSBill Paul !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) || 2338a94100faSBill Paul (RL_UDPPKT(rxstat) && 2339a94100faSBill Paul !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) { 2340a94100faSBill Paul m->m_pkthdr.csum_flags |= 2341a94100faSBill Paul CSUM_DATA_VALID|CSUM_PSEUDO_HDR; 2342a94100faSBill Paul m->m_pkthdr.csum_data = 0xffff; 2343a94100faSBill Paul } 2344deb5c680SPyun YongHyeon } else { 2345deb5c680SPyun YongHyeon /* 2346deb5c680SPyun YongHyeon * RTL8168C/RTL816CP/RTL8111C/RTL8111CP 2347deb5c680SPyun YongHyeon */ 2348deb5c680SPyun YongHyeon if ((rxstat & RL_RDESC_STAT_PROTOID) && 2349deb5c680SPyun YongHyeon (rxvlan & RL_RDESC_IPV4)) 2350deb5c680SPyun YongHyeon m->m_pkthdr.csum_flags |= 2351deb5c680SPyun YongHyeon CSUM_IP_CHECKED; 2352deb5c680SPyun YongHyeon if (!(rxstat & RL_RDESC_STAT_IPSUMBAD) && 2353deb5c680SPyun YongHyeon (rxvlan & RL_RDESC_IPV4)) 2354deb5c680SPyun YongHyeon m->m_pkthdr.csum_flags |= 2355deb5c680SPyun YongHyeon CSUM_IP_VALID; 2356deb5c680SPyun YongHyeon if (((rxstat & RL_RDESC_STAT_TCP) && 2357deb5c680SPyun YongHyeon !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) || 2358deb5c680SPyun YongHyeon ((rxstat & RL_RDESC_STAT_UDP) && 2359deb5c680SPyun YongHyeon !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) { 2360deb5c680SPyun YongHyeon m->m_pkthdr.csum_flags |= 2361deb5c680SPyun YongHyeon CSUM_DATA_VALID|CSUM_PSEUDO_HDR; 2362deb5c680SPyun YongHyeon m->m_pkthdr.csum_data = 0xffff; 2363deb5c680SPyun YongHyeon } 2364deb5c680SPyun YongHyeon } 2365a94100faSBill Paul } 2366ed510fb0SBill Paul maxpkt--; 2367d147662cSGleb Smirnoff if (rxvlan & RL_RDESC_VLANCTL_TAG) { 236878ba57b9SAndre Oppermann m->m_pkthdr.ether_vtag = 2369bddff934SPyun YongHyeon bswap16((rxvlan & RL_RDESC_VLANCTL_DATA)); 237078ba57b9SAndre Oppermann m->m_flags |= M_VLANTAG; 2371d147662cSGleb Smirnoff } 23725120abbfSSam Leffler RL_UNLOCK(sc); 2373a94100faSBill Paul (*ifp->if_input)(ifp, m); 23745120abbfSSam Leffler RL_LOCK(sc); 23751abcdbd1SAttilio Rao rx_npkts++; 2376a94100faSBill Paul } 2377a94100faSBill Paul 2378a94100faSBill Paul /* Flush the RX DMA ring */ 2379a94100faSBill Paul 2380a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 2381a94100faSBill Paul sc->rl_ldata.rl_rx_list_map, 2382a94100faSBill Paul BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 2383a94100faSBill Paul 2384a94100faSBill Paul sc->rl_ldata.rl_rx_prodidx = i; 2385ed510fb0SBill Paul 23861abcdbd1SAttilio Rao if (rx_npktsp != NULL) 23871abcdbd1SAttilio Rao *rx_npktsp = rx_npkts; 2388ed510fb0SBill Paul if (maxpkt) 2389ed510fb0SBill Paul return (EAGAIN); 2390ed510fb0SBill Paul 2391ed510fb0SBill Paul return (0); 2392a94100faSBill Paul } 2393a94100faSBill Paul 2394a94100faSBill Paul static void 23957b5ffebfSPyun YongHyeon re_txeof(struct rl_softc *sc) 2396a94100faSBill Paul { 2397a94100faSBill Paul struct ifnet *ifp; 2398d65abd66SPyun YongHyeon struct rl_txdesc *txd; 2399a94100faSBill Paul u_int32_t txstat; 2400d65abd66SPyun YongHyeon int cons; 2401d65abd66SPyun YongHyeon 2402d65abd66SPyun YongHyeon cons = sc->rl_ldata.rl_tx_considx; 2403d65abd66SPyun YongHyeon if (cons == sc->rl_ldata.rl_tx_prodidx) 2404d65abd66SPyun YongHyeon return; 2405a94100faSBill Paul 2406fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 2407579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP 2408ce3ee1e7SLuigi Rizzo if (netmap_tx_irq(ifp, 0)) 2409579a6e3cSLuigi Rizzo return; 2410579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */ 2411a94100faSBill Paul /* Invalidate the TX descriptor list */ 2412a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 2413a94100faSBill Paul sc->rl_ldata.rl_tx_list_map, 2414d65abd66SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 2415a94100faSBill Paul 2416d65abd66SPyun YongHyeon for (; cons != sc->rl_ldata.rl_tx_prodidx; 2417d65abd66SPyun YongHyeon cons = RL_TX_DESC_NXT(sc, cons)) { 2418d65abd66SPyun YongHyeon txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat); 2419d65abd66SPyun YongHyeon if (txstat & RL_TDESC_STAT_OWN) 2420a94100faSBill Paul break; 2421a94100faSBill Paul /* 2422a94100faSBill Paul * We only stash mbufs in the last descriptor 2423a94100faSBill Paul * in a fragment chain, which also happens to 2424a94100faSBill Paul * be the only place where the TX status bits 2425a94100faSBill Paul * are valid. 2426a94100faSBill Paul */ 2427a94100faSBill Paul if (txstat & RL_TDESC_CMD_EOF) { 2428d65abd66SPyun YongHyeon txd = &sc->rl_ldata.rl_tx_desc[cons]; 2429d65abd66SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, 2430d65abd66SPyun YongHyeon txd->tx_dmamap, BUS_DMASYNC_POSTWRITE); 2431d65abd66SPyun YongHyeon bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, 2432d65abd66SPyun YongHyeon txd->tx_dmamap); 2433d65abd66SPyun YongHyeon KASSERT(txd->tx_m != NULL, 2434d65abd66SPyun YongHyeon ("%s: freeing NULL mbufs!", __func__)); 2435d65abd66SPyun YongHyeon m_freem(txd->tx_m); 2436d65abd66SPyun YongHyeon txd->tx_m = NULL; 2437a94100faSBill Paul if (txstat & (RL_TDESC_STAT_EXCESSCOL| 2438a94100faSBill Paul RL_TDESC_STAT_COLCNT)) 2439c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1); 2440a94100faSBill Paul if (txstat & RL_TDESC_STAT_TXERRSUM) 2441c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 2442a94100faSBill Paul else 2443c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 2444a94100faSBill Paul } 2445a94100faSBill Paul sc->rl_ldata.rl_tx_free++; 2446d65abd66SPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2447a94100faSBill Paul } 2448d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_considx = cons; 2449a94100faSBill Paul 2450a94100faSBill Paul /* No changes made to the TX ring, so no flush needed */ 2451a94100faSBill Paul 2452d65abd66SPyun YongHyeon if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) { 2453ed510fb0SBill Paul #ifdef RE_TX_MODERATION 2454a94100faSBill Paul /* 2455b4b95879SMarius Strobl * If not all descriptors have been reaped yet, reload 2456b4b95879SMarius Strobl * the timer so that we will eventually get another 2457a94100faSBill Paul * interrupt that will cause us to re-enter this routine. 2458a94100faSBill Paul * This is done in case the transmitter has gone idle. 2459a94100faSBill Paul */ 2460a94100faSBill Paul CSR_WRITE_4(sc, RL_TIMERCNT, 1); 2461ed510fb0SBill Paul #endif 2462b4b95879SMarius Strobl } else 2463b4b95879SMarius Strobl sc->rl_watchdog_timer = 0; 2464a94100faSBill Paul } 2465a94100faSBill Paul 2466a94100faSBill Paul static void 24677b5ffebfSPyun YongHyeon re_tick(void *xsc) 2468a94100faSBill Paul { 2469a94100faSBill Paul struct rl_softc *sc; 2470d1754a9bSJohn Baldwin struct mii_data *mii; 2471a94100faSBill Paul 2472a94100faSBill Paul sc = xsc; 247397b9d4baSJohn-Mark Gurney 247497b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 247597b9d4baSJohn-Mark Gurney 24761d545c7aSMarius Strobl mii = device_get_softc(sc->rl_miibus); 2477a94100faSBill Paul mii_tick(mii); 24780fe200d9SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_LINK) == 0) 24790fe200d9SPyun YongHyeon re_miibus_statchg(sc->rl_dev); 2480c2d2e19cSPyun YongHyeon /* 2481c2d2e19cSPyun YongHyeon * Reclaim transmitted frames here. Technically it is not 2482c2d2e19cSPyun YongHyeon * necessary to do here but it ensures periodic reclamation 2483c2d2e19cSPyun YongHyeon * regardless of Tx completion interrupt which seems to be 2484c2d2e19cSPyun YongHyeon * lost on PCIe based controllers under certain situations. 2485c2d2e19cSPyun YongHyeon */ 2486c2d2e19cSPyun YongHyeon re_txeof(sc); 2487130b6dfbSPyun YongHyeon re_watchdog(sc); 2488d1754a9bSJohn Baldwin callout_reset(&sc->rl_stat_callout, hz, re_tick, sc); 2489a94100faSBill Paul } 2490a94100faSBill Paul 2491a94100faSBill Paul #ifdef DEVICE_POLLING 24921abcdbd1SAttilio Rao static int 2493a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 2494a94100faSBill Paul { 2495a94100faSBill Paul struct rl_softc *sc = ifp->if_softc; 24961abcdbd1SAttilio Rao int rx_npkts = 0; 2497a94100faSBill Paul 2498a94100faSBill Paul RL_LOCK(sc); 249940929967SGleb Smirnoff if (ifp->if_drv_flags & IFF_DRV_RUNNING) 25001abcdbd1SAttilio Rao rx_npkts = re_poll_locked(ifp, cmd, count); 250197b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 25021abcdbd1SAttilio Rao return (rx_npkts); 250397b9d4baSJohn-Mark Gurney } 250497b9d4baSJohn-Mark Gurney 25051abcdbd1SAttilio Rao static int 250697b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count) 250797b9d4baSJohn-Mark Gurney { 250897b9d4baSJohn-Mark Gurney struct rl_softc *sc = ifp->if_softc; 25091abcdbd1SAttilio Rao int rx_npkts; 251097b9d4baSJohn-Mark Gurney 251197b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 251297b9d4baSJohn-Mark Gurney 2513a94100faSBill Paul sc->rxcycles = count; 25141abcdbd1SAttilio Rao re_rxeof(sc, &rx_npkts); 2515a94100faSBill Paul re_txeof(sc); 2516a94100faSBill Paul 251737652939SMax Laier if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2518d180a66fSPyun YongHyeon re_start_locked(ifp); 2519a94100faSBill Paul 2520a94100faSBill Paul if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */ 2521a94100faSBill Paul u_int16_t status; 2522a94100faSBill Paul 2523a94100faSBill Paul status = CSR_READ_2(sc, RL_ISR); 2524a94100faSBill Paul if (status == 0xffff) 25251abcdbd1SAttilio Rao return (rx_npkts); 2526a94100faSBill Paul if (status) 2527a94100faSBill Paul CSR_WRITE_2(sc, RL_ISR, status); 2528818951afSPyun YongHyeon if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) && 2529818951afSPyun YongHyeon (sc->rl_flags & RL_FLAG_PCIE)) 2530818951afSPyun YongHyeon CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 2531a94100faSBill Paul 2532a94100faSBill Paul /* 2533a94100faSBill Paul * XXX check behaviour on receiver stalls. 2534a94100faSBill Paul */ 2535a94100faSBill Paul 25368476c243SPyun YongHyeon if (status & RL_ISR_SYSTEM_ERR) { 25378476c243SPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 253897b9d4baSJohn-Mark Gurney re_init_locked(sc); 2539a94100faSBill Paul } 25408476c243SPyun YongHyeon } 25411abcdbd1SAttilio Rao return (rx_npkts); 2542a94100faSBill Paul } 2543a94100faSBill Paul #endif /* DEVICE_POLLING */ 2544a94100faSBill Paul 2545ef544f63SPaolo Pisati static int 25467b5ffebfSPyun YongHyeon re_intr(void *arg) 2547a94100faSBill Paul { 2548a94100faSBill Paul struct rl_softc *sc; 2549ed510fb0SBill Paul uint16_t status; 2550a94100faSBill Paul 2551a94100faSBill Paul sc = arg; 2552ed510fb0SBill Paul 2553ed510fb0SBill Paul status = CSR_READ_2(sc, RL_ISR); 2554498bd0d3SBill Paul if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0) 2555ef544f63SPaolo Pisati return (FILTER_STRAY); 2556ed510fb0SBill Paul CSR_WRITE_2(sc, RL_IMR, 0); 2557ed510fb0SBill Paul 2558cbc4d2dbSJohn Baldwin taskqueue_enqueue(taskqueue_fast, &sc->rl_inttask); 2559ed510fb0SBill Paul 2560ef544f63SPaolo Pisati return (FILTER_HANDLED); 2561ed510fb0SBill Paul } 2562ed510fb0SBill Paul 2563ed510fb0SBill Paul static void 25647b5ffebfSPyun YongHyeon re_int_task(void *arg, int npending) 2565ed510fb0SBill Paul { 2566ed510fb0SBill Paul struct rl_softc *sc; 2567ed510fb0SBill Paul struct ifnet *ifp; 2568ed510fb0SBill Paul u_int16_t status; 2569ed510fb0SBill Paul int rval = 0; 2570ed510fb0SBill Paul 2571ed510fb0SBill Paul sc = arg; 2572ed510fb0SBill Paul ifp = sc->rl_ifp; 2573a94100faSBill Paul 2574a94100faSBill Paul RL_LOCK(sc); 257597b9d4baSJohn-Mark Gurney 2576a94100faSBill Paul status = CSR_READ_2(sc, RL_ISR); 2577a94100faSBill Paul CSR_WRITE_2(sc, RL_ISR, status); 2578a94100faSBill Paul 2579d65abd66SPyun YongHyeon if (sc->suspended || 2580d65abd66SPyun YongHyeon (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 2581ed510fb0SBill Paul RL_UNLOCK(sc); 2582ed510fb0SBill Paul return; 2583ed510fb0SBill Paul } 2584a94100faSBill Paul 2585ed510fb0SBill Paul #ifdef DEVICE_POLLING 2586ed510fb0SBill Paul if (ifp->if_capenable & IFCAP_POLLING) { 2587ed510fb0SBill Paul RL_UNLOCK(sc); 2588ed510fb0SBill Paul return; 2589ed510fb0SBill Paul } 2590ed510fb0SBill Paul #endif 2591a94100faSBill Paul 2592ed510fb0SBill Paul if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW)) 25931abcdbd1SAttilio Rao rval = re_rxeof(sc, NULL); 2594ed510fb0SBill Paul 2595818951afSPyun YongHyeon /* 2596818951afSPyun YongHyeon * Some chips will ignore a second TX request issued 2597818951afSPyun YongHyeon * while an existing transmission is in progress. If 2598818951afSPyun YongHyeon * the transmitter goes idle but there are still 2599818951afSPyun YongHyeon * packets waiting to be sent, we need to restart the 2600818951afSPyun YongHyeon * channel here to flush them out. This only seems to 2601818951afSPyun YongHyeon * be required with the PCIe devices. 2602818951afSPyun YongHyeon */ 2603818951afSPyun YongHyeon if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) && 2604818951afSPyun YongHyeon (sc->rl_flags & RL_FLAG_PCIE)) 2605818951afSPyun YongHyeon CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 26063d85c23dSPyun YongHyeon if (status & ( 2607ed510fb0SBill Paul #ifdef RE_TX_MODERATION 26083d85c23dSPyun YongHyeon RL_ISR_TIMEOUT_EXPIRED| 2609ed510fb0SBill Paul #else 26103d85c23dSPyun YongHyeon RL_ISR_TX_OK| 2611ed510fb0SBill Paul #endif 2612ed510fb0SBill Paul RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL)) 2613a94100faSBill Paul re_txeof(sc); 2614a94100faSBill Paul 26158476c243SPyun YongHyeon if (status & RL_ISR_SYSTEM_ERR) { 26168476c243SPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 261797b9d4baSJohn-Mark Gurney re_init_locked(sc); 26188476c243SPyun YongHyeon } 2619a94100faSBill Paul 262052732175SMax Laier if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2621d180a66fSPyun YongHyeon re_start_locked(ifp); 2622a94100faSBill Paul 2623a94100faSBill Paul RL_UNLOCK(sc); 2624ed510fb0SBill Paul 2625ed510fb0SBill Paul if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) { 2626cbc4d2dbSJohn Baldwin taskqueue_enqueue(taskqueue_fast, &sc->rl_inttask); 2627ed510fb0SBill Paul return; 2628ed510fb0SBill Paul } 2629ed510fb0SBill Paul 2630ed510fb0SBill Paul CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS); 2631a94100faSBill Paul } 2632a94100faSBill Paul 2633502be0f7SPyun YongHyeon static void 2634502be0f7SPyun YongHyeon re_intr_msi(void *xsc) 2635502be0f7SPyun YongHyeon { 2636502be0f7SPyun YongHyeon struct rl_softc *sc; 2637502be0f7SPyun YongHyeon struct ifnet *ifp; 2638502be0f7SPyun YongHyeon uint16_t intrs, status; 2639502be0f7SPyun YongHyeon 2640502be0f7SPyun YongHyeon sc = xsc; 2641502be0f7SPyun YongHyeon RL_LOCK(sc); 2642502be0f7SPyun YongHyeon 2643502be0f7SPyun YongHyeon ifp = sc->rl_ifp; 2644502be0f7SPyun YongHyeon #ifdef DEVICE_POLLING 2645502be0f7SPyun YongHyeon if (ifp->if_capenable & IFCAP_POLLING) { 2646502be0f7SPyun YongHyeon RL_UNLOCK(sc); 2647502be0f7SPyun YongHyeon return; 2648502be0f7SPyun YongHyeon } 2649502be0f7SPyun YongHyeon #endif 2650502be0f7SPyun YongHyeon /* Disable interrupts. */ 2651502be0f7SPyun YongHyeon CSR_WRITE_2(sc, RL_IMR, 0); 2652502be0f7SPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 2653502be0f7SPyun YongHyeon RL_UNLOCK(sc); 2654502be0f7SPyun YongHyeon return; 2655502be0f7SPyun YongHyeon } 2656502be0f7SPyun YongHyeon 2657502be0f7SPyun YongHyeon intrs = RL_INTRS_CPLUS; 2658502be0f7SPyun YongHyeon status = CSR_READ_2(sc, RL_ISR); 2659502be0f7SPyun YongHyeon CSR_WRITE_2(sc, RL_ISR, status); 2660502be0f7SPyun YongHyeon if (sc->rl_int_rx_act > 0) { 2661502be0f7SPyun YongHyeon intrs &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | RL_ISR_FIFO_OFLOW | 2662502be0f7SPyun YongHyeon RL_ISR_RX_OVERRUN); 2663502be0f7SPyun YongHyeon status &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | RL_ISR_FIFO_OFLOW | 2664502be0f7SPyun YongHyeon RL_ISR_RX_OVERRUN); 2665502be0f7SPyun YongHyeon } 2666502be0f7SPyun YongHyeon 2667502be0f7SPyun YongHyeon if (status & (RL_ISR_TIMEOUT_EXPIRED | RL_ISR_RX_OK | RL_ISR_RX_ERR | 2668502be0f7SPyun YongHyeon RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN)) { 2669502be0f7SPyun YongHyeon re_rxeof(sc, NULL); 2670502be0f7SPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) { 2671502be0f7SPyun YongHyeon if (sc->rl_int_rx_mod != 0 && 2672502be0f7SPyun YongHyeon (status & (RL_ISR_RX_OK | RL_ISR_RX_ERR | 2673502be0f7SPyun YongHyeon RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN)) != 0) { 2674502be0f7SPyun YongHyeon /* Rearm one-shot timer. */ 2675502be0f7SPyun YongHyeon CSR_WRITE_4(sc, RL_TIMERCNT, 1); 2676502be0f7SPyun YongHyeon intrs &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | 2677502be0f7SPyun YongHyeon RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN); 2678502be0f7SPyun YongHyeon sc->rl_int_rx_act = 1; 2679502be0f7SPyun YongHyeon } else { 2680502be0f7SPyun YongHyeon intrs |= RL_ISR_RX_OK | RL_ISR_RX_ERR | 2681502be0f7SPyun YongHyeon RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN; 2682502be0f7SPyun YongHyeon sc->rl_int_rx_act = 0; 2683502be0f7SPyun YongHyeon } 2684502be0f7SPyun YongHyeon } 2685502be0f7SPyun YongHyeon } 2686502be0f7SPyun YongHyeon 2687502be0f7SPyun YongHyeon /* 2688502be0f7SPyun YongHyeon * Some chips will ignore a second TX request issued 2689502be0f7SPyun YongHyeon * while an existing transmission is in progress. If 2690502be0f7SPyun YongHyeon * the transmitter goes idle but there are still 2691502be0f7SPyun YongHyeon * packets waiting to be sent, we need to restart the 2692502be0f7SPyun YongHyeon * channel here to flush them out. This only seems to 2693502be0f7SPyun YongHyeon * be required with the PCIe devices. 2694502be0f7SPyun YongHyeon */ 2695502be0f7SPyun YongHyeon if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) && 2696502be0f7SPyun YongHyeon (sc->rl_flags & RL_FLAG_PCIE)) 2697502be0f7SPyun YongHyeon CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 2698502be0f7SPyun YongHyeon if (status & (RL_ISR_TX_OK | RL_ISR_TX_ERR | RL_ISR_TX_DESC_UNAVAIL)) 2699502be0f7SPyun YongHyeon re_txeof(sc); 2700502be0f7SPyun YongHyeon 2701502be0f7SPyun YongHyeon if (status & RL_ISR_SYSTEM_ERR) { 2702502be0f7SPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2703502be0f7SPyun YongHyeon re_init_locked(sc); 2704502be0f7SPyun YongHyeon } 2705502be0f7SPyun YongHyeon 2706502be0f7SPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) { 2707502be0f7SPyun YongHyeon if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2708502be0f7SPyun YongHyeon re_start_locked(ifp); 2709502be0f7SPyun YongHyeon CSR_WRITE_2(sc, RL_IMR, intrs); 2710502be0f7SPyun YongHyeon } 2711502be0f7SPyun YongHyeon RL_UNLOCK(sc); 2712502be0f7SPyun YongHyeon } 2713502be0f7SPyun YongHyeon 2714d65abd66SPyun YongHyeon static int 27157b5ffebfSPyun YongHyeon re_encap(struct rl_softc *sc, struct mbuf **m_head) 2716d65abd66SPyun YongHyeon { 2717d65abd66SPyun YongHyeon struct rl_txdesc *txd, *txd_last; 2718d65abd66SPyun YongHyeon bus_dma_segment_t segs[RL_NTXSEGS]; 2719d65abd66SPyun YongHyeon bus_dmamap_t map; 2720d65abd66SPyun YongHyeon struct mbuf *m_new; 2721d65abd66SPyun YongHyeon struct rl_desc *desc; 2722d65abd66SPyun YongHyeon int nsegs, prod; 2723d65abd66SPyun YongHyeon int i, error, ei, si; 2724d65abd66SPyun YongHyeon int padlen; 2725ccf34c81SPyun YongHyeon uint32_t cmdstat, csum_flags, vlanctl; 2726a94100faSBill Paul 2727d65abd66SPyun YongHyeon RL_LOCK_ASSERT(sc); 2728738489d1SPyun YongHyeon M_ASSERTPKTHDR((*m_head)); 27290fc4974fSBill Paul 27300fc4974fSBill Paul /* 27310fc4974fSBill Paul * With some of the RealTek chips, using the checksum offload 27320fc4974fSBill Paul * support in conjunction with the autopadding feature results 27330fc4974fSBill Paul * in the transmission of corrupt frames. For example, if we 27340fc4974fSBill Paul * need to send a really small IP fragment that's less than 60 27350fc4974fSBill Paul * bytes in size, and IP header checksumming is enabled, the 27360fc4974fSBill Paul * resulting ethernet frame that appears on the wire will 273799c8ae87SPyun YongHyeon * have garbled payload. To work around this, if TX IP checksum 27380fc4974fSBill Paul * offload is enabled, we always manually pad short frames out 2739d65abd66SPyun YongHyeon * to the minimum ethernet frame size. 27400fc4974fSBill Paul */ 2741f2e491c9SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_AUTOPAD) == 0 && 2742deb5c680SPyun YongHyeon (*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN && 274399c8ae87SPyun YongHyeon ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) { 2744d65abd66SPyun YongHyeon padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len; 2745d65abd66SPyun YongHyeon if (M_WRITABLE(*m_head) == 0) { 2746d65abd66SPyun YongHyeon /* Get a writable copy. */ 2747c6499eccSGleb Smirnoff m_new = m_dup(*m_head, M_NOWAIT); 2748d65abd66SPyun YongHyeon m_freem(*m_head); 2749d65abd66SPyun YongHyeon if (m_new == NULL) { 2750d65abd66SPyun YongHyeon *m_head = NULL; 2751a94100faSBill Paul return (ENOBUFS); 2752a94100faSBill Paul } 2753d65abd66SPyun YongHyeon *m_head = m_new; 2754d65abd66SPyun YongHyeon } 2755d65abd66SPyun YongHyeon if ((*m_head)->m_next != NULL || 2756d65abd66SPyun YongHyeon M_TRAILINGSPACE(*m_head) < padlen) { 2757c6499eccSGleb Smirnoff m_new = m_defrag(*m_head, M_NOWAIT); 2758b4b95879SMarius Strobl if (m_new == NULL) { 2759b4b95879SMarius Strobl m_freem(*m_head); 2760b4b95879SMarius Strobl *m_head = NULL; 276180a2a305SJohn-Mark Gurney return (ENOBUFS); 2762b4b95879SMarius Strobl } 2763d65abd66SPyun YongHyeon } else 2764d65abd66SPyun YongHyeon m_new = *m_head; 2765a94100faSBill Paul 27660fc4974fSBill Paul /* 27670fc4974fSBill Paul * Manually pad short frames, and zero the pad space 27680fc4974fSBill Paul * to avoid leaking data. 27690fc4974fSBill Paul */ 2770d65abd66SPyun YongHyeon bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen); 2771d65abd66SPyun YongHyeon m_new->m_pkthdr.len += padlen; 27720fc4974fSBill Paul m_new->m_len = m_new->m_pkthdr.len; 2773d65abd66SPyun YongHyeon *m_head = m_new; 27740fc4974fSBill Paul } 27750fc4974fSBill Paul 2776d65abd66SPyun YongHyeon prod = sc->rl_ldata.rl_tx_prodidx; 2777d65abd66SPyun YongHyeon txd = &sc->rl_ldata.rl_tx_desc[prod]; 2778d65abd66SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap, 2779d65abd66SPyun YongHyeon *m_head, segs, &nsegs, BUS_DMA_NOWAIT); 2780d65abd66SPyun YongHyeon if (error == EFBIG) { 2781c6499eccSGleb Smirnoff m_new = m_collapse(*m_head, M_NOWAIT, RL_NTXSEGS); 2782d65abd66SPyun YongHyeon if (m_new == NULL) { 2783d65abd66SPyun YongHyeon m_freem(*m_head); 2784b4b95879SMarius Strobl *m_head = NULL; 2785d65abd66SPyun YongHyeon return (ENOBUFS); 2786a94100faSBill Paul } 2787d65abd66SPyun YongHyeon *m_head = m_new; 2788d65abd66SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, 2789d65abd66SPyun YongHyeon txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT); 2790d65abd66SPyun YongHyeon if (error != 0) { 2791d65abd66SPyun YongHyeon m_freem(*m_head); 2792d65abd66SPyun YongHyeon *m_head = NULL; 2793d65abd66SPyun YongHyeon return (error); 2794a94100faSBill Paul } 2795d65abd66SPyun YongHyeon } else if (error != 0) 2796d65abd66SPyun YongHyeon return (error); 2797d65abd66SPyun YongHyeon if (nsegs == 0) { 2798d65abd66SPyun YongHyeon m_freem(*m_head); 2799d65abd66SPyun YongHyeon *m_head = NULL; 2800d65abd66SPyun YongHyeon return (EIO); 2801d65abd66SPyun YongHyeon } 2802d65abd66SPyun YongHyeon 2803d65abd66SPyun YongHyeon /* Check for number of available descriptors. */ 2804d65abd66SPyun YongHyeon if (sc->rl_ldata.rl_tx_free - nsegs <= 1) { 2805d65abd66SPyun YongHyeon bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap); 2806d65abd66SPyun YongHyeon return (ENOBUFS); 2807d65abd66SPyun YongHyeon } 2808d65abd66SPyun YongHyeon 2809d65abd66SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap, 2810d65abd66SPyun YongHyeon BUS_DMASYNC_PREWRITE); 2811a94100faSBill Paul 2812a94100faSBill Paul /* 2813d65abd66SPyun YongHyeon * Set up checksum offload. Note: checksum offload bits must 2814d65abd66SPyun YongHyeon * appear in all descriptors of a multi-descriptor transmit 2815d65abd66SPyun YongHyeon * attempt. This is according to testing done with an 8169 2816d65abd66SPyun YongHyeon * chip. This is a requirement. 2817a94100faSBill Paul */ 2818deb5c680SPyun YongHyeon vlanctl = 0; 2819d65abd66SPyun YongHyeon csum_flags = 0; 2820d6d7d923SPyun YongHyeon if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0) { 2821d6d7d923SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_DESCV2) != 0) { 2822d6d7d923SPyun YongHyeon csum_flags |= RL_TDESC_CMD_LGSEND; 2823d6d7d923SPyun YongHyeon vlanctl |= ((uint32_t)(*m_head)->m_pkthdr.tso_segsz << 2824d6d7d923SPyun YongHyeon RL_TDESC_CMD_MSSVALV2_SHIFT); 2825d6d7d923SPyun YongHyeon } else { 2826d6d7d923SPyun YongHyeon csum_flags |= RL_TDESC_CMD_LGSEND | 2827d65abd66SPyun YongHyeon ((uint32_t)(*m_head)->m_pkthdr.tso_segsz << 2828d65abd66SPyun YongHyeon RL_TDESC_CMD_MSSVAL_SHIFT); 2829d6d7d923SPyun YongHyeon } 2830d6d7d923SPyun YongHyeon } else { 283199c8ae87SPyun YongHyeon /* 283299c8ae87SPyun YongHyeon * Unconditionally enable IP checksum if TCP or UDP 283399c8ae87SPyun YongHyeon * checksum is required. Otherwise, TCP/UDP checksum 28342df05392SSergey Kandaurov * doesn't make effects. 283599c8ae87SPyun YongHyeon */ 283699c8ae87SPyun YongHyeon if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) { 2837deb5c680SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) { 2838d65abd66SPyun YongHyeon csum_flags |= RL_TDESC_CMD_IPCSUM; 2839deb5c680SPyun YongHyeon if (((*m_head)->m_pkthdr.csum_flags & 2840deb5c680SPyun YongHyeon CSUM_TCP) != 0) 2841d65abd66SPyun YongHyeon csum_flags |= RL_TDESC_CMD_TCPCSUM; 2842deb5c680SPyun YongHyeon if (((*m_head)->m_pkthdr.csum_flags & 2843deb5c680SPyun YongHyeon CSUM_UDP) != 0) 2844d65abd66SPyun YongHyeon csum_flags |= RL_TDESC_CMD_UDPCSUM; 2845deb5c680SPyun YongHyeon } else { 2846deb5c680SPyun YongHyeon vlanctl |= RL_TDESC_CMD_IPCSUMV2; 2847deb5c680SPyun YongHyeon if (((*m_head)->m_pkthdr.csum_flags & 2848deb5c680SPyun YongHyeon CSUM_TCP) != 0) 2849deb5c680SPyun YongHyeon vlanctl |= RL_TDESC_CMD_TCPCSUMV2; 2850deb5c680SPyun YongHyeon if (((*m_head)->m_pkthdr.csum_flags & 2851deb5c680SPyun YongHyeon CSUM_UDP) != 0) 2852deb5c680SPyun YongHyeon vlanctl |= RL_TDESC_CMD_UDPCSUMV2; 2853deb5c680SPyun YongHyeon } 2854d65abd66SPyun YongHyeon } 285599c8ae87SPyun YongHyeon } 2856a94100faSBill Paul 2857ccf34c81SPyun YongHyeon /* 2858ccf34c81SPyun YongHyeon * Set up hardware VLAN tagging. Note: vlan tag info must 2859ccf34c81SPyun YongHyeon * appear in all descriptors of a multi-descriptor 2860ccf34c81SPyun YongHyeon * transmission attempt. 2861ccf34c81SPyun YongHyeon */ 2862ccf34c81SPyun YongHyeon if ((*m_head)->m_flags & M_VLANTAG) 2863bddff934SPyun YongHyeon vlanctl |= bswap16((*m_head)->m_pkthdr.ether_vtag) | 2864deb5c680SPyun YongHyeon RL_TDESC_VLANCTL_TAG; 2865ccf34c81SPyun YongHyeon 2866d65abd66SPyun YongHyeon si = prod; 2867d65abd66SPyun YongHyeon for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) { 2868d65abd66SPyun YongHyeon desc = &sc->rl_ldata.rl_tx_list[prod]; 2869deb5c680SPyun YongHyeon desc->rl_vlanctl = htole32(vlanctl); 2870d65abd66SPyun YongHyeon desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr)); 2871d65abd66SPyun YongHyeon desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr)); 2872d65abd66SPyun YongHyeon cmdstat = segs[i].ds_len; 2873d65abd66SPyun YongHyeon if (i != 0) 2874d65abd66SPyun YongHyeon cmdstat |= RL_TDESC_CMD_OWN; 2875d65abd66SPyun YongHyeon if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1) 2876d65abd66SPyun YongHyeon cmdstat |= RL_TDESC_CMD_EOR; 2877d65abd66SPyun YongHyeon desc->rl_cmdstat = htole32(cmdstat | csum_flags); 2878d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_free--; 2879d65abd66SPyun YongHyeon } 2880d65abd66SPyun YongHyeon /* Update producer index. */ 2881d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_prodidx = prod; 2882a94100faSBill Paul 2883d65abd66SPyun YongHyeon /* Set EOF on the last descriptor. */ 2884d65abd66SPyun YongHyeon ei = RL_TX_DESC_PRV(sc, prod); 2885d65abd66SPyun YongHyeon desc = &sc->rl_ldata.rl_tx_list[ei]; 2886d65abd66SPyun YongHyeon desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF); 2887d65abd66SPyun YongHyeon 2888d65abd66SPyun YongHyeon desc = &sc->rl_ldata.rl_tx_list[si]; 2889d65abd66SPyun YongHyeon /* Set SOF and transfer ownership of packet to the chip. */ 2890d65abd66SPyun YongHyeon desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF); 2891a94100faSBill Paul 2892d65abd66SPyun YongHyeon /* 2893d65abd66SPyun YongHyeon * Insure that the map for this transmission 2894d65abd66SPyun YongHyeon * is placed at the array index of the last descriptor 2895d65abd66SPyun YongHyeon * in this chain. (Swap last and first dmamaps.) 2896d65abd66SPyun YongHyeon */ 2897d65abd66SPyun YongHyeon txd_last = &sc->rl_ldata.rl_tx_desc[ei]; 2898d65abd66SPyun YongHyeon map = txd->tx_dmamap; 2899d65abd66SPyun YongHyeon txd->tx_dmamap = txd_last->tx_dmamap; 2900d65abd66SPyun YongHyeon txd_last->tx_dmamap = map; 2901d65abd66SPyun YongHyeon txd_last->tx_m = *m_head; 2902a94100faSBill Paul 2903a94100faSBill Paul return (0); 2904a94100faSBill Paul } 2905a94100faSBill Paul 290697b9d4baSJohn-Mark Gurney static void 2907d180a66fSPyun YongHyeon re_start(struct ifnet *ifp) 290897b9d4baSJohn-Mark Gurney { 2909d180a66fSPyun YongHyeon struct rl_softc *sc; 291097b9d4baSJohn-Mark Gurney 2911d180a66fSPyun YongHyeon sc = ifp->if_softc; 2912d180a66fSPyun YongHyeon RL_LOCK(sc); 2913d180a66fSPyun YongHyeon re_start_locked(ifp); 2914d180a66fSPyun YongHyeon RL_UNLOCK(sc); 291597b9d4baSJohn-Mark Gurney } 291697b9d4baSJohn-Mark Gurney 2917a94100faSBill Paul /* 2918a94100faSBill Paul * Main transmit routine for C+ and gigE NICs. 2919a94100faSBill Paul */ 2920a94100faSBill Paul static void 2921d180a66fSPyun YongHyeon re_start_locked(struct ifnet *ifp) 2922a94100faSBill Paul { 2923a94100faSBill Paul struct rl_softc *sc; 2924d65abd66SPyun YongHyeon struct mbuf *m_head; 2925d65abd66SPyun YongHyeon int queued; 2926a94100faSBill Paul 2927a94100faSBill Paul sc = ifp->if_softc; 292897b9d4baSJohn-Mark Gurney 2929579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP 2930579a6e3cSLuigi Rizzo /* XXX is this necessary ? */ 2931579a6e3cSLuigi Rizzo if (ifp->if_capenable & IFCAP_NETMAP) { 2932579a6e3cSLuigi Rizzo struct netmap_kring *kring = &NA(ifp)->tx_rings[0]; 2933579a6e3cSLuigi Rizzo if (sc->rl_ldata.rl_tx_prodidx != kring->nr_hwcur) { 2934579a6e3cSLuigi Rizzo /* kick the tx unit */ 2935579a6e3cSLuigi Rizzo CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 2936579a6e3cSLuigi Rizzo #ifdef RE_TX_MODERATION 2937579a6e3cSLuigi Rizzo CSR_WRITE_4(sc, RL_TIMERCNT, 1); 2938579a6e3cSLuigi Rizzo #endif 2939579a6e3cSLuigi Rizzo sc->rl_watchdog_timer = 5; 2940579a6e3cSLuigi Rizzo } 2941579a6e3cSLuigi Rizzo return; 2942579a6e3cSLuigi Rizzo } 2943579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */ 2944e9f8886eSMarius Strobl 2945d65abd66SPyun YongHyeon if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 2946d180a66fSPyun YongHyeon IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0) 2947ed510fb0SBill Paul return; 2948a94100faSBill Paul 2949d65abd66SPyun YongHyeon for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) && 2950d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_free > 1;) { 295152732175SMax Laier IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head); 2952a94100faSBill Paul if (m_head == NULL) 2953a94100faSBill Paul break; 2954a94100faSBill Paul 2955d65abd66SPyun YongHyeon if (re_encap(sc, &m_head) != 0) { 2956b4b95879SMarius Strobl if (m_head == NULL) 2957b4b95879SMarius Strobl break; 295852732175SMax Laier IFQ_DRV_PREPEND(&ifp->if_snd, m_head); 295913f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2960a94100faSBill Paul break; 2961a94100faSBill Paul } 2962a94100faSBill Paul 2963a94100faSBill Paul /* 2964a94100faSBill Paul * If there's a BPF listener, bounce a copy of this frame 2965a94100faSBill Paul * to him. 2966a94100faSBill Paul */ 296759a0d28bSChristian S.J. Peron ETHER_BPF_MTAP(ifp, m_head); 296852732175SMax Laier 296952732175SMax Laier queued++; 2970a94100faSBill Paul } 2971a94100faSBill Paul 2972ed510fb0SBill Paul if (queued == 0) { 2973ed510fb0SBill Paul #ifdef RE_TX_MODERATION 2974d65abd66SPyun YongHyeon if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) 2975ed510fb0SBill Paul CSR_WRITE_4(sc, RL_TIMERCNT, 1); 2976ed510fb0SBill Paul #endif 297752732175SMax Laier return; 2978ed510fb0SBill Paul } 297952732175SMax Laier 2980a94100faSBill Paul /* Flush the TX descriptors */ 2981a94100faSBill Paul 2982a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 2983a94100faSBill Paul sc->rl_ldata.rl_tx_list_map, 2984a94100faSBill Paul BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 2985a94100faSBill Paul 29860fc4974fSBill Paul CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 2987a94100faSBill Paul 2988ed510fb0SBill Paul #ifdef RE_TX_MODERATION 2989a94100faSBill Paul /* 2990a94100faSBill Paul * Use the countdown timer for interrupt moderation. 2991a94100faSBill Paul * 'TX done' interrupts are disabled. Instead, we reset the 2992a94100faSBill Paul * countdown timer, which will begin counting until it hits 2993a94100faSBill Paul * the value in the TIMERINT register, and then trigger an 2994a94100faSBill Paul * interrupt. Each time we write to the TIMERCNT register, 2995a94100faSBill Paul * the timer count is reset to 0. 2996a94100faSBill Paul */ 2997a94100faSBill Paul CSR_WRITE_4(sc, RL_TIMERCNT, 1); 2998ed510fb0SBill Paul #endif 2999a94100faSBill Paul 3000a94100faSBill Paul /* 3001a94100faSBill Paul * Set a timeout in case the chip goes out to lunch. 3002a94100faSBill Paul */ 30031d545c7aSMarius Strobl sc->rl_watchdog_timer = 5; 3004a94100faSBill Paul } 3005a94100faSBill Paul 3006a94100faSBill Paul static void 300781eee0ebSPyun YongHyeon re_set_jumbo(struct rl_softc *sc, int jumbo) 300881eee0ebSPyun YongHyeon { 300981eee0ebSPyun YongHyeon 301081eee0ebSPyun YongHyeon if (sc->rl_hwrev->rl_rev == RL_HWREV_8168E_VL) { 301181eee0ebSPyun YongHyeon pci_set_max_read_req(sc->rl_dev, 4096); 301281eee0ebSPyun YongHyeon return; 301381eee0ebSPyun YongHyeon } 301481eee0ebSPyun YongHyeon 301581eee0ebSPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG); 301681eee0ebSPyun YongHyeon if (jumbo != 0) { 3017e7e7593cSPyun YongHyeon CSR_WRITE_1(sc, sc->rl_cfg3, CSR_READ_1(sc, sc->rl_cfg3) | 301881eee0ebSPyun YongHyeon RL_CFG3_JUMBO_EN0); 301981eee0ebSPyun YongHyeon switch (sc->rl_hwrev->rl_rev) { 302081eee0ebSPyun YongHyeon case RL_HWREV_8168DP: 302181eee0ebSPyun YongHyeon break; 302281eee0ebSPyun YongHyeon case RL_HWREV_8168E: 3023e7e7593cSPyun YongHyeon CSR_WRITE_1(sc, sc->rl_cfg4, 3024e7e7593cSPyun YongHyeon CSR_READ_1(sc, sc->rl_cfg4) | 0x01); 302581eee0ebSPyun YongHyeon break; 302681eee0ebSPyun YongHyeon default: 3027e7e7593cSPyun YongHyeon CSR_WRITE_1(sc, sc->rl_cfg4, 3028e7e7593cSPyun YongHyeon CSR_READ_1(sc, sc->rl_cfg4) | RL_CFG4_JUMBO_EN1); 302981eee0ebSPyun YongHyeon } 303081eee0ebSPyun YongHyeon } else { 3031e7e7593cSPyun YongHyeon CSR_WRITE_1(sc, sc->rl_cfg3, CSR_READ_1(sc, sc->rl_cfg3) & 303281eee0ebSPyun YongHyeon ~RL_CFG3_JUMBO_EN0); 303381eee0ebSPyun YongHyeon switch (sc->rl_hwrev->rl_rev) { 303481eee0ebSPyun YongHyeon case RL_HWREV_8168DP: 303581eee0ebSPyun YongHyeon break; 303681eee0ebSPyun YongHyeon case RL_HWREV_8168E: 3037e7e7593cSPyun YongHyeon CSR_WRITE_1(sc, sc->rl_cfg4, 3038e7e7593cSPyun YongHyeon CSR_READ_1(sc, sc->rl_cfg4) & ~0x01); 303981eee0ebSPyun YongHyeon break; 304081eee0ebSPyun YongHyeon default: 3041e7e7593cSPyun YongHyeon CSR_WRITE_1(sc, sc->rl_cfg4, 3042e7e7593cSPyun YongHyeon CSR_READ_1(sc, sc->rl_cfg4) & ~RL_CFG4_JUMBO_EN1); 304381eee0ebSPyun YongHyeon } 304481eee0ebSPyun YongHyeon } 304581eee0ebSPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 304681eee0ebSPyun YongHyeon 304781eee0ebSPyun YongHyeon switch (sc->rl_hwrev->rl_rev) { 304881eee0ebSPyun YongHyeon case RL_HWREV_8168DP: 304981eee0ebSPyun YongHyeon pci_set_max_read_req(sc->rl_dev, 4096); 305081eee0ebSPyun YongHyeon break; 305181eee0ebSPyun YongHyeon default: 305281eee0ebSPyun YongHyeon if (jumbo != 0) 305381eee0ebSPyun YongHyeon pci_set_max_read_req(sc->rl_dev, 512); 305481eee0ebSPyun YongHyeon else 305581eee0ebSPyun YongHyeon pci_set_max_read_req(sc->rl_dev, 4096); 305681eee0ebSPyun YongHyeon } 305781eee0ebSPyun YongHyeon } 305881eee0ebSPyun YongHyeon 305981eee0ebSPyun YongHyeon static void 30607b5ffebfSPyun YongHyeon re_init(void *xsc) 3061a94100faSBill Paul { 3062a94100faSBill Paul struct rl_softc *sc = xsc; 306397b9d4baSJohn-Mark Gurney 306497b9d4baSJohn-Mark Gurney RL_LOCK(sc); 306597b9d4baSJohn-Mark Gurney re_init_locked(sc); 306697b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 306797b9d4baSJohn-Mark Gurney } 306897b9d4baSJohn-Mark Gurney 306997b9d4baSJohn-Mark Gurney static void 30707b5ffebfSPyun YongHyeon re_init_locked(struct rl_softc *sc) 307197b9d4baSJohn-Mark Gurney { 3072fc74a9f9SBrooks Davis struct ifnet *ifp = sc->rl_ifp; 3073a94100faSBill Paul struct mii_data *mii; 3074566ca8caSJung-uk Kim uint32_t reg; 307570acaecfSPyun YongHyeon uint16_t cfg; 30764d3d7085SBernd Walter union { 30774d3d7085SBernd Walter uint32_t align_dummy; 30784d3d7085SBernd Walter u_char eaddr[ETHER_ADDR_LEN]; 30794d3d7085SBernd Walter } eaddr; 3080a94100faSBill Paul 308197b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 308297b9d4baSJohn-Mark Gurney 3083a94100faSBill Paul mii = device_get_softc(sc->rl_miibus); 3084a94100faSBill Paul 30858476c243SPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 30868476c243SPyun YongHyeon return; 30878476c243SPyun YongHyeon 3088a94100faSBill Paul /* 3089a94100faSBill Paul * Cancel pending I/O and free all RX/TX buffers. 3090a94100faSBill Paul */ 3091a94100faSBill Paul re_stop(sc); 3092a94100faSBill Paul 3093b659f1f0SPyun YongHyeon /* Put controller into known state. */ 3094b659f1f0SPyun YongHyeon re_reset(sc); 3095b659f1f0SPyun YongHyeon 3096a94100faSBill Paul /* 30974a814a5eSPyun YongHyeon * For C+ mode, initialize the RX descriptors and mbufs. 30984a814a5eSPyun YongHyeon */ 309981eee0ebSPyun YongHyeon if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) { 310081eee0ebSPyun YongHyeon if (ifp->if_mtu > RL_MTU) { 310181eee0ebSPyun YongHyeon if (re_jrx_list_init(sc) != 0) { 310281eee0ebSPyun YongHyeon device_printf(sc->rl_dev, 310381eee0ebSPyun YongHyeon "no memory for jumbo RX buffers\n"); 310481eee0ebSPyun YongHyeon re_stop(sc); 310581eee0ebSPyun YongHyeon return; 310681eee0ebSPyun YongHyeon } 310781eee0ebSPyun YongHyeon /* Disable checksum offloading for jumbo frames. */ 310881eee0ebSPyun YongHyeon ifp->if_capenable &= ~(IFCAP_HWCSUM | IFCAP_TSO4); 310981eee0ebSPyun YongHyeon ifp->if_hwassist &= ~(RE_CSUM_FEATURES | CSUM_TSO); 311081eee0ebSPyun YongHyeon } else { 311181eee0ebSPyun YongHyeon if (re_rx_list_init(sc) != 0) { 311281eee0ebSPyun YongHyeon device_printf(sc->rl_dev, 311381eee0ebSPyun YongHyeon "no memory for RX buffers\n"); 311481eee0ebSPyun YongHyeon re_stop(sc); 311581eee0ebSPyun YongHyeon return; 311681eee0ebSPyun YongHyeon } 311781eee0ebSPyun YongHyeon } 311881eee0ebSPyun YongHyeon re_set_jumbo(sc, ifp->if_mtu > RL_MTU); 311981eee0ebSPyun YongHyeon } else { 31204a814a5eSPyun YongHyeon if (re_rx_list_init(sc) != 0) { 31214a814a5eSPyun YongHyeon device_printf(sc->rl_dev, "no memory for RX buffers\n"); 31224a814a5eSPyun YongHyeon re_stop(sc); 31234a814a5eSPyun YongHyeon return; 31244a814a5eSPyun YongHyeon } 312581eee0ebSPyun YongHyeon if ((sc->rl_flags & RL_FLAG_PCIE) != 0 && 312681eee0ebSPyun YongHyeon pci_get_device(sc->rl_dev) != RT_DEVICEID_8101E) { 312781eee0ebSPyun YongHyeon if (ifp->if_mtu > RL_MTU) 312881eee0ebSPyun YongHyeon pci_set_max_read_req(sc->rl_dev, 512); 312981eee0ebSPyun YongHyeon else 313081eee0ebSPyun YongHyeon pci_set_max_read_req(sc->rl_dev, 4096); 313181eee0ebSPyun YongHyeon } 313281eee0ebSPyun YongHyeon } 31334a814a5eSPyun YongHyeon re_tx_list_init(sc); 31344a814a5eSPyun YongHyeon 31354a814a5eSPyun YongHyeon /* 3136c2c6548bSBill Paul * Enable C+ RX and TX mode, as well as VLAN stripping and 3137edd03374SBill Paul * RX checksum offload. We must configure the C+ register 3138c2c6548bSBill Paul * before all others. 3139c2c6548bSBill Paul */ 314070acaecfSPyun YongHyeon cfg = RL_CPLUSCMD_PCI_MRW; 314170acaecfSPyun YongHyeon if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) 314270acaecfSPyun YongHyeon cfg |= RL_CPLUSCMD_RXCSUM_ENB; 314370acaecfSPyun YongHyeon if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) 314470acaecfSPyun YongHyeon cfg |= RL_CPLUSCMD_VLANSTRIP; 3145deb5c680SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_MACSTAT) != 0) { 3146deb5c680SPyun YongHyeon cfg |= RL_CPLUSCMD_MACSTAT_DIS; 3147deb5c680SPyun YongHyeon /* XXX magic. */ 3148deb5c680SPyun YongHyeon cfg |= 0x0001; 3149deb5c680SPyun YongHyeon } else 3150deb5c680SPyun YongHyeon cfg |= RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB; 3151deb5c680SPyun YongHyeon CSR_WRITE_2(sc, RL_CPLUS_CMD, cfg); 315281eee0ebSPyun YongHyeon if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SC || 315381eee0ebSPyun YongHyeon sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE) { 3154566ca8caSJung-uk Kim reg = 0x000fff00; 3155e7e7593cSPyun YongHyeon if ((CSR_READ_1(sc, sc->rl_cfg2) & RL_CFG2_PCI66MHZ) != 0) 3156566ca8caSJung-uk Kim reg |= 0x000000ff; 315781eee0ebSPyun YongHyeon if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE) 3158566ca8caSJung-uk Kim reg |= 0x00f00000; 3159566ca8caSJung-uk Kim CSR_WRITE_4(sc, 0x7c, reg); 3160566ca8caSJung-uk Kim /* Disable interrupt mitigation. */ 3161566ca8caSJung-uk Kim CSR_WRITE_2(sc, 0xe2, 0); 3162566ca8caSJung-uk Kim } 3163ae644087SPyun YongHyeon /* 3164ae644087SPyun YongHyeon * Disable TSO if interface MTU size is greater than MSS 3165ae644087SPyun YongHyeon * allowed in controller. 3166ae644087SPyun YongHyeon */ 3167ae644087SPyun YongHyeon if (ifp->if_mtu > RL_TSO_MTU && (ifp->if_capenable & IFCAP_TSO4) != 0) { 3168ae644087SPyun YongHyeon ifp->if_capenable &= ~IFCAP_TSO4; 3169ae644087SPyun YongHyeon ifp->if_hwassist &= ~CSUM_TSO; 3170ae644087SPyun YongHyeon } 3171c2c6548bSBill Paul 3172c2c6548bSBill Paul /* 3173a94100faSBill Paul * Init our MAC address. Even though the chipset 3174a94100faSBill Paul * documentation doesn't mention it, we need to enter "Config 3175a94100faSBill Paul * register write enable" mode to modify the ID registers. 3176a94100faSBill Paul */ 31774d3d7085SBernd Walter /* Copy MAC address on stack to align. */ 31784d3d7085SBernd Walter bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN); 3179a94100faSBill Paul CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG); 3180ed510fb0SBill Paul CSR_WRITE_4(sc, RL_IDR0, 3181ed510fb0SBill Paul htole32(*(u_int32_t *)(&eaddr.eaddr[0]))); 3182ed510fb0SBill Paul CSR_WRITE_4(sc, RL_IDR4, 3183ed510fb0SBill Paul htole32(*(u_int32_t *)(&eaddr.eaddr[4]))); 3184a94100faSBill Paul CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 3185a94100faSBill Paul 3186a94100faSBill Paul /* 3187d01fac16SPyun YongHyeon * Load the addresses of the RX and TX lists into the chip. 3188d01fac16SPyun YongHyeon */ 3189d01fac16SPyun YongHyeon 3190d01fac16SPyun YongHyeon CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI, 3191d01fac16SPyun YongHyeon RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr)); 3192d01fac16SPyun YongHyeon CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO, 3193d01fac16SPyun YongHyeon RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr)); 3194d01fac16SPyun YongHyeon 3195d01fac16SPyun YongHyeon CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI, 3196d01fac16SPyun YongHyeon RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr)); 3197d01fac16SPyun YongHyeon CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO, 3198d01fac16SPyun YongHyeon RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr)); 3199d01fac16SPyun YongHyeon 320014013280SMarius Strobl if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0) { 320114013280SMarius Strobl /* Disable RXDV gate. */ 3202f1a5f291SMarius Strobl CSR_WRITE_4(sc, RL_MISC, CSR_READ_4(sc, RL_MISC) & 3203f1a5f291SMarius Strobl ~0x00080000); 320414013280SMarius Strobl } 320514013280SMarius Strobl 320614013280SMarius Strobl /* 320714013280SMarius Strobl * Enable transmit and receive for pre-RTL8168G controllers. 320814013280SMarius Strobl * RX/TX MACs should be enabled before RX/TX configuration. 320914013280SMarius Strobl */ 321014013280SMarius Strobl if ((sc->rl_flags & RL_FLAG_8168G_PLUS) == 0) 321114013280SMarius Strobl CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB | RL_CMD_RX_ENB); 3212f1a5f291SMarius Strobl 3213d01fac16SPyun YongHyeon /* 3214ff191365SJung-uk Kim * Set the initial TX configuration. 3215a94100faSBill Paul */ 3216abc8ff44SBill Paul if (sc->rl_testmode) { 3217abc8ff44SBill Paul if (sc->rl_type == RL_8169) 3218abc8ff44SBill Paul CSR_WRITE_4(sc, RL_TXCFG, 3219abc8ff44SBill Paul RL_TXCFG_CONFIG|RL_LOOPTEST_ON); 3220a94100faSBill Paul else 3221abc8ff44SBill Paul CSR_WRITE_4(sc, RL_TXCFG, 3222abc8ff44SBill Paul RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS); 3223abc8ff44SBill Paul } else 3224a94100faSBill Paul CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG); 3225d01fac16SPyun YongHyeon 3226d01fac16SPyun YongHyeon CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16); 3227d01fac16SPyun YongHyeon 3228a94100faSBill Paul /* 3229ff191365SJung-uk Kim * Set the initial RX configuration. 3230a94100faSBill Paul */ 3231ff191365SJung-uk Kim re_set_rxmode(sc); 3232a94100faSBill Paul 3233483cc440SPyun YongHyeon /* Configure interrupt moderation. */ 3234483cc440SPyun YongHyeon if (sc->rl_type == RL_8169) { 3235483cc440SPyun YongHyeon /* Magic from vendor. */ 32365e6906eeSPyun YongHyeon CSR_WRITE_2(sc, RL_INTRMOD, 0x5100); 3237483cc440SPyun YongHyeon } 3238483cc440SPyun YongHyeon 32390f55f9d6SMarius Strobl /* 324014013280SMarius Strobl * Enable transmit and receive for RTL8168G and later controllers. 324114013280SMarius Strobl * RX/TX MACs should be enabled after RX/TX configuration. 32420f55f9d6SMarius Strobl */ 324314013280SMarius Strobl if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0) 32440f55f9d6SMarius Strobl CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB | RL_CMD_RX_ENB); 32450f55f9d6SMarius Strobl 3246a94100faSBill Paul #ifdef DEVICE_POLLING 3247a94100faSBill Paul /* 3248a94100faSBill Paul * Disable interrupts if we are polling. 3249a94100faSBill Paul */ 325040929967SGleb Smirnoff if (ifp->if_capenable & IFCAP_POLLING) 3251a94100faSBill Paul CSR_WRITE_2(sc, RL_IMR, 0); 3252a94100faSBill Paul else /* otherwise ... */ 325340929967SGleb Smirnoff #endif 3254ed510fb0SBill Paul 3255a94100faSBill Paul /* 3256a94100faSBill Paul * Enable interrupts. 3257a94100faSBill Paul */ 3258a94100faSBill Paul if (sc->rl_testmode) 3259a94100faSBill Paul CSR_WRITE_2(sc, RL_IMR, 0); 3260a94100faSBill Paul else 3261a94100faSBill Paul CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS); 3262ed510fb0SBill Paul CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS); 3263a94100faSBill Paul 3264a94100faSBill Paul /* Set initial TX threshold */ 3265a94100faSBill Paul sc->rl_txthresh = RL_TX_THRESH_INIT; 3266a94100faSBill Paul 3267a94100faSBill Paul /* Start RX/TX process. */ 3268a94100faSBill Paul CSR_WRITE_4(sc, RL_MISSEDPKT, 0); 3269a94100faSBill Paul 3270a94100faSBill Paul /* 3271a94100faSBill Paul * Initialize the timer interrupt register so that 3272a94100faSBill Paul * a timer interrupt will be generated once the timer 3273a94100faSBill Paul * reaches a certain number of ticks. The timer is 3274502be0f7SPyun YongHyeon * reloaded on each transmit. 3275502be0f7SPyun YongHyeon */ 3276502be0f7SPyun YongHyeon #ifdef RE_TX_MODERATION 3277502be0f7SPyun YongHyeon /* 3278502be0f7SPyun YongHyeon * Use timer interrupt register to moderate TX interrupt 3279a94100faSBill Paul * moderation, which dramatically improves TX frame rate. 3280a94100faSBill Paul */ 3281a94100faSBill Paul if (sc->rl_type == RL_8169) 3282a94100faSBill Paul CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800); 3283a94100faSBill Paul else 3284a94100faSBill Paul CSR_WRITE_4(sc, RL_TIMERINT, 0x400); 3285502be0f7SPyun YongHyeon #else 3286502be0f7SPyun YongHyeon /* 3287502be0f7SPyun YongHyeon * Use timer interrupt register to moderate RX interrupt 3288502be0f7SPyun YongHyeon * moderation. 3289502be0f7SPyun YongHyeon */ 3290502be0f7SPyun YongHyeon if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0 && 3291502be0f7SPyun YongHyeon intr_filter == 0) { 3292502be0f7SPyun YongHyeon if (sc->rl_type == RL_8169) 3293502be0f7SPyun YongHyeon CSR_WRITE_4(sc, RL_TIMERINT_8169, 3294502be0f7SPyun YongHyeon RL_USECS(sc->rl_int_rx_mod)); 3295502be0f7SPyun YongHyeon } else { 3296502be0f7SPyun YongHyeon if (sc->rl_type == RL_8169) 3297502be0f7SPyun YongHyeon CSR_WRITE_4(sc, RL_TIMERINT_8169, RL_USECS(0)); 3298502be0f7SPyun YongHyeon } 3299ed510fb0SBill Paul #endif 3300a94100faSBill Paul 3301a94100faSBill Paul /* 3302a94100faSBill Paul * For 8169 gigE NICs, set the max allowed RX packet 3303a94100faSBill Paul * size so we can receive jumbo frames. 3304a94100faSBill Paul */ 330589feeee4SPyun YongHyeon if (sc->rl_type == RL_8169) { 330681eee0ebSPyun YongHyeon if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) { 330781eee0ebSPyun YongHyeon /* 330881eee0ebSPyun YongHyeon * For controllers that use new jumbo frame scheme, 33092df05392SSergey Kandaurov * set maximum size of jumbo frame depending on 331081eee0ebSPyun YongHyeon * controller revisions. 331181eee0ebSPyun YongHyeon */ 331281eee0ebSPyun YongHyeon if (ifp->if_mtu > RL_MTU) 331381eee0ebSPyun YongHyeon CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 331481eee0ebSPyun YongHyeon sc->rl_hwrev->rl_max_mtu + 331581eee0ebSPyun YongHyeon ETHER_VLAN_ENCAP_LEN + ETHER_HDR_LEN + 331681eee0ebSPyun YongHyeon ETHER_CRC_LEN); 331789feeee4SPyun YongHyeon else 331881eee0ebSPyun YongHyeon CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 331981eee0ebSPyun YongHyeon RE_RX_DESC_BUFLEN); 332081eee0ebSPyun YongHyeon } else if ((sc->rl_flags & RL_FLAG_PCIE) != 0 && 332181eee0ebSPyun YongHyeon sc->rl_hwrev->rl_max_mtu == RL_MTU) { 332281eee0ebSPyun YongHyeon /* RTL810x has no jumbo frame support. */ 332381eee0ebSPyun YongHyeon CSR_WRITE_2(sc, RL_MAXRXPKTLEN, RE_RX_DESC_BUFLEN); 332481eee0ebSPyun YongHyeon } else 3325a94100faSBill Paul CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383); 332689feeee4SPyun YongHyeon } 3327a94100faSBill Paul 332897b9d4baSJohn-Mark Gurney if (sc->rl_testmode) 3329a94100faSBill Paul return; 3330a94100faSBill Paul 3331e7e7593cSPyun YongHyeon CSR_WRITE_1(sc, sc->rl_cfg1, CSR_READ_1(sc, sc->rl_cfg1) | 3332e7e7593cSPyun YongHyeon RL_CFG1_DRVLOAD); 3333a94100faSBill Paul 333413f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 333513f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 3336a94100faSBill Paul 3337351a76f9SPyun YongHyeon sc->rl_flags &= ~RL_FLAG_LINK; 33381662c49eSPyun YongHyeon mii_mediachg(mii); 33391662c49eSPyun YongHyeon 33401d545c7aSMarius Strobl sc->rl_watchdog_timer = 0; 3341d1754a9bSJohn Baldwin callout_reset(&sc->rl_stat_callout, hz, re_tick, sc); 3342a94100faSBill Paul } 3343a94100faSBill Paul 3344a94100faSBill Paul /* 3345a94100faSBill Paul * Set media options. 3346a94100faSBill Paul */ 3347a94100faSBill Paul static int 33487b5ffebfSPyun YongHyeon re_ifmedia_upd(struct ifnet *ifp) 3349a94100faSBill Paul { 3350a94100faSBill Paul struct rl_softc *sc; 3351a94100faSBill Paul struct mii_data *mii; 33526f0f9b12SPyun YongHyeon int error; 3353a94100faSBill Paul 3354a94100faSBill Paul sc = ifp->if_softc; 3355a94100faSBill Paul mii = device_get_softc(sc->rl_miibus); 3356d1754a9bSJohn Baldwin RL_LOCK(sc); 33576f0f9b12SPyun YongHyeon error = mii_mediachg(mii); 3358d1754a9bSJohn Baldwin RL_UNLOCK(sc); 3359a94100faSBill Paul 33606f0f9b12SPyun YongHyeon return (error); 3361a94100faSBill Paul } 3362a94100faSBill Paul 3363a94100faSBill Paul /* 3364a94100faSBill Paul * Report current media status. 3365a94100faSBill Paul */ 3366a94100faSBill Paul static void 33677b5ffebfSPyun YongHyeon re_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 3368a94100faSBill Paul { 3369a94100faSBill Paul struct rl_softc *sc; 3370a94100faSBill Paul struct mii_data *mii; 3371a94100faSBill Paul 3372a94100faSBill Paul sc = ifp->if_softc; 3373a94100faSBill Paul mii = device_get_softc(sc->rl_miibus); 3374a94100faSBill Paul 3375d1754a9bSJohn Baldwin RL_LOCK(sc); 3376a94100faSBill Paul mii_pollstat(mii); 3377a94100faSBill Paul ifmr->ifm_active = mii->mii_media_active; 3378a94100faSBill Paul ifmr->ifm_status = mii->mii_media_status; 337957c81d92SPyun YongHyeon RL_UNLOCK(sc); 3380a94100faSBill Paul } 3381a94100faSBill Paul 3382a94100faSBill Paul static int 33837b5ffebfSPyun YongHyeon re_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 3384a94100faSBill Paul { 3385a94100faSBill Paul struct rl_softc *sc = ifp->if_softc; 3386a94100faSBill Paul struct ifreq *ifr = (struct ifreq *) data; 3387a94100faSBill Paul struct mii_data *mii; 338840929967SGleb Smirnoff int error = 0; 3389a94100faSBill Paul 3390a94100faSBill Paul switch (command) { 3391a94100faSBill Paul case SIOCSIFMTU: 339281eee0ebSPyun YongHyeon if (ifr->ifr_mtu < ETHERMIN || 3393ab9f923eSPyun YongHyeon ifr->ifr_mtu > sc->rl_hwrev->rl_max_mtu || 3394ab9f923eSPyun YongHyeon ((sc->rl_flags & RL_FLAG_FASTETHER) != 0 && 3395ab9f923eSPyun YongHyeon ifr->ifr_mtu > RL_MTU)) { 3396c1d0b573SPyun YongHyeon error = EINVAL; 3397c1d0b573SPyun YongHyeon break; 3398c1d0b573SPyun YongHyeon } 3399c1d0b573SPyun YongHyeon RL_LOCK(sc); 340081eee0ebSPyun YongHyeon if (ifp->if_mtu != ifr->ifr_mtu) { 3401a94100faSBill Paul ifp->if_mtu = ifr->ifr_mtu; 340281eee0ebSPyun YongHyeon if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 && 340381eee0ebSPyun YongHyeon (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) { 340481eee0ebSPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 340581eee0ebSPyun YongHyeon re_init_locked(sc); 340681eee0ebSPyun YongHyeon } 3407ae644087SPyun YongHyeon if (ifp->if_mtu > RL_TSO_MTU && 3408ae644087SPyun YongHyeon (ifp->if_capenable & IFCAP_TSO4) != 0) { 340981eee0ebSPyun YongHyeon ifp->if_capenable &= ~(IFCAP_TSO4 | 341081eee0ebSPyun YongHyeon IFCAP_VLAN_HWTSO); 3411ae644087SPyun YongHyeon ifp->if_hwassist &= ~CSUM_TSO; 341281eee0ebSPyun YongHyeon } 3413ecafbbb5SPyun YongHyeon VLAN_CAPABILITIES(ifp); 3414ae644087SPyun YongHyeon } 3415d1754a9bSJohn Baldwin RL_UNLOCK(sc); 3416a94100faSBill Paul break; 3417a94100faSBill Paul case SIOCSIFFLAGS: 341897b9d4baSJohn-Mark Gurney RL_LOCK(sc); 3419eed497bbSPyun YongHyeon if ((ifp->if_flags & IFF_UP) != 0) { 3420eed497bbSPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) { 3421eed497bbSPyun YongHyeon if (((ifp->if_flags ^ sc->rl_if_flags) 34223021aef8SPyun YongHyeon & (IFF_PROMISC | IFF_ALLMULTI)) != 0) 3423ff191365SJung-uk Kim re_set_rxmode(sc); 3424eed497bbSPyun YongHyeon } else 342597b9d4baSJohn-Mark Gurney re_init_locked(sc); 3426eed497bbSPyun YongHyeon } else { 3427eed497bbSPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 3428a94100faSBill Paul re_stop(sc); 3429eed497bbSPyun YongHyeon } 3430eed497bbSPyun YongHyeon sc->rl_if_flags = ifp->if_flags; 343197b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 3432a94100faSBill Paul break; 3433a94100faSBill Paul case SIOCADDMULTI: 3434a94100faSBill Paul case SIOCDELMULTI: 343597b9d4baSJohn-Mark Gurney RL_LOCK(sc); 34368476c243SPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 3437ff191365SJung-uk Kim re_set_rxmode(sc); 343897b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 3439a94100faSBill Paul break; 3440a94100faSBill Paul case SIOCGIFMEDIA: 3441a94100faSBill Paul case SIOCSIFMEDIA: 3442a94100faSBill Paul mii = device_get_softc(sc->rl_miibus); 3443a94100faSBill Paul error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 3444a94100faSBill Paul break; 3445a94100faSBill Paul case SIOCSIFCAP: 344640929967SGleb Smirnoff { 3447f051cb85SGleb Smirnoff int mask, reinit; 3448f051cb85SGleb Smirnoff 3449f051cb85SGleb Smirnoff mask = ifr->ifr_reqcap ^ ifp->if_capenable; 3450f051cb85SGleb Smirnoff reinit = 0; 345140929967SGleb Smirnoff #ifdef DEVICE_POLLING 345240929967SGleb Smirnoff if (mask & IFCAP_POLLING) { 345340929967SGleb Smirnoff if (ifr->ifr_reqcap & IFCAP_POLLING) { 345440929967SGleb Smirnoff error = ether_poll_register(re_poll, ifp); 345540929967SGleb Smirnoff if (error) 345640929967SGleb Smirnoff return (error); 3457d1754a9bSJohn Baldwin RL_LOCK(sc); 345840929967SGleb Smirnoff /* Disable interrupts */ 345940929967SGleb Smirnoff CSR_WRITE_2(sc, RL_IMR, 0x0000); 346040929967SGleb Smirnoff ifp->if_capenable |= IFCAP_POLLING; 346140929967SGleb Smirnoff RL_UNLOCK(sc); 346240929967SGleb Smirnoff } else { 346340929967SGleb Smirnoff error = ether_poll_deregister(ifp); 346440929967SGleb Smirnoff /* Enable interrupts. */ 346540929967SGleb Smirnoff RL_LOCK(sc); 346640929967SGleb Smirnoff CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS); 346740929967SGleb Smirnoff ifp->if_capenable &= ~IFCAP_POLLING; 346840929967SGleb Smirnoff RL_UNLOCK(sc); 346940929967SGleb Smirnoff } 347040929967SGleb Smirnoff } 347140929967SGleb Smirnoff #endif /* DEVICE_POLLING */ 3472600af6c2SPyun YongHyeon RL_LOCK(sc); 3473d3b181aeSPyun YongHyeon if ((mask & IFCAP_TXCSUM) != 0 && 3474d3b181aeSPyun YongHyeon (ifp->if_capabilities & IFCAP_TXCSUM) != 0) { 3475d3b181aeSPyun YongHyeon ifp->if_capenable ^= IFCAP_TXCSUM; 347674a03446SPyun YongHyeon if ((ifp->if_capenable & IFCAP_TXCSUM) != 0) 3477bc2a1002SPyun YongHyeon ifp->if_hwassist |= RE_CSUM_FEATURES; 347874a03446SPyun YongHyeon else 3479b61178a9SPyun YongHyeon ifp->if_hwassist &= ~RE_CSUM_FEATURES; 3480f051cb85SGleb Smirnoff reinit = 1; 348140929967SGleb Smirnoff } 3482d3b181aeSPyun YongHyeon if ((mask & IFCAP_RXCSUM) != 0 && 3483d3b181aeSPyun YongHyeon (ifp->if_capabilities & IFCAP_RXCSUM) != 0) { 3484d3b181aeSPyun YongHyeon ifp->if_capenable ^= IFCAP_RXCSUM; 3485d3b181aeSPyun YongHyeon reinit = 1; 3486d3b181aeSPyun YongHyeon } 3487ecafbbb5SPyun YongHyeon if ((mask & IFCAP_TSO4) != 0 && 3488fca1e0abSBjoern A. Zeeb (ifp->if_capabilities & IFCAP_TSO4) != 0) { 3489dc74159dSPyun YongHyeon ifp->if_capenable ^= IFCAP_TSO4; 3490ecafbbb5SPyun YongHyeon if ((IFCAP_TSO4 & ifp->if_capenable) != 0) 3491dc74159dSPyun YongHyeon ifp->if_hwassist |= CSUM_TSO; 3492dc74159dSPyun YongHyeon else 3493dc74159dSPyun YongHyeon ifp->if_hwassist &= ~CSUM_TSO; 3494ae644087SPyun YongHyeon if (ifp->if_mtu > RL_TSO_MTU && 3495ae644087SPyun YongHyeon (ifp->if_capenable & IFCAP_TSO4) != 0) { 3496ae644087SPyun YongHyeon ifp->if_capenable &= ~IFCAP_TSO4; 3497ae644087SPyun YongHyeon ifp->if_hwassist &= ~CSUM_TSO; 3498ae644087SPyun YongHyeon } 3499dc74159dSPyun YongHyeon } 3500ecafbbb5SPyun YongHyeon if ((mask & IFCAP_VLAN_HWTSO) != 0 && 3501ecafbbb5SPyun YongHyeon (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0) 3502ecafbbb5SPyun YongHyeon ifp->if_capenable ^= IFCAP_VLAN_HWTSO; 3503ecafbbb5SPyun YongHyeon if ((mask & IFCAP_VLAN_HWTAGGING) != 0 && 3504ecafbbb5SPyun YongHyeon (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) { 3505ecafbbb5SPyun YongHyeon ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING; 3506ecafbbb5SPyun YongHyeon /* TSO over VLAN requires VLAN hardware tagging. */ 3507ecafbbb5SPyun YongHyeon if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) 3508ecafbbb5SPyun YongHyeon ifp->if_capenable &= ~IFCAP_VLAN_HWTSO; 3509ecafbbb5SPyun YongHyeon reinit = 1; 3510ecafbbb5SPyun YongHyeon } 351181eee0ebSPyun YongHyeon if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 && 351281eee0ebSPyun YongHyeon (mask & (IFCAP_HWCSUM | IFCAP_TSO4 | 351381eee0ebSPyun YongHyeon IFCAP_VLAN_HWTSO)) != 0) 351481eee0ebSPyun YongHyeon reinit = 1; 35157467bd53SPyun YongHyeon if ((mask & IFCAP_WOL) != 0 && 35167467bd53SPyun YongHyeon (ifp->if_capabilities & IFCAP_WOL) != 0) { 35177467bd53SPyun YongHyeon if ((mask & IFCAP_WOL_UCAST) != 0) 35187467bd53SPyun YongHyeon ifp->if_capenable ^= IFCAP_WOL_UCAST; 35197467bd53SPyun YongHyeon if ((mask & IFCAP_WOL_MCAST) != 0) 35207467bd53SPyun YongHyeon ifp->if_capenable ^= IFCAP_WOL_MCAST; 35217467bd53SPyun YongHyeon if ((mask & IFCAP_WOL_MAGIC) != 0) 35227467bd53SPyun YongHyeon ifp->if_capenable ^= IFCAP_WOL_MAGIC; 35237467bd53SPyun YongHyeon } 35248476c243SPyun YongHyeon if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING) { 35258476c243SPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 3526600af6c2SPyun YongHyeon re_init_locked(sc); 35278476c243SPyun YongHyeon } 3528600af6c2SPyun YongHyeon RL_UNLOCK(sc); 3529960fd5b3SPyun YongHyeon VLAN_CAPABILITIES(ifp); 353040929967SGleb Smirnoff } 3531a94100faSBill Paul break; 3532a94100faSBill Paul default: 3533a94100faSBill Paul error = ether_ioctl(ifp, command, data); 3534a94100faSBill Paul break; 3535a94100faSBill Paul } 3536a94100faSBill Paul 3537a94100faSBill Paul return (error); 3538a94100faSBill Paul } 3539a94100faSBill Paul 3540a94100faSBill Paul static void 35417b5ffebfSPyun YongHyeon re_watchdog(struct rl_softc *sc) 35421d545c7aSMarius Strobl { 3543130b6dfbSPyun YongHyeon struct ifnet *ifp; 3544a94100faSBill Paul 35451d545c7aSMarius Strobl RL_LOCK_ASSERT(sc); 35461d545c7aSMarius Strobl 35471d545c7aSMarius Strobl if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0) 35481d545c7aSMarius Strobl return; 35491d545c7aSMarius Strobl 3550130b6dfbSPyun YongHyeon ifp = sc->rl_ifp; 3551a94100faSBill Paul re_txeof(sc); 3552130b6dfbSPyun YongHyeon if (sc->rl_ldata.rl_tx_free == sc->rl_ldata.rl_tx_desc_cnt) { 3553130b6dfbSPyun YongHyeon if_printf(ifp, "watchdog timeout (missed Tx interrupts) " 3554130b6dfbSPyun YongHyeon "-- recovering\n"); 3555130b6dfbSPyun YongHyeon if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 3556d180a66fSPyun YongHyeon re_start_locked(ifp); 3557130b6dfbSPyun YongHyeon return; 3558130b6dfbSPyun YongHyeon } 3559130b6dfbSPyun YongHyeon 3560130b6dfbSPyun YongHyeon if_printf(ifp, "watchdog timeout\n"); 3561c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 3562130b6dfbSPyun YongHyeon 35631abcdbd1SAttilio Rao re_rxeof(sc, NULL); 35648476c243SPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 356597b9d4baSJohn-Mark Gurney re_init_locked(sc); 3566130b6dfbSPyun YongHyeon if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 3567d180a66fSPyun YongHyeon re_start_locked(ifp); 3568a94100faSBill Paul } 3569a94100faSBill Paul 3570a94100faSBill Paul /* 3571a94100faSBill Paul * Stop the adapter and free any mbufs allocated to the 3572a94100faSBill Paul * RX and TX lists. 3573a94100faSBill Paul */ 3574a94100faSBill Paul static void 35757b5ffebfSPyun YongHyeon re_stop(struct rl_softc *sc) 3576a94100faSBill Paul { 35770ce0868aSPyun YongHyeon int i; 3578a94100faSBill Paul struct ifnet *ifp; 3579d65abd66SPyun YongHyeon struct rl_txdesc *txd; 3580d65abd66SPyun YongHyeon struct rl_rxdesc *rxd; 3581a94100faSBill Paul 358297b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 358397b9d4baSJohn-Mark Gurney 3584fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 3585a94100faSBill Paul 35861d545c7aSMarius Strobl sc->rl_watchdog_timer = 0; 3587d1754a9bSJohn Baldwin callout_stop(&sc->rl_stat_callout); 358813f4c340SRobert Watson ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 3589a94100faSBill Paul 3590fcb220acSPyun YongHyeon /* 3591fcb220acSPyun YongHyeon * Disable accepting frames to put RX MAC into idle state. 3592fcb220acSPyun YongHyeon * Otherwise it's possible to get frames while stop command 3593fcb220acSPyun YongHyeon * execution is in progress and controller can DMA the frame 3594fcb220acSPyun YongHyeon * to already freed RX buffer during that period. 3595fcb220acSPyun YongHyeon */ 3596fcb220acSPyun YongHyeon CSR_WRITE_4(sc, RL_RXCFG, CSR_READ_4(sc, RL_RXCFG) & 3597fcb220acSPyun YongHyeon ~(RL_RXCFG_RX_ALLPHYS | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_MULTI | 3598fcb220acSPyun YongHyeon RL_RXCFG_RX_BROAD)); 3599fcb220acSPyun YongHyeon 360014013280SMarius Strobl if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0) { 360114013280SMarius Strobl /* Enable RXDV gate. */ 360214013280SMarius Strobl CSR_WRITE_4(sc, RL_MISC, CSR_READ_4(sc, RL_MISC) | 360314013280SMarius Strobl 0x00080000); 360414013280SMarius Strobl } 360514013280SMarius Strobl 3606eef0e496SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_WAIT_TXPOLL) != 0) { 3607eef0e496SPyun YongHyeon for (i = RL_TIMEOUT; i > 0; i--) { 3608eef0e496SPyun YongHyeon if ((CSR_READ_1(sc, sc->rl_txstart) & 3609eef0e496SPyun YongHyeon RL_TXSTART_START) == 0) 3610eef0e496SPyun YongHyeon break; 3611eef0e496SPyun YongHyeon DELAY(20); 3612eef0e496SPyun YongHyeon } 3613eef0e496SPyun YongHyeon if (i == 0) 3614eef0e496SPyun YongHyeon device_printf(sc->rl_dev, 3615eef0e496SPyun YongHyeon "stopping TX poll timed out!\n"); 3616eef0e496SPyun YongHyeon CSR_WRITE_1(sc, RL_COMMAND, 0x00); 3617eef0e496SPyun YongHyeon } else if ((sc->rl_flags & RL_FLAG_CMDSTOP) != 0) { 3618ead8fc66SPyun YongHyeon CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_STOPREQ | RL_CMD_TX_ENB | 3619ead8fc66SPyun YongHyeon RL_CMD_RX_ENB); 3620eef0e496SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_CMDSTOP_WAIT_TXQ) != 0) { 3621eef0e496SPyun YongHyeon for (i = RL_TIMEOUT; i > 0; i--) { 3622eef0e496SPyun YongHyeon if ((CSR_READ_4(sc, RL_TXCFG) & 3623eef0e496SPyun YongHyeon RL_TXCFG_QUEUE_EMPTY) != 0) 3624eef0e496SPyun YongHyeon break; 3625eef0e496SPyun YongHyeon DELAY(100); 3626eef0e496SPyun YongHyeon } 3627eef0e496SPyun YongHyeon if (i == 0) 3628eef0e496SPyun YongHyeon device_printf(sc->rl_dev, 3629eef0e496SPyun YongHyeon "stopping TXQ timed out!\n"); 3630eef0e496SPyun YongHyeon } 3631eef0e496SPyun YongHyeon } else 3632a94100faSBill Paul CSR_WRITE_1(sc, RL_COMMAND, 0x00); 3633ead8fc66SPyun YongHyeon DELAY(1000); 3634a94100faSBill Paul CSR_WRITE_2(sc, RL_IMR, 0x0000); 3635ed510fb0SBill Paul CSR_WRITE_2(sc, RL_ISR, 0xFFFF); 3636a94100faSBill Paul 3637a94100faSBill Paul if (sc->rl_head != NULL) { 3638a94100faSBill Paul m_freem(sc->rl_head); 3639a94100faSBill Paul sc->rl_head = sc->rl_tail = NULL; 3640a94100faSBill Paul } 3641a94100faSBill Paul 3642a94100faSBill Paul /* Free the TX list buffers. */ 3643d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) { 3644d65abd66SPyun YongHyeon txd = &sc->rl_ldata.rl_tx_desc[i]; 3645d65abd66SPyun YongHyeon if (txd->tx_m != NULL) { 3646d65abd66SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, 3647d65abd66SPyun YongHyeon txd->tx_dmamap, BUS_DMASYNC_POSTWRITE); 3648d65abd66SPyun YongHyeon bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, 3649d65abd66SPyun YongHyeon txd->tx_dmamap); 3650d65abd66SPyun YongHyeon m_freem(txd->tx_m); 3651d65abd66SPyun YongHyeon txd->tx_m = NULL; 3652a94100faSBill Paul } 3653a94100faSBill Paul } 3654a94100faSBill Paul 3655a94100faSBill Paul /* Free the RX list buffers. */ 3656d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 3657d65abd66SPyun YongHyeon rxd = &sc->rl_ldata.rl_rx_desc[i]; 3658d65abd66SPyun YongHyeon if (rxd->rx_m != NULL) { 3659cba16362SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, 3660d65abd66SPyun YongHyeon rxd->rx_dmamap, BUS_DMASYNC_POSTREAD); 3661d65abd66SPyun YongHyeon bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, 3662d65abd66SPyun YongHyeon rxd->rx_dmamap); 3663d65abd66SPyun YongHyeon m_freem(rxd->rx_m); 3664d65abd66SPyun YongHyeon rxd->rx_m = NULL; 3665a94100faSBill Paul } 3666a94100faSBill Paul } 36671f32d3b7SPyun YongHyeon 36681f32d3b7SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) { 36691f32d3b7SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 36701f32d3b7SPyun YongHyeon rxd = &sc->rl_ldata.rl_jrx_desc[i]; 36711f32d3b7SPyun YongHyeon if (rxd->rx_m != NULL) { 36721f32d3b7SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, 36731f32d3b7SPyun YongHyeon rxd->rx_dmamap, BUS_DMASYNC_POSTREAD); 36741f32d3b7SPyun YongHyeon bus_dmamap_unload(sc->rl_ldata.rl_jrx_mtag, 36751f32d3b7SPyun YongHyeon rxd->rx_dmamap); 36761f32d3b7SPyun YongHyeon m_freem(rxd->rx_m); 36771f32d3b7SPyun YongHyeon rxd->rx_m = NULL; 36781f32d3b7SPyun YongHyeon } 36791f32d3b7SPyun YongHyeon } 36801f32d3b7SPyun YongHyeon } 3681a94100faSBill Paul } 3682a94100faSBill Paul 3683a94100faSBill Paul /* 3684a94100faSBill Paul * Device suspend routine. Stop the interface and save some PCI 3685a94100faSBill Paul * settings in case the BIOS doesn't restore them properly on 3686a94100faSBill Paul * resume. 3687a94100faSBill Paul */ 3688a94100faSBill Paul static int 36897b5ffebfSPyun YongHyeon re_suspend(device_t dev) 3690a94100faSBill Paul { 3691a94100faSBill Paul struct rl_softc *sc; 3692a94100faSBill Paul 3693a94100faSBill Paul sc = device_get_softc(dev); 3694a94100faSBill Paul 369597b9d4baSJohn-Mark Gurney RL_LOCK(sc); 3696a94100faSBill Paul re_stop(sc); 36977467bd53SPyun YongHyeon re_setwol(sc); 3698a94100faSBill Paul sc->suspended = 1; 369997b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 3700a94100faSBill Paul 3701a94100faSBill Paul return (0); 3702a94100faSBill Paul } 3703a94100faSBill Paul 3704a94100faSBill Paul /* 3705a94100faSBill Paul * Device resume routine. Restore some PCI settings in case the BIOS 3706a94100faSBill Paul * doesn't, re-enable busmastering, and restart the interface if 3707a94100faSBill Paul * appropriate. 3708a94100faSBill Paul */ 3709a94100faSBill Paul static int 37107b5ffebfSPyun YongHyeon re_resume(device_t dev) 3711a94100faSBill Paul { 3712a94100faSBill Paul struct rl_softc *sc; 3713a94100faSBill Paul struct ifnet *ifp; 3714a94100faSBill Paul 3715a94100faSBill Paul sc = device_get_softc(dev); 371697b9d4baSJohn-Mark Gurney 371797b9d4baSJohn-Mark Gurney RL_LOCK(sc); 371897b9d4baSJohn-Mark Gurney 3719fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 372061f45a72SPyun YongHyeon /* Take controller out of sleep mode. */ 372161f45a72SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) { 372261f45a72SPyun YongHyeon if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80) 372361f45a72SPyun YongHyeon CSR_WRITE_1(sc, RL_GPIO, 372461f45a72SPyun YongHyeon CSR_READ_1(sc, RL_GPIO) | 0x01); 372561f45a72SPyun YongHyeon } 3726a94100faSBill Paul 37277467bd53SPyun YongHyeon /* 37287467bd53SPyun YongHyeon * Clear WOL matching such that normal Rx filtering 37297467bd53SPyun YongHyeon * wouldn't interfere with WOL patterns. 37307467bd53SPyun YongHyeon */ 37317467bd53SPyun YongHyeon re_clrwol(sc); 373201d1a6c3SPyun YongHyeon 373301d1a6c3SPyun YongHyeon /* reinitialize interface if necessary */ 373401d1a6c3SPyun YongHyeon if (ifp->if_flags & IFF_UP) 373501d1a6c3SPyun YongHyeon re_init_locked(sc); 373601d1a6c3SPyun YongHyeon 3737a94100faSBill Paul sc->suspended = 0; 373897b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 3739a94100faSBill Paul 3740a94100faSBill Paul return (0); 3741a94100faSBill Paul } 3742a94100faSBill Paul 3743a94100faSBill Paul /* 3744a94100faSBill Paul * Stop all chip I/O so that the kernel's probe routines don't 3745a94100faSBill Paul * get confused by errant DMAs when rebooting. 3746a94100faSBill Paul */ 37476a087a87SPyun YongHyeon static int 37487b5ffebfSPyun YongHyeon re_shutdown(device_t dev) 3749a94100faSBill Paul { 3750a94100faSBill Paul struct rl_softc *sc; 3751a94100faSBill Paul 3752a94100faSBill Paul sc = device_get_softc(dev); 3753a94100faSBill Paul 375497b9d4baSJohn-Mark Gurney RL_LOCK(sc); 3755a94100faSBill Paul re_stop(sc); 3756536fde34SMaxim Sobolev /* 3757536fde34SMaxim Sobolev * Mark interface as down since otherwise we will panic if 3758536fde34SMaxim Sobolev * interrupt comes in later on, which can happen in some 375972293673SRuslan Ermilov * cases. 3760536fde34SMaxim Sobolev */ 3761536fde34SMaxim Sobolev sc->rl_ifp->if_flags &= ~IFF_UP; 37627467bd53SPyun YongHyeon re_setwol(sc); 376397b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 37646a087a87SPyun YongHyeon 37656a087a87SPyun YongHyeon return (0); 3766a94100faSBill Paul } 37677467bd53SPyun YongHyeon 37687467bd53SPyun YongHyeon static void 37696830588dSPyun YongHyeon re_set_linkspeed(struct rl_softc *sc) 37706830588dSPyun YongHyeon { 37716830588dSPyun YongHyeon struct mii_softc *miisc; 37726830588dSPyun YongHyeon struct mii_data *mii; 37736830588dSPyun YongHyeon int aneg, i, phyno; 37746830588dSPyun YongHyeon 37756830588dSPyun YongHyeon RL_LOCK_ASSERT(sc); 37766830588dSPyun YongHyeon 37776830588dSPyun YongHyeon mii = device_get_softc(sc->rl_miibus); 37786830588dSPyun YongHyeon mii_pollstat(mii); 37796830588dSPyun YongHyeon aneg = 0; 37806830588dSPyun YongHyeon if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) == 37816830588dSPyun YongHyeon (IFM_ACTIVE | IFM_AVALID)) { 37826830588dSPyun YongHyeon switch IFM_SUBTYPE(mii->mii_media_active) { 37836830588dSPyun YongHyeon case IFM_10_T: 37846830588dSPyun YongHyeon case IFM_100_TX: 37856830588dSPyun YongHyeon return; 37866830588dSPyun YongHyeon case IFM_1000_T: 37876830588dSPyun YongHyeon aneg++; 37886830588dSPyun YongHyeon break; 37896830588dSPyun YongHyeon default: 37906830588dSPyun YongHyeon break; 37916830588dSPyun YongHyeon } 37926830588dSPyun YongHyeon } 37936830588dSPyun YongHyeon miisc = LIST_FIRST(&mii->mii_phys); 37946830588dSPyun YongHyeon phyno = miisc->mii_phy; 37956830588dSPyun YongHyeon LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 37966830588dSPyun YongHyeon PHY_RESET(miisc); 37976830588dSPyun YongHyeon re_miibus_writereg(sc->rl_dev, phyno, MII_100T2CR, 0); 37986830588dSPyun YongHyeon re_miibus_writereg(sc->rl_dev, phyno, 37996830588dSPyun YongHyeon MII_ANAR, ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA); 38006830588dSPyun YongHyeon re_miibus_writereg(sc->rl_dev, phyno, 38016830588dSPyun YongHyeon MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG); 38026830588dSPyun YongHyeon DELAY(1000); 38036830588dSPyun YongHyeon if (aneg != 0) { 38046830588dSPyun YongHyeon /* 38056830588dSPyun YongHyeon * Poll link state until re(4) get a 10/100Mbps link. 38066830588dSPyun YongHyeon */ 38076830588dSPyun YongHyeon for (i = 0; i < MII_ANEGTICKS_GIGE; i++) { 38086830588dSPyun YongHyeon mii_pollstat(mii); 38096830588dSPyun YongHyeon if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) 38106830588dSPyun YongHyeon == (IFM_ACTIVE | IFM_AVALID)) { 38116830588dSPyun YongHyeon switch (IFM_SUBTYPE(mii->mii_media_active)) { 38126830588dSPyun YongHyeon case IFM_10_T: 38136830588dSPyun YongHyeon case IFM_100_TX: 38146830588dSPyun YongHyeon return; 38156830588dSPyun YongHyeon default: 38166830588dSPyun YongHyeon break; 38176830588dSPyun YongHyeon } 38186830588dSPyun YongHyeon } 38196830588dSPyun YongHyeon RL_UNLOCK(sc); 38206830588dSPyun YongHyeon pause("relnk", hz); 38216830588dSPyun YongHyeon RL_LOCK(sc); 38226830588dSPyun YongHyeon } 38236830588dSPyun YongHyeon if (i == MII_ANEGTICKS_GIGE) 38246830588dSPyun YongHyeon device_printf(sc->rl_dev, 38256830588dSPyun YongHyeon "establishing a link failed, WOL may not work!"); 38266830588dSPyun YongHyeon } 38276830588dSPyun YongHyeon /* 38286830588dSPyun YongHyeon * No link, force MAC to have 100Mbps, full-duplex link. 38296830588dSPyun YongHyeon * MAC does not require reprogramming on resolved speed/duplex, 38306830588dSPyun YongHyeon * so this is just for completeness. 38316830588dSPyun YongHyeon */ 38326830588dSPyun YongHyeon mii->mii_media_status = IFM_AVALID | IFM_ACTIVE; 38336830588dSPyun YongHyeon mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX; 38346830588dSPyun YongHyeon } 38356830588dSPyun YongHyeon 38366830588dSPyun YongHyeon static void 38377b5ffebfSPyun YongHyeon re_setwol(struct rl_softc *sc) 38387467bd53SPyun YongHyeon { 38397467bd53SPyun YongHyeon struct ifnet *ifp; 38407467bd53SPyun YongHyeon int pmc; 38417467bd53SPyun YongHyeon uint16_t pmstat; 38427467bd53SPyun YongHyeon uint8_t v; 38437467bd53SPyun YongHyeon 38447467bd53SPyun YongHyeon RL_LOCK_ASSERT(sc); 38457467bd53SPyun YongHyeon 38463b0a4aefSJohn Baldwin if (pci_find_cap(sc->rl_dev, PCIY_PMG, &pmc) != 0) 38477467bd53SPyun YongHyeon return; 38487467bd53SPyun YongHyeon 38497467bd53SPyun YongHyeon ifp = sc->rl_ifp; 385061f45a72SPyun YongHyeon /* Put controller into sleep mode. */ 385161f45a72SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) { 385261f45a72SPyun YongHyeon if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80) 385361f45a72SPyun YongHyeon CSR_WRITE_1(sc, RL_GPIO, 385461f45a72SPyun YongHyeon CSR_READ_1(sc, RL_GPIO) & ~0x01); 385561f45a72SPyun YongHyeon } 3856fcb220acSPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL) != 0) { 3857e9f8886eSMarius Strobl if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0) { 3858e9f8886eSMarius Strobl /* Disable RXDV gate. */ 3859e9f8886eSMarius Strobl CSR_WRITE_4(sc, RL_MISC, CSR_READ_4(sc, RL_MISC) & 3860e9f8886eSMarius Strobl ~0x00080000); 3861e9f8886eSMarius Strobl } 3862fcb220acSPyun YongHyeon re_set_rxmode(sc); 38636830588dSPyun YongHyeon if ((sc->rl_flags & RL_FLAG_WOL_MANLINK) != 0) 38646830588dSPyun YongHyeon re_set_linkspeed(sc); 3865fcb220acSPyun YongHyeon if ((sc->rl_flags & RL_FLAG_WOLRXENB) != 0) 3866886ff602SPyun YongHyeon CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RX_ENB); 3867fcb220acSPyun YongHyeon } 38687467bd53SPyun YongHyeon /* Enable config register write. */ 38697467bd53SPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE); 38707467bd53SPyun YongHyeon 38717467bd53SPyun YongHyeon /* Enable PME. */ 3872e7e7593cSPyun YongHyeon v = CSR_READ_1(sc, sc->rl_cfg1); 38737467bd53SPyun YongHyeon v &= ~RL_CFG1_PME; 38747467bd53SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL) != 0) 38757467bd53SPyun YongHyeon v |= RL_CFG1_PME; 3876e7e7593cSPyun YongHyeon CSR_WRITE_1(sc, sc->rl_cfg1, v); 38777467bd53SPyun YongHyeon 3878e7e7593cSPyun YongHyeon v = CSR_READ_1(sc, sc->rl_cfg3); 38797467bd53SPyun YongHyeon v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC); 38807467bd53SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0) 38817467bd53SPyun YongHyeon v |= RL_CFG3_WOL_MAGIC; 3882e7e7593cSPyun YongHyeon CSR_WRITE_1(sc, sc->rl_cfg3, v); 38837467bd53SPyun YongHyeon 3884e7e7593cSPyun YongHyeon v = CSR_READ_1(sc, sc->rl_cfg5); 388544f7cbf5SPyun YongHyeon v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST | 388644f7cbf5SPyun YongHyeon RL_CFG5_WOL_LANWAKE); 38877467bd53SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0) 38887467bd53SPyun YongHyeon v |= RL_CFG5_WOL_UCAST; 38897467bd53SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0) 38907467bd53SPyun YongHyeon v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST; 38917467bd53SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL) != 0) 38927467bd53SPyun YongHyeon v |= RL_CFG5_WOL_LANWAKE; 3893e7e7593cSPyun YongHyeon CSR_WRITE_1(sc, sc->rl_cfg5, v); 38947467bd53SPyun YongHyeon 389544f7cbf5SPyun YongHyeon /* Config register write done. */ 389644f7cbf5SPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 389744f7cbf5SPyun YongHyeon 3898bc6b129bSPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL) == 0 && 3899d0c45156SPyun YongHyeon (sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0) 3900d0c45156SPyun YongHyeon CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) & ~0x80); 39017467bd53SPyun YongHyeon /* 39027467bd53SPyun YongHyeon * It seems that hardware resets its link speed to 100Mbps in 39037467bd53SPyun YongHyeon * power down mode so switching to 100Mbps in driver is not 39047467bd53SPyun YongHyeon * needed. 39057467bd53SPyun YongHyeon */ 39067467bd53SPyun YongHyeon 39077467bd53SPyun YongHyeon /* Request PME if WOL is requested. */ 39087467bd53SPyun YongHyeon pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2); 39097467bd53SPyun YongHyeon pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE); 39107467bd53SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL) != 0) 39117467bd53SPyun YongHyeon pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE; 39127467bd53SPyun YongHyeon pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2); 39137467bd53SPyun YongHyeon } 39147467bd53SPyun YongHyeon 39157467bd53SPyun YongHyeon static void 39167b5ffebfSPyun YongHyeon re_clrwol(struct rl_softc *sc) 39177467bd53SPyun YongHyeon { 39187467bd53SPyun YongHyeon int pmc; 39197467bd53SPyun YongHyeon uint8_t v; 39207467bd53SPyun YongHyeon 39217467bd53SPyun YongHyeon RL_LOCK_ASSERT(sc); 39227467bd53SPyun YongHyeon 39233b0a4aefSJohn Baldwin if (pci_find_cap(sc->rl_dev, PCIY_PMG, &pmc) != 0) 39247467bd53SPyun YongHyeon return; 39257467bd53SPyun YongHyeon 39267467bd53SPyun YongHyeon /* Enable config register write. */ 39277467bd53SPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE); 39287467bd53SPyun YongHyeon 3929e7e7593cSPyun YongHyeon v = CSR_READ_1(sc, sc->rl_cfg3); 39307467bd53SPyun YongHyeon v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC); 3931e7e7593cSPyun YongHyeon CSR_WRITE_1(sc, sc->rl_cfg3, v); 39327467bd53SPyun YongHyeon 39337467bd53SPyun YongHyeon /* Config register write done. */ 3934f98dd8cfSPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 39357467bd53SPyun YongHyeon 3936e7e7593cSPyun YongHyeon v = CSR_READ_1(sc, sc->rl_cfg5); 39377467bd53SPyun YongHyeon v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST); 39387467bd53SPyun YongHyeon v &= ~RL_CFG5_WOL_LANWAKE; 3939e7e7593cSPyun YongHyeon CSR_WRITE_1(sc, sc->rl_cfg5, v); 39407467bd53SPyun YongHyeon } 39410534aae0SPyun YongHyeon 39420534aae0SPyun YongHyeon static void 39430534aae0SPyun YongHyeon re_add_sysctls(struct rl_softc *sc) 39440534aae0SPyun YongHyeon { 39450534aae0SPyun YongHyeon struct sysctl_ctx_list *ctx; 39460534aae0SPyun YongHyeon struct sysctl_oid_list *children; 3947502be0f7SPyun YongHyeon int error; 39480534aae0SPyun YongHyeon 39490534aae0SPyun YongHyeon ctx = device_get_sysctl_ctx(sc->rl_dev); 39500534aae0SPyun YongHyeon children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->rl_dev)); 39510534aae0SPyun YongHyeon 39520534aae0SPyun YongHyeon SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "stats", 39530534aae0SPyun YongHyeon CTLTYPE_INT | CTLFLAG_RW, sc, 0, re_sysctl_stats, "I", 39540534aae0SPyun YongHyeon "Statistics Information"); 3955502be0f7SPyun YongHyeon if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0) 3956502be0f7SPyun YongHyeon return; 3957502be0f7SPyun YongHyeon 3958502be0f7SPyun YongHyeon SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "int_rx_mod", 3959502be0f7SPyun YongHyeon CTLTYPE_INT | CTLFLAG_RW, &sc->rl_int_rx_mod, 0, 3960502be0f7SPyun YongHyeon sysctl_hw_re_int_mod, "I", "re RX interrupt moderation"); 3961502be0f7SPyun YongHyeon /* Pull in device tunables. */ 3962502be0f7SPyun YongHyeon sc->rl_int_rx_mod = RL_TIMER_DEFAULT; 3963502be0f7SPyun YongHyeon error = resource_int_value(device_get_name(sc->rl_dev), 3964502be0f7SPyun YongHyeon device_get_unit(sc->rl_dev), "int_rx_mod", &sc->rl_int_rx_mod); 3965502be0f7SPyun YongHyeon if (error == 0) { 3966502be0f7SPyun YongHyeon if (sc->rl_int_rx_mod < RL_TIMER_MIN || 3967502be0f7SPyun YongHyeon sc->rl_int_rx_mod > RL_TIMER_MAX) { 3968502be0f7SPyun YongHyeon device_printf(sc->rl_dev, "int_rx_mod value out of " 3969502be0f7SPyun YongHyeon "range; using default: %d\n", 3970502be0f7SPyun YongHyeon RL_TIMER_DEFAULT); 3971502be0f7SPyun YongHyeon sc->rl_int_rx_mod = RL_TIMER_DEFAULT; 3972502be0f7SPyun YongHyeon } 3973502be0f7SPyun YongHyeon } 39740534aae0SPyun YongHyeon } 39750534aae0SPyun YongHyeon 39760534aae0SPyun YongHyeon static int 39770534aae0SPyun YongHyeon re_sysctl_stats(SYSCTL_HANDLER_ARGS) 39780534aae0SPyun YongHyeon { 39790534aae0SPyun YongHyeon struct rl_softc *sc; 39800534aae0SPyun YongHyeon struct rl_stats *stats; 39810534aae0SPyun YongHyeon int error, i, result; 39820534aae0SPyun YongHyeon 39830534aae0SPyun YongHyeon result = -1; 39840534aae0SPyun YongHyeon error = sysctl_handle_int(oidp, &result, 0, req); 39850534aae0SPyun YongHyeon if (error || req->newptr == NULL) 39860534aae0SPyun YongHyeon return (error); 39870534aae0SPyun YongHyeon 39880534aae0SPyun YongHyeon if (result == 1) { 39890534aae0SPyun YongHyeon sc = (struct rl_softc *)arg1; 39900534aae0SPyun YongHyeon RL_LOCK(sc); 399116a4824bSPyun YongHyeon if ((sc->rl_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 399216a4824bSPyun YongHyeon RL_UNLOCK(sc); 399316a4824bSPyun YongHyeon goto done; 399416a4824bSPyun YongHyeon } 39950534aae0SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_stag, 39960534aae0SPyun YongHyeon sc->rl_ldata.rl_smap, BUS_DMASYNC_PREREAD); 39970534aae0SPyun YongHyeon CSR_WRITE_4(sc, RL_DUMPSTATS_HI, 39980534aae0SPyun YongHyeon RL_ADDR_HI(sc->rl_ldata.rl_stats_addr)); 39990534aae0SPyun YongHyeon CSR_WRITE_4(sc, RL_DUMPSTATS_LO, 40000534aae0SPyun YongHyeon RL_ADDR_LO(sc->rl_ldata.rl_stats_addr)); 40010534aae0SPyun YongHyeon CSR_WRITE_4(sc, RL_DUMPSTATS_LO, 40020534aae0SPyun YongHyeon RL_ADDR_LO(sc->rl_ldata.rl_stats_addr | 40030534aae0SPyun YongHyeon RL_DUMPSTATS_START)); 40040534aae0SPyun YongHyeon for (i = RL_TIMEOUT; i > 0; i--) { 40050534aae0SPyun YongHyeon if ((CSR_READ_4(sc, RL_DUMPSTATS_LO) & 40060534aae0SPyun YongHyeon RL_DUMPSTATS_START) == 0) 40070534aae0SPyun YongHyeon break; 40080534aae0SPyun YongHyeon DELAY(1000); 40090534aae0SPyun YongHyeon } 40100534aae0SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_stag, 40110534aae0SPyun YongHyeon sc->rl_ldata.rl_smap, BUS_DMASYNC_POSTREAD); 40120534aae0SPyun YongHyeon RL_UNLOCK(sc); 40130534aae0SPyun YongHyeon if (i == 0) { 40140534aae0SPyun YongHyeon device_printf(sc->rl_dev, 40150534aae0SPyun YongHyeon "DUMP statistics request timed out\n"); 40160534aae0SPyun YongHyeon return (ETIMEDOUT); 40170534aae0SPyun YongHyeon } 401816a4824bSPyun YongHyeon done: 40190534aae0SPyun YongHyeon stats = sc->rl_ldata.rl_stats; 40200534aae0SPyun YongHyeon printf("%s statistics:\n", device_get_nameunit(sc->rl_dev)); 40210534aae0SPyun YongHyeon printf("Tx frames : %ju\n", 40220534aae0SPyun YongHyeon (uintmax_t)le64toh(stats->rl_tx_pkts)); 40230534aae0SPyun YongHyeon printf("Rx frames : %ju\n", 40240534aae0SPyun YongHyeon (uintmax_t)le64toh(stats->rl_rx_pkts)); 40250534aae0SPyun YongHyeon printf("Tx errors : %ju\n", 40260534aae0SPyun YongHyeon (uintmax_t)le64toh(stats->rl_tx_errs)); 40270534aae0SPyun YongHyeon printf("Rx errors : %u\n", 40280534aae0SPyun YongHyeon le32toh(stats->rl_rx_errs)); 40290534aae0SPyun YongHyeon printf("Rx missed frames : %u\n", 40300534aae0SPyun YongHyeon (uint32_t)le16toh(stats->rl_missed_pkts)); 40310534aae0SPyun YongHyeon printf("Rx frame alignment errs : %u\n", 40320534aae0SPyun YongHyeon (uint32_t)le16toh(stats->rl_rx_framealign_errs)); 40330534aae0SPyun YongHyeon printf("Tx single collisions : %u\n", 40340534aae0SPyun YongHyeon le32toh(stats->rl_tx_onecoll)); 40350534aae0SPyun YongHyeon printf("Tx multiple collisions : %u\n", 40360534aae0SPyun YongHyeon le32toh(stats->rl_tx_multicolls)); 40370534aae0SPyun YongHyeon printf("Rx unicast frames : %ju\n", 40380534aae0SPyun YongHyeon (uintmax_t)le64toh(stats->rl_rx_ucasts)); 40390534aae0SPyun YongHyeon printf("Rx broadcast frames : %ju\n", 40400534aae0SPyun YongHyeon (uintmax_t)le64toh(stats->rl_rx_bcasts)); 40410534aae0SPyun YongHyeon printf("Rx multicast frames : %u\n", 40420534aae0SPyun YongHyeon le32toh(stats->rl_rx_mcasts)); 40430534aae0SPyun YongHyeon printf("Tx aborts : %u\n", 40440534aae0SPyun YongHyeon (uint32_t)le16toh(stats->rl_tx_aborts)); 40450534aae0SPyun YongHyeon printf("Tx underruns : %u\n", 40460534aae0SPyun YongHyeon (uint32_t)le16toh(stats->rl_rx_underruns)); 40470534aae0SPyun YongHyeon } 40480534aae0SPyun YongHyeon 40490534aae0SPyun YongHyeon return (error); 40500534aae0SPyun YongHyeon } 4051502be0f7SPyun YongHyeon 4052502be0f7SPyun YongHyeon static int 4053502be0f7SPyun YongHyeon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high) 4054502be0f7SPyun YongHyeon { 4055502be0f7SPyun YongHyeon int error, value; 4056502be0f7SPyun YongHyeon 4057502be0f7SPyun YongHyeon if (arg1 == NULL) 4058502be0f7SPyun YongHyeon return (EINVAL); 4059502be0f7SPyun YongHyeon value = *(int *)arg1; 4060502be0f7SPyun YongHyeon error = sysctl_handle_int(oidp, &value, 0, req); 4061502be0f7SPyun YongHyeon if (error || req->newptr == NULL) 4062502be0f7SPyun YongHyeon return (error); 4063502be0f7SPyun YongHyeon if (value < low || value > high) 4064502be0f7SPyun YongHyeon return (EINVAL); 4065502be0f7SPyun YongHyeon *(int *)arg1 = value; 4066502be0f7SPyun YongHyeon 4067502be0f7SPyun YongHyeon return (0); 4068502be0f7SPyun YongHyeon } 4069502be0f7SPyun YongHyeon 4070502be0f7SPyun YongHyeon static int 4071502be0f7SPyun YongHyeon sysctl_hw_re_int_mod(SYSCTL_HANDLER_ARGS) 4072502be0f7SPyun YongHyeon { 4073502be0f7SPyun YongHyeon 4074502be0f7SPyun YongHyeon return (sysctl_int_range(oidp, arg1, arg2, req, RL_TIMER_MIN, 4075502be0f7SPyun YongHyeon RL_TIMER_MAX)); 4076502be0f7SPyun YongHyeon } 4077