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> 130a94100faSBill Paul #include <net/if_arp.h> 131a94100faSBill Paul #include <net/ethernet.h> 132a94100faSBill Paul #include <net/if_dl.h> 133a94100faSBill Paul #include <net/if_media.h> 134fc74a9f9SBrooks Davis #include <net/if_types.h> 135a94100faSBill Paul #include <net/if_vlan_var.h> 136a94100faSBill Paul 137a94100faSBill Paul #include <net/bpf.h> 138a94100faSBill Paul 139a94100faSBill Paul #include <machine/bus.h> 140a94100faSBill Paul #include <machine/resource.h> 141a94100faSBill Paul #include <sys/bus.h> 142a94100faSBill Paul #include <sys/rman.h> 143a94100faSBill Paul 144a94100faSBill Paul #include <dev/mii/mii.h> 145a94100faSBill Paul #include <dev/mii/miivar.h> 146a94100faSBill Paul 147a94100faSBill Paul #include <dev/pci/pcireg.h> 148a94100faSBill Paul #include <dev/pci/pcivar.h> 149a94100faSBill Paul 150d65abd66SPyun YongHyeon #include <pci/if_rlreg.h> 151d65abd66SPyun YongHyeon 152a94100faSBill Paul MODULE_DEPEND(re, pci, 1, 1, 1); 153a94100faSBill Paul MODULE_DEPEND(re, ether, 1, 1, 1); 154a94100faSBill Paul MODULE_DEPEND(re, miibus, 1, 1, 1); 155a94100faSBill Paul 156298bfdf3SWarner Losh /* "device miibus" required. See GENERIC if you get errors here. */ 157a94100faSBill Paul #include "miibus_if.h" 158a94100faSBill Paul 1595774c5ffSPyun YongHyeon /* Tunables. */ 160c2d2e19cSPyun YongHyeon static int msi_disable = 0; 1615774c5ffSPyun YongHyeon TUNABLE_INT("hw.re.msi_disable", &msi_disable); 1622c21710bSPyun YongHyeon static int prefer_iomap = 0; 1632c21710bSPyun YongHyeon TUNABLE_INT("hw.re.prefer_iomap", &prefer_iomap); 1645774c5ffSPyun YongHyeon 165a94100faSBill Paul #define RE_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP) 166a94100faSBill Paul 167a94100faSBill Paul /* 168a94100faSBill Paul * Various supported device vendors/types and their names. 169a94100faSBill Paul */ 170a94100faSBill Paul static struct rl_type re_devs[] = { 1719dfcacbeSPyun YongHyeon { DLINK_VENDORID, DLINK_DEVICEID_528T, 0, 17232aa5f0eSAnton Berezin "D-Link DGE-528(T) Gigabit Ethernet Adapter" }, 1739dfcacbeSPyun YongHyeon { RT_VENDORID, RT_DEVICEID_8139, 0, 174a94100faSBill Paul "RealTek 8139C+ 10/100BaseTX" }, 1759dfcacbeSPyun YongHyeon { RT_VENDORID, RT_DEVICEID_8101E, 0, 1768281a098SPyun YongHyeon "RealTek 8101E/8102E/8102EL/8103E PCIe 10/100baseTX" }, 1779dfcacbeSPyun YongHyeon { RT_VENDORID, RT_DEVICEID_8168, 0, 178d0c45156SPyun YongHyeon "RealTek 8168/8111 B/C/CP/D/DP/E PCIe Gigabit Ethernet" }, 1799dfcacbeSPyun YongHyeon { RT_VENDORID, RT_DEVICEID_8169, 0, 180715922d7SPyun YongHyeon "RealTek 8169/8169S/8169SB(L)/8110S/8110SB(L) Gigabit Ethernet" }, 1819dfcacbeSPyun YongHyeon { RT_VENDORID, RT_DEVICEID_8169SC, 0, 1822ee2c3b4SRemko Lodder "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" }, 1839dfcacbeSPyun YongHyeon { COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, 0, 184ea263191SMIHIRA Sanpei Yoshiro "Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" }, 1859dfcacbeSPyun YongHyeon { LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, 0, 18626390635SJohn Baldwin "Linksys EG1032 (RTL8169S) Gigabit Ethernet" }, 1879dfcacbeSPyun YongHyeon { USR_VENDORID, USR_DEVICEID_997902, 0, 188dfdb409eSPyun YongHyeon "US Robotics 997902 (RTL8169S) Gigabit Ethernet" } 189a94100faSBill Paul }; 190a94100faSBill Paul 191a94100faSBill Paul static struct rl_hwrev re_hwrevs[] = { 192a94100faSBill Paul { RL_HWREV_8139, RL_8139, "" }, 193a94100faSBill Paul { RL_HWREV_8139A, RL_8139, "A" }, 194a94100faSBill Paul { RL_HWREV_8139AG, RL_8139, "A-G" }, 195a94100faSBill Paul { RL_HWREV_8139B, RL_8139, "B" }, 196a94100faSBill Paul { RL_HWREV_8130, RL_8139, "8130" }, 197a94100faSBill Paul { RL_HWREV_8139C, RL_8139, "C" }, 198a94100faSBill Paul { RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C" }, 199a94100faSBill Paul { RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+"}, 200498bd0d3SBill Paul { RL_HWREV_8168_SPIN1, RL_8169, "8168"}, 201a94100faSBill Paul { RL_HWREV_8169, RL_8169, "8169"}, 20269a6b7fbSBill Paul { RL_HWREV_8169S, RL_8169, "8169S"}, 20369a6b7fbSBill Paul { RL_HWREV_8110S, RL_8169, "8110S"}, 204566ca8caSJung-uk Kim { RL_HWREV_8169_8110SB, RL_8169, "8169SB/8110SB"}, 205566ca8caSJung-uk Kim { RL_HWREV_8169_8110SC, RL_8169, "8169SC/8110SC"}, 206566ca8caSJung-uk Kim { RL_HWREV_8169_8110SBL, RL_8169, "8169SBL/8110SBL"}, 207566ca8caSJung-uk Kim { RL_HWREV_8169_8110SCE, RL_8169, "8169SC/8110SC"}, 208a94100faSBill Paul { RL_HWREV_8100, RL_8139, "8100"}, 209a94100faSBill Paul { RL_HWREV_8101, RL_8139, "8101"}, 210ed510fb0SBill Paul { RL_HWREV_8100E, RL_8169, "8100E"}, 211ed510fb0SBill Paul { RL_HWREV_8101E, RL_8169, "8101E"}, 212b1d62f0fSPyun YongHyeon { RL_HWREV_8102E, RL_8169, "8102E"}, 213b1d62f0fSPyun YongHyeon { RL_HWREV_8102EL, RL_8169, "8102EL"}, 2143d22427cSTai-hwa Liang { RL_HWREV_8102EL_SPIN1, RL_8169, "8102EL"}, 2158281a098SPyun YongHyeon { RL_HWREV_8103E, RL_8169, "8103E"}, 216498bd0d3SBill Paul { RL_HWREV_8168_SPIN2, RL_8169, "8168"}, 2171acbb78aSPyun YongHyeon { RL_HWREV_8168_SPIN3, RL_8169, "8168"}, 218deb5c680SPyun YongHyeon { RL_HWREV_8168C, RL_8169, "8168C/8111C"}, 219deb5c680SPyun YongHyeon { RL_HWREV_8168C_SPIN2, RL_8169, "8168C/8111C"}, 220deb5c680SPyun YongHyeon { RL_HWREV_8168CP, RL_8169, "8168CP/8111CP"}, 2215fa06abeSPyun YongHyeon { RL_HWREV_8168D, RL_8169, "8168D/8111D"}, 2225fa06abeSPyun YongHyeon { RL_HWREV_8168DP, RL_8169, "8168DP/8111DP"}, 223d0c45156SPyun YongHyeon { RL_HWREV_8168E, RL_8169, "8168E/8111E"}, 224a94100faSBill Paul { 0, 0, NULL } 225a94100faSBill Paul }; 226a94100faSBill Paul 227a94100faSBill Paul static int re_probe (device_t); 228a94100faSBill Paul static int re_attach (device_t); 229a94100faSBill Paul static int re_detach (device_t); 230a94100faSBill Paul 231d65abd66SPyun YongHyeon static int re_encap (struct rl_softc *, struct mbuf **); 232a94100faSBill Paul 233a94100faSBill Paul static void re_dma_map_addr (void *, bus_dma_segment_t *, int, int); 234a94100faSBill Paul static int re_allocmem (device_t, struct rl_softc *); 235d65abd66SPyun YongHyeon static __inline void re_discard_rxbuf 236d65abd66SPyun YongHyeon (struct rl_softc *, int); 237d65abd66SPyun YongHyeon static int re_newbuf (struct rl_softc *, int); 238a94100faSBill Paul static int re_rx_list_init (struct rl_softc *); 239a94100faSBill Paul static int re_tx_list_init (struct rl_softc *); 24022a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX 24122a11c96SJohn-Mark Gurney static __inline void re_fixup_rx 24222a11c96SJohn-Mark Gurney (struct mbuf *); 24322a11c96SJohn-Mark Gurney #endif 2441abcdbd1SAttilio Rao static int re_rxeof (struct rl_softc *, int *); 245a94100faSBill Paul static void re_txeof (struct rl_softc *); 24697b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING 2471abcdbd1SAttilio Rao static int re_poll (struct ifnet *, enum poll_cmd, int); 2481abcdbd1SAttilio Rao static int re_poll_locked (struct ifnet *, enum poll_cmd, int); 24997b9d4baSJohn-Mark Gurney #endif 250ef544f63SPaolo Pisati static int re_intr (void *); 251a94100faSBill Paul static void re_tick (void *); 252ed510fb0SBill Paul static void re_tx_task (void *, int); 253ed510fb0SBill Paul static void re_int_task (void *, int); 254a94100faSBill Paul static void re_start (struct ifnet *); 255a94100faSBill Paul static int re_ioctl (struct ifnet *, u_long, caddr_t); 256a94100faSBill Paul static void re_init (void *); 25797b9d4baSJohn-Mark Gurney static void re_init_locked (struct rl_softc *); 258a94100faSBill Paul static void re_stop (struct rl_softc *); 2591d545c7aSMarius Strobl static void re_watchdog (struct rl_softc *); 260a94100faSBill Paul static int re_suspend (device_t); 261a94100faSBill Paul static int re_resume (device_t); 2626a087a87SPyun YongHyeon static int re_shutdown (device_t); 263a94100faSBill Paul static int re_ifmedia_upd (struct ifnet *); 264a94100faSBill Paul static void re_ifmedia_sts (struct ifnet *, struct ifmediareq *); 265a94100faSBill Paul 266a94100faSBill Paul static void re_eeprom_putbyte (struct rl_softc *, int); 267a94100faSBill Paul static void re_eeprom_getword (struct rl_softc *, int, u_int16_t *); 268ed510fb0SBill Paul static void re_read_eeprom (struct rl_softc *, caddr_t, int, int); 269a94100faSBill Paul static int re_gmii_readreg (device_t, int, int); 270a94100faSBill Paul static int re_gmii_writereg (device_t, int, int, int); 271a94100faSBill Paul 272a94100faSBill Paul static int re_miibus_readreg (device_t, int, int); 273a94100faSBill Paul static int re_miibus_writereg (device_t, int, int, int); 274a94100faSBill Paul static void re_miibus_statchg (device_t); 275a94100faSBill Paul 276ff191365SJung-uk Kim static void re_set_rxmode (struct rl_softc *); 277a94100faSBill Paul static void re_reset (struct rl_softc *); 2787467bd53SPyun YongHyeon static void re_setwol (struct rl_softc *); 2797467bd53SPyun YongHyeon static void re_clrwol (struct rl_softc *); 280a94100faSBill Paul 281ed510fb0SBill Paul #ifdef RE_DIAG 282a94100faSBill Paul static int re_diag (struct rl_softc *); 283ed510fb0SBill Paul #endif 284a94100faSBill Paul 2850534aae0SPyun YongHyeon static void re_add_sysctls (struct rl_softc *); 2860534aae0SPyun YongHyeon static int re_sysctl_stats (SYSCTL_HANDLER_ARGS); 2870534aae0SPyun YongHyeon 288a94100faSBill Paul static device_method_t re_methods[] = { 289a94100faSBill Paul /* Device interface */ 290a94100faSBill Paul DEVMETHOD(device_probe, re_probe), 291a94100faSBill Paul DEVMETHOD(device_attach, re_attach), 292a94100faSBill Paul DEVMETHOD(device_detach, re_detach), 293a94100faSBill Paul DEVMETHOD(device_suspend, re_suspend), 294a94100faSBill Paul DEVMETHOD(device_resume, re_resume), 295a94100faSBill Paul DEVMETHOD(device_shutdown, re_shutdown), 296a94100faSBill Paul 297a94100faSBill Paul /* bus interface */ 298a94100faSBill Paul DEVMETHOD(bus_print_child, bus_generic_print_child), 299a94100faSBill Paul DEVMETHOD(bus_driver_added, bus_generic_driver_added), 300a94100faSBill Paul 301a94100faSBill Paul /* MII interface */ 302a94100faSBill Paul DEVMETHOD(miibus_readreg, re_miibus_readreg), 303a94100faSBill Paul DEVMETHOD(miibus_writereg, re_miibus_writereg), 304a94100faSBill Paul DEVMETHOD(miibus_statchg, re_miibus_statchg), 305a94100faSBill Paul 306a94100faSBill Paul { 0, 0 } 307a94100faSBill Paul }; 308a94100faSBill Paul 309a94100faSBill Paul static driver_t re_driver = { 310a94100faSBill Paul "re", 311a94100faSBill Paul re_methods, 312a94100faSBill Paul sizeof(struct rl_softc) 313a94100faSBill Paul }; 314a94100faSBill Paul 315a94100faSBill Paul static devclass_t re_devclass; 316a94100faSBill Paul 317a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0); 318a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0); 319a94100faSBill Paul 320a94100faSBill Paul #define EE_SET(x) \ 321a94100faSBill Paul CSR_WRITE_1(sc, RL_EECMD, \ 322a94100faSBill Paul CSR_READ_1(sc, RL_EECMD) | x) 323a94100faSBill Paul 324a94100faSBill Paul #define EE_CLR(x) \ 325a94100faSBill Paul CSR_WRITE_1(sc, RL_EECMD, \ 326a94100faSBill Paul CSR_READ_1(sc, RL_EECMD) & ~x) 327a94100faSBill Paul 328a94100faSBill Paul /* 329a94100faSBill Paul * Send a read command and address to the EEPROM, check for ACK. 330a94100faSBill Paul */ 331a94100faSBill Paul static void 3327b5ffebfSPyun YongHyeon re_eeprom_putbyte(struct rl_softc *sc, int addr) 333a94100faSBill Paul { 3340ce0868aSPyun YongHyeon int d, i; 335a94100faSBill Paul 336ed510fb0SBill Paul d = addr | (RL_9346_READ << sc->rl_eewidth); 337a94100faSBill Paul 338a94100faSBill Paul /* 339a94100faSBill Paul * Feed in each bit and strobe the clock. 340a94100faSBill Paul */ 341ed510fb0SBill Paul 342ed510fb0SBill Paul for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) { 343a94100faSBill Paul if (d & i) { 344a94100faSBill Paul EE_SET(RL_EE_DATAIN); 345a94100faSBill Paul } else { 346a94100faSBill Paul EE_CLR(RL_EE_DATAIN); 347a94100faSBill Paul } 348a94100faSBill Paul DELAY(100); 349a94100faSBill Paul EE_SET(RL_EE_CLK); 350a94100faSBill Paul DELAY(150); 351a94100faSBill Paul EE_CLR(RL_EE_CLK); 352a94100faSBill Paul DELAY(100); 353a94100faSBill Paul } 354a94100faSBill Paul } 355a94100faSBill Paul 356a94100faSBill Paul /* 357a94100faSBill Paul * Read a word of data stored in the EEPROM at address 'addr.' 358a94100faSBill Paul */ 359a94100faSBill Paul static void 3607b5ffebfSPyun YongHyeon re_eeprom_getword(struct rl_softc *sc, int addr, u_int16_t *dest) 361a94100faSBill Paul { 3620ce0868aSPyun YongHyeon int i; 363a94100faSBill Paul u_int16_t word = 0; 364a94100faSBill Paul 365a94100faSBill Paul /* 366a94100faSBill Paul * Send address of word we want to read. 367a94100faSBill Paul */ 368a94100faSBill Paul re_eeprom_putbyte(sc, addr); 369a94100faSBill Paul 370a94100faSBill Paul /* 371a94100faSBill Paul * Start reading bits from EEPROM. 372a94100faSBill Paul */ 373a94100faSBill Paul for (i = 0x8000; i; i >>= 1) { 374a94100faSBill Paul EE_SET(RL_EE_CLK); 375a94100faSBill Paul DELAY(100); 376a94100faSBill Paul if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT) 377a94100faSBill Paul word |= i; 378a94100faSBill Paul EE_CLR(RL_EE_CLK); 379a94100faSBill Paul DELAY(100); 380a94100faSBill Paul } 381a94100faSBill Paul 382a94100faSBill Paul *dest = word; 383a94100faSBill Paul } 384a94100faSBill Paul 385a94100faSBill Paul /* 386a94100faSBill Paul * Read a sequence of words from the EEPROM. 387a94100faSBill Paul */ 388a94100faSBill Paul static void 3897b5ffebfSPyun YongHyeon re_read_eeprom(struct rl_softc *sc, caddr_t dest, int off, int cnt) 390a94100faSBill Paul { 391a94100faSBill Paul int i; 392a94100faSBill Paul u_int16_t word = 0, *ptr; 393a94100faSBill Paul 394ed510fb0SBill Paul CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM); 395ed510fb0SBill Paul 396ed510fb0SBill Paul DELAY(100); 397ed510fb0SBill Paul 398a94100faSBill Paul for (i = 0; i < cnt; i++) { 399ed510fb0SBill Paul CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL); 400a94100faSBill Paul re_eeprom_getword(sc, off + i, &word); 401ed510fb0SBill Paul CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL); 402a94100faSBill Paul ptr = (u_int16_t *)(dest + (i * 2)); 403be099007SPyun YongHyeon *ptr = word; 404a94100faSBill Paul } 405ed510fb0SBill Paul 406ed510fb0SBill Paul CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM); 407a94100faSBill Paul } 408a94100faSBill Paul 409a94100faSBill Paul static int 4107b5ffebfSPyun YongHyeon re_gmii_readreg(device_t dev, int phy, int reg) 411a94100faSBill Paul { 412a94100faSBill Paul struct rl_softc *sc; 413a94100faSBill Paul u_int32_t rval; 414a94100faSBill Paul int i; 415a94100faSBill Paul 416a94100faSBill Paul sc = device_get_softc(dev); 417a94100faSBill Paul 4189bac70b8SBill Paul /* Let the rgephy driver read the GMEDIASTAT register */ 4199bac70b8SBill Paul 4209bac70b8SBill Paul if (reg == RL_GMEDIASTAT) { 4219bac70b8SBill Paul rval = CSR_READ_1(sc, RL_GMEDIASTAT); 4229bac70b8SBill Paul return (rval); 4239bac70b8SBill Paul } 4249bac70b8SBill Paul 425a94100faSBill Paul CSR_WRITE_4(sc, RL_PHYAR, reg << 16); 426a94100faSBill Paul 42796b774f4SPyun YongHyeon for (i = 0; i < RL_PHY_TIMEOUT; i++) { 428a94100faSBill Paul rval = CSR_READ_4(sc, RL_PHYAR); 429a94100faSBill Paul if (rval & RL_PHYAR_BUSY) 430a94100faSBill Paul break; 4312bc085c6SPyun YongHyeon DELAY(25); 432a94100faSBill Paul } 433a94100faSBill Paul 43496b774f4SPyun YongHyeon if (i == RL_PHY_TIMEOUT) { 4356b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "PHY read failed\n"); 436a94100faSBill Paul return (0); 437a94100faSBill Paul } 438a94100faSBill Paul 4392bc085c6SPyun YongHyeon /* 4402bc085c6SPyun YongHyeon * Controller requires a 20us delay to process next MDIO request. 4412bc085c6SPyun YongHyeon */ 4422bc085c6SPyun YongHyeon DELAY(20); 4432bc085c6SPyun YongHyeon 444a94100faSBill Paul return (rval & RL_PHYAR_PHYDATA); 445a94100faSBill Paul } 446a94100faSBill Paul 447a94100faSBill Paul static int 4487b5ffebfSPyun YongHyeon re_gmii_writereg(device_t dev, int phy, int reg, int data) 449a94100faSBill Paul { 450a94100faSBill Paul struct rl_softc *sc; 451a94100faSBill Paul u_int32_t rval; 452a94100faSBill Paul int i; 453a94100faSBill Paul 454a94100faSBill Paul sc = device_get_softc(dev); 455a94100faSBill Paul 456a94100faSBill Paul CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) | 4579bac70b8SBill Paul (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY); 458a94100faSBill Paul 45996b774f4SPyun YongHyeon for (i = 0; i < RL_PHY_TIMEOUT; i++) { 460a94100faSBill Paul rval = CSR_READ_4(sc, RL_PHYAR); 461a94100faSBill Paul if (!(rval & RL_PHYAR_BUSY)) 462a94100faSBill Paul break; 4632bc085c6SPyun YongHyeon DELAY(25); 464a94100faSBill Paul } 465a94100faSBill Paul 46696b774f4SPyun YongHyeon if (i == RL_PHY_TIMEOUT) { 4676b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "PHY write failed\n"); 468a94100faSBill Paul return (0); 469a94100faSBill Paul } 470a94100faSBill Paul 4712bc085c6SPyun YongHyeon /* 4722bc085c6SPyun YongHyeon * Controller requires a 20us delay to process next MDIO request. 4732bc085c6SPyun YongHyeon */ 4742bc085c6SPyun YongHyeon DELAY(20); 4752bc085c6SPyun YongHyeon 476a94100faSBill Paul return (0); 477a94100faSBill Paul } 478a94100faSBill Paul 479a94100faSBill Paul static int 4807b5ffebfSPyun YongHyeon re_miibus_readreg(device_t dev, int phy, int reg) 481a94100faSBill Paul { 482a94100faSBill Paul struct rl_softc *sc; 483a94100faSBill Paul u_int16_t rval = 0; 484a94100faSBill Paul u_int16_t re8139_reg = 0; 485a94100faSBill Paul 486a94100faSBill Paul sc = device_get_softc(dev); 487a94100faSBill Paul 488a94100faSBill Paul if (sc->rl_type == RL_8169) { 489a94100faSBill Paul rval = re_gmii_readreg(dev, phy, reg); 490a94100faSBill Paul return (rval); 491a94100faSBill Paul } 492a94100faSBill Paul 493a94100faSBill Paul switch (reg) { 494a94100faSBill Paul case MII_BMCR: 495a94100faSBill Paul re8139_reg = RL_BMCR; 496a94100faSBill Paul break; 497a94100faSBill Paul case MII_BMSR: 498a94100faSBill Paul re8139_reg = RL_BMSR; 499a94100faSBill Paul break; 500a94100faSBill Paul case MII_ANAR: 501a94100faSBill Paul re8139_reg = RL_ANAR; 502a94100faSBill Paul break; 503a94100faSBill Paul case MII_ANER: 504a94100faSBill Paul re8139_reg = RL_ANER; 505a94100faSBill Paul break; 506a94100faSBill Paul case MII_ANLPAR: 507a94100faSBill Paul re8139_reg = RL_LPAR; 508a94100faSBill Paul break; 509a94100faSBill Paul case MII_PHYIDR1: 510a94100faSBill Paul case MII_PHYIDR2: 511a94100faSBill Paul return (0); 512a94100faSBill Paul /* 513a94100faSBill Paul * Allow the rlphy driver to read the media status 514a94100faSBill Paul * register. If we have a link partner which does not 515a94100faSBill Paul * support NWAY, this is the register which will tell 516a94100faSBill Paul * us the results of parallel detection. 517a94100faSBill Paul */ 518a94100faSBill Paul case RL_MEDIASTAT: 519a94100faSBill Paul rval = CSR_READ_1(sc, RL_MEDIASTAT); 520a94100faSBill Paul return (rval); 521a94100faSBill Paul default: 5226b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "bad phy register\n"); 523a94100faSBill Paul return (0); 524a94100faSBill Paul } 525a94100faSBill Paul rval = CSR_READ_2(sc, re8139_reg); 526baa12772SPyun YongHyeon if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) { 527baa12772SPyun YongHyeon /* 8139C+ has different bit layout. */ 528baa12772SPyun YongHyeon rval &= ~(BMCR_LOOP | BMCR_ISO); 529baa12772SPyun YongHyeon } 530a94100faSBill Paul return (rval); 531a94100faSBill Paul } 532a94100faSBill Paul 533a94100faSBill Paul static int 5347b5ffebfSPyun YongHyeon re_miibus_writereg(device_t dev, int phy, int reg, int data) 535a94100faSBill Paul { 536a94100faSBill Paul struct rl_softc *sc; 537a94100faSBill Paul u_int16_t re8139_reg = 0; 538a94100faSBill Paul int rval = 0; 539a94100faSBill Paul 540a94100faSBill Paul sc = device_get_softc(dev); 541a94100faSBill Paul 542a94100faSBill Paul if (sc->rl_type == RL_8169) { 543a94100faSBill Paul rval = re_gmii_writereg(dev, phy, reg, data); 544a94100faSBill Paul return (rval); 545a94100faSBill Paul } 546a94100faSBill Paul 547a94100faSBill Paul switch (reg) { 548a94100faSBill Paul case MII_BMCR: 549a94100faSBill Paul re8139_reg = RL_BMCR; 550baa12772SPyun YongHyeon if (sc->rl_type == RL_8139CPLUS) { 551baa12772SPyun YongHyeon /* 8139C+ has different bit layout. */ 552baa12772SPyun YongHyeon data &= ~(BMCR_LOOP | BMCR_ISO); 553baa12772SPyun YongHyeon } 554a94100faSBill Paul break; 555a94100faSBill Paul case MII_BMSR: 556a94100faSBill Paul re8139_reg = RL_BMSR; 557a94100faSBill Paul break; 558a94100faSBill Paul case MII_ANAR: 559a94100faSBill Paul re8139_reg = RL_ANAR; 560a94100faSBill Paul break; 561a94100faSBill Paul case MII_ANER: 562a94100faSBill Paul re8139_reg = RL_ANER; 563a94100faSBill Paul break; 564a94100faSBill Paul case MII_ANLPAR: 565a94100faSBill Paul re8139_reg = RL_LPAR; 566a94100faSBill Paul break; 567a94100faSBill Paul case MII_PHYIDR1: 568a94100faSBill Paul case MII_PHYIDR2: 569a94100faSBill Paul return (0); 570a94100faSBill Paul break; 571a94100faSBill Paul default: 5726b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "bad phy register\n"); 573a94100faSBill Paul return (0); 574a94100faSBill Paul } 575a94100faSBill Paul CSR_WRITE_2(sc, re8139_reg, data); 576a94100faSBill Paul return (0); 577a94100faSBill Paul } 578a94100faSBill Paul 579a94100faSBill Paul static void 5807b5ffebfSPyun YongHyeon re_miibus_statchg(device_t dev) 581a94100faSBill Paul { 582130b6dfbSPyun YongHyeon struct rl_softc *sc; 583130b6dfbSPyun YongHyeon struct ifnet *ifp; 584130b6dfbSPyun YongHyeon struct mii_data *mii; 585a11e2f18SBruce M Simpson 586130b6dfbSPyun YongHyeon sc = device_get_softc(dev); 587130b6dfbSPyun YongHyeon mii = device_get_softc(sc->rl_miibus); 588130b6dfbSPyun YongHyeon ifp = sc->rl_ifp; 589130b6dfbSPyun YongHyeon if (mii == NULL || ifp == NULL || 590130b6dfbSPyun YongHyeon (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 591130b6dfbSPyun YongHyeon return; 592130b6dfbSPyun YongHyeon 593130b6dfbSPyun YongHyeon sc->rl_flags &= ~RL_FLAG_LINK; 594130b6dfbSPyun YongHyeon if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) == 595130b6dfbSPyun YongHyeon (IFM_ACTIVE | IFM_AVALID)) { 596130b6dfbSPyun YongHyeon switch (IFM_SUBTYPE(mii->mii_media_active)) { 597130b6dfbSPyun YongHyeon case IFM_10_T: 598130b6dfbSPyun YongHyeon case IFM_100_TX: 599130b6dfbSPyun YongHyeon sc->rl_flags |= RL_FLAG_LINK; 600130b6dfbSPyun YongHyeon break; 601130b6dfbSPyun YongHyeon case IFM_1000_T: 602130b6dfbSPyun YongHyeon if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0) 603130b6dfbSPyun YongHyeon break; 604130b6dfbSPyun YongHyeon sc->rl_flags |= RL_FLAG_LINK; 605130b6dfbSPyun YongHyeon break; 606130b6dfbSPyun YongHyeon default: 607130b6dfbSPyun YongHyeon break; 608130b6dfbSPyun YongHyeon } 609130b6dfbSPyun YongHyeon } 610130b6dfbSPyun YongHyeon /* 611130b6dfbSPyun YongHyeon * RealTek controllers does not provide any interface to 612130b6dfbSPyun YongHyeon * Tx/Rx MACs for resolved speed, duplex and flow-control 613130b6dfbSPyun YongHyeon * parameters. 614130b6dfbSPyun YongHyeon */ 615a94100faSBill Paul } 616a94100faSBill Paul 617a94100faSBill Paul /* 618ff191365SJung-uk Kim * Set the RX configuration and 64-bit multicast hash filter. 619a94100faSBill Paul */ 620a94100faSBill Paul static void 621ff191365SJung-uk Kim re_set_rxmode(struct rl_softc *sc) 622a94100faSBill Paul { 623a94100faSBill Paul struct ifnet *ifp; 624a94100faSBill Paul struct ifmultiaddr *ifma; 625ff191365SJung-uk Kim uint32_t hashes[2] = { 0, 0 }; 626ff191365SJung-uk Kim uint32_t h, rxfilt; 627a94100faSBill Paul 62897b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 62997b9d4baSJohn-Mark Gurney 630fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 631a94100faSBill Paul 632ff191365SJung-uk Kim rxfilt = RL_RXCFG_CONFIG | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_BROAD; 633a94100faSBill Paul 634ff191365SJung-uk Kim if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) { 6357c103000SPyun YongHyeon if (ifp->if_flags & IFF_PROMISC) 6367c103000SPyun YongHyeon rxfilt |= RL_RXCFG_RX_ALLPHYS; 637a0637caaSPyun YongHyeon /* 638a0637caaSPyun YongHyeon * Unlike other hardwares, we have to explicitly set 639a0637caaSPyun YongHyeon * RL_RXCFG_RX_MULTI to receive multicast frames in 640a0637caaSPyun YongHyeon * promiscuous mode. 641a0637caaSPyun YongHyeon */ 642a94100faSBill Paul rxfilt |= RL_RXCFG_RX_MULTI; 643ff191365SJung-uk Kim hashes[0] = hashes[1] = 0xffffffff; 644ff191365SJung-uk Kim goto done; 645a94100faSBill Paul } 646a94100faSBill Paul 647eb956cd0SRobert Watson if_maddr_rlock(ifp); 648a94100faSBill Paul TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 649a94100faSBill Paul if (ifma->ifma_addr->sa_family != AF_LINK) 650a94100faSBill Paul continue; 6510e939c0cSChristian Weisgerber h = ether_crc32_be(LLADDR((struct sockaddr_dl *) 6520e939c0cSChristian Weisgerber ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 653a94100faSBill Paul if (h < 32) 654a94100faSBill Paul hashes[0] |= (1 << h); 655a94100faSBill Paul else 656a94100faSBill Paul hashes[1] |= (1 << (h - 32)); 657a94100faSBill Paul } 658eb956cd0SRobert Watson if_maddr_runlock(ifp); 659a94100faSBill Paul 660ff191365SJung-uk Kim if (hashes[0] != 0 || hashes[1] != 0) { 661bb7dfefbSBill Paul /* 662ff191365SJung-uk Kim * For some unfathomable reason, RealTek decided to 663ff191365SJung-uk Kim * reverse the order of the multicast hash registers 664ff191365SJung-uk Kim * in the PCI Express parts. This means we have to 665ff191365SJung-uk Kim * write the hash pattern in reverse order for those 666ff191365SJung-uk Kim * devices. 667bb7dfefbSBill Paul */ 668aaab4fbeSJung-uk Kim if ((sc->rl_flags & RL_FLAG_PCIE) != 0) { 669ff191365SJung-uk Kim h = bswap32(hashes[0]); 670ff191365SJung-uk Kim hashes[0] = bswap32(hashes[1]); 671ff191365SJung-uk Kim hashes[1] = h; 672ff191365SJung-uk Kim } 673ff191365SJung-uk Kim rxfilt |= RL_RXCFG_RX_MULTI; 674ff191365SJung-uk Kim } 675ff191365SJung-uk Kim 676ff191365SJung-uk Kim done: 677a94100faSBill Paul CSR_WRITE_4(sc, RL_MAR0, hashes[0]); 678a94100faSBill Paul CSR_WRITE_4(sc, RL_MAR4, hashes[1]); 679ff191365SJung-uk Kim CSR_WRITE_4(sc, RL_RXCFG, rxfilt); 680bb7dfefbSBill Paul } 681a94100faSBill Paul 682a94100faSBill Paul static void 6837b5ffebfSPyun YongHyeon re_reset(struct rl_softc *sc) 684a94100faSBill Paul { 6850ce0868aSPyun YongHyeon int i; 686a94100faSBill Paul 68797b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 68897b9d4baSJohn-Mark Gurney 689a94100faSBill Paul CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET); 690a94100faSBill Paul 691a94100faSBill Paul for (i = 0; i < RL_TIMEOUT; i++) { 692a94100faSBill Paul DELAY(10); 693a94100faSBill Paul if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET)) 694a94100faSBill Paul break; 695a94100faSBill Paul } 696a94100faSBill Paul if (i == RL_TIMEOUT) 6976b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "reset never completed!\n"); 698a94100faSBill Paul 699566ca8caSJung-uk Kim if ((sc->rl_flags & RL_FLAG_MACRESET) != 0) 700a94100faSBill Paul CSR_WRITE_1(sc, 0x82, 1); 701566ca8caSJung-uk Kim if (sc->rl_hwrev == RL_HWREV_8169S) 702566ca8caSJung-uk Kim re_gmii_writereg(sc->rl_dev, 1, 0x0b, 0); 703a94100faSBill Paul } 704a94100faSBill Paul 705ed510fb0SBill Paul #ifdef RE_DIAG 706ed510fb0SBill Paul 707a94100faSBill Paul /* 708a94100faSBill Paul * The following routine is designed to test for a defect on some 709a94100faSBill Paul * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64# 710a94100faSBill Paul * lines connected to the bus, however for a 32-bit only card, they 711a94100faSBill Paul * should be pulled high. The result of this defect is that the 712a94100faSBill Paul * NIC will not work right if you plug it into a 64-bit slot: DMA 713a94100faSBill Paul * operations will be done with 64-bit transfers, which will fail 714a94100faSBill Paul * because the 64-bit data lines aren't connected. 715a94100faSBill Paul * 716a94100faSBill Paul * There's no way to work around this (short of talking a soldering 717a94100faSBill Paul * iron to the board), however we can detect it. The method we use 718a94100faSBill Paul * here is to put the NIC into digital loopback mode, set the receiver 719a94100faSBill Paul * to promiscuous mode, and then try to send a frame. We then compare 720a94100faSBill Paul * the frame data we sent to what was received. If the data matches, 721a94100faSBill Paul * then the NIC is working correctly, otherwise we know the user has 722a94100faSBill Paul * a defective NIC which has been mistakenly plugged into a 64-bit PCI 723a94100faSBill Paul * slot. In the latter case, there's no way the NIC can work correctly, 724a94100faSBill Paul * so we print out a message on the console and abort the device attach. 725a94100faSBill Paul */ 726a94100faSBill Paul 727a94100faSBill Paul static int 7287b5ffebfSPyun YongHyeon re_diag(struct rl_softc *sc) 729a94100faSBill Paul { 730fc74a9f9SBrooks Davis struct ifnet *ifp = sc->rl_ifp; 731a94100faSBill Paul struct mbuf *m0; 732a94100faSBill Paul struct ether_header *eh; 733a94100faSBill Paul struct rl_desc *cur_rx; 734a94100faSBill Paul u_int16_t status; 735a94100faSBill Paul u_int32_t rxstat; 736ed510fb0SBill Paul int total_len, i, error = 0, phyaddr; 737a94100faSBill Paul u_int8_t dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' }; 738a94100faSBill Paul u_int8_t src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' }; 739a94100faSBill Paul 740a94100faSBill Paul /* Allocate a single mbuf */ 741a94100faSBill Paul MGETHDR(m0, M_DONTWAIT, MT_DATA); 742a94100faSBill Paul if (m0 == NULL) 743a94100faSBill Paul return (ENOBUFS); 744a94100faSBill Paul 74597b9d4baSJohn-Mark Gurney RL_LOCK(sc); 74697b9d4baSJohn-Mark Gurney 747a94100faSBill Paul /* 748a94100faSBill Paul * Initialize the NIC in test mode. This sets the chip up 749a94100faSBill Paul * so that it can send and receive frames, but performs the 750a94100faSBill Paul * following special functions: 751a94100faSBill Paul * - Puts receiver in promiscuous mode 752a94100faSBill Paul * - Enables digital loopback mode 753a94100faSBill Paul * - Leaves interrupts turned off 754a94100faSBill Paul */ 755a94100faSBill Paul 756a94100faSBill Paul ifp->if_flags |= IFF_PROMISC; 757a94100faSBill Paul sc->rl_testmode = 1; 7588476c243SPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 75997b9d4baSJohn-Mark Gurney re_init_locked(sc); 760351a76f9SPyun YongHyeon sc->rl_flags |= RL_FLAG_LINK; 761ed510fb0SBill Paul if (sc->rl_type == RL_8169) 762ed510fb0SBill Paul phyaddr = 1; 763ed510fb0SBill Paul else 764ed510fb0SBill Paul phyaddr = 0; 765ed510fb0SBill Paul 766ed510fb0SBill Paul re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET); 767ed510fb0SBill Paul for (i = 0; i < RL_TIMEOUT; i++) { 768ed510fb0SBill Paul status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR); 769ed510fb0SBill Paul if (!(status & BMCR_RESET)) 770ed510fb0SBill Paul break; 771ed510fb0SBill Paul } 772ed510fb0SBill Paul 773ed510fb0SBill Paul re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP); 774ed510fb0SBill Paul CSR_WRITE_2(sc, RL_ISR, RL_INTRS); 775ed510fb0SBill Paul 776804af9a1SBill Paul DELAY(100000); 777a94100faSBill Paul 778a94100faSBill Paul /* Put some data in the mbuf */ 779a94100faSBill Paul 780a94100faSBill Paul eh = mtod(m0, struct ether_header *); 781a94100faSBill Paul bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN); 782a94100faSBill Paul bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN); 783a94100faSBill Paul eh->ether_type = htons(ETHERTYPE_IP); 784a94100faSBill Paul m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN; 785a94100faSBill Paul 7867cae6651SBill Paul /* 7877cae6651SBill Paul * Queue the packet, start transmission. 7887cae6651SBill Paul * Note: IF_HANDOFF() ultimately calls re_start() for us. 7897cae6651SBill Paul */ 790a94100faSBill Paul 791abc8ff44SBill Paul CSR_WRITE_2(sc, RL_ISR, 0xFFFF); 79297b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 79352732175SMax Laier /* XXX: re_diag must not be called when in ALTQ mode */ 7947cae6651SBill Paul IF_HANDOFF(&ifp->if_snd, m0, ifp); 79597b9d4baSJohn-Mark Gurney RL_LOCK(sc); 796a94100faSBill Paul m0 = NULL; 797a94100faSBill Paul 798a94100faSBill Paul /* Wait for it to propagate through the chip */ 799a94100faSBill Paul 800abc8ff44SBill Paul DELAY(100000); 801a94100faSBill Paul for (i = 0; i < RL_TIMEOUT; i++) { 802a94100faSBill Paul status = CSR_READ_2(sc, RL_ISR); 803ed510fb0SBill Paul CSR_WRITE_2(sc, RL_ISR, status); 804abc8ff44SBill Paul if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) == 805abc8ff44SBill Paul (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) 806a94100faSBill Paul break; 807a94100faSBill Paul DELAY(10); 808a94100faSBill Paul } 809a94100faSBill Paul 810a94100faSBill Paul if (i == RL_TIMEOUT) { 8116b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, 8126b9f5c94SGleb Smirnoff "diagnostic failed, failed to receive packet in" 8136b9f5c94SGleb Smirnoff " loopback mode\n"); 814a94100faSBill Paul error = EIO; 815a94100faSBill Paul goto done; 816a94100faSBill Paul } 817a94100faSBill Paul 818a94100faSBill Paul /* 819a94100faSBill Paul * The packet should have been dumped into the first 820a94100faSBill Paul * entry in the RX DMA ring. Grab it from there. 821a94100faSBill Paul */ 822a94100faSBill Paul 823a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 824a94100faSBill Paul sc->rl_ldata.rl_rx_list_map, 825a94100faSBill Paul BUS_DMASYNC_POSTREAD); 826d65abd66SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, 827d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc[0].rx_dmamap, 828d65abd66SPyun YongHyeon BUS_DMASYNC_POSTREAD); 829d65abd66SPyun YongHyeon bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, 830d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc[0].rx_dmamap); 831a94100faSBill Paul 832d65abd66SPyun YongHyeon m0 = sc->rl_ldata.rl_rx_desc[0].rx_m; 833d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc[0].rx_m = NULL; 834a94100faSBill Paul eh = mtod(m0, struct ether_header *); 835a94100faSBill Paul 836a94100faSBill Paul cur_rx = &sc->rl_ldata.rl_rx_list[0]; 837a94100faSBill Paul total_len = RL_RXBYTES(cur_rx); 838a94100faSBill Paul rxstat = le32toh(cur_rx->rl_cmdstat); 839a94100faSBill Paul 840a94100faSBill Paul if (total_len != ETHER_MIN_LEN) { 8416b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, 8426b9f5c94SGleb Smirnoff "diagnostic failed, received short packet\n"); 843a94100faSBill Paul error = EIO; 844a94100faSBill Paul goto done; 845a94100faSBill Paul } 846a94100faSBill Paul 847a94100faSBill Paul /* Test that the received packet data matches what we sent. */ 848a94100faSBill Paul 849a94100faSBill Paul if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) || 850a94100faSBill Paul bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) || 851a94100faSBill Paul ntohs(eh->ether_type) != ETHERTYPE_IP) { 8526b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n"); 8536b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n", 854a94100faSBill Paul dst, ":", src, ":", ETHERTYPE_IP); 8556b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n", 856a94100faSBill Paul eh->ether_dhost, ":", eh->ether_shost, ":", 857a94100faSBill Paul ntohs(eh->ether_type)); 8586b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "You may have a defective 32-bit " 8596b9f5c94SGleb Smirnoff "NIC plugged into a 64-bit PCI slot.\n"); 8606b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "Please re-install the NIC in a " 8616b9f5c94SGleb Smirnoff "32-bit slot for proper operation.\n"); 8626b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "Read the re(4) man page for more " 8636b9f5c94SGleb Smirnoff "details.\n"); 864a94100faSBill Paul error = EIO; 865a94100faSBill Paul } 866a94100faSBill Paul 867a94100faSBill Paul done: 868a94100faSBill Paul /* Turn interface off, release resources */ 869a94100faSBill Paul 870a94100faSBill Paul sc->rl_testmode = 0; 871351a76f9SPyun YongHyeon sc->rl_flags &= ~RL_FLAG_LINK; 872a94100faSBill Paul ifp->if_flags &= ~IFF_PROMISC; 873a94100faSBill Paul re_stop(sc); 874a94100faSBill Paul if (m0 != NULL) 875a94100faSBill Paul m_freem(m0); 876a94100faSBill Paul 87797b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 87897b9d4baSJohn-Mark Gurney 879a94100faSBill Paul return (error); 880a94100faSBill Paul } 881a94100faSBill Paul 882ed510fb0SBill Paul #endif 883ed510fb0SBill Paul 884a94100faSBill Paul /* 885a94100faSBill Paul * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device 886a94100faSBill Paul * IDs against our list and return a device name if we find a match. 887a94100faSBill Paul */ 888a94100faSBill Paul static int 8897b5ffebfSPyun YongHyeon re_probe(device_t dev) 890a94100faSBill Paul { 891a94100faSBill Paul struct rl_type *t; 892dfdb409eSPyun YongHyeon uint16_t devid, vendor; 893dfdb409eSPyun YongHyeon uint16_t revid, sdevid; 894dfdb409eSPyun YongHyeon int i; 895a94100faSBill Paul 896dfdb409eSPyun YongHyeon vendor = pci_get_vendor(dev); 897dfdb409eSPyun YongHyeon devid = pci_get_device(dev); 898dfdb409eSPyun YongHyeon revid = pci_get_revid(dev); 899dfdb409eSPyun YongHyeon sdevid = pci_get_subdevice(dev); 900a94100faSBill Paul 901dfdb409eSPyun YongHyeon if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) { 902dfdb409eSPyun YongHyeon if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) { 90326390635SJohn Baldwin /* 90426390635SJohn Baldwin * Only attach to rev. 3 of the Linksys EG1032 adapter. 905dfdb409eSPyun YongHyeon * Rev. 2 is supported by sk(4). 90626390635SJohn Baldwin */ 907a94100faSBill Paul return (ENXIO); 908a94100faSBill Paul } 909dfdb409eSPyun YongHyeon } 910dfdb409eSPyun YongHyeon 911dfdb409eSPyun YongHyeon if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) { 912dfdb409eSPyun YongHyeon if (revid != 0x20) { 913dfdb409eSPyun YongHyeon /* 8139, let rl(4) take care of this device. */ 914dfdb409eSPyun YongHyeon return (ENXIO); 915dfdb409eSPyun YongHyeon } 916dfdb409eSPyun YongHyeon } 917dfdb409eSPyun YongHyeon 918dfdb409eSPyun YongHyeon t = re_devs; 919dfdb409eSPyun YongHyeon for (i = 0; i < sizeof(re_devs) / sizeof(re_devs[0]); i++, t++) { 920dfdb409eSPyun YongHyeon if (vendor == t->rl_vid && devid == t->rl_did) { 921a94100faSBill Paul device_set_desc(dev, t->rl_name); 922d2b677bbSWarner Losh return (BUS_PROBE_DEFAULT); 923a94100faSBill Paul } 924a94100faSBill Paul } 925a94100faSBill Paul 926a94100faSBill Paul return (ENXIO); 927a94100faSBill Paul } 928a94100faSBill Paul 929a94100faSBill Paul /* 930a94100faSBill Paul * Map a single buffer address. 931a94100faSBill Paul */ 932a94100faSBill Paul 933a94100faSBill Paul static void 9347b5ffebfSPyun YongHyeon re_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error) 935a94100faSBill Paul { 9368fd99e38SPyun YongHyeon bus_addr_t *addr; 937a94100faSBill Paul 938a94100faSBill Paul if (error) 939a94100faSBill Paul return; 940a94100faSBill Paul 941a94100faSBill Paul KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 942a94100faSBill Paul addr = arg; 943a94100faSBill Paul *addr = segs->ds_addr; 944a94100faSBill Paul } 945a94100faSBill Paul 946a94100faSBill Paul static int 9477b5ffebfSPyun YongHyeon re_allocmem(device_t dev, struct rl_softc *sc) 948a94100faSBill Paul { 94966366ca4SPyun YongHyeon bus_addr_t lowaddr; 950d65abd66SPyun YongHyeon bus_size_t rx_list_size, tx_list_size; 951a94100faSBill Paul int error; 952a94100faSBill Paul int i; 953a94100faSBill Paul 954d65abd66SPyun YongHyeon rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc); 955d65abd66SPyun YongHyeon tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc); 956d65abd66SPyun YongHyeon 957d65abd66SPyun YongHyeon /* 958d65abd66SPyun YongHyeon * Allocate the parent bus DMA tag appropriate for PCI. 959ce628393SPyun YongHyeon * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD 960ce628393SPyun YongHyeon * register should be set. However some RealTek chips are known 961ce628393SPyun YongHyeon * to be buggy on DAC handling, therefore disable DAC by limiting 962ce628393SPyun YongHyeon * DMA address space to 32bit. PCIe variants of RealTek chips 96366366ca4SPyun YongHyeon * may not have the limitation. 964d65abd66SPyun YongHyeon */ 96566366ca4SPyun YongHyeon lowaddr = BUS_SPACE_MAXADDR; 96666366ca4SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_PCIE) == 0) 96766366ca4SPyun YongHyeon lowaddr = BUS_SPACE_MAXADDR_32BIT; 968d65abd66SPyun YongHyeon error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0, 96966366ca4SPyun YongHyeon lowaddr, BUS_SPACE_MAXADDR, NULL, NULL, 970d65abd66SPyun YongHyeon BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0, 971d65abd66SPyun YongHyeon NULL, NULL, &sc->rl_parent_tag); 972d65abd66SPyun YongHyeon if (error) { 973d65abd66SPyun YongHyeon device_printf(dev, "could not allocate parent DMA tag\n"); 974d65abd66SPyun YongHyeon return (error); 975d65abd66SPyun YongHyeon } 976d65abd66SPyun YongHyeon 977d65abd66SPyun YongHyeon /* 978d65abd66SPyun YongHyeon * Allocate map for TX mbufs. 979d65abd66SPyun YongHyeon */ 980d65abd66SPyun YongHyeon error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0, 981d65abd66SPyun YongHyeon BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, 982d65abd66SPyun YongHyeon NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0, 983d65abd66SPyun YongHyeon NULL, NULL, &sc->rl_ldata.rl_tx_mtag); 984d65abd66SPyun YongHyeon if (error) { 985d65abd66SPyun YongHyeon device_printf(dev, "could not allocate TX DMA tag\n"); 986d65abd66SPyun YongHyeon return (error); 987d65abd66SPyun YongHyeon } 988d65abd66SPyun YongHyeon 989a94100faSBill Paul /* 990a94100faSBill Paul * Allocate map for RX mbufs. 991a94100faSBill Paul */ 992d65abd66SPyun YongHyeon 993d65abd66SPyun YongHyeon error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0, 994d65abd66SPyun YongHyeon BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 995d65abd66SPyun YongHyeon MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag); 996a94100faSBill Paul if (error) { 997d65abd66SPyun YongHyeon device_printf(dev, "could not allocate RX DMA tag\n"); 998d65abd66SPyun YongHyeon return (error); 999a94100faSBill Paul } 1000a94100faSBill Paul 1001a94100faSBill Paul /* 1002a94100faSBill Paul * Allocate map for TX descriptor list. 1003a94100faSBill Paul */ 1004a94100faSBill Paul error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN, 1005a94100faSBill Paul 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 1006d65abd66SPyun YongHyeon NULL, tx_list_size, 1, tx_list_size, 0, 1007a94100faSBill Paul NULL, NULL, &sc->rl_ldata.rl_tx_list_tag); 1008a94100faSBill Paul if (error) { 1009d65abd66SPyun YongHyeon device_printf(dev, "could not allocate TX DMA ring tag\n"); 1010d65abd66SPyun YongHyeon return (error); 1011a94100faSBill Paul } 1012a94100faSBill Paul 1013a94100faSBill Paul /* Allocate DMA'able memory for the TX ring */ 1014a94100faSBill Paul 1015a94100faSBill Paul error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag, 1016d65abd66SPyun YongHyeon (void **)&sc->rl_ldata.rl_tx_list, 1017d65abd66SPyun YongHyeon BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, 1018a94100faSBill Paul &sc->rl_ldata.rl_tx_list_map); 1019d65abd66SPyun YongHyeon if (error) { 1020d65abd66SPyun YongHyeon device_printf(dev, "could not allocate TX DMA ring\n"); 1021d65abd66SPyun YongHyeon return (error); 1022d65abd66SPyun YongHyeon } 1023a94100faSBill Paul 1024a94100faSBill Paul /* Load the map for the TX ring. */ 1025a94100faSBill Paul 1026d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_list_addr = 0; 1027a94100faSBill Paul error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag, 1028a94100faSBill Paul sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list, 1029d65abd66SPyun YongHyeon tx_list_size, re_dma_map_addr, 1030a94100faSBill Paul &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT); 1031d65abd66SPyun YongHyeon if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) { 1032d65abd66SPyun YongHyeon device_printf(dev, "could not load TX DMA ring\n"); 1033d65abd66SPyun YongHyeon return (ENOMEM); 1034d65abd66SPyun YongHyeon } 1035a94100faSBill Paul 1036a94100faSBill Paul /* Create DMA maps for TX buffers */ 1037a94100faSBill Paul 1038d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) { 1039d65abd66SPyun YongHyeon error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0, 1040d65abd66SPyun YongHyeon &sc->rl_ldata.rl_tx_desc[i].tx_dmamap); 1041a94100faSBill Paul if (error) { 1042d65abd66SPyun YongHyeon device_printf(dev, "could not create DMA map for TX\n"); 1043d65abd66SPyun YongHyeon return (error); 1044a94100faSBill Paul } 1045a94100faSBill Paul } 1046a94100faSBill Paul 1047a94100faSBill Paul /* 1048a94100faSBill Paul * Allocate map for RX descriptor list. 1049a94100faSBill Paul */ 1050a94100faSBill Paul error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN, 1051a94100faSBill Paul 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 1052d65abd66SPyun YongHyeon NULL, rx_list_size, 1, rx_list_size, 0, 1053a94100faSBill Paul NULL, NULL, &sc->rl_ldata.rl_rx_list_tag); 1054a94100faSBill Paul if (error) { 1055d65abd66SPyun YongHyeon device_printf(dev, "could not create RX DMA ring tag\n"); 1056d65abd66SPyun YongHyeon return (error); 1057a94100faSBill Paul } 1058a94100faSBill Paul 1059a94100faSBill Paul /* Allocate DMA'able memory for the RX ring */ 1060a94100faSBill Paul 1061a94100faSBill Paul error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag, 1062d65abd66SPyun YongHyeon (void **)&sc->rl_ldata.rl_rx_list, 1063d65abd66SPyun YongHyeon BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, 1064a94100faSBill Paul &sc->rl_ldata.rl_rx_list_map); 1065d65abd66SPyun YongHyeon if (error) { 1066d65abd66SPyun YongHyeon device_printf(dev, "could not allocate RX DMA ring\n"); 1067d65abd66SPyun YongHyeon return (error); 1068d65abd66SPyun YongHyeon } 1069a94100faSBill Paul 1070a94100faSBill Paul /* Load the map for the RX ring. */ 1071a94100faSBill Paul 1072d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_list_addr = 0; 1073a94100faSBill Paul error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag, 1074a94100faSBill Paul sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list, 1075d65abd66SPyun YongHyeon rx_list_size, re_dma_map_addr, 1076a94100faSBill Paul &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT); 1077d65abd66SPyun YongHyeon if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) { 1078d65abd66SPyun YongHyeon device_printf(dev, "could not load RX DMA ring\n"); 1079d65abd66SPyun YongHyeon return (ENOMEM); 1080d65abd66SPyun YongHyeon } 1081a94100faSBill Paul 1082a94100faSBill Paul /* Create DMA maps for RX buffers */ 1083a94100faSBill Paul 1084d65abd66SPyun YongHyeon error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0, 1085d65abd66SPyun YongHyeon &sc->rl_ldata.rl_rx_sparemap); 1086a94100faSBill Paul if (error) { 1087d65abd66SPyun YongHyeon device_printf(dev, "could not create spare DMA map for RX\n"); 1088d65abd66SPyun YongHyeon return (error); 1089d65abd66SPyun YongHyeon } 1090d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 1091d65abd66SPyun YongHyeon error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0, 1092d65abd66SPyun YongHyeon &sc->rl_ldata.rl_rx_desc[i].rx_dmamap); 1093d65abd66SPyun YongHyeon if (error) { 1094d65abd66SPyun YongHyeon device_printf(dev, "could not create DMA map for RX\n"); 1095d65abd66SPyun YongHyeon return (error); 1096a94100faSBill Paul } 1097a94100faSBill Paul } 1098a94100faSBill Paul 10990534aae0SPyun YongHyeon /* Create DMA map for statistics. */ 11000534aae0SPyun YongHyeon error = bus_dma_tag_create(sc->rl_parent_tag, RL_DUMP_ALIGN, 0, 11010534aae0SPyun YongHyeon BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 11020534aae0SPyun YongHyeon sizeof(struct rl_stats), 1, sizeof(struct rl_stats), 0, NULL, NULL, 11030534aae0SPyun YongHyeon &sc->rl_ldata.rl_stag); 11040534aae0SPyun YongHyeon if (error) { 11050534aae0SPyun YongHyeon device_printf(dev, "could not create statistics DMA tag\n"); 11060534aae0SPyun YongHyeon return (error); 11070534aae0SPyun YongHyeon } 11080534aae0SPyun YongHyeon /* Allocate DMA'able memory for statistics. */ 11090534aae0SPyun YongHyeon error = bus_dmamem_alloc(sc->rl_ldata.rl_stag, 11100534aae0SPyun YongHyeon (void **)&sc->rl_ldata.rl_stats, 11110534aae0SPyun YongHyeon BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, 11120534aae0SPyun YongHyeon &sc->rl_ldata.rl_smap); 11130534aae0SPyun YongHyeon if (error) { 11140534aae0SPyun YongHyeon device_printf(dev, 11150534aae0SPyun YongHyeon "could not allocate statistics DMA memory\n"); 11160534aae0SPyun YongHyeon return (error); 11170534aae0SPyun YongHyeon } 11180534aae0SPyun YongHyeon /* Load the map for statistics. */ 11190534aae0SPyun YongHyeon sc->rl_ldata.rl_stats_addr = 0; 11200534aae0SPyun YongHyeon error = bus_dmamap_load(sc->rl_ldata.rl_stag, sc->rl_ldata.rl_smap, 11210534aae0SPyun YongHyeon sc->rl_ldata.rl_stats, sizeof(struct rl_stats), re_dma_map_addr, 11220534aae0SPyun YongHyeon &sc->rl_ldata.rl_stats_addr, BUS_DMA_NOWAIT); 11230534aae0SPyun YongHyeon if (error != 0 || sc->rl_ldata.rl_stats_addr == 0) { 11240534aae0SPyun YongHyeon device_printf(dev, "could not load statistics DMA memory\n"); 11250534aae0SPyun YongHyeon return (ENOMEM); 11260534aae0SPyun YongHyeon } 11270534aae0SPyun YongHyeon 1128a94100faSBill Paul return (0); 1129a94100faSBill Paul } 1130a94100faSBill Paul 1131a94100faSBill Paul /* 1132a94100faSBill Paul * Attach the interface. Allocate softc structures, do ifmedia 1133a94100faSBill Paul * setup and ethernet/BPF attach. 1134a94100faSBill Paul */ 1135a94100faSBill Paul static int 11367b5ffebfSPyun YongHyeon re_attach(device_t dev) 1137a94100faSBill Paul { 1138a94100faSBill Paul u_char eaddr[ETHER_ADDR_LEN]; 1139be099007SPyun YongHyeon u_int16_t as[ETHER_ADDR_LEN / 2]; 1140a94100faSBill Paul struct rl_softc *sc; 1141a94100faSBill Paul struct ifnet *ifp; 1142a94100faSBill Paul struct rl_hwrev *hw_rev; 1143a94100faSBill Paul int hwrev; 1144ace7ed5dSPyun YongHyeon u_int16_t devid, re_did = 0; 11458e5d93dbSMarius Strobl int error = 0, i, phy, rid; 11465774c5ffSPyun YongHyeon int msic, reg; 114703ca7ae8SPyun YongHyeon uint8_t cfg; 1148a94100faSBill Paul 1149a94100faSBill Paul sc = device_get_softc(dev); 1150ed510fb0SBill Paul sc->rl_dev = dev; 1151a94100faSBill Paul 1152a94100faSBill Paul mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 115397b9d4baSJohn-Mark Gurney MTX_DEF); 1154d1754a9bSJohn Baldwin callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0); 1155d1754a9bSJohn Baldwin 1156a94100faSBill Paul /* 1157a94100faSBill Paul * Map control/status registers. 1158a94100faSBill Paul */ 1159a94100faSBill Paul pci_enable_busmaster(dev); 1160a94100faSBill Paul 1161ace7ed5dSPyun YongHyeon devid = pci_get_device(dev); 11622c21710bSPyun YongHyeon /* 11632c21710bSPyun YongHyeon * Prefer memory space register mapping over IO space. 11642c21710bSPyun YongHyeon * Because RTL8169SC does not seem to work when memory mapping 11652c21710bSPyun YongHyeon * is used always activate io mapping. 11662c21710bSPyun YongHyeon */ 11672c21710bSPyun YongHyeon if (devid == RT_DEVICEID_8169SC) 11682c21710bSPyun YongHyeon prefer_iomap = 1; 11692c21710bSPyun YongHyeon if (prefer_iomap == 0) { 1170ace7ed5dSPyun YongHyeon sc->rl_res_id = PCIR_BAR(1); 1171ace7ed5dSPyun YongHyeon sc->rl_res_type = SYS_RES_MEMORY; 1172ace7ed5dSPyun YongHyeon /* RTL8168/8101E seems to use different BARs. */ 1173ace7ed5dSPyun YongHyeon if (devid == RT_DEVICEID_8168 || devid == RT_DEVICEID_8101E) 1174ace7ed5dSPyun YongHyeon sc->rl_res_id = PCIR_BAR(2); 11752c21710bSPyun YongHyeon } else { 11762c21710bSPyun YongHyeon sc->rl_res_id = PCIR_BAR(0); 11772c21710bSPyun YongHyeon sc->rl_res_type = SYS_RES_IOPORT; 11782c21710bSPyun YongHyeon } 1179ace7ed5dSPyun YongHyeon sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type, 1180ace7ed5dSPyun YongHyeon &sc->rl_res_id, RF_ACTIVE); 11812c21710bSPyun YongHyeon if (sc->rl_res == NULL && prefer_iomap == 0) { 1182ace7ed5dSPyun YongHyeon sc->rl_res_id = PCIR_BAR(0); 1183ace7ed5dSPyun YongHyeon sc->rl_res_type = SYS_RES_IOPORT; 1184ace7ed5dSPyun YongHyeon sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type, 1185ace7ed5dSPyun YongHyeon &sc->rl_res_id, RF_ACTIVE); 11862c21710bSPyun YongHyeon } 1187ace7ed5dSPyun YongHyeon if (sc->rl_res == NULL) { 1188d1754a9bSJohn Baldwin device_printf(dev, "couldn't map ports/memory\n"); 1189a94100faSBill Paul error = ENXIO; 1190a94100faSBill Paul goto fail; 1191a94100faSBill Paul } 1192a94100faSBill Paul 1193a94100faSBill Paul sc->rl_btag = rman_get_bustag(sc->rl_res); 1194a94100faSBill Paul sc->rl_bhandle = rman_get_bushandle(sc->rl_res); 1195a94100faSBill Paul 11965774c5ffSPyun YongHyeon msic = 0; 11975774c5ffSPyun YongHyeon if (pci_find_extcap(dev, PCIY_EXPRESS, ®) == 0) { 1198818951afSPyun YongHyeon sc->rl_flags |= RL_FLAG_PCIE; 1199485d41c0SPyun YongHyeon if (devid != RT_DEVICEID_8101E) { 120002d77e45SPyun YongHyeon /* Set PCIe maximum read request size to 2048. */ 120102d77e45SPyun YongHyeon if (pci_get_max_read_req(dev) < 2048) 120202d77e45SPyun YongHyeon pci_set_max_read_req(dev, 2048); 1203485d41c0SPyun YongHyeon } 12045774c5ffSPyun YongHyeon msic = pci_msi_count(dev); 12055774c5ffSPyun YongHyeon if (bootverbose) 12065774c5ffSPyun YongHyeon device_printf(dev, "MSI count : %d\n", msic); 12075774c5ffSPyun YongHyeon } 1208f1bb696aSPyun YongHyeon if (msic > 0 && msi_disable == 0) { 1209f1bb696aSPyun YongHyeon msic = 1; 12105774c5ffSPyun YongHyeon if (pci_alloc_msi(dev, &msic) == 0) { 12115774c5ffSPyun YongHyeon if (msic == RL_MSI_MESSAGES) { 12125774c5ffSPyun YongHyeon device_printf(dev, "Using %d MSI messages\n", 12135774c5ffSPyun YongHyeon msic); 1214351a76f9SPyun YongHyeon sc->rl_flags |= RL_FLAG_MSI; 1215339a44fbSPyun YongHyeon /* Explicitly set MSI enable bit. */ 1216339a44fbSPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE); 1217339a44fbSPyun YongHyeon cfg = CSR_READ_1(sc, RL_CFG2); 1218339a44fbSPyun YongHyeon cfg |= RL_CFG2_MSI; 1219339a44fbSPyun YongHyeon CSR_WRITE_1(sc, RL_CFG2, cfg); 1220f98dd8cfSPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 12215774c5ffSPyun YongHyeon } else 12225774c5ffSPyun YongHyeon pci_release_msi(dev); 12235774c5ffSPyun YongHyeon } 12245774c5ffSPyun YongHyeon } 1225a94100faSBill Paul 12265774c5ffSPyun YongHyeon /* Allocate interrupt */ 1227351a76f9SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_MSI) == 0) { 12285774c5ffSPyun YongHyeon rid = 0; 12295774c5ffSPyun YongHyeon sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 12305774c5ffSPyun YongHyeon RF_SHAREABLE | RF_ACTIVE); 12315774c5ffSPyun YongHyeon if (sc->rl_irq[0] == NULL) { 12325774c5ffSPyun YongHyeon device_printf(dev, "couldn't allocate IRQ resources\n"); 1233a94100faSBill Paul error = ENXIO; 1234a94100faSBill Paul goto fail; 1235a94100faSBill Paul } 12365774c5ffSPyun YongHyeon } else { 12375774c5ffSPyun YongHyeon for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) { 12385774c5ffSPyun YongHyeon sc->rl_irq[i] = bus_alloc_resource_any(dev, 12395774c5ffSPyun YongHyeon SYS_RES_IRQ, &rid, RF_ACTIVE); 12405774c5ffSPyun YongHyeon if (sc->rl_irq[i] == NULL) { 12415774c5ffSPyun YongHyeon device_printf(dev, 12425774c5ffSPyun YongHyeon "couldn't llocate IRQ resources for " 12435774c5ffSPyun YongHyeon "message %d\n", rid); 12445774c5ffSPyun YongHyeon error = ENXIO; 12455774c5ffSPyun YongHyeon goto fail; 12465774c5ffSPyun YongHyeon } 12475774c5ffSPyun YongHyeon } 12485774c5ffSPyun YongHyeon } 1249a94100faSBill Paul 12504d2bf239SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_MSI) == 0) { 12514d2bf239SPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE); 12524d2bf239SPyun YongHyeon cfg = CSR_READ_1(sc, RL_CFG2); 12534d2bf239SPyun YongHyeon if ((cfg & RL_CFG2_MSI) != 0) { 12544d2bf239SPyun YongHyeon device_printf(dev, "turning off MSI enable bit.\n"); 12554d2bf239SPyun YongHyeon cfg &= ~RL_CFG2_MSI; 12564d2bf239SPyun YongHyeon CSR_WRITE_1(sc, RL_CFG2, cfg); 12574d2bf239SPyun YongHyeon } 12584d2bf239SPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 12594d2bf239SPyun YongHyeon } 12604d2bf239SPyun YongHyeon 1261a94100faSBill Paul /* Reset the adapter. */ 126297b9d4baSJohn-Mark Gurney RL_LOCK(sc); 1263a94100faSBill Paul re_reset(sc); 126497b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 1265abc8ff44SBill Paul 1266abc8ff44SBill Paul hw_rev = re_hwrevs; 1267a810fc83SPyun YongHyeon hwrev = CSR_READ_4(sc, RL_TXCFG); 1268566ca8caSJung-uk Kim switch (hwrev & 0x70000000) { 1269566ca8caSJung-uk Kim case 0x00000000: 1270566ca8caSJung-uk Kim case 0x10000000: 1271566ca8caSJung-uk Kim device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0xfc800000); 1272566ca8caSJung-uk Kim hwrev &= (RL_TXCFG_HWREV | 0x80000000); 1273566ca8caSJung-uk Kim break; 1274566ca8caSJung-uk Kim default: 1275a810fc83SPyun YongHyeon device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0x7c800000); 1276a810fc83SPyun YongHyeon hwrev &= RL_TXCFG_HWREV; 1277566ca8caSJung-uk Kim break; 1278566ca8caSJung-uk Kim } 1279566ca8caSJung-uk Kim device_printf(dev, "MAC rev. 0x%08x\n", hwrev & 0x00700000); 1280abc8ff44SBill Paul while (hw_rev->rl_desc != NULL) { 1281abc8ff44SBill Paul if (hw_rev->rl_rev == hwrev) { 1282abc8ff44SBill Paul sc->rl_type = hw_rev->rl_type; 1283566ca8caSJung-uk Kim sc->rl_hwrev = hw_rev->rl_rev; 1284abc8ff44SBill Paul break; 1285abc8ff44SBill Paul } 1286abc8ff44SBill Paul hw_rev++; 1287abc8ff44SBill Paul } 1288d65abd66SPyun YongHyeon if (hw_rev->rl_desc == NULL) { 1289a810fc83SPyun YongHyeon device_printf(dev, "Unknown H/W revision: 0x%08x\n", hwrev); 1290d65abd66SPyun YongHyeon error = ENXIO; 1291d65abd66SPyun YongHyeon goto fail; 1292d65abd66SPyun YongHyeon } 1293abc8ff44SBill Paul 1294351a76f9SPyun YongHyeon switch (hw_rev->rl_rev) { 1295351a76f9SPyun YongHyeon case RL_HWREV_8139CPLUS: 1296f2e491c9SPyun YongHyeon sc->rl_flags |= RL_FLAG_NOJUMBO | RL_FLAG_FASTETHER | 1297f2e491c9SPyun YongHyeon RL_FLAG_AUTOPAD; 1298351a76f9SPyun YongHyeon break; 1299351a76f9SPyun YongHyeon case RL_HWREV_8100E: 1300351a76f9SPyun YongHyeon case RL_HWREV_8101E: 1301aaab4fbeSJung-uk Kim sc->rl_flags |= RL_FLAG_NOJUMBO | RL_FLAG_PHYWAKE | 1302aaab4fbeSJung-uk Kim RL_FLAG_FASTETHER; 1303351a76f9SPyun YongHyeon break; 1304b1d62f0fSPyun YongHyeon case RL_HWREV_8102E: 1305b1d62f0fSPyun YongHyeon case RL_HWREV_8102EL: 13063d22427cSTai-hwa Liang case RL_HWREV_8102EL_SPIN1: 1307aaab4fbeSJung-uk Kim sc->rl_flags |= RL_FLAG_NOJUMBO | RL_FLAG_PHYWAKE | 1308aaab4fbeSJung-uk Kim RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | 1309f2e491c9SPyun YongHyeon RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD; 1310b1d62f0fSPyun YongHyeon break; 13118281a098SPyun YongHyeon case RL_HWREV_8103E: 13128281a098SPyun YongHyeon sc->rl_flags |= RL_FLAG_NOJUMBO | RL_FLAG_PHYWAKE | 13138281a098SPyun YongHyeon RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | 13148281a098SPyun YongHyeon RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | 13158281a098SPyun YongHyeon RL_FLAG_MACSLEEP; 13168281a098SPyun YongHyeon break; 1317351a76f9SPyun YongHyeon case RL_HWREV_8168_SPIN1: 1318351a76f9SPyun YongHyeon case RL_HWREV_8168_SPIN2: 1319886ff602SPyun YongHyeon sc->rl_flags |= RL_FLAG_WOLRXENB; 1320886ff602SPyun YongHyeon /* FALLTHROUGH */ 1321351a76f9SPyun YongHyeon case RL_HWREV_8168_SPIN3: 1322aaab4fbeSJung-uk Kim sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_MACSTAT; 1323deb5c680SPyun YongHyeon break; 1324deb5c680SPyun YongHyeon case RL_HWREV_8168C_SPIN2: 132561f45a72SPyun YongHyeon sc->rl_flags |= RL_FLAG_MACSLEEP; 132661f45a72SPyun YongHyeon /* FALLTHROUGH */ 132761f45a72SPyun YongHyeon case RL_HWREV_8168C: 132861f45a72SPyun YongHyeon if ((hwrev & 0x00700000) == 0x00200000) 132961f45a72SPyun YongHyeon sc->rl_flags |= RL_FLAG_MACSLEEP; 133061f45a72SPyun YongHyeon /* FALLTHROUGH */ 1331deb5c680SPyun YongHyeon case RL_HWREV_8168CP: 133259ef640dSPyun YongHyeon case RL_HWREV_8168D: 13335fa06abeSPyun YongHyeon case RL_HWREV_8168DP: 1334aaab4fbeSJung-uk Kim sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | 1335f2e491c9SPyun YongHyeon RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP | 1336f2e491c9SPyun YongHyeon RL_FLAG_AUTOPAD; 1337deb5c680SPyun YongHyeon /* 1338deb5c680SPyun YongHyeon * These controllers support jumbo frame but it seems 1339deb5c680SPyun YongHyeon * that enabling it requires touching additional magic 1340deb5c680SPyun YongHyeon * registers. Depending on MAC revisions some 1341deb5c680SPyun YongHyeon * controllers need to disable checksum offload. So 1342deb5c680SPyun YongHyeon * disable jumbo frame until I have better idea what 1343deb5c680SPyun YongHyeon * it really requires to make it support. 1344deb5c680SPyun YongHyeon * RTL8168C/CP : supports up to 6KB jumbo frame. 1345deb5c680SPyun YongHyeon * RTL8111C/CP : supports up to 9KB jumbo frame. 1346deb5c680SPyun YongHyeon */ 1347deb5c680SPyun YongHyeon sc->rl_flags |= RL_FLAG_NOJUMBO; 1348351a76f9SPyun YongHyeon break; 1349d0c45156SPyun YongHyeon case RL_HWREV_8168E: 1350d0c45156SPyun YongHyeon sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM | 1351d0c45156SPyun YongHyeon RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | 1352d0c45156SPyun YongHyeon RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_NOJUMBO; 1353d0c45156SPyun YongHyeon break; 1354566ca8caSJung-uk Kim case RL_HWREV_8169_8110SB: 1355566ca8caSJung-uk Kim case RL_HWREV_8169_8110SBL: 1356566ca8caSJung-uk Kim case RL_HWREV_8169_8110SC: 1357566ca8caSJung-uk Kim case RL_HWREV_8169_8110SCE: 1358566ca8caSJung-uk Kim sc->rl_flags |= RL_FLAG_PHYWAKE; 1359566ca8caSJung-uk Kim /* FALLTHROUGH */ 13600596d7e6SPyun YongHyeon case RL_HWREV_8169: 13610596d7e6SPyun YongHyeon case RL_HWREV_8169S: 1362566ca8caSJung-uk Kim case RL_HWREV_8110S: 1363566ca8caSJung-uk Kim sc->rl_flags |= RL_FLAG_MACRESET; 1364351a76f9SPyun YongHyeon break; 1365351a76f9SPyun YongHyeon default: 1366351a76f9SPyun YongHyeon break; 1367351a76f9SPyun YongHyeon } 1368351a76f9SPyun YongHyeon 1369deb5c680SPyun YongHyeon /* Enable PME. */ 1370deb5c680SPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE); 1371deb5c680SPyun YongHyeon cfg = CSR_READ_1(sc, RL_CFG1); 1372deb5c680SPyun YongHyeon cfg |= RL_CFG1_PME; 1373deb5c680SPyun YongHyeon CSR_WRITE_1(sc, RL_CFG1, cfg); 1374deb5c680SPyun YongHyeon cfg = CSR_READ_1(sc, RL_CFG5); 1375deb5c680SPyun YongHyeon cfg &= RL_CFG5_PME_STS; 1376deb5c680SPyun YongHyeon CSR_WRITE_1(sc, RL_CFG5, cfg); 1377deb5c680SPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 1378deb5c680SPyun YongHyeon 1379deb5c680SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_PAR) != 0) { 1380deb5c680SPyun YongHyeon /* 1381deb5c680SPyun YongHyeon * XXX Should have a better way to extract station 1382deb5c680SPyun YongHyeon * address from EEPROM. 1383deb5c680SPyun YongHyeon */ 1384deb5c680SPyun YongHyeon for (i = 0; i < ETHER_ADDR_LEN; i++) 1385deb5c680SPyun YongHyeon eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i); 1386deb5c680SPyun YongHyeon } else { 1387141f92e7SPyun YongHyeon sc->rl_eewidth = RL_9356_ADDR_LEN; 1388ed510fb0SBill Paul re_read_eeprom(sc, (caddr_t)&re_did, 0, 1); 1389a94100faSBill Paul if (re_did != 0x8129) 1390141f92e7SPyun YongHyeon sc->rl_eewidth = RL_9346_ADDR_LEN; 1391a94100faSBill Paul 1392a94100faSBill Paul /* 1393a94100faSBill Paul * Get station address from the EEPROM. 1394a94100faSBill Paul */ 1395ed510fb0SBill Paul re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3); 1396be099007SPyun YongHyeon for (i = 0; i < ETHER_ADDR_LEN / 2; i++) 1397be099007SPyun YongHyeon as[i] = le16toh(as[i]); 1398be099007SPyun YongHyeon bcopy(as, eaddr, sizeof(eaddr)); 1399deb5c680SPyun YongHyeon } 1400ed510fb0SBill Paul 1401ed510fb0SBill Paul if (sc->rl_type == RL_8169) { 1402d65abd66SPyun YongHyeon /* Set RX length mask and number of descriptors. */ 1403ed510fb0SBill Paul sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN; 1404ed510fb0SBill Paul sc->rl_txstart = RL_GTXSTART; 1405d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT; 1406d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT; 1407ed510fb0SBill Paul } else { 1408d65abd66SPyun YongHyeon /* Set RX length mask and number of descriptors. */ 1409ed510fb0SBill Paul sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN; 1410ed510fb0SBill Paul sc->rl_txstart = RL_TXSTART; 1411d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT; 1412d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT; 1413abc8ff44SBill Paul } 14149bac70b8SBill Paul 1415a94100faSBill Paul error = re_allocmem(dev, sc); 1416a94100faSBill Paul if (error) 1417a94100faSBill Paul goto fail; 14180534aae0SPyun YongHyeon re_add_sysctls(sc); 1419a94100faSBill Paul 1420cd036ec1SBrooks Davis ifp = sc->rl_ifp = if_alloc(IFT_ETHER); 1421cd036ec1SBrooks Davis if (ifp == NULL) { 1422d1754a9bSJohn Baldwin device_printf(dev, "can not if_alloc()\n"); 1423cd036ec1SBrooks Davis error = ENOSPC; 1424cd036ec1SBrooks Davis goto fail; 1425cd036ec1SBrooks Davis } 1426cd036ec1SBrooks Davis 142761f45a72SPyun YongHyeon /* Take controller out of deep sleep mode. */ 142861f45a72SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) { 142961f45a72SPyun YongHyeon if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80) 143061f45a72SPyun YongHyeon CSR_WRITE_1(sc, RL_GPIO, 143161f45a72SPyun YongHyeon CSR_READ_1(sc, RL_GPIO) | 0x01); 143261f45a72SPyun YongHyeon else 143361f45a72SPyun YongHyeon CSR_WRITE_1(sc, RL_GPIO, 143461f45a72SPyun YongHyeon CSR_READ_1(sc, RL_GPIO) & ~0x01); 143561f45a72SPyun YongHyeon } 143661f45a72SPyun YongHyeon 1437351a76f9SPyun YongHyeon /* Take PHY out of power down mode. */ 1438d0c45156SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0) 1439d0c45156SPyun YongHyeon CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) | 0x80); 1440351a76f9SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_PHYWAKE) != 0) { 1441351a76f9SPyun YongHyeon re_gmii_writereg(dev, 1, 0x1f, 0); 1442351a76f9SPyun YongHyeon re_gmii_writereg(dev, 1, 0x0e, 0); 1443351a76f9SPyun YongHyeon } 1444351a76f9SPyun YongHyeon 14458e5d93dbSMarius Strobl #define RE_PHYAD_INTERNAL 0 14468e5d93dbSMarius Strobl 14478e5d93dbSMarius Strobl /* Do MII setup. */ 14488e5d93dbSMarius Strobl phy = RE_PHYAD_INTERNAL; 14498e5d93dbSMarius Strobl if (sc->rl_type == RL_8169) 14508e5d93dbSMarius Strobl phy = 1; 14518e5d93dbSMarius Strobl error = mii_attach(dev, &sc->rl_miibus, ifp, re_ifmedia_upd, 145264436f6eSPyun YongHyeon re_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, MIIF_DOPAUSE); 14538e5d93dbSMarius Strobl if (error != 0) { 14548e5d93dbSMarius Strobl device_printf(dev, "attaching PHYs failed\n"); 1455a94100faSBill Paul goto fail; 1456a94100faSBill Paul } 1457a94100faSBill Paul 1458a94100faSBill Paul ifp->if_softc = sc; 14599bf40edeSBrooks Davis if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 1460a94100faSBill Paul ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1461a94100faSBill Paul ifp->if_ioctl = re_ioctl; 1462a94100faSBill Paul ifp->if_start = re_start; 1463d6d7d923SPyun YongHyeon ifp->if_hwassist = RE_CSUM_FEATURES | CSUM_TSO; 1464d6d7d923SPyun YongHyeon ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_TSO4; 1465498bd0d3SBill Paul ifp->if_capenable = ifp->if_capabilities; 1466a94100faSBill Paul ifp->if_init = re_init; 146752732175SMax Laier IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN); 146852732175SMax Laier ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN; 146952732175SMax Laier IFQ_SET_READY(&ifp->if_snd); 1470a94100faSBill Paul 1471ed510fb0SBill Paul TASK_INIT(&sc->rl_txtask, 1, re_tx_task, ifp); 1472ed510fb0SBill Paul TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc); 1473ed510fb0SBill Paul 1474a94100faSBill Paul /* 1475a94100faSBill Paul * Call MI attach routine. 1476a94100faSBill Paul */ 1477a94100faSBill Paul ether_ifattach(ifp, eaddr); 1478a94100faSBill Paul 1479960fd5b3SPyun YongHyeon /* VLAN capability setup */ 1480960fd5b3SPyun YongHyeon ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING; 1481960fd5b3SPyun YongHyeon if (ifp->if_capabilities & IFCAP_HWCSUM) 1482960fd5b3SPyun YongHyeon ifp->if_capabilities |= IFCAP_VLAN_HWCSUM; 14837467bd53SPyun YongHyeon /* Enable WOL if PM is supported. */ 14847467bd53SPyun YongHyeon if (pci_find_extcap(sc->rl_dev, PCIY_PMG, ®) == 0) 14857467bd53SPyun YongHyeon ifp->if_capabilities |= IFCAP_WOL; 1486960fd5b3SPyun YongHyeon ifp->if_capenable = ifp->if_capabilities; 1487a2a8420cSPyun YongHyeon /* 1488d6d7d923SPyun YongHyeon * Don't enable TSO by default for old controllers. Under 1489d6d7d923SPyun YongHyeon * certain circumtances the controller generated corrupted 1490a2a8420cSPyun YongHyeon * packets in TSO size. 1491a2a8420cSPyun YongHyeon */ 1492d6d7d923SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) { 1493a2a8420cSPyun YongHyeon ifp->if_hwassist &= ~CSUM_TSO; 1494ecafbbb5SPyun YongHyeon ifp->if_capenable &= ~(IFCAP_TSO4 | IFCAP_VLAN_HWTSO); 1495d6d7d923SPyun YongHyeon } 1496960fd5b3SPyun YongHyeon #ifdef DEVICE_POLLING 1497960fd5b3SPyun YongHyeon ifp->if_capabilities |= IFCAP_POLLING; 1498960fd5b3SPyun YongHyeon #endif 1499960fd5b3SPyun YongHyeon /* 1500960fd5b3SPyun YongHyeon * Tell the upper layer(s) we support long frames. 1501960fd5b3SPyun YongHyeon * Must appear after the call to ether_ifattach() because 1502960fd5b3SPyun YongHyeon * ether_ifattach() sets ifi_hdrlen to the default value. 1503960fd5b3SPyun YongHyeon */ 1504960fd5b3SPyun YongHyeon ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header); 1505960fd5b3SPyun YongHyeon 1506ed510fb0SBill Paul #ifdef RE_DIAG 1507ed510fb0SBill Paul /* 1508ed510fb0SBill Paul * Perform hardware diagnostic on the original RTL8169. 1509ed510fb0SBill Paul * Some 32-bit cards were incorrectly wired and would 1510ed510fb0SBill Paul * malfunction if plugged into a 64-bit slot. 1511ed510fb0SBill Paul */ 1512a94100faSBill Paul 1513ed510fb0SBill Paul if (hwrev == RL_HWREV_8169) { 1514ed510fb0SBill Paul error = re_diag(sc); 1515a94100faSBill Paul if (error) { 1516ed510fb0SBill Paul device_printf(dev, 1517ed510fb0SBill Paul "attach aborted due to hardware diag failure\n"); 1518a94100faSBill Paul ether_ifdetach(ifp); 1519a94100faSBill Paul goto fail; 1520a94100faSBill Paul } 1521ed510fb0SBill Paul } 1522ed510fb0SBill Paul #endif 1523a94100faSBill Paul 1524a94100faSBill Paul /* Hook interrupt last to avoid having to lock softc */ 1525351a76f9SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_MSI) == 0) 15265774c5ffSPyun YongHyeon error = bus_setup_intr(dev, sc->rl_irq[0], 15275774c5ffSPyun YongHyeon INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc, 15285774c5ffSPyun YongHyeon &sc->rl_intrhand[0]); 15295774c5ffSPyun YongHyeon else { 15305774c5ffSPyun YongHyeon for (i = 0; i < RL_MSI_MESSAGES; i++) { 15315774c5ffSPyun YongHyeon error = bus_setup_intr(dev, sc->rl_irq[i], 15325774c5ffSPyun YongHyeon INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc, 15335774c5ffSPyun YongHyeon &sc->rl_intrhand[i]); 15345774c5ffSPyun YongHyeon if (error != 0) 15355774c5ffSPyun YongHyeon break; 15365774c5ffSPyun YongHyeon } 15375774c5ffSPyun YongHyeon } 1538a94100faSBill Paul if (error) { 1539d1754a9bSJohn Baldwin device_printf(dev, "couldn't set up irq\n"); 1540a94100faSBill Paul ether_ifdetach(ifp); 1541a94100faSBill Paul } 1542a94100faSBill Paul 1543a94100faSBill Paul fail: 1544ed510fb0SBill Paul 1545a94100faSBill Paul if (error) 1546a94100faSBill Paul re_detach(dev); 1547a94100faSBill Paul 1548a94100faSBill Paul return (error); 1549a94100faSBill Paul } 1550a94100faSBill Paul 1551a94100faSBill Paul /* 1552a94100faSBill Paul * Shutdown hardware and free up resources. This can be called any 1553a94100faSBill Paul * time after the mutex has been initialized. It is called in both 1554a94100faSBill Paul * the error case in attach and the normal detach case so it needs 1555a94100faSBill Paul * to be careful about only freeing resources that have actually been 1556a94100faSBill Paul * allocated. 1557a94100faSBill Paul */ 1558a94100faSBill Paul static int 15597b5ffebfSPyun YongHyeon re_detach(device_t dev) 1560a94100faSBill Paul { 1561a94100faSBill Paul struct rl_softc *sc; 1562a94100faSBill Paul struct ifnet *ifp; 15635774c5ffSPyun YongHyeon int i, rid; 1564a94100faSBill Paul 1565a94100faSBill Paul sc = device_get_softc(dev); 1566fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 1567aedd16d9SJohn-Mark Gurney KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized")); 156897b9d4baSJohn-Mark Gurney 156981cf2eb6SPyun YongHyeon /* These should only be active if attach succeeded */ 157081cf2eb6SPyun YongHyeon if (device_is_attached(dev)) { 157140929967SGleb Smirnoff #ifdef DEVICE_POLLING 157240929967SGleb Smirnoff if (ifp->if_capenable & IFCAP_POLLING) 157340929967SGleb Smirnoff ether_poll_deregister(ifp); 157440929967SGleb Smirnoff #endif 157597b9d4baSJohn-Mark Gurney RL_LOCK(sc); 157697b9d4baSJohn-Mark Gurney #if 0 157797b9d4baSJohn-Mark Gurney sc->suspended = 1; 157897b9d4baSJohn-Mark Gurney #endif 1579a94100faSBill Paul re_stop(sc); 1580525e6a87SRuslan Ermilov RL_UNLOCK(sc); 1581d1754a9bSJohn Baldwin callout_drain(&sc->rl_stat_callout); 15823d4c1b57SJohn Baldwin taskqueue_drain(taskqueue_fast, &sc->rl_inttask); 15833d4c1b57SJohn Baldwin taskqueue_drain(taskqueue_fast, &sc->rl_txtask); 1584a94100faSBill Paul /* 1585a94100faSBill Paul * Force off the IFF_UP flag here, in case someone 1586a94100faSBill Paul * still had a BPF descriptor attached to this 158797b9d4baSJohn-Mark Gurney * interface. If they do, ether_ifdetach() will cause 1588a94100faSBill Paul * the BPF code to try and clear the promisc mode 1589a94100faSBill Paul * flag, which will bubble down to re_ioctl(), 1590a94100faSBill Paul * which will try to call re_init() again. This will 1591a94100faSBill Paul * turn the NIC back on and restart the MII ticker, 1592a94100faSBill Paul * which will panic the system when the kernel tries 1593a94100faSBill Paul * to invoke the re_tick() function that isn't there 1594a94100faSBill Paul * anymore. 1595a94100faSBill Paul */ 1596a94100faSBill Paul ifp->if_flags &= ~IFF_UP; 1597525e6a87SRuslan Ermilov ether_ifdetach(ifp); 1598a94100faSBill Paul } 1599a94100faSBill Paul if (sc->rl_miibus) 1600a94100faSBill Paul device_delete_child(dev, sc->rl_miibus); 1601a94100faSBill Paul bus_generic_detach(dev); 1602a94100faSBill Paul 160397b9d4baSJohn-Mark Gurney /* 160497b9d4baSJohn-Mark Gurney * The rest is resource deallocation, so we should already be 160597b9d4baSJohn-Mark Gurney * stopped here. 160697b9d4baSJohn-Mark Gurney */ 160797b9d4baSJohn-Mark Gurney 16085774c5ffSPyun YongHyeon for (i = 0; i < RL_MSI_MESSAGES; i++) { 16095774c5ffSPyun YongHyeon if (sc->rl_intrhand[i] != NULL) { 16105774c5ffSPyun YongHyeon bus_teardown_intr(dev, sc->rl_irq[i], 16115774c5ffSPyun YongHyeon sc->rl_intrhand[i]); 16125774c5ffSPyun YongHyeon sc->rl_intrhand[i] = NULL; 16135774c5ffSPyun YongHyeon } 16145774c5ffSPyun YongHyeon } 1615ad4f426eSWarner Losh if (ifp != NULL) 1616ad4f426eSWarner Losh if_free(ifp); 1617351a76f9SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_MSI) == 0) { 16185774c5ffSPyun YongHyeon if (sc->rl_irq[0] != NULL) { 16195774c5ffSPyun YongHyeon bus_release_resource(dev, SYS_RES_IRQ, 0, 16205774c5ffSPyun YongHyeon sc->rl_irq[0]); 16215774c5ffSPyun YongHyeon sc->rl_irq[0] = NULL; 16225774c5ffSPyun YongHyeon } 16235774c5ffSPyun YongHyeon } else { 16245774c5ffSPyun YongHyeon for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) { 16255774c5ffSPyun YongHyeon if (sc->rl_irq[i] != NULL) { 16265774c5ffSPyun YongHyeon bus_release_resource(dev, SYS_RES_IRQ, rid, 16275774c5ffSPyun YongHyeon sc->rl_irq[i]); 16285774c5ffSPyun YongHyeon sc->rl_irq[i] = NULL; 16295774c5ffSPyun YongHyeon } 16305774c5ffSPyun YongHyeon } 16315774c5ffSPyun YongHyeon pci_release_msi(dev); 16325774c5ffSPyun YongHyeon } 1633a94100faSBill Paul if (sc->rl_res) 1634ace7ed5dSPyun YongHyeon bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id, 1635ace7ed5dSPyun YongHyeon sc->rl_res); 1636a94100faSBill Paul 1637a94100faSBill Paul /* Unload and free the RX DMA ring memory and map */ 1638a94100faSBill Paul 1639a94100faSBill Paul if (sc->rl_ldata.rl_rx_list_tag) { 16400534aae0SPyun YongHyeon if (sc->rl_ldata.rl_rx_list_map) 1641a94100faSBill Paul bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag, 1642a94100faSBill Paul sc->rl_ldata.rl_rx_list_map); 16430534aae0SPyun YongHyeon if (sc->rl_ldata.rl_rx_list_map && sc->rl_ldata.rl_rx_list) 1644a94100faSBill Paul bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag, 1645a94100faSBill Paul sc->rl_ldata.rl_rx_list, 1646a94100faSBill Paul sc->rl_ldata.rl_rx_list_map); 1647a94100faSBill Paul bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag); 1648a94100faSBill Paul } 1649a94100faSBill Paul 1650a94100faSBill Paul /* Unload and free the TX DMA ring memory and map */ 1651a94100faSBill Paul 1652a94100faSBill Paul if (sc->rl_ldata.rl_tx_list_tag) { 16530534aae0SPyun YongHyeon if (sc->rl_ldata.rl_tx_list_map) 1654a94100faSBill Paul bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag, 1655a94100faSBill Paul sc->rl_ldata.rl_tx_list_map); 16560534aae0SPyun YongHyeon if (sc->rl_ldata.rl_tx_list_map && sc->rl_ldata.rl_tx_list) 1657a94100faSBill Paul bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag, 1658a94100faSBill Paul sc->rl_ldata.rl_tx_list, 1659a94100faSBill Paul sc->rl_ldata.rl_tx_list_map); 1660a94100faSBill Paul bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag); 1661a94100faSBill Paul } 1662a94100faSBill Paul 1663a94100faSBill Paul /* Destroy all the RX and TX buffer maps */ 1664a94100faSBill Paul 1665d65abd66SPyun YongHyeon if (sc->rl_ldata.rl_tx_mtag) { 1666d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) 1667d65abd66SPyun YongHyeon bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag, 1668d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_desc[i].tx_dmamap); 1669d65abd66SPyun YongHyeon bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag); 1670d65abd66SPyun YongHyeon } 1671d65abd66SPyun YongHyeon if (sc->rl_ldata.rl_rx_mtag) { 1672d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) 1673d65abd66SPyun YongHyeon bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag, 1674d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc[i].rx_dmamap); 1675d65abd66SPyun YongHyeon if (sc->rl_ldata.rl_rx_sparemap) 1676d65abd66SPyun YongHyeon bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag, 1677d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_sparemap); 1678d65abd66SPyun YongHyeon bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag); 1679a94100faSBill Paul } 1680a94100faSBill Paul 1681a94100faSBill Paul /* Unload and free the stats buffer and map */ 1682a94100faSBill Paul 1683a94100faSBill Paul if (sc->rl_ldata.rl_stag) { 16840534aae0SPyun YongHyeon if (sc->rl_ldata.rl_smap) 1685a94100faSBill Paul bus_dmamap_unload(sc->rl_ldata.rl_stag, 1686a94100faSBill Paul sc->rl_ldata.rl_smap); 16870534aae0SPyun YongHyeon if (sc->rl_ldata.rl_smap && sc->rl_ldata.rl_stats) 16880534aae0SPyun YongHyeon bus_dmamem_free(sc->rl_ldata.rl_stag, 16890534aae0SPyun YongHyeon sc->rl_ldata.rl_stats, sc->rl_ldata.rl_smap); 1690a94100faSBill Paul bus_dma_tag_destroy(sc->rl_ldata.rl_stag); 1691a94100faSBill Paul } 1692a94100faSBill Paul 1693a94100faSBill Paul if (sc->rl_parent_tag) 1694a94100faSBill Paul bus_dma_tag_destroy(sc->rl_parent_tag); 1695a94100faSBill Paul 1696a94100faSBill Paul mtx_destroy(&sc->rl_mtx); 1697a94100faSBill Paul 1698a94100faSBill Paul return (0); 1699a94100faSBill Paul } 1700a94100faSBill Paul 1701d65abd66SPyun YongHyeon static __inline void 17027b5ffebfSPyun YongHyeon re_discard_rxbuf(struct rl_softc *sc, int idx) 1703a94100faSBill Paul { 1704d65abd66SPyun YongHyeon struct rl_desc *desc; 1705d65abd66SPyun YongHyeon struct rl_rxdesc *rxd; 1706d65abd66SPyun YongHyeon uint32_t cmdstat; 1707a94100faSBill Paul 1708d65abd66SPyun YongHyeon rxd = &sc->rl_ldata.rl_rx_desc[idx]; 1709d65abd66SPyun YongHyeon desc = &sc->rl_ldata.rl_rx_list[idx]; 1710d65abd66SPyun YongHyeon desc->rl_vlanctl = 0; 1711d65abd66SPyun YongHyeon cmdstat = rxd->rx_size; 1712d65abd66SPyun YongHyeon if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1) 1713d65abd66SPyun YongHyeon cmdstat |= RL_RDESC_CMD_EOR; 1714d65abd66SPyun YongHyeon desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN); 1715d65abd66SPyun YongHyeon } 1716d65abd66SPyun YongHyeon 1717d65abd66SPyun YongHyeon static int 17187b5ffebfSPyun YongHyeon re_newbuf(struct rl_softc *sc, int idx) 1719d65abd66SPyun YongHyeon { 1720d65abd66SPyun YongHyeon struct mbuf *m; 1721d65abd66SPyun YongHyeon struct rl_rxdesc *rxd; 1722d65abd66SPyun YongHyeon bus_dma_segment_t segs[1]; 1723d65abd66SPyun YongHyeon bus_dmamap_t map; 1724d65abd66SPyun YongHyeon struct rl_desc *desc; 1725d65abd66SPyun YongHyeon uint32_t cmdstat; 1726d65abd66SPyun YongHyeon int error, nsegs; 1727d65abd66SPyun YongHyeon 1728d65abd66SPyun YongHyeon m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1729d65abd66SPyun YongHyeon if (m == NULL) 1730a94100faSBill Paul return (ENOBUFS); 1731a94100faSBill Paul 1732a94100faSBill Paul m->m_len = m->m_pkthdr.len = MCLBYTES; 173322a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX 173422a11c96SJohn-Mark Gurney /* 173522a11c96SJohn-Mark Gurney * This is part of an evil trick to deal with non-x86 platforms. 173622a11c96SJohn-Mark Gurney * The RealTek chip requires RX buffers to be aligned on 64-bit 173722a11c96SJohn-Mark Gurney * boundaries, but that will hose non-x86 machines. To get around 173822a11c96SJohn-Mark Gurney * this, we leave some empty space at the start of each buffer 173922a11c96SJohn-Mark Gurney * and for non-x86 hosts, we copy the buffer back six bytes 174022a11c96SJohn-Mark Gurney * to achieve word alignment. This is slightly more efficient 174122a11c96SJohn-Mark Gurney * than allocating a new buffer, copying the contents, and 174222a11c96SJohn-Mark Gurney * discarding the old buffer. 174322a11c96SJohn-Mark Gurney */ 174422a11c96SJohn-Mark Gurney m_adj(m, RE_ETHER_ALIGN); 174522a11c96SJohn-Mark Gurney #endif 1746d65abd66SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag, 1747d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT); 1748d65abd66SPyun YongHyeon if (error != 0) { 1749d65abd66SPyun YongHyeon m_freem(m); 1750d65abd66SPyun YongHyeon return (ENOBUFS); 1751d65abd66SPyun YongHyeon } 1752d65abd66SPyun YongHyeon KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs)); 1753a94100faSBill Paul 1754d65abd66SPyun YongHyeon rxd = &sc->rl_ldata.rl_rx_desc[idx]; 1755d65abd66SPyun YongHyeon if (rxd->rx_m != NULL) { 1756d65abd66SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap, 1757d65abd66SPyun YongHyeon BUS_DMASYNC_POSTREAD); 1758d65abd66SPyun YongHyeon bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap); 1759a94100faSBill Paul } 1760a94100faSBill Paul 1761d65abd66SPyun YongHyeon rxd->rx_m = m; 1762d65abd66SPyun YongHyeon map = rxd->rx_dmamap; 1763d65abd66SPyun YongHyeon rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap; 1764d65abd66SPyun YongHyeon rxd->rx_size = segs[0].ds_len; 1765d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_sparemap = map; 1766d65abd66SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap, 1767a94100faSBill Paul BUS_DMASYNC_PREREAD); 1768a94100faSBill Paul 1769d65abd66SPyun YongHyeon desc = &sc->rl_ldata.rl_rx_list[idx]; 1770d65abd66SPyun YongHyeon desc->rl_vlanctl = 0; 1771d65abd66SPyun YongHyeon desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr)); 1772d65abd66SPyun YongHyeon desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr)); 1773d65abd66SPyun YongHyeon cmdstat = segs[0].ds_len; 1774d65abd66SPyun YongHyeon if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1) 1775d65abd66SPyun YongHyeon cmdstat |= RL_RDESC_CMD_EOR; 1776d65abd66SPyun YongHyeon desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN); 1777d65abd66SPyun YongHyeon 1778a94100faSBill Paul return (0); 1779a94100faSBill Paul } 1780a94100faSBill Paul 178122a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX 178222a11c96SJohn-Mark Gurney static __inline void 17837b5ffebfSPyun YongHyeon re_fixup_rx(struct mbuf *m) 178422a11c96SJohn-Mark Gurney { 178522a11c96SJohn-Mark Gurney int i; 178622a11c96SJohn-Mark Gurney uint16_t *src, *dst; 178722a11c96SJohn-Mark Gurney 178822a11c96SJohn-Mark Gurney src = mtod(m, uint16_t *); 178922a11c96SJohn-Mark Gurney dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src; 179022a11c96SJohn-Mark Gurney 179122a11c96SJohn-Mark Gurney for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++) 179222a11c96SJohn-Mark Gurney *dst++ = *src++; 179322a11c96SJohn-Mark Gurney 179422a11c96SJohn-Mark Gurney m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN; 179522a11c96SJohn-Mark Gurney } 179622a11c96SJohn-Mark Gurney #endif 179722a11c96SJohn-Mark Gurney 1798a94100faSBill Paul static int 17997b5ffebfSPyun YongHyeon re_tx_list_init(struct rl_softc *sc) 1800a94100faSBill Paul { 1801d65abd66SPyun YongHyeon struct rl_desc *desc; 1802d65abd66SPyun YongHyeon int i; 180397b9d4baSJohn-Mark Gurney 180497b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 180597b9d4baSJohn-Mark Gurney 1806d65abd66SPyun YongHyeon bzero(sc->rl_ldata.rl_tx_list, 1807d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc)); 1808d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) 1809d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_desc[i].tx_m = NULL; 1810d65abd66SPyun YongHyeon /* Set EOR. */ 1811d65abd66SPyun YongHyeon desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1]; 1812d65abd66SPyun YongHyeon desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR); 1813a94100faSBill Paul 1814a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 1815d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_list_map, 1816d65abd66SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1817d65abd66SPyun YongHyeon 1818a94100faSBill Paul sc->rl_ldata.rl_tx_prodidx = 0; 1819a94100faSBill Paul sc->rl_ldata.rl_tx_considx = 0; 1820d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt; 1821a94100faSBill Paul 1822a94100faSBill Paul return (0); 1823a94100faSBill Paul } 1824a94100faSBill Paul 1825a94100faSBill Paul static int 18267b5ffebfSPyun YongHyeon re_rx_list_init(struct rl_softc *sc) 1827a94100faSBill Paul { 1828d65abd66SPyun YongHyeon int error, i; 1829a94100faSBill Paul 1830d65abd66SPyun YongHyeon bzero(sc->rl_ldata.rl_rx_list, 1831d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc)); 1832d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 1833d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc[i].rx_m = NULL; 1834d65abd66SPyun YongHyeon if ((error = re_newbuf(sc, i)) != 0) 1835d65abd66SPyun YongHyeon return (error); 1836a94100faSBill Paul } 1837a94100faSBill Paul 1838a94100faSBill Paul /* Flush the RX descriptors */ 1839a94100faSBill Paul 1840a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 1841a94100faSBill Paul sc->rl_ldata.rl_rx_list_map, 1842a94100faSBill Paul BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 1843a94100faSBill Paul 1844a94100faSBill Paul sc->rl_ldata.rl_rx_prodidx = 0; 1845a94100faSBill Paul sc->rl_head = sc->rl_tail = NULL; 1846a94100faSBill Paul 1847a94100faSBill Paul return (0); 1848a94100faSBill Paul } 1849a94100faSBill Paul 1850a94100faSBill Paul /* 1851a94100faSBill Paul * RX handler for C+ and 8169. For the gigE chips, we support 1852a94100faSBill Paul * the reception of jumbo frames that have been fragmented 1853a94100faSBill Paul * across multiple 2K mbuf cluster buffers. 1854a94100faSBill Paul */ 1855ed510fb0SBill Paul static int 18561abcdbd1SAttilio Rao re_rxeof(struct rl_softc *sc, int *rx_npktsp) 1857a94100faSBill Paul { 1858a94100faSBill Paul struct mbuf *m; 1859a94100faSBill Paul struct ifnet *ifp; 1860a94100faSBill Paul int i, total_len; 1861a94100faSBill Paul struct rl_desc *cur_rx; 1862a94100faSBill Paul u_int32_t rxstat, rxvlan; 18631abcdbd1SAttilio Rao int maxpkt = 16, rx_npkts = 0; 1864a94100faSBill Paul 18655120abbfSSam Leffler RL_LOCK_ASSERT(sc); 18665120abbfSSam Leffler 1867fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 1868a94100faSBill Paul 1869a94100faSBill Paul /* Invalidate the descriptor memory */ 1870a94100faSBill Paul 1871a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 1872a94100faSBill Paul sc->rl_ldata.rl_rx_list_map, 1873d65abd66SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 1874a94100faSBill Paul 1875d65abd66SPyun YongHyeon for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0; 1876d65abd66SPyun YongHyeon i = RL_RX_DESC_NXT(sc, i)) { 18775b6d1d9dSPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 18785b6d1d9dSPyun YongHyeon break; 1879a94100faSBill Paul cur_rx = &sc->rl_ldata.rl_rx_list[i]; 1880a94100faSBill Paul rxstat = le32toh(cur_rx->rl_cmdstat); 1881d65abd66SPyun YongHyeon if ((rxstat & RL_RDESC_STAT_OWN) != 0) 1882d65abd66SPyun YongHyeon break; 1883d65abd66SPyun YongHyeon total_len = rxstat & sc->rl_rxlenmask; 1884a94100faSBill Paul rxvlan = le32toh(cur_rx->rl_vlanctl); 1885d65abd66SPyun YongHyeon m = sc->rl_ldata.rl_rx_desc[i].rx_m; 1886a94100faSBill Paul 1887a94100faSBill Paul if (!(rxstat & RL_RDESC_STAT_EOF)) { 1888d65abd66SPyun YongHyeon if (re_newbuf(sc, i) != 0) { 1889d65abd66SPyun YongHyeon /* 1890d65abd66SPyun YongHyeon * If this is part of a multi-fragment packet, 1891d65abd66SPyun YongHyeon * discard all the pieces. 1892d65abd66SPyun YongHyeon */ 1893d65abd66SPyun YongHyeon if (sc->rl_head != NULL) { 1894d65abd66SPyun YongHyeon m_freem(sc->rl_head); 1895d65abd66SPyun YongHyeon sc->rl_head = sc->rl_tail = NULL; 1896d65abd66SPyun YongHyeon } 1897d65abd66SPyun YongHyeon re_discard_rxbuf(sc, i); 1898d65abd66SPyun YongHyeon continue; 1899d65abd66SPyun YongHyeon } 190022a11c96SJohn-Mark Gurney m->m_len = RE_RX_DESC_BUFLEN; 1901a94100faSBill Paul if (sc->rl_head == NULL) 1902a94100faSBill Paul sc->rl_head = sc->rl_tail = m; 1903a94100faSBill Paul else { 1904a94100faSBill Paul m->m_flags &= ~M_PKTHDR; 1905a94100faSBill Paul sc->rl_tail->m_next = m; 1906a94100faSBill Paul sc->rl_tail = m; 1907a94100faSBill Paul } 1908a94100faSBill Paul continue; 1909a94100faSBill Paul } 1910a94100faSBill Paul 1911a94100faSBill Paul /* 1912a94100faSBill Paul * NOTE: for the 8139C+, the frame length field 1913a94100faSBill Paul * is always 12 bits in size, but for the gigE chips, 1914a94100faSBill Paul * it is 13 bits (since the max RX frame length is 16K). 1915a94100faSBill Paul * Unfortunately, all 32 bits in the status word 1916a94100faSBill Paul * were already used, so to make room for the extra 1917a94100faSBill Paul * length bit, RealTek took out the 'frame alignment 1918a94100faSBill Paul * error' bit and shifted the other status bits 1919a94100faSBill Paul * over one slot. The OWN, EOR, FS and LS bits are 1920a94100faSBill Paul * still in the same places. We have already extracted 1921a94100faSBill Paul * the frame length and checked the OWN bit, so rather 1922a94100faSBill Paul * than using an alternate bit mapping, we shift the 1923a94100faSBill Paul * status bits one space to the right so we can evaluate 1924a94100faSBill Paul * them using the 8169 status as though it was in the 1925a94100faSBill Paul * same format as that of the 8139C+. 1926a94100faSBill Paul */ 1927a94100faSBill Paul if (sc->rl_type == RL_8169) 1928a94100faSBill Paul rxstat >>= 1; 1929a94100faSBill Paul 193022a11c96SJohn-Mark Gurney /* 193122a11c96SJohn-Mark Gurney * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be 193222a11c96SJohn-Mark Gurney * set, but if CRC is clear, it will still be a valid frame. 193322a11c96SJohn-Mark Gurney */ 193422a11c96SJohn-Mark Gurney if (rxstat & RL_RDESC_STAT_RXERRSUM && !(total_len > 8191 && 193522a11c96SJohn-Mark Gurney (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)) { 1936a94100faSBill Paul ifp->if_ierrors++; 1937a94100faSBill Paul /* 1938a94100faSBill Paul * If this is part of a multi-fragment packet, 1939a94100faSBill Paul * discard all the pieces. 1940a94100faSBill Paul */ 1941a94100faSBill Paul if (sc->rl_head != NULL) { 1942a94100faSBill Paul m_freem(sc->rl_head); 1943a94100faSBill Paul sc->rl_head = sc->rl_tail = NULL; 1944a94100faSBill Paul } 1945d65abd66SPyun YongHyeon re_discard_rxbuf(sc, i); 1946a94100faSBill Paul continue; 1947a94100faSBill Paul } 1948a94100faSBill Paul 1949a94100faSBill Paul /* 1950a94100faSBill Paul * If allocating a replacement mbuf fails, 1951a94100faSBill Paul * reload the current one. 1952a94100faSBill Paul */ 1953a94100faSBill Paul 1954d65abd66SPyun YongHyeon if (re_newbuf(sc, i) != 0) { 1955d65abd66SPyun YongHyeon ifp->if_iqdrops++; 1956a94100faSBill Paul if (sc->rl_head != NULL) { 1957a94100faSBill Paul m_freem(sc->rl_head); 1958a94100faSBill Paul sc->rl_head = sc->rl_tail = NULL; 1959a94100faSBill Paul } 1960d65abd66SPyun YongHyeon re_discard_rxbuf(sc, i); 1961a94100faSBill Paul continue; 1962a94100faSBill Paul } 1963a94100faSBill Paul 1964a94100faSBill Paul if (sc->rl_head != NULL) { 196522a11c96SJohn-Mark Gurney m->m_len = total_len % RE_RX_DESC_BUFLEN; 196622a11c96SJohn-Mark Gurney if (m->m_len == 0) 196722a11c96SJohn-Mark Gurney m->m_len = RE_RX_DESC_BUFLEN; 1968a94100faSBill Paul /* 1969a94100faSBill Paul * Special case: if there's 4 bytes or less 1970a94100faSBill Paul * in this buffer, the mbuf can be discarded: 1971a94100faSBill Paul * the last 4 bytes is the CRC, which we don't 1972a94100faSBill Paul * care about anyway. 1973a94100faSBill Paul */ 1974a94100faSBill Paul if (m->m_len <= ETHER_CRC_LEN) { 1975a94100faSBill Paul sc->rl_tail->m_len -= 1976a94100faSBill Paul (ETHER_CRC_LEN - m->m_len); 1977a94100faSBill Paul m_freem(m); 1978a94100faSBill Paul } else { 1979a94100faSBill Paul m->m_len -= ETHER_CRC_LEN; 1980a94100faSBill Paul m->m_flags &= ~M_PKTHDR; 1981a94100faSBill Paul sc->rl_tail->m_next = m; 1982a94100faSBill Paul } 1983a94100faSBill Paul m = sc->rl_head; 1984a94100faSBill Paul sc->rl_head = sc->rl_tail = NULL; 1985a94100faSBill Paul m->m_pkthdr.len = total_len - ETHER_CRC_LEN; 1986a94100faSBill Paul } else 1987a94100faSBill Paul m->m_pkthdr.len = m->m_len = 1988a94100faSBill Paul (total_len - ETHER_CRC_LEN); 1989a94100faSBill Paul 199022a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX 199122a11c96SJohn-Mark Gurney re_fixup_rx(m); 199222a11c96SJohn-Mark Gurney #endif 1993a94100faSBill Paul ifp->if_ipackets++; 1994a94100faSBill Paul m->m_pkthdr.rcvif = ifp; 1995a94100faSBill Paul 1996a94100faSBill Paul /* Do RX checksumming if enabled */ 1997a94100faSBill Paul 1998a94100faSBill Paul if (ifp->if_capenable & IFCAP_RXCSUM) { 1999deb5c680SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) { 2000a94100faSBill Paul /* Check IP header checksum */ 2001a94100faSBill Paul if (rxstat & RL_RDESC_STAT_PROTOID) 2002deb5c680SPyun YongHyeon m->m_pkthdr.csum_flags |= 2003deb5c680SPyun YongHyeon CSUM_IP_CHECKED; 2004a94100faSBill Paul if (!(rxstat & RL_RDESC_STAT_IPSUMBAD)) 2005deb5c680SPyun YongHyeon m->m_pkthdr.csum_flags |= 2006deb5c680SPyun YongHyeon CSUM_IP_VALID; 2007a94100faSBill Paul 2008a94100faSBill Paul /* Check TCP/UDP checksum */ 2009a94100faSBill Paul if ((RL_TCPPKT(rxstat) && 2010a94100faSBill Paul !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) || 2011a94100faSBill Paul (RL_UDPPKT(rxstat) && 2012a94100faSBill Paul !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) { 2013a94100faSBill Paul m->m_pkthdr.csum_flags |= 2014a94100faSBill Paul CSUM_DATA_VALID|CSUM_PSEUDO_HDR; 2015a94100faSBill Paul m->m_pkthdr.csum_data = 0xffff; 2016a94100faSBill Paul } 2017deb5c680SPyun YongHyeon } else { 2018deb5c680SPyun YongHyeon /* 2019deb5c680SPyun YongHyeon * RTL8168C/RTL816CP/RTL8111C/RTL8111CP 2020deb5c680SPyun YongHyeon */ 2021deb5c680SPyun YongHyeon if ((rxstat & RL_RDESC_STAT_PROTOID) && 2022deb5c680SPyun YongHyeon (rxvlan & RL_RDESC_IPV4)) 2023deb5c680SPyun YongHyeon m->m_pkthdr.csum_flags |= 2024deb5c680SPyun YongHyeon CSUM_IP_CHECKED; 2025deb5c680SPyun YongHyeon if (!(rxstat & RL_RDESC_STAT_IPSUMBAD) && 2026deb5c680SPyun YongHyeon (rxvlan & RL_RDESC_IPV4)) 2027deb5c680SPyun YongHyeon m->m_pkthdr.csum_flags |= 2028deb5c680SPyun YongHyeon CSUM_IP_VALID; 2029deb5c680SPyun YongHyeon if (((rxstat & RL_RDESC_STAT_TCP) && 2030deb5c680SPyun YongHyeon !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) || 2031deb5c680SPyun YongHyeon ((rxstat & RL_RDESC_STAT_UDP) && 2032deb5c680SPyun YongHyeon !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) { 2033deb5c680SPyun YongHyeon m->m_pkthdr.csum_flags |= 2034deb5c680SPyun YongHyeon CSUM_DATA_VALID|CSUM_PSEUDO_HDR; 2035deb5c680SPyun YongHyeon m->m_pkthdr.csum_data = 0xffff; 2036deb5c680SPyun YongHyeon } 2037deb5c680SPyun YongHyeon } 2038a94100faSBill Paul } 2039ed510fb0SBill Paul maxpkt--; 2040d147662cSGleb Smirnoff if (rxvlan & RL_RDESC_VLANCTL_TAG) { 204178ba57b9SAndre Oppermann m->m_pkthdr.ether_vtag = 2042bddff934SPyun YongHyeon bswap16((rxvlan & RL_RDESC_VLANCTL_DATA)); 204378ba57b9SAndre Oppermann m->m_flags |= M_VLANTAG; 2044d147662cSGleb Smirnoff } 20455120abbfSSam Leffler RL_UNLOCK(sc); 2046a94100faSBill Paul (*ifp->if_input)(ifp, m); 20475120abbfSSam Leffler RL_LOCK(sc); 20481abcdbd1SAttilio Rao rx_npkts++; 2049a94100faSBill Paul } 2050a94100faSBill Paul 2051a94100faSBill Paul /* Flush the RX DMA ring */ 2052a94100faSBill Paul 2053a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 2054a94100faSBill Paul sc->rl_ldata.rl_rx_list_map, 2055a94100faSBill Paul BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 2056a94100faSBill Paul 2057a94100faSBill Paul sc->rl_ldata.rl_rx_prodidx = i; 2058ed510fb0SBill Paul 20591abcdbd1SAttilio Rao if (rx_npktsp != NULL) 20601abcdbd1SAttilio Rao *rx_npktsp = rx_npkts; 2061ed510fb0SBill Paul if (maxpkt) 2062ed510fb0SBill Paul return (EAGAIN); 2063ed510fb0SBill Paul 2064ed510fb0SBill Paul return (0); 2065a94100faSBill Paul } 2066a94100faSBill Paul 2067a94100faSBill Paul static void 20687b5ffebfSPyun YongHyeon re_txeof(struct rl_softc *sc) 2069a94100faSBill Paul { 2070a94100faSBill Paul struct ifnet *ifp; 2071d65abd66SPyun YongHyeon struct rl_txdesc *txd; 2072a94100faSBill Paul u_int32_t txstat; 2073d65abd66SPyun YongHyeon int cons; 2074d65abd66SPyun YongHyeon 2075d65abd66SPyun YongHyeon cons = sc->rl_ldata.rl_tx_considx; 2076d65abd66SPyun YongHyeon if (cons == sc->rl_ldata.rl_tx_prodidx) 2077d65abd66SPyun YongHyeon return; 2078a94100faSBill Paul 2079fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 2080a94100faSBill Paul /* Invalidate the TX descriptor list */ 2081a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 2082a94100faSBill Paul sc->rl_ldata.rl_tx_list_map, 2083d65abd66SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 2084a94100faSBill Paul 2085d65abd66SPyun YongHyeon for (; cons != sc->rl_ldata.rl_tx_prodidx; 2086d65abd66SPyun YongHyeon cons = RL_TX_DESC_NXT(sc, cons)) { 2087d65abd66SPyun YongHyeon txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat); 2088d65abd66SPyun YongHyeon if (txstat & RL_TDESC_STAT_OWN) 2089a94100faSBill Paul break; 2090a94100faSBill Paul /* 2091a94100faSBill Paul * We only stash mbufs in the last descriptor 2092a94100faSBill Paul * in a fragment chain, which also happens to 2093a94100faSBill Paul * be the only place where the TX status bits 2094a94100faSBill Paul * are valid. 2095a94100faSBill Paul */ 2096a94100faSBill Paul if (txstat & RL_TDESC_CMD_EOF) { 2097d65abd66SPyun YongHyeon txd = &sc->rl_ldata.rl_tx_desc[cons]; 2098d65abd66SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, 2099d65abd66SPyun YongHyeon txd->tx_dmamap, BUS_DMASYNC_POSTWRITE); 2100d65abd66SPyun YongHyeon bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, 2101d65abd66SPyun YongHyeon txd->tx_dmamap); 2102d65abd66SPyun YongHyeon KASSERT(txd->tx_m != NULL, 2103d65abd66SPyun YongHyeon ("%s: freeing NULL mbufs!", __func__)); 2104d65abd66SPyun YongHyeon m_freem(txd->tx_m); 2105d65abd66SPyun YongHyeon txd->tx_m = NULL; 2106a94100faSBill Paul if (txstat & (RL_TDESC_STAT_EXCESSCOL| 2107a94100faSBill Paul RL_TDESC_STAT_COLCNT)) 2108a94100faSBill Paul ifp->if_collisions++; 2109a94100faSBill Paul if (txstat & RL_TDESC_STAT_TXERRSUM) 2110a94100faSBill Paul ifp->if_oerrors++; 2111a94100faSBill Paul else 2112a94100faSBill Paul ifp->if_opackets++; 2113a94100faSBill Paul } 2114a94100faSBill Paul sc->rl_ldata.rl_tx_free++; 2115d65abd66SPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2116a94100faSBill Paul } 2117d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_considx = cons; 2118a94100faSBill Paul 2119a94100faSBill Paul /* No changes made to the TX ring, so no flush needed */ 2120a94100faSBill Paul 2121d65abd66SPyun YongHyeon if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) { 2122ed510fb0SBill Paul #ifdef RE_TX_MODERATION 2123a94100faSBill Paul /* 2124b4b95879SMarius Strobl * If not all descriptors have been reaped yet, reload 2125b4b95879SMarius Strobl * the timer so that we will eventually get another 2126a94100faSBill Paul * interrupt that will cause us to re-enter this routine. 2127a94100faSBill Paul * This is done in case the transmitter has gone idle. 2128a94100faSBill Paul */ 2129a94100faSBill Paul CSR_WRITE_4(sc, RL_TIMERCNT, 1); 2130ed510fb0SBill Paul #endif 2131b4b95879SMarius Strobl } else 2132b4b95879SMarius Strobl sc->rl_watchdog_timer = 0; 2133a94100faSBill Paul } 2134a94100faSBill Paul 2135a94100faSBill Paul static void 21367b5ffebfSPyun YongHyeon re_tick(void *xsc) 2137a94100faSBill Paul { 2138a94100faSBill Paul struct rl_softc *sc; 2139d1754a9bSJohn Baldwin struct mii_data *mii; 2140a94100faSBill Paul 2141a94100faSBill Paul sc = xsc; 214297b9d4baSJohn-Mark Gurney 214397b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 214497b9d4baSJohn-Mark Gurney 21451d545c7aSMarius Strobl mii = device_get_softc(sc->rl_miibus); 2146a94100faSBill Paul mii_tick(mii); 21470fe200d9SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_LINK) == 0) 21480fe200d9SPyun YongHyeon re_miibus_statchg(sc->rl_dev); 2149c2d2e19cSPyun YongHyeon /* 2150c2d2e19cSPyun YongHyeon * Reclaim transmitted frames here. Technically it is not 2151c2d2e19cSPyun YongHyeon * necessary to do here but it ensures periodic reclamation 2152c2d2e19cSPyun YongHyeon * regardless of Tx completion interrupt which seems to be 2153c2d2e19cSPyun YongHyeon * lost on PCIe based controllers under certain situations. 2154c2d2e19cSPyun YongHyeon */ 2155c2d2e19cSPyun YongHyeon re_txeof(sc); 2156130b6dfbSPyun YongHyeon re_watchdog(sc); 2157d1754a9bSJohn Baldwin callout_reset(&sc->rl_stat_callout, hz, re_tick, sc); 2158a94100faSBill Paul } 2159a94100faSBill Paul 2160a94100faSBill Paul #ifdef DEVICE_POLLING 21611abcdbd1SAttilio Rao static int 2162a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 2163a94100faSBill Paul { 2164a94100faSBill Paul struct rl_softc *sc = ifp->if_softc; 21651abcdbd1SAttilio Rao int rx_npkts = 0; 2166a94100faSBill Paul 2167a94100faSBill Paul RL_LOCK(sc); 216840929967SGleb Smirnoff if (ifp->if_drv_flags & IFF_DRV_RUNNING) 21691abcdbd1SAttilio Rao rx_npkts = re_poll_locked(ifp, cmd, count); 217097b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 21711abcdbd1SAttilio Rao return (rx_npkts); 217297b9d4baSJohn-Mark Gurney } 217397b9d4baSJohn-Mark Gurney 21741abcdbd1SAttilio Rao static int 217597b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count) 217697b9d4baSJohn-Mark Gurney { 217797b9d4baSJohn-Mark Gurney struct rl_softc *sc = ifp->if_softc; 21781abcdbd1SAttilio Rao int rx_npkts; 217997b9d4baSJohn-Mark Gurney 218097b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 218197b9d4baSJohn-Mark Gurney 2182a94100faSBill Paul sc->rxcycles = count; 21831abcdbd1SAttilio Rao re_rxeof(sc, &rx_npkts); 2184a94100faSBill Paul re_txeof(sc); 2185a94100faSBill Paul 218637652939SMax Laier if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2187ed510fb0SBill Paul taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask); 2188a94100faSBill Paul 2189a94100faSBill Paul if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */ 2190a94100faSBill Paul u_int16_t status; 2191a94100faSBill Paul 2192a94100faSBill Paul status = CSR_READ_2(sc, RL_ISR); 2193a94100faSBill Paul if (status == 0xffff) 21941abcdbd1SAttilio Rao return (rx_npkts); 2195a94100faSBill Paul if (status) 2196a94100faSBill Paul CSR_WRITE_2(sc, RL_ISR, status); 2197818951afSPyun YongHyeon if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) && 2198818951afSPyun YongHyeon (sc->rl_flags & RL_FLAG_PCIE)) 2199818951afSPyun YongHyeon CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 2200a94100faSBill Paul 2201a94100faSBill Paul /* 2202a94100faSBill Paul * XXX check behaviour on receiver stalls. 2203a94100faSBill Paul */ 2204a94100faSBill Paul 22058476c243SPyun YongHyeon if (status & RL_ISR_SYSTEM_ERR) { 22068476c243SPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 220797b9d4baSJohn-Mark Gurney re_init_locked(sc); 2208a94100faSBill Paul } 22098476c243SPyun YongHyeon } 22101abcdbd1SAttilio Rao return (rx_npkts); 2211a94100faSBill Paul } 2212a94100faSBill Paul #endif /* DEVICE_POLLING */ 2213a94100faSBill Paul 2214ef544f63SPaolo Pisati static int 22157b5ffebfSPyun YongHyeon re_intr(void *arg) 2216a94100faSBill Paul { 2217a94100faSBill Paul struct rl_softc *sc; 2218ed510fb0SBill Paul uint16_t status; 2219a94100faSBill Paul 2220a94100faSBill Paul sc = arg; 2221ed510fb0SBill Paul 2222ed510fb0SBill Paul status = CSR_READ_2(sc, RL_ISR); 2223498bd0d3SBill Paul if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0) 2224ef544f63SPaolo Pisati return (FILTER_STRAY); 2225ed510fb0SBill Paul CSR_WRITE_2(sc, RL_IMR, 0); 2226ed510fb0SBill Paul 2227ed510fb0SBill Paul taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask); 2228ed510fb0SBill Paul 2229ef544f63SPaolo Pisati return (FILTER_HANDLED); 2230ed510fb0SBill Paul } 2231ed510fb0SBill Paul 2232ed510fb0SBill Paul static void 22337b5ffebfSPyun YongHyeon re_int_task(void *arg, int npending) 2234ed510fb0SBill Paul { 2235ed510fb0SBill Paul struct rl_softc *sc; 2236ed510fb0SBill Paul struct ifnet *ifp; 2237ed510fb0SBill Paul u_int16_t status; 2238ed510fb0SBill Paul int rval = 0; 2239ed510fb0SBill Paul 2240ed510fb0SBill Paul sc = arg; 2241ed510fb0SBill Paul ifp = sc->rl_ifp; 2242a94100faSBill Paul 2243a94100faSBill Paul RL_LOCK(sc); 224497b9d4baSJohn-Mark Gurney 2245a94100faSBill Paul status = CSR_READ_2(sc, RL_ISR); 2246a94100faSBill Paul CSR_WRITE_2(sc, RL_ISR, status); 2247a94100faSBill Paul 2248d65abd66SPyun YongHyeon if (sc->suspended || 2249d65abd66SPyun YongHyeon (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 2250ed510fb0SBill Paul RL_UNLOCK(sc); 2251ed510fb0SBill Paul return; 2252ed510fb0SBill Paul } 2253a94100faSBill Paul 2254ed510fb0SBill Paul #ifdef DEVICE_POLLING 2255ed510fb0SBill Paul if (ifp->if_capenable & IFCAP_POLLING) { 2256ed510fb0SBill Paul RL_UNLOCK(sc); 2257ed510fb0SBill Paul return; 2258ed510fb0SBill Paul } 2259ed510fb0SBill Paul #endif 2260a94100faSBill Paul 2261ed510fb0SBill Paul if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW)) 22621abcdbd1SAttilio Rao rval = re_rxeof(sc, NULL); 2263ed510fb0SBill Paul 2264818951afSPyun YongHyeon /* 2265818951afSPyun YongHyeon * Some chips will ignore a second TX request issued 2266818951afSPyun YongHyeon * while an existing transmission is in progress. If 2267818951afSPyun YongHyeon * the transmitter goes idle but there are still 2268818951afSPyun YongHyeon * packets waiting to be sent, we need to restart the 2269818951afSPyun YongHyeon * channel here to flush them out. This only seems to 2270818951afSPyun YongHyeon * be required with the PCIe devices. 2271818951afSPyun YongHyeon */ 2272818951afSPyun YongHyeon if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) && 2273818951afSPyun YongHyeon (sc->rl_flags & RL_FLAG_PCIE)) 2274818951afSPyun YongHyeon CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 22753d85c23dSPyun YongHyeon if (status & ( 2276ed510fb0SBill Paul #ifdef RE_TX_MODERATION 22773d85c23dSPyun YongHyeon RL_ISR_TIMEOUT_EXPIRED| 2278ed510fb0SBill Paul #else 22793d85c23dSPyun YongHyeon RL_ISR_TX_OK| 2280ed510fb0SBill Paul #endif 2281ed510fb0SBill Paul RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL)) 2282a94100faSBill Paul re_txeof(sc); 2283a94100faSBill Paul 22848476c243SPyun YongHyeon if (status & RL_ISR_SYSTEM_ERR) { 22858476c243SPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 228697b9d4baSJohn-Mark Gurney re_init_locked(sc); 22878476c243SPyun YongHyeon } 2288a94100faSBill Paul 228952732175SMax Laier if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2290ed510fb0SBill Paul taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask); 2291a94100faSBill Paul 2292a94100faSBill Paul RL_UNLOCK(sc); 2293ed510fb0SBill Paul 2294ed510fb0SBill Paul if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) { 2295ed510fb0SBill Paul taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask); 2296ed510fb0SBill Paul return; 2297ed510fb0SBill Paul } 2298ed510fb0SBill Paul 2299ed510fb0SBill Paul CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS); 2300a94100faSBill Paul } 2301a94100faSBill Paul 2302d65abd66SPyun YongHyeon static int 23037b5ffebfSPyun YongHyeon re_encap(struct rl_softc *sc, struct mbuf **m_head) 2304d65abd66SPyun YongHyeon { 2305d65abd66SPyun YongHyeon struct rl_txdesc *txd, *txd_last; 2306d65abd66SPyun YongHyeon bus_dma_segment_t segs[RL_NTXSEGS]; 2307d65abd66SPyun YongHyeon bus_dmamap_t map; 2308d65abd66SPyun YongHyeon struct mbuf *m_new; 2309d65abd66SPyun YongHyeon struct rl_desc *desc; 2310d65abd66SPyun YongHyeon int nsegs, prod; 2311d65abd66SPyun YongHyeon int i, error, ei, si; 2312d65abd66SPyun YongHyeon int padlen; 2313ccf34c81SPyun YongHyeon uint32_t cmdstat, csum_flags, vlanctl; 2314a94100faSBill Paul 2315d65abd66SPyun YongHyeon RL_LOCK_ASSERT(sc); 2316738489d1SPyun YongHyeon M_ASSERTPKTHDR((*m_head)); 23170fc4974fSBill Paul 23180fc4974fSBill Paul /* 23190fc4974fSBill Paul * With some of the RealTek chips, using the checksum offload 23200fc4974fSBill Paul * support in conjunction with the autopadding feature results 23210fc4974fSBill Paul * in the transmission of corrupt frames. For example, if we 23220fc4974fSBill Paul * need to send a really small IP fragment that's less than 60 23230fc4974fSBill Paul * bytes in size, and IP header checksumming is enabled, the 23240fc4974fSBill Paul * resulting ethernet frame that appears on the wire will 232599c8ae87SPyun YongHyeon * have garbled payload. To work around this, if TX IP checksum 23260fc4974fSBill Paul * offload is enabled, we always manually pad short frames out 2327d65abd66SPyun YongHyeon * to the minimum ethernet frame size. 23280fc4974fSBill Paul */ 2329f2e491c9SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_AUTOPAD) == 0 && 2330deb5c680SPyun YongHyeon (*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN && 233199c8ae87SPyun YongHyeon ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) { 2332d65abd66SPyun YongHyeon padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len; 2333d65abd66SPyun YongHyeon if (M_WRITABLE(*m_head) == 0) { 2334d65abd66SPyun YongHyeon /* Get a writable copy. */ 2335d65abd66SPyun YongHyeon m_new = m_dup(*m_head, M_DONTWAIT); 2336d65abd66SPyun YongHyeon m_freem(*m_head); 2337d65abd66SPyun YongHyeon if (m_new == NULL) { 2338d65abd66SPyun YongHyeon *m_head = NULL; 2339a94100faSBill Paul return (ENOBUFS); 2340a94100faSBill Paul } 2341d65abd66SPyun YongHyeon *m_head = m_new; 2342d65abd66SPyun YongHyeon } 2343d65abd66SPyun YongHyeon if ((*m_head)->m_next != NULL || 2344d65abd66SPyun YongHyeon M_TRAILINGSPACE(*m_head) < padlen) { 234580a2a305SJohn-Mark Gurney m_new = m_defrag(*m_head, M_DONTWAIT); 2346b4b95879SMarius Strobl if (m_new == NULL) { 2347b4b95879SMarius Strobl m_freem(*m_head); 2348b4b95879SMarius Strobl *m_head = NULL; 234980a2a305SJohn-Mark Gurney return (ENOBUFS); 2350b4b95879SMarius Strobl } 2351d65abd66SPyun YongHyeon } else 2352d65abd66SPyun YongHyeon m_new = *m_head; 2353a94100faSBill Paul 23540fc4974fSBill Paul /* 23550fc4974fSBill Paul * Manually pad short frames, and zero the pad space 23560fc4974fSBill Paul * to avoid leaking data. 23570fc4974fSBill Paul */ 2358d65abd66SPyun YongHyeon bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen); 2359d65abd66SPyun YongHyeon m_new->m_pkthdr.len += padlen; 23600fc4974fSBill Paul m_new->m_len = m_new->m_pkthdr.len; 2361d65abd66SPyun YongHyeon *m_head = m_new; 23620fc4974fSBill Paul } 23630fc4974fSBill Paul 2364d65abd66SPyun YongHyeon prod = sc->rl_ldata.rl_tx_prodidx; 2365d65abd66SPyun YongHyeon txd = &sc->rl_ldata.rl_tx_desc[prod]; 2366d65abd66SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap, 2367d65abd66SPyun YongHyeon *m_head, segs, &nsegs, BUS_DMA_NOWAIT); 2368d65abd66SPyun YongHyeon if (error == EFBIG) { 2369304a4c6fSJohn Baldwin m_new = m_collapse(*m_head, M_DONTWAIT, RL_NTXSEGS); 2370d65abd66SPyun YongHyeon if (m_new == NULL) { 2371d65abd66SPyun YongHyeon m_freem(*m_head); 2372b4b95879SMarius Strobl *m_head = NULL; 2373d65abd66SPyun YongHyeon return (ENOBUFS); 2374a94100faSBill Paul } 2375d65abd66SPyun YongHyeon *m_head = m_new; 2376d65abd66SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, 2377d65abd66SPyun YongHyeon txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT); 2378d65abd66SPyun YongHyeon if (error != 0) { 2379d65abd66SPyun YongHyeon m_freem(*m_head); 2380d65abd66SPyun YongHyeon *m_head = NULL; 2381d65abd66SPyun YongHyeon return (error); 2382a94100faSBill Paul } 2383d65abd66SPyun YongHyeon } else if (error != 0) 2384d65abd66SPyun YongHyeon return (error); 2385d65abd66SPyun YongHyeon if (nsegs == 0) { 2386d65abd66SPyun YongHyeon m_freem(*m_head); 2387d65abd66SPyun YongHyeon *m_head = NULL; 2388d65abd66SPyun YongHyeon return (EIO); 2389d65abd66SPyun YongHyeon } 2390d65abd66SPyun YongHyeon 2391d65abd66SPyun YongHyeon /* Check for number of available descriptors. */ 2392d65abd66SPyun YongHyeon if (sc->rl_ldata.rl_tx_free - nsegs <= 1) { 2393d65abd66SPyun YongHyeon bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap); 2394d65abd66SPyun YongHyeon return (ENOBUFS); 2395d65abd66SPyun YongHyeon } 2396d65abd66SPyun YongHyeon 2397d65abd66SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap, 2398d65abd66SPyun YongHyeon BUS_DMASYNC_PREWRITE); 2399a94100faSBill Paul 2400a94100faSBill Paul /* 2401d65abd66SPyun YongHyeon * Set up checksum offload. Note: checksum offload bits must 2402d65abd66SPyun YongHyeon * appear in all descriptors of a multi-descriptor transmit 2403d65abd66SPyun YongHyeon * attempt. This is according to testing done with an 8169 2404d65abd66SPyun YongHyeon * chip. This is a requirement. 2405a94100faSBill Paul */ 2406deb5c680SPyun YongHyeon vlanctl = 0; 2407d65abd66SPyun YongHyeon csum_flags = 0; 2408d6d7d923SPyun YongHyeon if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0) { 2409d6d7d923SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_DESCV2) != 0) { 2410d6d7d923SPyun YongHyeon csum_flags |= RL_TDESC_CMD_LGSEND; 2411d6d7d923SPyun YongHyeon vlanctl |= ((uint32_t)(*m_head)->m_pkthdr.tso_segsz << 2412d6d7d923SPyun YongHyeon RL_TDESC_CMD_MSSVALV2_SHIFT); 2413d6d7d923SPyun YongHyeon } else { 2414d6d7d923SPyun YongHyeon csum_flags |= RL_TDESC_CMD_LGSEND | 2415d65abd66SPyun YongHyeon ((uint32_t)(*m_head)->m_pkthdr.tso_segsz << 2416d65abd66SPyun YongHyeon RL_TDESC_CMD_MSSVAL_SHIFT); 2417d6d7d923SPyun YongHyeon } 2418d6d7d923SPyun YongHyeon } else { 241999c8ae87SPyun YongHyeon /* 242099c8ae87SPyun YongHyeon * Unconditionally enable IP checksum if TCP or UDP 242199c8ae87SPyun YongHyeon * checksum is required. Otherwise, TCP/UDP checksum 242299c8ae87SPyun YongHyeon * does't make effects. 242399c8ae87SPyun YongHyeon */ 242499c8ae87SPyun YongHyeon if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) { 2425deb5c680SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) { 2426d65abd66SPyun YongHyeon csum_flags |= RL_TDESC_CMD_IPCSUM; 2427deb5c680SPyun YongHyeon if (((*m_head)->m_pkthdr.csum_flags & 2428deb5c680SPyun YongHyeon CSUM_TCP) != 0) 2429d65abd66SPyun YongHyeon csum_flags |= RL_TDESC_CMD_TCPCSUM; 2430deb5c680SPyun YongHyeon if (((*m_head)->m_pkthdr.csum_flags & 2431deb5c680SPyun YongHyeon CSUM_UDP) != 0) 2432d65abd66SPyun YongHyeon csum_flags |= RL_TDESC_CMD_UDPCSUM; 2433deb5c680SPyun YongHyeon } else { 2434deb5c680SPyun YongHyeon vlanctl |= RL_TDESC_CMD_IPCSUMV2; 2435deb5c680SPyun YongHyeon if (((*m_head)->m_pkthdr.csum_flags & 2436deb5c680SPyun YongHyeon CSUM_TCP) != 0) 2437deb5c680SPyun YongHyeon vlanctl |= RL_TDESC_CMD_TCPCSUMV2; 2438deb5c680SPyun YongHyeon if (((*m_head)->m_pkthdr.csum_flags & 2439deb5c680SPyun YongHyeon CSUM_UDP) != 0) 2440deb5c680SPyun YongHyeon vlanctl |= RL_TDESC_CMD_UDPCSUMV2; 2441deb5c680SPyun YongHyeon } 2442d65abd66SPyun YongHyeon } 244399c8ae87SPyun YongHyeon } 2444a94100faSBill Paul 2445ccf34c81SPyun YongHyeon /* 2446ccf34c81SPyun YongHyeon * Set up hardware VLAN tagging. Note: vlan tag info must 2447ccf34c81SPyun YongHyeon * appear in all descriptors of a multi-descriptor 2448ccf34c81SPyun YongHyeon * transmission attempt. 2449ccf34c81SPyun YongHyeon */ 2450ccf34c81SPyun YongHyeon if ((*m_head)->m_flags & M_VLANTAG) 2451bddff934SPyun YongHyeon vlanctl |= bswap16((*m_head)->m_pkthdr.ether_vtag) | 2452deb5c680SPyun YongHyeon RL_TDESC_VLANCTL_TAG; 2453ccf34c81SPyun YongHyeon 2454d65abd66SPyun YongHyeon si = prod; 2455d65abd66SPyun YongHyeon for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) { 2456d65abd66SPyun YongHyeon desc = &sc->rl_ldata.rl_tx_list[prod]; 2457deb5c680SPyun YongHyeon desc->rl_vlanctl = htole32(vlanctl); 2458d65abd66SPyun YongHyeon desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr)); 2459d65abd66SPyun YongHyeon desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr)); 2460d65abd66SPyun YongHyeon cmdstat = segs[i].ds_len; 2461d65abd66SPyun YongHyeon if (i != 0) 2462d65abd66SPyun YongHyeon cmdstat |= RL_TDESC_CMD_OWN; 2463d65abd66SPyun YongHyeon if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1) 2464d65abd66SPyun YongHyeon cmdstat |= RL_TDESC_CMD_EOR; 2465d65abd66SPyun YongHyeon desc->rl_cmdstat = htole32(cmdstat | csum_flags); 2466d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_free--; 2467d65abd66SPyun YongHyeon } 2468d65abd66SPyun YongHyeon /* Update producer index. */ 2469d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_prodidx = prod; 2470a94100faSBill Paul 2471d65abd66SPyun YongHyeon /* Set EOF on the last descriptor. */ 2472d65abd66SPyun YongHyeon ei = RL_TX_DESC_PRV(sc, prod); 2473d65abd66SPyun YongHyeon desc = &sc->rl_ldata.rl_tx_list[ei]; 2474d65abd66SPyun YongHyeon desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF); 2475d65abd66SPyun YongHyeon 2476d65abd66SPyun YongHyeon desc = &sc->rl_ldata.rl_tx_list[si]; 2477d65abd66SPyun YongHyeon /* Set SOF and transfer ownership of packet to the chip. */ 2478d65abd66SPyun YongHyeon desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF); 2479a94100faSBill Paul 2480d65abd66SPyun YongHyeon /* 2481d65abd66SPyun YongHyeon * Insure that the map for this transmission 2482d65abd66SPyun YongHyeon * is placed at the array index of the last descriptor 2483d65abd66SPyun YongHyeon * in this chain. (Swap last and first dmamaps.) 2484d65abd66SPyun YongHyeon */ 2485d65abd66SPyun YongHyeon txd_last = &sc->rl_ldata.rl_tx_desc[ei]; 2486d65abd66SPyun YongHyeon map = txd->tx_dmamap; 2487d65abd66SPyun YongHyeon txd->tx_dmamap = txd_last->tx_dmamap; 2488d65abd66SPyun YongHyeon txd_last->tx_dmamap = map; 2489d65abd66SPyun YongHyeon txd_last->tx_m = *m_head; 2490a94100faSBill Paul 2491a94100faSBill Paul return (0); 2492a94100faSBill Paul } 2493a94100faSBill Paul 249497b9d4baSJohn-Mark Gurney static void 24957b5ffebfSPyun YongHyeon re_tx_task(void *arg, int npending) 249697b9d4baSJohn-Mark Gurney { 2497ed510fb0SBill Paul struct ifnet *ifp; 249897b9d4baSJohn-Mark Gurney 2499ed510fb0SBill Paul ifp = arg; 2500ed510fb0SBill Paul re_start(ifp); 250197b9d4baSJohn-Mark Gurney } 250297b9d4baSJohn-Mark Gurney 2503a94100faSBill Paul /* 2504a94100faSBill Paul * Main transmit routine for C+ and gigE NICs. 2505a94100faSBill Paul */ 2506a94100faSBill Paul static void 25077b5ffebfSPyun YongHyeon re_start(struct ifnet *ifp) 2508a94100faSBill Paul { 2509a94100faSBill Paul struct rl_softc *sc; 2510d65abd66SPyun YongHyeon struct mbuf *m_head; 2511d65abd66SPyun YongHyeon int queued; 2512a94100faSBill Paul 2513a94100faSBill Paul sc = ifp->if_softc; 251497b9d4baSJohn-Mark Gurney 2515ed510fb0SBill Paul RL_LOCK(sc); 2516ed510fb0SBill Paul 2517d65abd66SPyun YongHyeon if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 2518351a76f9SPyun YongHyeon IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0) { 2519ed510fb0SBill Paul RL_UNLOCK(sc); 2520ed510fb0SBill Paul return; 2521ed510fb0SBill Paul } 2522a94100faSBill Paul 2523d65abd66SPyun YongHyeon for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) && 2524d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_free > 1;) { 252552732175SMax Laier IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head); 2526a94100faSBill Paul if (m_head == NULL) 2527a94100faSBill Paul break; 2528a94100faSBill Paul 2529d65abd66SPyun YongHyeon if (re_encap(sc, &m_head) != 0) { 2530b4b95879SMarius Strobl if (m_head == NULL) 2531b4b95879SMarius Strobl break; 253252732175SMax Laier IFQ_DRV_PREPEND(&ifp->if_snd, m_head); 253313f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2534a94100faSBill Paul break; 2535a94100faSBill Paul } 2536a94100faSBill Paul 2537a94100faSBill Paul /* 2538a94100faSBill Paul * If there's a BPF listener, bounce a copy of this frame 2539a94100faSBill Paul * to him. 2540a94100faSBill Paul */ 254159a0d28bSChristian S.J. Peron ETHER_BPF_MTAP(ifp, m_head); 254252732175SMax Laier 254352732175SMax Laier queued++; 2544a94100faSBill Paul } 2545a94100faSBill Paul 2546ed510fb0SBill Paul if (queued == 0) { 2547ed510fb0SBill Paul #ifdef RE_TX_MODERATION 2548d65abd66SPyun YongHyeon if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) 2549ed510fb0SBill Paul CSR_WRITE_4(sc, RL_TIMERCNT, 1); 2550ed510fb0SBill Paul #endif 2551ed510fb0SBill Paul RL_UNLOCK(sc); 255252732175SMax Laier return; 2553ed510fb0SBill Paul } 255452732175SMax Laier 2555a94100faSBill Paul /* Flush the TX descriptors */ 2556a94100faSBill Paul 2557a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 2558a94100faSBill Paul sc->rl_ldata.rl_tx_list_map, 2559a94100faSBill Paul BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 2560a94100faSBill Paul 25610fc4974fSBill Paul CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 2562a94100faSBill Paul 2563ed510fb0SBill Paul #ifdef RE_TX_MODERATION 2564a94100faSBill Paul /* 2565a94100faSBill Paul * Use the countdown timer for interrupt moderation. 2566a94100faSBill Paul * 'TX done' interrupts are disabled. Instead, we reset the 2567a94100faSBill Paul * countdown timer, which will begin counting until it hits 2568a94100faSBill Paul * the value in the TIMERINT register, and then trigger an 2569a94100faSBill Paul * interrupt. Each time we write to the TIMERCNT register, 2570a94100faSBill Paul * the timer count is reset to 0. 2571a94100faSBill Paul */ 2572a94100faSBill Paul CSR_WRITE_4(sc, RL_TIMERCNT, 1); 2573ed510fb0SBill Paul #endif 2574a94100faSBill Paul 2575a94100faSBill Paul /* 2576a94100faSBill Paul * Set a timeout in case the chip goes out to lunch. 2577a94100faSBill Paul */ 25781d545c7aSMarius Strobl sc->rl_watchdog_timer = 5; 2579ed510fb0SBill Paul 2580ed510fb0SBill Paul RL_UNLOCK(sc); 2581a94100faSBill Paul } 2582a94100faSBill Paul 2583a94100faSBill Paul static void 25847b5ffebfSPyun YongHyeon re_init(void *xsc) 2585a94100faSBill Paul { 2586a94100faSBill Paul struct rl_softc *sc = xsc; 258797b9d4baSJohn-Mark Gurney 258897b9d4baSJohn-Mark Gurney RL_LOCK(sc); 258997b9d4baSJohn-Mark Gurney re_init_locked(sc); 259097b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 259197b9d4baSJohn-Mark Gurney } 259297b9d4baSJohn-Mark Gurney 259397b9d4baSJohn-Mark Gurney static void 25947b5ffebfSPyun YongHyeon re_init_locked(struct rl_softc *sc) 259597b9d4baSJohn-Mark Gurney { 2596fc74a9f9SBrooks Davis struct ifnet *ifp = sc->rl_ifp; 2597a94100faSBill Paul struct mii_data *mii; 2598566ca8caSJung-uk Kim uint32_t reg; 259970acaecfSPyun YongHyeon uint16_t cfg; 26004d3d7085SBernd Walter union { 26014d3d7085SBernd Walter uint32_t align_dummy; 26024d3d7085SBernd Walter u_char eaddr[ETHER_ADDR_LEN]; 26034d3d7085SBernd Walter } eaddr; 2604a94100faSBill Paul 260597b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 260697b9d4baSJohn-Mark Gurney 2607a94100faSBill Paul mii = device_get_softc(sc->rl_miibus); 2608a94100faSBill Paul 26098476c243SPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 26108476c243SPyun YongHyeon return; 26118476c243SPyun YongHyeon 2612a94100faSBill Paul /* 2613a94100faSBill Paul * Cancel pending I/O and free all RX/TX buffers. 2614a94100faSBill Paul */ 2615a94100faSBill Paul re_stop(sc); 2616a94100faSBill Paul 2617b659f1f0SPyun YongHyeon /* Put controller into known state. */ 2618b659f1f0SPyun YongHyeon re_reset(sc); 2619b659f1f0SPyun YongHyeon 2620a94100faSBill Paul /* 2621c2c6548bSBill Paul * Enable C+ RX and TX mode, as well as VLAN stripping and 2622edd03374SBill Paul * RX checksum offload. We must configure the C+ register 2623c2c6548bSBill Paul * before all others. 2624c2c6548bSBill Paul */ 262570acaecfSPyun YongHyeon cfg = RL_CPLUSCMD_PCI_MRW; 262670acaecfSPyun YongHyeon if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) 262770acaecfSPyun YongHyeon cfg |= RL_CPLUSCMD_RXCSUM_ENB; 262870acaecfSPyun YongHyeon if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) 262970acaecfSPyun YongHyeon cfg |= RL_CPLUSCMD_VLANSTRIP; 2630deb5c680SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_MACSTAT) != 0) { 2631deb5c680SPyun YongHyeon cfg |= RL_CPLUSCMD_MACSTAT_DIS; 2632deb5c680SPyun YongHyeon /* XXX magic. */ 2633deb5c680SPyun YongHyeon cfg |= 0x0001; 2634deb5c680SPyun YongHyeon } else 2635deb5c680SPyun YongHyeon cfg |= RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB; 2636deb5c680SPyun YongHyeon CSR_WRITE_2(sc, RL_CPLUS_CMD, cfg); 2637566ca8caSJung-uk Kim if (sc->rl_hwrev == RL_HWREV_8169_8110SC || 2638566ca8caSJung-uk Kim sc->rl_hwrev == RL_HWREV_8169_8110SCE) { 2639566ca8caSJung-uk Kim reg = 0x000fff00; 2640566ca8caSJung-uk Kim if ((CSR_READ_1(sc, RL_CFG2) & RL_CFG2_PCI66MHZ) != 0) 2641566ca8caSJung-uk Kim reg |= 0x000000ff; 2642566ca8caSJung-uk Kim if (sc->rl_hwrev == RL_HWREV_8169_8110SCE) 2643566ca8caSJung-uk Kim reg |= 0x00f00000; 2644566ca8caSJung-uk Kim CSR_WRITE_4(sc, 0x7c, reg); 2645566ca8caSJung-uk Kim /* Disable interrupt mitigation. */ 2646566ca8caSJung-uk Kim CSR_WRITE_2(sc, 0xe2, 0); 2647566ca8caSJung-uk Kim } 2648ae644087SPyun YongHyeon /* 2649ae644087SPyun YongHyeon * Disable TSO if interface MTU size is greater than MSS 2650ae644087SPyun YongHyeon * allowed in controller. 2651ae644087SPyun YongHyeon */ 2652ae644087SPyun YongHyeon if (ifp->if_mtu > RL_TSO_MTU && (ifp->if_capenable & IFCAP_TSO4) != 0) { 2653ae644087SPyun YongHyeon ifp->if_capenable &= ~IFCAP_TSO4; 2654ae644087SPyun YongHyeon ifp->if_hwassist &= ~CSUM_TSO; 2655ae644087SPyun YongHyeon } 2656c2c6548bSBill Paul 2657c2c6548bSBill Paul /* 2658a94100faSBill Paul * Init our MAC address. Even though the chipset 2659a94100faSBill Paul * documentation doesn't mention it, we need to enter "Config 2660a94100faSBill Paul * register write enable" mode to modify the ID registers. 2661a94100faSBill Paul */ 26624d3d7085SBernd Walter /* Copy MAC address on stack to align. */ 26634d3d7085SBernd Walter bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN); 2664a94100faSBill Paul CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG); 2665ed510fb0SBill Paul CSR_WRITE_4(sc, RL_IDR0, 2666ed510fb0SBill Paul htole32(*(u_int32_t *)(&eaddr.eaddr[0]))); 2667ed510fb0SBill Paul CSR_WRITE_4(sc, RL_IDR4, 2668ed510fb0SBill Paul htole32(*(u_int32_t *)(&eaddr.eaddr[4]))); 2669a94100faSBill Paul CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 2670a94100faSBill Paul 2671a94100faSBill Paul /* 2672a94100faSBill Paul * For C+ mode, initialize the RX descriptors and mbufs. 2673a94100faSBill Paul */ 2674a94100faSBill Paul re_rx_list_init(sc); 2675a94100faSBill Paul re_tx_list_init(sc); 2676a94100faSBill Paul 2677a94100faSBill Paul /* 2678d01fac16SPyun YongHyeon * Load the addresses of the RX and TX lists into the chip. 2679d01fac16SPyun YongHyeon */ 2680d01fac16SPyun YongHyeon 2681d01fac16SPyun YongHyeon CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI, 2682d01fac16SPyun YongHyeon RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr)); 2683d01fac16SPyun YongHyeon CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO, 2684d01fac16SPyun YongHyeon RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr)); 2685d01fac16SPyun YongHyeon 2686d01fac16SPyun YongHyeon CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI, 2687d01fac16SPyun YongHyeon RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr)); 2688d01fac16SPyun YongHyeon CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO, 2689d01fac16SPyun YongHyeon RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr)); 2690d01fac16SPyun YongHyeon 2691d01fac16SPyun YongHyeon /* 2692a94100faSBill Paul * Enable transmit and receive. 2693a94100faSBill Paul */ 2694a94100faSBill Paul CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB); 2695a94100faSBill Paul 2696a94100faSBill Paul /* 2697ff191365SJung-uk Kim * Set the initial TX configuration. 2698a94100faSBill Paul */ 2699abc8ff44SBill Paul if (sc->rl_testmode) { 2700abc8ff44SBill Paul if (sc->rl_type == RL_8169) 2701abc8ff44SBill Paul CSR_WRITE_4(sc, RL_TXCFG, 2702abc8ff44SBill Paul RL_TXCFG_CONFIG|RL_LOOPTEST_ON); 2703a94100faSBill Paul else 2704abc8ff44SBill Paul CSR_WRITE_4(sc, RL_TXCFG, 2705abc8ff44SBill Paul RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS); 2706abc8ff44SBill Paul } else 2707a94100faSBill Paul CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG); 2708d01fac16SPyun YongHyeon 2709d01fac16SPyun YongHyeon CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16); 2710d01fac16SPyun YongHyeon 2711a94100faSBill Paul /* 2712ff191365SJung-uk Kim * Set the initial RX configuration. 2713a94100faSBill Paul */ 2714ff191365SJung-uk Kim re_set_rxmode(sc); 2715a94100faSBill Paul 2716483cc440SPyun YongHyeon /* Configure interrupt moderation. */ 2717483cc440SPyun YongHyeon if (sc->rl_type == RL_8169) { 2718483cc440SPyun YongHyeon switch (sc->rl_hwrev) { 2719483cc440SPyun YongHyeon case RL_HWREV_8100E: 2720483cc440SPyun YongHyeon case RL_HWREV_8101E: 2721483cc440SPyun YongHyeon case RL_HWREV_8102E: 2722483cc440SPyun YongHyeon case RL_HWREV_8102EL: 2723483cc440SPyun YongHyeon case RL_HWREV_8102EL_SPIN1: 2724483cc440SPyun YongHyeon case RL_HWREV_8103E: 2725483cc440SPyun YongHyeon CSR_WRITE_2(sc, RL_INTRMOD, 0); 2726483cc440SPyun YongHyeon break; 2727483cc440SPyun YongHyeon default: 2728483cc440SPyun YongHyeon /* Magic from vendor. */ 27295e6906eeSPyun YongHyeon CSR_WRITE_2(sc, RL_INTRMOD, 0x5100); 2730483cc440SPyun YongHyeon break; 2731483cc440SPyun YongHyeon } 2732483cc440SPyun YongHyeon } 2733483cc440SPyun YongHyeon 2734a94100faSBill Paul #ifdef DEVICE_POLLING 2735a94100faSBill Paul /* 2736a94100faSBill Paul * Disable interrupts if we are polling. 2737a94100faSBill Paul */ 273840929967SGleb Smirnoff if (ifp->if_capenable & IFCAP_POLLING) 2739a94100faSBill Paul CSR_WRITE_2(sc, RL_IMR, 0); 2740a94100faSBill Paul else /* otherwise ... */ 274140929967SGleb Smirnoff #endif 2742ed510fb0SBill Paul 2743a94100faSBill Paul /* 2744a94100faSBill Paul * Enable interrupts. 2745a94100faSBill Paul */ 2746a94100faSBill Paul if (sc->rl_testmode) 2747a94100faSBill Paul CSR_WRITE_2(sc, RL_IMR, 0); 2748a94100faSBill Paul else 2749a94100faSBill Paul CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS); 2750ed510fb0SBill Paul CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS); 2751a94100faSBill Paul 2752a94100faSBill Paul /* Set initial TX threshold */ 2753a94100faSBill Paul sc->rl_txthresh = RL_TX_THRESH_INIT; 2754a94100faSBill Paul 2755a94100faSBill Paul /* Start RX/TX process. */ 2756a94100faSBill Paul CSR_WRITE_4(sc, RL_MISSEDPKT, 0); 2757a94100faSBill Paul #ifdef notdef 2758a94100faSBill Paul /* Enable receiver and transmitter. */ 2759a94100faSBill Paul CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB); 2760a94100faSBill Paul #endif 2761a94100faSBill Paul 2762ed510fb0SBill Paul #ifdef RE_TX_MODERATION 2763a94100faSBill Paul /* 2764a94100faSBill Paul * Initialize the timer interrupt register so that 2765a94100faSBill Paul * a timer interrupt will be generated once the timer 2766a94100faSBill Paul * reaches a certain number of ticks. The timer is 2767a94100faSBill Paul * reloaded on each transmit. This gives us TX interrupt 2768a94100faSBill Paul * moderation, which dramatically improves TX frame rate. 2769a94100faSBill Paul */ 2770a94100faSBill Paul if (sc->rl_type == RL_8169) 2771a94100faSBill Paul CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800); 2772a94100faSBill Paul else 2773a94100faSBill Paul CSR_WRITE_4(sc, RL_TIMERINT, 0x400); 2774ed510fb0SBill Paul #endif 2775a94100faSBill Paul 2776a94100faSBill Paul /* 2777a94100faSBill Paul * For 8169 gigE NICs, set the max allowed RX packet 2778a94100faSBill Paul * size so we can receive jumbo frames. 2779a94100faSBill Paul */ 2780*89feeee4SPyun YongHyeon if (sc->rl_type == RL_8169) { 2781*89feeee4SPyun YongHyeon if ((sc->rl_flags & (RL_FLAG_PCIE | RL_FLAG_NOJUMBO)) == 2782*89feeee4SPyun YongHyeon (RL_FLAG_PCIE | RL_FLAG_NOJUMBO)) 2783*89feeee4SPyun YongHyeon CSR_WRITE_2(sc, RL_MAXRXPKTLEN, RE_RX_DESC_BUFLEN); 2784*89feeee4SPyun YongHyeon else 2785a94100faSBill Paul CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383); 2786*89feeee4SPyun YongHyeon } 2787a94100faSBill Paul 278897b9d4baSJohn-Mark Gurney if (sc->rl_testmode) 2789a94100faSBill Paul return; 2790a94100faSBill Paul 2791a94100faSBill Paul mii_mediachg(mii); 2792a94100faSBill Paul 279319ecd231SPyun YongHyeon CSR_WRITE_1(sc, RL_CFG1, CSR_READ_1(sc, RL_CFG1) | RL_CFG1_DRVLOAD); 2794a94100faSBill Paul 279513f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 279613f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2797a94100faSBill Paul 2798351a76f9SPyun YongHyeon sc->rl_flags &= ~RL_FLAG_LINK; 27991d545c7aSMarius Strobl sc->rl_watchdog_timer = 0; 2800d1754a9bSJohn Baldwin callout_reset(&sc->rl_stat_callout, hz, re_tick, sc); 2801a94100faSBill Paul } 2802a94100faSBill Paul 2803a94100faSBill Paul /* 2804a94100faSBill Paul * Set media options. 2805a94100faSBill Paul */ 2806a94100faSBill Paul static int 28077b5ffebfSPyun YongHyeon re_ifmedia_upd(struct ifnet *ifp) 2808a94100faSBill Paul { 2809a94100faSBill Paul struct rl_softc *sc; 2810a94100faSBill Paul struct mii_data *mii; 28116f0f9b12SPyun YongHyeon int error; 2812a94100faSBill Paul 2813a94100faSBill Paul sc = ifp->if_softc; 2814a94100faSBill Paul mii = device_get_softc(sc->rl_miibus); 2815d1754a9bSJohn Baldwin RL_LOCK(sc); 28166f0f9b12SPyun YongHyeon error = mii_mediachg(mii); 2817d1754a9bSJohn Baldwin RL_UNLOCK(sc); 2818a94100faSBill Paul 28196f0f9b12SPyun YongHyeon return (error); 2820a94100faSBill Paul } 2821a94100faSBill Paul 2822a94100faSBill Paul /* 2823a94100faSBill Paul * Report current media status. 2824a94100faSBill Paul */ 2825a94100faSBill Paul static void 28267b5ffebfSPyun YongHyeon re_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 2827a94100faSBill Paul { 2828a94100faSBill Paul struct rl_softc *sc; 2829a94100faSBill Paul struct mii_data *mii; 2830a94100faSBill Paul 2831a94100faSBill Paul sc = ifp->if_softc; 2832a94100faSBill Paul mii = device_get_softc(sc->rl_miibus); 2833a94100faSBill Paul 2834d1754a9bSJohn Baldwin RL_LOCK(sc); 2835a94100faSBill Paul mii_pollstat(mii); 2836d1754a9bSJohn Baldwin RL_UNLOCK(sc); 2837a94100faSBill Paul ifmr->ifm_active = mii->mii_media_active; 2838a94100faSBill Paul ifmr->ifm_status = mii->mii_media_status; 2839a94100faSBill Paul } 2840a94100faSBill Paul 2841a94100faSBill Paul static int 28427b5ffebfSPyun YongHyeon re_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 2843a94100faSBill Paul { 2844a94100faSBill Paul struct rl_softc *sc = ifp->if_softc; 2845a94100faSBill Paul struct ifreq *ifr = (struct ifreq *) data; 2846a94100faSBill Paul struct mii_data *mii; 284740929967SGleb Smirnoff int error = 0; 2848a94100faSBill Paul 2849a94100faSBill Paul switch (command) { 2850a94100faSBill Paul case SIOCSIFMTU: 2851c1d0b573SPyun YongHyeon if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > RL_JUMBO_MTU) { 2852a94100faSBill Paul error = EINVAL; 2853c1d0b573SPyun YongHyeon break; 2854c1d0b573SPyun YongHyeon } 2855351a76f9SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_NOJUMBO) != 0 && 2856c1d0b573SPyun YongHyeon ifr->ifr_mtu > RL_MAX_FRAMELEN) { 2857c1d0b573SPyun YongHyeon error = EINVAL; 2858c1d0b573SPyun YongHyeon break; 2859c1d0b573SPyun YongHyeon } 2860c1d0b573SPyun YongHyeon RL_LOCK(sc); 2861c1d0b573SPyun YongHyeon if (ifp->if_mtu != ifr->ifr_mtu) 2862a94100faSBill Paul ifp->if_mtu = ifr->ifr_mtu; 2863ae644087SPyun YongHyeon if (ifp->if_mtu > RL_TSO_MTU && 2864ae644087SPyun YongHyeon (ifp->if_capenable & IFCAP_TSO4) != 0) { 2865ae644087SPyun YongHyeon ifp->if_capenable &= ~IFCAP_TSO4; 2866ae644087SPyun YongHyeon ifp->if_hwassist &= ~CSUM_TSO; 2867ecafbbb5SPyun YongHyeon VLAN_CAPABILITIES(ifp); 2868ae644087SPyun YongHyeon } 2869d1754a9bSJohn Baldwin RL_UNLOCK(sc); 2870a94100faSBill Paul break; 2871a94100faSBill Paul case SIOCSIFFLAGS: 287297b9d4baSJohn-Mark Gurney RL_LOCK(sc); 2873eed497bbSPyun YongHyeon if ((ifp->if_flags & IFF_UP) != 0) { 2874eed497bbSPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) { 2875eed497bbSPyun YongHyeon if (((ifp->if_flags ^ sc->rl_if_flags) 28763021aef8SPyun YongHyeon & (IFF_PROMISC | IFF_ALLMULTI)) != 0) 2877ff191365SJung-uk Kim re_set_rxmode(sc); 2878eed497bbSPyun YongHyeon } else 287997b9d4baSJohn-Mark Gurney re_init_locked(sc); 2880eed497bbSPyun YongHyeon } else { 2881eed497bbSPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 2882a94100faSBill Paul re_stop(sc); 2883eed497bbSPyun YongHyeon } 2884eed497bbSPyun YongHyeon sc->rl_if_flags = ifp->if_flags; 288597b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 2886a94100faSBill Paul break; 2887a94100faSBill Paul case SIOCADDMULTI: 2888a94100faSBill Paul case SIOCDELMULTI: 288997b9d4baSJohn-Mark Gurney RL_LOCK(sc); 28908476c243SPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 2891ff191365SJung-uk Kim re_set_rxmode(sc); 289297b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 2893a94100faSBill Paul break; 2894a94100faSBill Paul case SIOCGIFMEDIA: 2895a94100faSBill Paul case SIOCSIFMEDIA: 2896a94100faSBill Paul mii = device_get_softc(sc->rl_miibus); 2897a94100faSBill Paul error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 2898a94100faSBill Paul break; 2899a94100faSBill Paul case SIOCSIFCAP: 290040929967SGleb Smirnoff { 2901f051cb85SGleb Smirnoff int mask, reinit; 2902f051cb85SGleb Smirnoff 2903f051cb85SGleb Smirnoff mask = ifr->ifr_reqcap ^ ifp->if_capenable; 2904f051cb85SGleb Smirnoff reinit = 0; 290540929967SGleb Smirnoff #ifdef DEVICE_POLLING 290640929967SGleb Smirnoff if (mask & IFCAP_POLLING) { 290740929967SGleb Smirnoff if (ifr->ifr_reqcap & IFCAP_POLLING) { 290840929967SGleb Smirnoff error = ether_poll_register(re_poll, ifp); 290940929967SGleb Smirnoff if (error) 291040929967SGleb Smirnoff return (error); 2911d1754a9bSJohn Baldwin RL_LOCK(sc); 291240929967SGleb Smirnoff /* Disable interrupts */ 291340929967SGleb Smirnoff CSR_WRITE_2(sc, RL_IMR, 0x0000); 291440929967SGleb Smirnoff ifp->if_capenable |= IFCAP_POLLING; 291540929967SGleb Smirnoff RL_UNLOCK(sc); 291640929967SGleb Smirnoff } else { 291740929967SGleb Smirnoff error = ether_poll_deregister(ifp); 291840929967SGleb Smirnoff /* Enable interrupts. */ 291940929967SGleb Smirnoff RL_LOCK(sc); 292040929967SGleb Smirnoff CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS); 292140929967SGleb Smirnoff ifp->if_capenable &= ~IFCAP_POLLING; 292240929967SGleb Smirnoff RL_UNLOCK(sc); 292340929967SGleb Smirnoff } 292440929967SGleb Smirnoff } 292540929967SGleb Smirnoff #endif /* DEVICE_POLLING */ 292640929967SGleb Smirnoff if (mask & IFCAP_HWCSUM) { 2927f051cb85SGleb Smirnoff ifp->if_capenable ^= IFCAP_HWCSUM; 2928a94100faSBill Paul if (ifp->if_capenable & IFCAP_TXCSUM) 2929dc74159dSPyun YongHyeon ifp->if_hwassist |= RE_CSUM_FEATURES; 2930a94100faSBill Paul else 2931b61178a9SPyun YongHyeon ifp->if_hwassist &= ~RE_CSUM_FEATURES; 2932f051cb85SGleb Smirnoff reinit = 1; 293340929967SGleb Smirnoff } 2934ecafbbb5SPyun YongHyeon if ((mask & IFCAP_TSO4) != 0 && 2935ecafbbb5SPyun YongHyeon (ifp->if_capabilities & IFCAP_TSO) != 0) { 2936dc74159dSPyun YongHyeon ifp->if_capenable ^= IFCAP_TSO4; 2937ecafbbb5SPyun YongHyeon if ((IFCAP_TSO4 & ifp->if_capenable) != 0) 2938dc74159dSPyun YongHyeon ifp->if_hwassist |= CSUM_TSO; 2939dc74159dSPyun YongHyeon else 2940dc74159dSPyun YongHyeon ifp->if_hwassist &= ~CSUM_TSO; 2941ae644087SPyun YongHyeon if (ifp->if_mtu > RL_TSO_MTU && 2942ae644087SPyun YongHyeon (ifp->if_capenable & IFCAP_TSO4) != 0) { 2943ae644087SPyun YongHyeon ifp->if_capenable &= ~IFCAP_TSO4; 2944ae644087SPyun YongHyeon ifp->if_hwassist &= ~CSUM_TSO; 2945ae644087SPyun YongHyeon } 2946dc74159dSPyun YongHyeon } 2947ecafbbb5SPyun YongHyeon if ((mask & IFCAP_VLAN_HWTSO) != 0 && 2948ecafbbb5SPyun YongHyeon (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0) 2949ecafbbb5SPyun YongHyeon ifp->if_capenable ^= IFCAP_VLAN_HWTSO; 2950ecafbbb5SPyun YongHyeon if ((mask & IFCAP_VLAN_HWTAGGING) != 0 && 2951ecafbbb5SPyun YongHyeon (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) { 2952ecafbbb5SPyun YongHyeon ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING; 2953ecafbbb5SPyun YongHyeon /* TSO over VLAN requires VLAN hardware tagging. */ 2954ecafbbb5SPyun YongHyeon if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) 2955ecafbbb5SPyun YongHyeon ifp->if_capenable &= ~IFCAP_VLAN_HWTSO; 2956ecafbbb5SPyun YongHyeon reinit = 1; 2957ecafbbb5SPyun YongHyeon } 29587467bd53SPyun YongHyeon if ((mask & IFCAP_WOL) != 0 && 29597467bd53SPyun YongHyeon (ifp->if_capabilities & IFCAP_WOL) != 0) { 29607467bd53SPyun YongHyeon if ((mask & IFCAP_WOL_UCAST) != 0) 29617467bd53SPyun YongHyeon ifp->if_capenable ^= IFCAP_WOL_UCAST; 29627467bd53SPyun YongHyeon if ((mask & IFCAP_WOL_MCAST) != 0) 29637467bd53SPyun YongHyeon ifp->if_capenable ^= IFCAP_WOL_MCAST; 29647467bd53SPyun YongHyeon if ((mask & IFCAP_WOL_MAGIC) != 0) 29657467bd53SPyun YongHyeon ifp->if_capenable ^= IFCAP_WOL_MAGIC; 29667467bd53SPyun YongHyeon } 29678476c243SPyun YongHyeon if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING) { 29688476c243SPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2969f051cb85SGleb Smirnoff re_init(sc); 29708476c243SPyun YongHyeon } 2971960fd5b3SPyun YongHyeon VLAN_CAPABILITIES(ifp); 297240929967SGleb Smirnoff } 2973a94100faSBill Paul break; 2974a94100faSBill Paul default: 2975a94100faSBill Paul error = ether_ioctl(ifp, command, data); 2976a94100faSBill Paul break; 2977a94100faSBill Paul } 2978a94100faSBill Paul 2979a94100faSBill Paul return (error); 2980a94100faSBill Paul } 2981a94100faSBill Paul 2982a94100faSBill Paul static void 29837b5ffebfSPyun YongHyeon re_watchdog(struct rl_softc *sc) 29841d545c7aSMarius Strobl { 2985130b6dfbSPyun YongHyeon struct ifnet *ifp; 2986a94100faSBill Paul 29871d545c7aSMarius Strobl RL_LOCK_ASSERT(sc); 29881d545c7aSMarius Strobl 29891d545c7aSMarius Strobl if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0) 29901d545c7aSMarius Strobl return; 29911d545c7aSMarius Strobl 2992130b6dfbSPyun YongHyeon ifp = sc->rl_ifp; 2993a94100faSBill Paul re_txeof(sc); 2994130b6dfbSPyun YongHyeon if (sc->rl_ldata.rl_tx_free == sc->rl_ldata.rl_tx_desc_cnt) { 2995130b6dfbSPyun YongHyeon if_printf(ifp, "watchdog timeout (missed Tx interrupts) " 2996130b6dfbSPyun YongHyeon "-- recovering\n"); 2997130b6dfbSPyun YongHyeon if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2998130b6dfbSPyun YongHyeon taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask); 2999130b6dfbSPyun YongHyeon return; 3000130b6dfbSPyun YongHyeon } 3001130b6dfbSPyun YongHyeon 3002130b6dfbSPyun YongHyeon if_printf(ifp, "watchdog timeout\n"); 3003130b6dfbSPyun YongHyeon ifp->if_oerrors++; 3004130b6dfbSPyun YongHyeon 30051abcdbd1SAttilio Rao re_rxeof(sc, NULL); 30068476c243SPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 300797b9d4baSJohn-Mark Gurney re_init_locked(sc); 3008130b6dfbSPyun YongHyeon if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 3009130b6dfbSPyun YongHyeon taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask); 3010a94100faSBill Paul } 3011a94100faSBill Paul 3012a94100faSBill Paul /* 3013a94100faSBill Paul * Stop the adapter and free any mbufs allocated to the 3014a94100faSBill Paul * RX and TX lists. 3015a94100faSBill Paul */ 3016a94100faSBill Paul static void 30177b5ffebfSPyun YongHyeon re_stop(struct rl_softc *sc) 3018a94100faSBill Paul { 30190ce0868aSPyun YongHyeon int i; 3020a94100faSBill Paul struct ifnet *ifp; 3021d65abd66SPyun YongHyeon struct rl_txdesc *txd; 3022d65abd66SPyun YongHyeon struct rl_rxdesc *rxd; 3023a94100faSBill Paul 302497b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 302597b9d4baSJohn-Mark Gurney 3026fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 3027a94100faSBill Paul 30281d545c7aSMarius Strobl sc->rl_watchdog_timer = 0; 3029d1754a9bSJohn Baldwin callout_stop(&sc->rl_stat_callout); 303013f4c340SRobert Watson ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 3031a94100faSBill Paul 3032ead8fc66SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_CMDSTOP) != 0) 3033ead8fc66SPyun YongHyeon CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_STOPREQ | RL_CMD_TX_ENB | 3034ead8fc66SPyun YongHyeon RL_CMD_RX_ENB); 3035ead8fc66SPyun YongHyeon else 3036a94100faSBill Paul CSR_WRITE_1(sc, RL_COMMAND, 0x00); 3037ead8fc66SPyun YongHyeon DELAY(1000); 3038a94100faSBill Paul CSR_WRITE_2(sc, RL_IMR, 0x0000); 3039ed510fb0SBill Paul CSR_WRITE_2(sc, RL_ISR, 0xFFFF); 3040a94100faSBill Paul 3041a94100faSBill Paul if (sc->rl_head != NULL) { 3042a94100faSBill Paul m_freem(sc->rl_head); 3043a94100faSBill Paul sc->rl_head = sc->rl_tail = NULL; 3044a94100faSBill Paul } 3045a94100faSBill Paul 3046a94100faSBill Paul /* Free the TX list buffers. */ 3047a94100faSBill Paul 3048d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) { 3049d65abd66SPyun YongHyeon txd = &sc->rl_ldata.rl_tx_desc[i]; 3050d65abd66SPyun YongHyeon if (txd->tx_m != NULL) { 3051d65abd66SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, 3052d65abd66SPyun YongHyeon txd->tx_dmamap, BUS_DMASYNC_POSTWRITE); 3053d65abd66SPyun YongHyeon bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, 3054d65abd66SPyun YongHyeon txd->tx_dmamap); 3055d65abd66SPyun YongHyeon m_freem(txd->tx_m); 3056d65abd66SPyun YongHyeon txd->tx_m = NULL; 3057a94100faSBill Paul } 3058a94100faSBill Paul } 3059a94100faSBill Paul 3060a94100faSBill Paul /* Free the RX list buffers. */ 3061a94100faSBill Paul 3062d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 3063d65abd66SPyun YongHyeon rxd = &sc->rl_ldata.rl_rx_desc[i]; 3064d65abd66SPyun YongHyeon if (rxd->rx_m != NULL) { 3065d65abd66SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, 3066d65abd66SPyun YongHyeon rxd->rx_dmamap, BUS_DMASYNC_POSTREAD); 3067d65abd66SPyun YongHyeon bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, 3068d65abd66SPyun YongHyeon rxd->rx_dmamap); 3069d65abd66SPyun YongHyeon m_freem(rxd->rx_m); 3070d65abd66SPyun YongHyeon rxd->rx_m = NULL; 3071a94100faSBill Paul } 3072a94100faSBill Paul } 3073a94100faSBill Paul } 3074a94100faSBill Paul 3075a94100faSBill Paul /* 3076a94100faSBill Paul * Device suspend routine. Stop the interface and save some PCI 3077a94100faSBill Paul * settings in case the BIOS doesn't restore them properly on 3078a94100faSBill Paul * resume. 3079a94100faSBill Paul */ 3080a94100faSBill Paul static int 30817b5ffebfSPyun YongHyeon re_suspend(device_t dev) 3082a94100faSBill Paul { 3083a94100faSBill Paul struct rl_softc *sc; 3084a94100faSBill Paul 3085a94100faSBill Paul sc = device_get_softc(dev); 3086a94100faSBill Paul 308797b9d4baSJohn-Mark Gurney RL_LOCK(sc); 3088a94100faSBill Paul re_stop(sc); 30897467bd53SPyun YongHyeon re_setwol(sc); 3090a94100faSBill Paul sc->suspended = 1; 309197b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 3092a94100faSBill Paul 3093a94100faSBill Paul return (0); 3094a94100faSBill Paul } 3095a94100faSBill Paul 3096a94100faSBill Paul /* 3097a94100faSBill Paul * Device resume routine. Restore some PCI settings in case the BIOS 3098a94100faSBill Paul * doesn't, re-enable busmastering, and restart the interface if 3099a94100faSBill Paul * appropriate. 3100a94100faSBill Paul */ 3101a94100faSBill Paul static int 31027b5ffebfSPyun YongHyeon re_resume(device_t dev) 3103a94100faSBill Paul { 3104a94100faSBill Paul struct rl_softc *sc; 3105a94100faSBill Paul struct ifnet *ifp; 3106a94100faSBill Paul 3107a94100faSBill Paul sc = device_get_softc(dev); 310897b9d4baSJohn-Mark Gurney 310997b9d4baSJohn-Mark Gurney RL_LOCK(sc); 311097b9d4baSJohn-Mark Gurney 3111fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 311261f45a72SPyun YongHyeon /* Take controller out of sleep mode. */ 311361f45a72SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) { 311461f45a72SPyun YongHyeon if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80) 311561f45a72SPyun YongHyeon CSR_WRITE_1(sc, RL_GPIO, 311661f45a72SPyun YongHyeon CSR_READ_1(sc, RL_GPIO) | 0x01); 311761f45a72SPyun YongHyeon } 3118a94100faSBill Paul 31197467bd53SPyun YongHyeon /* 31207467bd53SPyun YongHyeon * Clear WOL matching such that normal Rx filtering 31217467bd53SPyun YongHyeon * wouldn't interfere with WOL patterns. 31227467bd53SPyun YongHyeon */ 31237467bd53SPyun YongHyeon re_clrwol(sc); 312401d1a6c3SPyun YongHyeon 312501d1a6c3SPyun YongHyeon /* reinitialize interface if necessary */ 312601d1a6c3SPyun YongHyeon if (ifp->if_flags & IFF_UP) 312701d1a6c3SPyun YongHyeon re_init_locked(sc); 312801d1a6c3SPyun YongHyeon 3129a94100faSBill Paul sc->suspended = 0; 313097b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 3131a94100faSBill Paul 3132a94100faSBill Paul return (0); 3133a94100faSBill Paul } 3134a94100faSBill Paul 3135a94100faSBill Paul /* 3136a94100faSBill Paul * Stop all chip I/O so that the kernel's probe routines don't 3137a94100faSBill Paul * get confused by errant DMAs when rebooting. 3138a94100faSBill Paul */ 31396a087a87SPyun YongHyeon static int 31407b5ffebfSPyun YongHyeon re_shutdown(device_t dev) 3141a94100faSBill Paul { 3142a94100faSBill Paul struct rl_softc *sc; 3143a94100faSBill Paul 3144a94100faSBill Paul sc = device_get_softc(dev); 3145a94100faSBill Paul 314697b9d4baSJohn-Mark Gurney RL_LOCK(sc); 3147a94100faSBill Paul re_stop(sc); 3148536fde34SMaxim Sobolev /* 3149536fde34SMaxim Sobolev * Mark interface as down since otherwise we will panic if 3150536fde34SMaxim Sobolev * interrupt comes in later on, which can happen in some 315172293673SRuslan Ermilov * cases. 3152536fde34SMaxim Sobolev */ 3153536fde34SMaxim Sobolev sc->rl_ifp->if_flags &= ~IFF_UP; 31547467bd53SPyun YongHyeon re_setwol(sc); 315597b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 31566a087a87SPyun YongHyeon 31576a087a87SPyun YongHyeon return (0); 3158a94100faSBill Paul } 31597467bd53SPyun YongHyeon 31607467bd53SPyun YongHyeon static void 31617b5ffebfSPyun YongHyeon re_setwol(struct rl_softc *sc) 31627467bd53SPyun YongHyeon { 31637467bd53SPyun YongHyeon struct ifnet *ifp; 31647467bd53SPyun YongHyeon int pmc; 31657467bd53SPyun YongHyeon uint16_t pmstat; 31667467bd53SPyun YongHyeon uint8_t v; 31677467bd53SPyun YongHyeon 31687467bd53SPyun YongHyeon RL_LOCK_ASSERT(sc); 31697467bd53SPyun YongHyeon 31707467bd53SPyun YongHyeon if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0) 31717467bd53SPyun YongHyeon return; 31727467bd53SPyun YongHyeon 31737467bd53SPyun YongHyeon ifp = sc->rl_ifp; 317461f45a72SPyun YongHyeon /* Put controller into sleep mode. */ 317561f45a72SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) { 317661f45a72SPyun YongHyeon if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80) 317761f45a72SPyun YongHyeon CSR_WRITE_1(sc, RL_GPIO, 317861f45a72SPyun YongHyeon CSR_READ_1(sc, RL_GPIO) & ~0x01); 317961f45a72SPyun YongHyeon } 3180886ff602SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL) != 0 && 3181886ff602SPyun YongHyeon (sc->rl_flags & RL_FLAG_WOLRXENB) != 0) 3182886ff602SPyun YongHyeon CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RX_ENB); 31837467bd53SPyun YongHyeon /* Enable config register write. */ 31847467bd53SPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE); 31857467bd53SPyun YongHyeon 31867467bd53SPyun YongHyeon /* Enable PME. */ 31877467bd53SPyun YongHyeon v = CSR_READ_1(sc, RL_CFG1); 31887467bd53SPyun YongHyeon v &= ~RL_CFG1_PME; 31897467bd53SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL) != 0) 31907467bd53SPyun YongHyeon v |= RL_CFG1_PME; 31917467bd53SPyun YongHyeon CSR_WRITE_1(sc, RL_CFG1, v); 31927467bd53SPyun YongHyeon 31937467bd53SPyun YongHyeon v = CSR_READ_1(sc, RL_CFG3); 31947467bd53SPyun YongHyeon v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC); 31957467bd53SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0) 31967467bd53SPyun YongHyeon v |= RL_CFG3_WOL_MAGIC; 31977467bd53SPyun YongHyeon CSR_WRITE_1(sc, RL_CFG3, v); 31987467bd53SPyun YongHyeon 31997467bd53SPyun YongHyeon /* Config register write done. */ 3200f98dd8cfSPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 32017467bd53SPyun YongHyeon 32027467bd53SPyun YongHyeon v = CSR_READ_1(sc, RL_CFG5); 32037467bd53SPyun YongHyeon v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST); 32047467bd53SPyun YongHyeon v &= ~RL_CFG5_WOL_LANWAKE; 32057467bd53SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0) 32067467bd53SPyun YongHyeon v |= RL_CFG5_WOL_UCAST; 32077467bd53SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0) 32087467bd53SPyun YongHyeon v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST; 32097467bd53SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL) != 0) 32107467bd53SPyun YongHyeon v |= RL_CFG5_WOL_LANWAKE; 32117467bd53SPyun YongHyeon CSR_WRITE_1(sc, RL_CFG5, v); 32127467bd53SPyun YongHyeon 3213d0c45156SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL) != 0 && 3214d0c45156SPyun YongHyeon (sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0) 3215d0c45156SPyun YongHyeon CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) & ~0x80); 32167467bd53SPyun YongHyeon /* 32177467bd53SPyun YongHyeon * It seems that hardware resets its link speed to 100Mbps in 32187467bd53SPyun YongHyeon * power down mode so switching to 100Mbps in driver is not 32197467bd53SPyun YongHyeon * needed. 32207467bd53SPyun YongHyeon */ 32217467bd53SPyun YongHyeon 32227467bd53SPyun YongHyeon /* Request PME if WOL is requested. */ 32237467bd53SPyun YongHyeon pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2); 32247467bd53SPyun YongHyeon pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE); 32257467bd53SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL) != 0) 32267467bd53SPyun YongHyeon pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE; 32277467bd53SPyun YongHyeon pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2); 32287467bd53SPyun YongHyeon } 32297467bd53SPyun YongHyeon 32307467bd53SPyun YongHyeon static void 32317b5ffebfSPyun YongHyeon re_clrwol(struct rl_softc *sc) 32327467bd53SPyun YongHyeon { 32337467bd53SPyun YongHyeon int pmc; 32347467bd53SPyun YongHyeon uint8_t v; 32357467bd53SPyun YongHyeon 32367467bd53SPyun YongHyeon RL_LOCK_ASSERT(sc); 32377467bd53SPyun YongHyeon 32387467bd53SPyun YongHyeon if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0) 32397467bd53SPyun YongHyeon return; 32407467bd53SPyun YongHyeon 32417467bd53SPyun YongHyeon /* Enable config register write. */ 32427467bd53SPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE); 32437467bd53SPyun YongHyeon 32447467bd53SPyun YongHyeon v = CSR_READ_1(sc, RL_CFG3); 32457467bd53SPyun YongHyeon v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC); 32467467bd53SPyun YongHyeon CSR_WRITE_1(sc, RL_CFG3, v); 32477467bd53SPyun YongHyeon 32487467bd53SPyun YongHyeon /* Config register write done. */ 3249f98dd8cfSPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 32507467bd53SPyun YongHyeon 32517467bd53SPyun YongHyeon v = CSR_READ_1(sc, RL_CFG5); 32527467bd53SPyun YongHyeon v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST); 32537467bd53SPyun YongHyeon v &= ~RL_CFG5_WOL_LANWAKE; 32547467bd53SPyun YongHyeon CSR_WRITE_1(sc, RL_CFG5, v); 32557467bd53SPyun YongHyeon } 32560534aae0SPyun YongHyeon 32570534aae0SPyun YongHyeon static void 32580534aae0SPyun YongHyeon re_add_sysctls(struct rl_softc *sc) 32590534aae0SPyun YongHyeon { 32600534aae0SPyun YongHyeon struct sysctl_ctx_list *ctx; 32610534aae0SPyun YongHyeon struct sysctl_oid_list *children; 32620534aae0SPyun YongHyeon 32630534aae0SPyun YongHyeon ctx = device_get_sysctl_ctx(sc->rl_dev); 32640534aae0SPyun YongHyeon children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->rl_dev)); 32650534aae0SPyun YongHyeon 32660534aae0SPyun YongHyeon SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "stats", 32670534aae0SPyun YongHyeon CTLTYPE_INT | CTLFLAG_RW, sc, 0, re_sysctl_stats, "I", 32680534aae0SPyun YongHyeon "Statistics Information"); 32690534aae0SPyun YongHyeon } 32700534aae0SPyun YongHyeon 32710534aae0SPyun YongHyeon static int 32720534aae0SPyun YongHyeon re_sysctl_stats(SYSCTL_HANDLER_ARGS) 32730534aae0SPyun YongHyeon { 32740534aae0SPyun YongHyeon struct rl_softc *sc; 32750534aae0SPyun YongHyeon struct rl_stats *stats; 32760534aae0SPyun YongHyeon int error, i, result; 32770534aae0SPyun YongHyeon 32780534aae0SPyun YongHyeon result = -1; 32790534aae0SPyun YongHyeon error = sysctl_handle_int(oidp, &result, 0, req); 32800534aae0SPyun YongHyeon if (error || req->newptr == NULL) 32810534aae0SPyun YongHyeon return (error); 32820534aae0SPyun YongHyeon 32830534aae0SPyun YongHyeon if (result == 1) { 32840534aae0SPyun YongHyeon sc = (struct rl_softc *)arg1; 32850534aae0SPyun YongHyeon RL_LOCK(sc); 328616a4824bSPyun YongHyeon if ((sc->rl_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 328716a4824bSPyun YongHyeon RL_UNLOCK(sc); 328816a4824bSPyun YongHyeon goto done; 328916a4824bSPyun YongHyeon } 32900534aae0SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_stag, 32910534aae0SPyun YongHyeon sc->rl_ldata.rl_smap, BUS_DMASYNC_PREREAD); 32920534aae0SPyun YongHyeon CSR_WRITE_4(sc, RL_DUMPSTATS_HI, 32930534aae0SPyun YongHyeon RL_ADDR_HI(sc->rl_ldata.rl_stats_addr)); 32940534aae0SPyun YongHyeon CSR_WRITE_4(sc, RL_DUMPSTATS_LO, 32950534aae0SPyun YongHyeon RL_ADDR_LO(sc->rl_ldata.rl_stats_addr)); 32960534aae0SPyun YongHyeon CSR_WRITE_4(sc, RL_DUMPSTATS_LO, 32970534aae0SPyun YongHyeon RL_ADDR_LO(sc->rl_ldata.rl_stats_addr | 32980534aae0SPyun YongHyeon RL_DUMPSTATS_START)); 32990534aae0SPyun YongHyeon for (i = RL_TIMEOUT; i > 0; i--) { 33000534aae0SPyun YongHyeon if ((CSR_READ_4(sc, RL_DUMPSTATS_LO) & 33010534aae0SPyun YongHyeon RL_DUMPSTATS_START) == 0) 33020534aae0SPyun YongHyeon break; 33030534aae0SPyun YongHyeon DELAY(1000); 33040534aae0SPyun YongHyeon } 33050534aae0SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_stag, 33060534aae0SPyun YongHyeon sc->rl_ldata.rl_smap, BUS_DMASYNC_POSTREAD); 33070534aae0SPyun YongHyeon RL_UNLOCK(sc); 33080534aae0SPyun YongHyeon if (i == 0) { 33090534aae0SPyun YongHyeon device_printf(sc->rl_dev, 33100534aae0SPyun YongHyeon "DUMP statistics request timedout\n"); 33110534aae0SPyun YongHyeon return (ETIMEDOUT); 33120534aae0SPyun YongHyeon } 331316a4824bSPyun YongHyeon done: 33140534aae0SPyun YongHyeon stats = sc->rl_ldata.rl_stats; 33150534aae0SPyun YongHyeon printf("%s statistics:\n", device_get_nameunit(sc->rl_dev)); 33160534aae0SPyun YongHyeon printf("Tx frames : %ju\n", 33170534aae0SPyun YongHyeon (uintmax_t)le64toh(stats->rl_tx_pkts)); 33180534aae0SPyun YongHyeon printf("Rx frames : %ju\n", 33190534aae0SPyun YongHyeon (uintmax_t)le64toh(stats->rl_rx_pkts)); 33200534aae0SPyun YongHyeon printf("Tx errors : %ju\n", 33210534aae0SPyun YongHyeon (uintmax_t)le64toh(stats->rl_tx_errs)); 33220534aae0SPyun YongHyeon printf("Rx errors : %u\n", 33230534aae0SPyun YongHyeon le32toh(stats->rl_rx_errs)); 33240534aae0SPyun YongHyeon printf("Rx missed frames : %u\n", 33250534aae0SPyun YongHyeon (uint32_t)le16toh(stats->rl_missed_pkts)); 33260534aae0SPyun YongHyeon printf("Rx frame alignment errs : %u\n", 33270534aae0SPyun YongHyeon (uint32_t)le16toh(stats->rl_rx_framealign_errs)); 33280534aae0SPyun YongHyeon printf("Tx single collisions : %u\n", 33290534aae0SPyun YongHyeon le32toh(stats->rl_tx_onecoll)); 33300534aae0SPyun YongHyeon printf("Tx multiple collisions : %u\n", 33310534aae0SPyun YongHyeon le32toh(stats->rl_tx_multicolls)); 33320534aae0SPyun YongHyeon printf("Rx unicast frames : %ju\n", 33330534aae0SPyun YongHyeon (uintmax_t)le64toh(stats->rl_rx_ucasts)); 33340534aae0SPyun YongHyeon printf("Rx broadcast frames : %ju\n", 33350534aae0SPyun YongHyeon (uintmax_t)le64toh(stats->rl_rx_bcasts)); 33360534aae0SPyun YongHyeon printf("Rx multicast frames : %u\n", 33370534aae0SPyun YongHyeon le32toh(stats->rl_rx_mcasts)); 33380534aae0SPyun YongHyeon printf("Tx aborts : %u\n", 33390534aae0SPyun YongHyeon (uint32_t)le16toh(stats->rl_tx_aborts)); 33400534aae0SPyun YongHyeon printf("Tx underruns : %u\n", 33410534aae0SPyun YongHyeon (uint32_t)le16toh(stats->rl_rx_underruns)); 33420534aae0SPyun YongHyeon } 33430534aae0SPyun YongHyeon 33440534aae0SPyun YongHyeon return (error); 33450534aae0SPyun YongHyeon } 3346