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> 126ed510fb0SBill Paul #include <sys/taskqueue.h> 127a94100faSBill Paul 128a94100faSBill Paul #include <net/if.h> 129a94100faSBill Paul #include <net/if_arp.h> 130a94100faSBill Paul #include <net/ethernet.h> 131a94100faSBill Paul #include <net/if_dl.h> 132a94100faSBill Paul #include <net/if_media.h> 133fc74a9f9SBrooks Davis #include <net/if_types.h> 134a94100faSBill Paul #include <net/if_vlan_var.h> 135a94100faSBill Paul 136a94100faSBill Paul #include <net/bpf.h> 137a94100faSBill Paul 138a94100faSBill Paul #include <machine/bus.h> 139a94100faSBill Paul #include <machine/resource.h> 140a94100faSBill Paul #include <sys/bus.h> 141a94100faSBill Paul #include <sys/rman.h> 142a94100faSBill Paul 143a94100faSBill Paul #include <dev/mii/mii.h> 144a94100faSBill Paul #include <dev/mii/miivar.h> 145a94100faSBill Paul 146a94100faSBill Paul #include <dev/pci/pcireg.h> 147a94100faSBill Paul #include <dev/pci/pcivar.h> 148a94100faSBill Paul 149a94100faSBill Paul MODULE_DEPEND(re, pci, 1, 1, 1); 150a94100faSBill Paul MODULE_DEPEND(re, ether, 1, 1, 1); 151a94100faSBill Paul MODULE_DEPEND(re, miibus, 1, 1, 1); 152a94100faSBill Paul 153298bfdf3SWarner Losh /* "device miibus" required. See GENERIC if you get errors here. */ 154a94100faSBill Paul #include "miibus_if.h" 155a94100faSBill Paul 156a94100faSBill Paul /* 157a94100faSBill Paul * Default to using PIO access for this driver. 158a94100faSBill Paul */ 159a94100faSBill Paul #define RE_USEIOSPACE 160a94100faSBill Paul 161a94100faSBill Paul #include <pci/if_rlreg.h> 162a94100faSBill Paul 1635774c5ffSPyun YongHyeon /* Tunables. */ 1645774c5ffSPyun YongHyeon static int msi_disable = 0; 1655774c5ffSPyun YongHyeon TUNABLE_INT("hw.re.msi_disable", &msi_disable); 1665774c5ffSPyun YongHyeon 167a94100faSBill Paul #define RE_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP) 168a94100faSBill Paul 169a94100faSBill Paul /* 170a94100faSBill Paul * Various supported device vendors/types and their names. 171a94100faSBill Paul */ 172a94100faSBill Paul static struct rl_type re_devs[] = { 17332aa5f0eSAnton Berezin { DLINK_VENDORID, DLINK_DEVICEID_528T, RL_HWREV_8169S, 17432aa5f0eSAnton Berezin "D-Link DGE-528(T) Gigabit Ethernet Adapter" }, 1753b9982e5SRemko Lodder { DLINK_VENDORID, DLINK_DEVICEID_528T, RL_HWREV_8169_8110SB, 1763b9982e5SRemko Lodder "D-Link DGE-528(T) Rev.B1 Gigabit Ethernet Adapter" }, 177a94100faSBill Paul { RT_VENDORID, RT_DEVICEID_8139, RL_HWREV_8139CPLUS, 178a94100faSBill Paul "RealTek 8139C+ 10/100BaseTX" }, 179ed510fb0SBill Paul { RT_VENDORID, RT_DEVICEID_8101E, RL_HWREV_8101E, 180ed510fb0SBill Paul "RealTek 8101E PCIe 10/100baseTX" }, 181498bd0d3SBill Paul { RT_VENDORID, RT_DEVICEID_8168, RL_HWREV_8168_SPIN1, 182498bd0d3SBill Paul "RealTek 8168/8111B PCIe Gigabit Ethernet" }, 183498bd0d3SBill Paul { RT_VENDORID, RT_DEVICEID_8168, RL_HWREV_8168_SPIN2, 184498bd0d3SBill Paul "RealTek 8168/8111B PCIe Gigabit Ethernet" }, 185a94100faSBill Paul { RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169, 186a94100faSBill Paul "RealTek 8169 Gigabit Ethernet" }, 18769a6b7fbSBill Paul { RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169S, 18869a6b7fbSBill Paul "RealTek 8169S Single-chip Gigabit Ethernet" }, 189ed510fb0SBill Paul { RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169_8110SB, 190ed510fb0SBill Paul "RealTek 8169SB/8110SB Single-chip Gigabit Ethernet" }, 1912ee2c3b4SRemko Lodder { RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169_8110SC, 1922ee2c3b4SRemko Lodder "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" }, 193498bd0d3SBill Paul { RT_VENDORID, RT_DEVICEID_8169SC, RL_HWREV_8169_8110SC, 194ed510fb0SBill Paul "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" }, 19569a6b7fbSBill Paul { RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8110S, 19669a6b7fbSBill Paul "RealTek 8110S Single-chip Gigabit Ethernet" }, 197ea263191SMIHIRA Sanpei Yoshiro { COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, RL_HWREV_8169S, 198ea263191SMIHIRA Sanpei Yoshiro "Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" }, 19926390635SJohn Baldwin { LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, RL_HWREV_8169S, 20026390635SJohn Baldwin "Linksys EG1032 (RTL8169S) Gigabit Ethernet" }, 2010fc4974fSBill Paul { USR_VENDORID, USR_DEVICEID_997902, RL_HWREV_8169S, 2020fc4974fSBill Paul "US Robotics 997902 (RTL8169S) Gigabit Ethernet" }, 203a94100faSBill Paul { 0, 0, 0, NULL } 204a94100faSBill Paul }; 205a94100faSBill Paul 206a94100faSBill Paul static struct rl_hwrev re_hwrevs[] = { 207a94100faSBill Paul { RL_HWREV_8139, RL_8139, "" }, 208a94100faSBill Paul { RL_HWREV_8139A, RL_8139, "A" }, 209a94100faSBill Paul { RL_HWREV_8139AG, RL_8139, "A-G" }, 210a94100faSBill Paul { RL_HWREV_8139B, RL_8139, "B" }, 211a94100faSBill Paul { RL_HWREV_8130, RL_8139, "8130" }, 212a94100faSBill Paul { RL_HWREV_8139C, RL_8139, "C" }, 213a94100faSBill Paul { RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C" }, 214a94100faSBill Paul { RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+"}, 215498bd0d3SBill Paul { RL_HWREV_8168_SPIN1, RL_8169, "8168"}, 216a94100faSBill Paul { RL_HWREV_8169, RL_8169, "8169"}, 21769a6b7fbSBill Paul { RL_HWREV_8169S, RL_8169, "8169S"}, 21869a6b7fbSBill Paul { RL_HWREV_8110S, RL_8169, "8110S"}, 219ed510fb0SBill Paul { RL_HWREV_8169_8110SB, RL_8169, "8169SB"}, 220ed510fb0SBill Paul { RL_HWREV_8169_8110SC, RL_8169, "8169SC"}, 221a94100faSBill Paul { RL_HWREV_8100, RL_8139, "8100"}, 222a94100faSBill Paul { RL_HWREV_8101, RL_8139, "8101"}, 223ed510fb0SBill Paul { RL_HWREV_8100E, RL_8169, "8100E"}, 224ed510fb0SBill Paul { RL_HWREV_8101E, RL_8169, "8101E"}, 225498bd0d3SBill Paul { RL_HWREV_8168_SPIN2, RL_8169, "8168"}, 226a94100faSBill Paul { 0, 0, NULL } 227a94100faSBill Paul }; 228a94100faSBill Paul 229a94100faSBill Paul static int re_probe (device_t); 230a94100faSBill Paul static int re_attach (device_t); 231a94100faSBill Paul static int re_detach (device_t); 232a94100faSBill Paul 23380a2a305SJohn-Mark Gurney static int re_encap (struct rl_softc *, struct mbuf **, int *); 234a94100faSBill Paul 235a94100faSBill Paul static void re_dma_map_addr (void *, bus_dma_segment_t *, int, int); 236a94100faSBill Paul static void re_dma_map_desc (void *, bus_dma_segment_t *, int, 237a94100faSBill Paul bus_size_t, int); 238a94100faSBill Paul static int re_allocmem (device_t, struct rl_softc *); 239a94100faSBill Paul static int re_newbuf (struct rl_softc *, int, struct mbuf *); 240a94100faSBill Paul static int re_rx_list_init (struct rl_softc *); 241a94100faSBill Paul static int re_tx_list_init (struct rl_softc *); 24222a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX 24322a11c96SJohn-Mark Gurney static __inline void re_fixup_rx 24422a11c96SJohn-Mark Gurney (struct mbuf *); 24522a11c96SJohn-Mark Gurney #endif 246ed510fb0SBill Paul static int re_rxeof (struct rl_softc *); 247a94100faSBill Paul static void re_txeof (struct rl_softc *); 24897b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING 2490187838bSRuslan Ermilov static void re_poll (struct ifnet *, enum poll_cmd, int); 2500187838bSRuslan Ermilov static void re_poll_locked (struct ifnet *, enum poll_cmd, int); 25197b9d4baSJohn-Mark Gurney #endif 252ef544f63SPaolo Pisati static int re_intr (void *); 253a94100faSBill Paul static void re_tick (void *); 254ed510fb0SBill Paul static void re_tx_task (void *, int); 255ed510fb0SBill Paul static void re_int_task (void *, int); 256a94100faSBill Paul static void re_start (struct ifnet *); 257a94100faSBill Paul static int re_ioctl (struct ifnet *, u_long, caddr_t); 258a94100faSBill Paul static void re_init (void *); 25997b9d4baSJohn-Mark Gurney static void re_init_locked (struct rl_softc *); 260a94100faSBill Paul static void re_stop (struct rl_softc *); 2611d545c7aSMarius Strobl static void re_watchdog (struct rl_softc *); 262a94100faSBill Paul static int re_suspend (device_t); 263a94100faSBill Paul static int re_resume (device_t); 2646a087a87SPyun YongHyeon static int re_shutdown (device_t); 265a94100faSBill Paul static int re_ifmedia_upd (struct ifnet *); 266a94100faSBill Paul static void re_ifmedia_sts (struct ifnet *, struct ifmediareq *); 267a94100faSBill Paul 268a94100faSBill Paul static void re_eeprom_putbyte (struct rl_softc *, int); 269a94100faSBill Paul static void re_eeprom_getword (struct rl_softc *, int, u_int16_t *); 270ed510fb0SBill Paul static void re_read_eeprom (struct rl_softc *, caddr_t, int, int); 271a94100faSBill Paul static int re_gmii_readreg (device_t, int, int); 272a94100faSBill Paul static int re_gmii_writereg (device_t, int, int, int); 273a94100faSBill Paul 274a94100faSBill Paul static int re_miibus_readreg (device_t, int, int); 275a94100faSBill Paul static int re_miibus_writereg (device_t, int, int, int); 276a94100faSBill Paul static void re_miibus_statchg (device_t); 277a94100faSBill Paul 278a94100faSBill Paul static void re_setmulti (struct rl_softc *); 279a94100faSBill Paul static void re_reset (struct rl_softc *); 280a94100faSBill Paul 281ed510fb0SBill Paul #ifdef RE_DIAG 282a94100faSBill Paul static int re_diag (struct rl_softc *); 283ed510fb0SBill Paul #endif 284a94100faSBill Paul 285a94100faSBill Paul #ifdef RE_USEIOSPACE 286a94100faSBill Paul #define RL_RES SYS_RES_IOPORT 287a94100faSBill Paul #define RL_RID RL_PCI_LOIO 288a94100faSBill Paul #else 289a94100faSBill Paul #define RL_RES SYS_RES_MEMORY 290a94100faSBill Paul #define RL_RID RL_PCI_LOMEM 291a94100faSBill Paul #endif 292a94100faSBill Paul 293a94100faSBill Paul static device_method_t re_methods[] = { 294a94100faSBill Paul /* Device interface */ 295a94100faSBill Paul DEVMETHOD(device_probe, re_probe), 296a94100faSBill Paul DEVMETHOD(device_attach, re_attach), 297a94100faSBill Paul DEVMETHOD(device_detach, re_detach), 298a94100faSBill Paul DEVMETHOD(device_suspend, re_suspend), 299a94100faSBill Paul DEVMETHOD(device_resume, re_resume), 300a94100faSBill Paul DEVMETHOD(device_shutdown, re_shutdown), 301a94100faSBill Paul 302a94100faSBill Paul /* bus interface */ 303a94100faSBill Paul DEVMETHOD(bus_print_child, bus_generic_print_child), 304a94100faSBill Paul DEVMETHOD(bus_driver_added, bus_generic_driver_added), 305a94100faSBill Paul 306a94100faSBill Paul /* MII interface */ 307a94100faSBill Paul DEVMETHOD(miibus_readreg, re_miibus_readreg), 308a94100faSBill Paul DEVMETHOD(miibus_writereg, re_miibus_writereg), 309a94100faSBill Paul DEVMETHOD(miibus_statchg, re_miibus_statchg), 310a94100faSBill Paul 311a94100faSBill Paul { 0, 0 } 312a94100faSBill Paul }; 313a94100faSBill Paul 314a94100faSBill Paul static driver_t re_driver = { 315a94100faSBill Paul "re", 316a94100faSBill Paul re_methods, 317a94100faSBill Paul sizeof(struct rl_softc) 318a94100faSBill Paul }; 319a94100faSBill Paul 320a94100faSBill Paul static devclass_t re_devclass; 321a94100faSBill Paul 322a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0); 323347934faSWarner Losh DRIVER_MODULE(re, cardbus, re_driver, re_devclass, 0, 0); 324a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0); 325a94100faSBill Paul 326a94100faSBill Paul #define EE_SET(x) \ 327a94100faSBill Paul CSR_WRITE_1(sc, RL_EECMD, \ 328a94100faSBill Paul CSR_READ_1(sc, RL_EECMD) | x) 329a94100faSBill Paul 330a94100faSBill Paul #define EE_CLR(x) \ 331a94100faSBill Paul CSR_WRITE_1(sc, RL_EECMD, \ 332a94100faSBill Paul CSR_READ_1(sc, RL_EECMD) & ~x) 333a94100faSBill Paul 334a94100faSBill Paul /* 335a94100faSBill Paul * Send a read command and address to the EEPROM, check for ACK. 336a94100faSBill Paul */ 337a94100faSBill Paul static void 338a94100faSBill Paul re_eeprom_putbyte(sc, addr) 339a94100faSBill Paul struct rl_softc *sc; 340a94100faSBill Paul int addr; 341a94100faSBill Paul { 342a94100faSBill Paul register int d, i; 343a94100faSBill Paul 344ed510fb0SBill Paul d = addr | (RL_9346_READ << sc->rl_eewidth); 345a94100faSBill Paul 346a94100faSBill Paul /* 347a94100faSBill Paul * Feed in each bit and strobe the clock. 348a94100faSBill Paul */ 349ed510fb0SBill Paul 350ed510fb0SBill Paul for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) { 351a94100faSBill Paul if (d & i) { 352a94100faSBill Paul EE_SET(RL_EE_DATAIN); 353a94100faSBill Paul } else { 354a94100faSBill Paul EE_CLR(RL_EE_DATAIN); 355a94100faSBill Paul } 356a94100faSBill Paul DELAY(100); 357a94100faSBill Paul EE_SET(RL_EE_CLK); 358a94100faSBill Paul DELAY(150); 359a94100faSBill Paul EE_CLR(RL_EE_CLK); 360a94100faSBill Paul DELAY(100); 361a94100faSBill Paul } 362ed510fb0SBill Paul 363ed510fb0SBill Paul return; 364a94100faSBill Paul } 365a94100faSBill Paul 366a94100faSBill Paul /* 367a94100faSBill Paul * Read a word of data stored in the EEPROM at address 'addr.' 368a94100faSBill Paul */ 369a94100faSBill Paul static void 370a94100faSBill Paul re_eeprom_getword(sc, addr, dest) 371a94100faSBill Paul struct rl_softc *sc; 372a94100faSBill Paul int addr; 373a94100faSBill Paul u_int16_t *dest; 374a94100faSBill Paul { 375a94100faSBill Paul register int i; 376a94100faSBill Paul u_int16_t word = 0; 377a94100faSBill Paul 378a94100faSBill Paul /* 379a94100faSBill Paul * Send address of word we want to read. 380a94100faSBill Paul */ 381a94100faSBill Paul re_eeprom_putbyte(sc, addr); 382a94100faSBill Paul 383a94100faSBill Paul /* 384a94100faSBill Paul * Start reading bits from EEPROM. 385a94100faSBill Paul */ 386a94100faSBill Paul for (i = 0x8000; i; i >>= 1) { 387a94100faSBill Paul EE_SET(RL_EE_CLK); 388a94100faSBill Paul DELAY(100); 389a94100faSBill Paul if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT) 390a94100faSBill Paul word |= i; 391a94100faSBill Paul EE_CLR(RL_EE_CLK); 392a94100faSBill Paul DELAY(100); 393a94100faSBill Paul } 394a94100faSBill Paul 395a94100faSBill Paul *dest = word; 396ed510fb0SBill Paul 397ed510fb0SBill Paul return; 398a94100faSBill Paul } 399a94100faSBill Paul 400a94100faSBill Paul /* 401a94100faSBill Paul * Read a sequence of words from the EEPROM. 402a94100faSBill Paul */ 403a94100faSBill Paul static void 404ed510fb0SBill Paul re_read_eeprom(sc, dest, off, cnt) 405a94100faSBill Paul struct rl_softc *sc; 406a94100faSBill Paul caddr_t dest; 407a94100faSBill Paul int off; 408a94100faSBill Paul int cnt; 409a94100faSBill Paul { 410a94100faSBill Paul int i; 411a94100faSBill Paul u_int16_t word = 0, *ptr; 412a94100faSBill Paul 413ed510fb0SBill Paul CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM); 414ed510fb0SBill Paul 415ed510fb0SBill Paul DELAY(100); 416ed510fb0SBill Paul 417a94100faSBill Paul for (i = 0; i < cnt; i++) { 418ed510fb0SBill Paul CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL); 419a94100faSBill Paul re_eeprom_getword(sc, off + i, &word); 420ed510fb0SBill Paul CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL); 421a94100faSBill Paul ptr = (u_int16_t *)(dest + (i * 2)); 422be099007SPyun YongHyeon *ptr = word; 423a94100faSBill Paul } 424ed510fb0SBill Paul 425ed510fb0SBill Paul CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM); 426ed510fb0SBill Paul 427ed510fb0SBill Paul return; 428a94100faSBill Paul } 429a94100faSBill Paul 430a94100faSBill Paul static int 431a94100faSBill Paul re_gmii_readreg(dev, phy, reg) 432a94100faSBill Paul device_t dev; 433a94100faSBill Paul int phy, reg; 434a94100faSBill Paul { 435a94100faSBill Paul struct rl_softc *sc; 436a94100faSBill Paul u_int32_t rval; 437a94100faSBill Paul int i; 438a94100faSBill Paul 439a94100faSBill Paul if (phy != 1) 440a94100faSBill Paul return (0); 441a94100faSBill Paul 442a94100faSBill Paul sc = device_get_softc(dev); 443a94100faSBill Paul 4449bac70b8SBill Paul /* Let the rgephy driver read the GMEDIASTAT register */ 4459bac70b8SBill Paul 4469bac70b8SBill Paul if (reg == RL_GMEDIASTAT) { 4479bac70b8SBill Paul rval = CSR_READ_1(sc, RL_GMEDIASTAT); 4489bac70b8SBill Paul return (rval); 4499bac70b8SBill Paul } 4509bac70b8SBill Paul 451a94100faSBill Paul CSR_WRITE_4(sc, RL_PHYAR, reg << 16); 452a94100faSBill Paul DELAY(1000); 453a94100faSBill Paul 454a94100faSBill Paul for (i = 0; i < RL_TIMEOUT; i++) { 455a94100faSBill Paul rval = CSR_READ_4(sc, RL_PHYAR); 456a94100faSBill Paul if (rval & RL_PHYAR_BUSY) 457a94100faSBill Paul break; 458a94100faSBill Paul DELAY(100); 459a94100faSBill Paul } 460a94100faSBill Paul 461a94100faSBill Paul if (i == RL_TIMEOUT) { 4626b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "PHY read failed\n"); 463a94100faSBill Paul return (0); 464a94100faSBill Paul } 465a94100faSBill Paul 466a94100faSBill Paul return (rval & RL_PHYAR_PHYDATA); 467a94100faSBill Paul } 468a94100faSBill Paul 469a94100faSBill Paul static int 470a94100faSBill Paul re_gmii_writereg(dev, phy, reg, data) 471a94100faSBill Paul device_t dev; 472a94100faSBill Paul int phy, reg, data; 473a94100faSBill Paul { 474a94100faSBill Paul struct rl_softc *sc; 475a94100faSBill Paul u_int32_t rval; 476a94100faSBill Paul int i; 477a94100faSBill Paul 478a94100faSBill Paul sc = device_get_softc(dev); 479a94100faSBill Paul 480a94100faSBill Paul CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) | 4819bac70b8SBill Paul (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY); 482a94100faSBill Paul DELAY(1000); 483a94100faSBill Paul 484a94100faSBill Paul for (i = 0; i < RL_TIMEOUT; i++) { 485a94100faSBill Paul rval = CSR_READ_4(sc, RL_PHYAR); 486a94100faSBill Paul if (!(rval & RL_PHYAR_BUSY)) 487a94100faSBill Paul break; 488a94100faSBill Paul DELAY(100); 489a94100faSBill Paul } 490a94100faSBill Paul 491a94100faSBill Paul if (i == RL_TIMEOUT) { 4926b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "PHY write failed\n"); 493a94100faSBill Paul return (0); 494a94100faSBill Paul } 495a94100faSBill Paul 496a94100faSBill Paul return (0); 497a94100faSBill Paul } 498a94100faSBill Paul 499a94100faSBill Paul static int 500a94100faSBill Paul re_miibus_readreg(dev, phy, reg) 501a94100faSBill Paul device_t dev; 502a94100faSBill Paul int phy, reg; 503a94100faSBill Paul { 504a94100faSBill Paul struct rl_softc *sc; 505a94100faSBill Paul u_int16_t rval = 0; 506a94100faSBill Paul u_int16_t re8139_reg = 0; 507a94100faSBill Paul 508a94100faSBill Paul sc = device_get_softc(dev); 509a94100faSBill Paul 510a94100faSBill Paul if (sc->rl_type == RL_8169) { 511a94100faSBill Paul rval = re_gmii_readreg(dev, phy, reg); 512a94100faSBill Paul return (rval); 513a94100faSBill Paul } 514a94100faSBill Paul 515a94100faSBill Paul /* Pretend the internal PHY is only at address 0 */ 516a94100faSBill Paul if (phy) { 517a94100faSBill Paul return (0); 518a94100faSBill Paul } 519a94100faSBill Paul switch (reg) { 520a94100faSBill Paul case MII_BMCR: 521a94100faSBill Paul re8139_reg = RL_BMCR; 522a94100faSBill Paul break; 523a94100faSBill Paul case MII_BMSR: 524a94100faSBill Paul re8139_reg = RL_BMSR; 525a94100faSBill Paul break; 526a94100faSBill Paul case MII_ANAR: 527a94100faSBill Paul re8139_reg = RL_ANAR; 528a94100faSBill Paul break; 529a94100faSBill Paul case MII_ANER: 530a94100faSBill Paul re8139_reg = RL_ANER; 531a94100faSBill Paul break; 532a94100faSBill Paul case MII_ANLPAR: 533a94100faSBill Paul re8139_reg = RL_LPAR; 534a94100faSBill Paul break; 535a94100faSBill Paul case MII_PHYIDR1: 536a94100faSBill Paul case MII_PHYIDR2: 537a94100faSBill Paul return (0); 538a94100faSBill Paul /* 539a94100faSBill Paul * Allow the rlphy driver to read the media status 540a94100faSBill Paul * register. If we have a link partner which does not 541a94100faSBill Paul * support NWAY, this is the register which will tell 542a94100faSBill Paul * us the results of parallel detection. 543a94100faSBill Paul */ 544a94100faSBill Paul case RL_MEDIASTAT: 545a94100faSBill Paul rval = CSR_READ_1(sc, RL_MEDIASTAT); 546a94100faSBill Paul return (rval); 547a94100faSBill Paul default: 5486b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "bad phy register\n"); 549a94100faSBill Paul return (0); 550a94100faSBill Paul } 551a94100faSBill Paul rval = CSR_READ_2(sc, re8139_reg); 552baa12772SPyun YongHyeon if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) { 553baa12772SPyun YongHyeon /* 8139C+ has different bit layout. */ 554baa12772SPyun YongHyeon rval &= ~(BMCR_LOOP | BMCR_ISO); 555baa12772SPyun YongHyeon } 556a94100faSBill Paul return (rval); 557a94100faSBill Paul } 558a94100faSBill Paul 559a94100faSBill Paul static int 560a94100faSBill Paul re_miibus_writereg(dev, phy, reg, data) 561a94100faSBill Paul device_t dev; 562a94100faSBill Paul int phy, reg, data; 563a94100faSBill Paul { 564a94100faSBill Paul struct rl_softc *sc; 565a94100faSBill Paul u_int16_t re8139_reg = 0; 566a94100faSBill Paul int rval = 0; 567a94100faSBill Paul 568a94100faSBill Paul sc = device_get_softc(dev); 569a94100faSBill Paul 570a94100faSBill Paul if (sc->rl_type == RL_8169) { 571a94100faSBill Paul rval = re_gmii_writereg(dev, phy, reg, data); 572a94100faSBill Paul return (rval); 573a94100faSBill Paul } 574a94100faSBill Paul 575a94100faSBill Paul /* Pretend the internal PHY is only at address 0 */ 57697b9d4baSJohn-Mark Gurney if (phy) 577a94100faSBill Paul return (0); 57897b9d4baSJohn-Mark Gurney 579a94100faSBill Paul switch (reg) { 580a94100faSBill Paul case MII_BMCR: 581a94100faSBill Paul re8139_reg = RL_BMCR; 582baa12772SPyun YongHyeon if (sc->rl_type == RL_8139CPLUS) { 583baa12772SPyun YongHyeon /* 8139C+ has different bit layout. */ 584baa12772SPyun YongHyeon data &= ~(BMCR_LOOP | BMCR_ISO); 585baa12772SPyun YongHyeon } 586a94100faSBill Paul break; 587a94100faSBill Paul case MII_BMSR: 588a94100faSBill Paul re8139_reg = RL_BMSR; 589a94100faSBill Paul break; 590a94100faSBill Paul case MII_ANAR: 591a94100faSBill Paul re8139_reg = RL_ANAR; 592a94100faSBill Paul break; 593a94100faSBill Paul case MII_ANER: 594a94100faSBill Paul re8139_reg = RL_ANER; 595a94100faSBill Paul break; 596a94100faSBill Paul case MII_ANLPAR: 597a94100faSBill Paul re8139_reg = RL_LPAR; 598a94100faSBill Paul break; 599a94100faSBill Paul case MII_PHYIDR1: 600a94100faSBill Paul case MII_PHYIDR2: 601a94100faSBill Paul return (0); 602a94100faSBill Paul break; 603a94100faSBill Paul default: 6046b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "bad phy register\n"); 605a94100faSBill Paul return (0); 606a94100faSBill Paul } 607a94100faSBill Paul CSR_WRITE_2(sc, re8139_reg, data); 608a94100faSBill Paul return (0); 609a94100faSBill Paul } 610a94100faSBill Paul 611a94100faSBill Paul static void 612a94100faSBill Paul re_miibus_statchg(dev) 613a94100faSBill Paul device_t dev; 614a94100faSBill Paul { 615a11e2f18SBruce M Simpson 616a94100faSBill Paul } 617a94100faSBill Paul 618a94100faSBill Paul /* 619a94100faSBill Paul * Program the 64-bit multicast hash filter. 620a94100faSBill Paul */ 621a94100faSBill Paul static void 622a94100faSBill Paul re_setmulti(sc) 623a94100faSBill Paul struct rl_softc *sc; 624a94100faSBill Paul { 625a94100faSBill Paul struct ifnet *ifp; 626a94100faSBill Paul int h = 0; 627a94100faSBill Paul u_int32_t hashes[2] = { 0, 0 }; 628a94100faSBill Paul struct ifmultiaddr *ifma; 629a94100faSBill Paul u_int32_t rxfilt; 630a94100faSBill Paul int mcnt = 0; 631bb7dfefbSBill Paul u_int32_t hwrev; 632a94100faSBill Paul 63397b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 63497b9d4baSJohn-Mark Gurney 635fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 636a94100faSBill Paul 637a94100faSBill Paul 6387c103000SPyun YongHyeon rxfilt = CSR_READ_4(sc, RL_RXCFG); 6397c103000SPyun YongHyeon rxfilt &= ~(RL_RXCFG_RX_ALLPHYS | RL_RXCFG_RX_MULTI); 640a94100faSBill Paul if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 6417c103000SPyun YongHyeon if (ifp->if_flags & IFF_PROMISC) 6427c103000SPyun YongHyeon rxfilt |= RL_RXCFG_RX_ALLPHYS; 6437c103000SPyun YongHyeon if (ifp->if_flags & IFF_ALLMULTI) 644a94100faSBill Paul rxfilt |= RL_RXCFG_RX_MULTI; 645a94100faSBill Paul CSR_WRITE_4(sc, RL_RXCFG, rxfilt); 646a94100faSBill Paul CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF); 647a94100faSBill Paul CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF); 648a94100faSBill Paul return; 649a94100faSBill Paul } 650a94100faSBill Paul 651a94100faSBill Paul /* first, zot all the existing hash bits */ 652a94100faSBill Paul CSR_WRITE_4(sc, RL_MAR0, 0); 653a94100faSBill Paul CSR_WRITE_4(sc, RL_MAR4, 0); 654a94100faSBill Paul 655a94100faSBill Paul /* now program new ones */ 65613b203d0SRobert Watson IF_ADDR_LOCK(ifp); 657a94100faSBill Paul TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 658a94100faSBill Paul if (ifma->ifma_addr->sa_family != AF_LINK) 659a94100faSBill Paul continue; 6600e939c0cSChristian Weisgerber h = ether_crc32_be(LLADDR((struct sockaddr_dl *) 6610e939c0cSChristian Weisgerber ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 662a94100faSBill Paul if (h < 32) 663a94100faSBill Paul hashes[0] |= (1 << h); 664a94100faSBill Paul else 665a94100faSBill Paul hashes[1] |= (1 << (h - 32)); 666a94100faSBill Paul mcnt++; 667a94100faSBill Paul } 66813b203d0SRobert Watson IF_ADDR_UNLOCK(ifp); 669a94100faSBill Paul 670a94100faSBill Paul if (mcnt) 671a94100faSBill Paul rxfilt |= RL_RXCFG_RX_MULTI; 672a94100faSBill Paul else 673a94100faSBill Paul rxfilt &= ~RL_RXCFG_RX_MULTI; 674a94100faSBill Paul 675a94100faSBill Paul CSR_WRITE_4(sc, RL_RXCFG, rxfilt); 676bb7dfefbSBill Paul 677bb7dfefbSBill Paul /* 678bb7dfefbSBill Paul * For some unfathomable reason, RealTek decided to reverse 679bb7dfefbSBill Paul * the order of the multicast hash registers in the PCI Express 680bb7dfefbSBill Paul * parts. This means we have to write the hash pattern in reverse 681bb7dfefbSBill Paul * order for those devices. 682bb7dfefbSBill Paul */ 683bb7dfefbSBill Paul 684bb7dfefbSBill Paul hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV; 685bb7dfefbSBill Paul 686bb7dfefbSBill Paul if (hwrev == RL_HWREV_8100E || hwrev == RL_HWREV_8101E || 687bb7dfefbSBill Paul hwrev == RL_HWREV_8168_SPIN1 || hwrev == RL_HWREV_8168_SPIN2) { 688bb7dfefbSBill Paul CSR_WRITE_4(sc, RL_MAR0, bswap32(hashes[1])); 689bb7dfefbSBill Paul CSR_WRITE_4(sc, RL_MAR4, bswap32(hashes[0])); 690bb7dfefbSBill Paul } else { 691a94100faSBill Paul CSR_WRITE_4(sc, RL_MAR0, hashes[0]); 692a94100faSBill Paul CSR_WRITE_4(sc, RL_MAR4, hashes[1]); 693a94100faSBill Paul } 694bb7dfefbSBill Paul } 695a94100faSBill Paul 696a94100faSBill Paul static void 697a94100faSBill Paul re_reset(sc) 698a94100faSBill Paul struct rl_softc *sc; 699a94100faSBill Paul { 700a94100faSBill Paul register int i; 701a94100faSBill Paul 70297b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 70397b9d4baSJohn-Mark Gurney 704a94100faSBill Paul CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET); 705a94100faSBill Paul 706a94100faSBill Paul for (i = 0; i < RL_TIMEOUT; i++) { 707a94100faSBill Paul DELAY(10); 708a94100faSBill Paul if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET)) 709a94100faSBill Paul break; 710a94100faSBill Paul } 711a94100faSBill Paul if (i == RL_TIMEOUT) 7126b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "reset never completed!\n"); 713a94100faSBill Paul 714a94100faSBill Paul CSR_WRITE_1(sc, 0x82, 1); 715a94100faSBill Paul } 716a94100faSBill Paul 717ed510fb0SBill Paul #ifdef RE_DIAG 718ed510fb0SBill Paul 719a94100faSBill Paul /* 720a94100faSBill Paul * The following routine is designed to test for a defect on some 721a94100faSBill Paul * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64# 722a94100faSBill Paul * lines connected to the bus, however for a 32-bit only card, they 723a94100faSBill Paul * should be pulled high. The result of this defect is that the 724a94100faSBill Paul * NIC will not work right if you plug it into a 64-bit slot: DMA 725a94100faSBill Paul * operations will be done with 64-bit transfers, which will fail 726a94100faSBill Paul * because the 64-bit data lines aren't connected. 727a94100faSBill Paul * 728a94100faSBill Paul * There's no way to work around this (short of talking a soldering 729a94100faSBill Paul * iron to the board), however we can detect it. The method we use 730a94100faSBill Paul * here is to put the NIC into digital loopback mode, set the receiver 731a94100faSBill Paul * to promiscuous mode, and then try to send a frame. We then compare 732a94100faSBill Paul * the frame data we sent to what was received. If the data matches, 733a94100faSBill Paul * then the NIC is working correctly, otherwise we know the user has 734a94100faSBill Paul * a defective NIC which has been mistakenly plugged into a 64-bit PCI 735a94100faSBill Paul * slot. In the latter case, there's no way the NIC can work correctly, 736a94100faSBill Paul * so we print out a message on the console and abort the device attach. 737a94100faSBill Paul */ 738a94100faSBill Paul 739a94100faSBill Paul static int 740a94100faSBill Paul re_diag(sc) 741a94100faSBill Paul struct rl_softc *sc; 742a94100faSBill Paul { 743fc74a9f9SBrooks Davis struct ifnet *ifp = sc->rl_ifp; 744a94100faSBill Paul struct mbuf *m0; 745a94100faSBill Paul struct ether_header *eh; 746a94100faSBill Paul struct rl_desc *cur_rx; 747a94100faSBill Paul u_int16_t status; 748a94100faSBill Paul u_int32_t rxstat; 749ed510fb0SBill Paul int total_len, i, error = 0, phyaddr; 750a94100faSBill Paul u_int8_t dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' }; 751a94100faSBill Paul u_int8_t src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' }; 752a94100faSBill Paul 753a94100faSBill Paul /* Allocate a single mbuf */ 754a94100faSBill Paul MGETHDR(m0, M_DONTWAIT, MT_DATA); 755a94100faSBill Paul if (m0 == NULL) 756a94100faSBill Paul return (ENOBUFS); 757a94100faSBill Paul 75897b9d4baSJohn-Mark Gurney RL_LOCK(sc); 75997b9d4baSJohn-Mark Gurney 760a94100faSBill Paul /* 761a94100faSBill Paul * Initialize the NIC in test mode. This sets the chip up 762a94100faSBill Paul * so that it can send and receive frames, but performs the 763a94100faSBill Paul * following special functions: 764a94100faSBill Paul * - Puts receiver in promiscuous mode 765a94100faSBill Paul * - Enables digital loopback mode 766a94100faSBill Paul * - Leaves interrupts turned off 767a94100faSBill Paul */ 768a94100faSBill Paul 769a94100faSBill Paul ifp->if_flags |= IFF_PROMISC; 770a94100faSBill Paul sc->rl_testmode = 1; 771ed510fb0SBill Paul re_reset(sc); 77297b9d4baSJohn-Mark Gurney re_init_locked(sc); 773ed510fb0SBill Paul sc->rl_link = 1; 774ed510fb0SBill Paul if (sc->rl_type == RL_8169) 775ed510fb0SBill Paul phyaddr = 1; 776ed510fb0SBill Paul else 777ed510fb0SBill Paul phyaddr = 0; 778ed510fb0SBill Paul 779ed510fb0SBill Paul re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET); 780ed510fb0SBill Paul for (i = 0; i < RL_TIMEOUT; i++) { 781ed510fb0SBill Paul status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR); 782ed510fb0SBill Paul if (!(status & BMCR_RESET)) 783ed510fb0SBill Paul break; 784ed510fb0SBill Paul } 785ed510fb0SBill Paul 786ed510fb0SBill Paul re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP); 787ed510fb0SBill Paul CSR_WRITE_2(sc, RL_ISR, RL_INTRS); 788ed510fb0SBill Paul 789804af9a1SBill Paul DELAY(100000); 790a94100faSBill Paul 791a94100faSBill Paul /* Put some data in the mbuf */ 792a94100faSBill Paul 793a94100faSBill Paul eh = mtod(m0, struct ether_header *); 794a94100faSBill Paul bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN); 795a94100faSBill Paul bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN); 796a94100faSBill Paul eh->ether_type = htons(ETHERTYPE_IP); 797a94100faSBill Paul m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN; 798a94100faSBill Paul 7997cae6651SBill Paul /* 8007cae6651SBill Paul * Queue the packet, start transmission. 8017cae6651SBill Paul * Note: IF_HANDOFF() ultimately calls re_start() for us. 8027cae6651SBill Paul */ 803a94100faSBill Paul 804abc8ff44SBill Paul CSR_WRITE_2(sc, RL_ISR, 0xFFFF); 80597b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 80652732175SMax Laier /* XXX: re_diag must not be called when in ALTQ mode */ 8077cae6651SBill Paul IF_HANDOFF(&ifp->if_snd, m0, ifp); 80897b9d4baSJohn-Mark Gurney RL_LOCK(sc); 809a94100faSBill Paul m0 = NULL; 810a94100faSBill Paul 811a94100faSBill Paul /* Wait for it to propagate through the chip */ 812a94100faSBill Paul 813abc8ff44SBill Paul DELAY(100000); 814a94100faSBill Paul for (i = 0; i < RL_TIMEOUT; i++) { 815a94100faSBill Paul status = CSR_READ_2(sc, RL_ISR); 816ed510fb0SBill Paul CSR_WRITE_2(sc, RL_ISR, status); 817abc8ff44SBill Paul if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) == 818abc8ff44SBill Paul (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) 819a94100faSBill Paul break; 820a94100faSBill Paul DELAY(10); 821a94100faSBill Paul } 822a94100faSBill Paul 823a94100faSBill Paul if (i == RL_TIMEOUT) { 8246b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, 8256b9f5c94SGleb Smirnoff "diagnostic failed, failed to receive packet in" 8266b9f5c94SGleb Smirnoff " loopback mode\n"); 827a94100faSBill Paul error = EIO; 828a94100faSBill Paul goto done; 829a94100faSBill Paul } 830a94100faSBill Paul 831a94100faSBill Paul /* 832a94100faSBill Paul * The packet should have been dumped into the first 833a94100faSBill Paul * entry in the RX DMA ring. Grab it from there. 834a94100faSBill Paul */ 835a94100faSBill Paul 836a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 837a94100faSBill Paul sc->rl_ldata.rl_rx_list_map, 838a94100faSBill Paul BUS_DMASYNC_POSTREAD); 839a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_mtag, 840a94100faSBill Paul sc->rl_ldata.rl_rx_dmamap[0], 841a94100faSBill Paul BUS_DMASYNC_POSTWRITE); 842a94100faSBill Paul bus_dmamap_unload(sc->rl_ldata.rl_mtag, 843a94100faSBill Paul sc->rl_ldata.rl_rx_dmamap[0]); 844a94100faSBill Paul 845a94100faSBill Paul m0 = sc->rl_ldata.rl_rx_mbuf[0]; 846a94100faSBill Paul sc->rl_ldata.rl_rx_mbuf[0] = NULL; 847a94100faSBill Paul eh = mtod(m0, struct ether_header *); 848a94100faSBill Paul 849a94100faSBill Paul cur_rx = &sc->rl_ldata.rl_rx_list[0]; 850a94100faSBill Paul total_len = RL_RXBYTES(cur_rx); 851a94100faSBill Paul rxstat = le32toh(cur_rx->rl_cmdstat); 852a94100faSBill Paul 853a94100faSBill Paul if (total_len != ETHER_MIN_LEN) { 8546b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, 8556b9f5c94SGleb Smirnoff "diagnostic failed, received short packet\n"); 856a94100faSBill Paul error = EIO; 857a94100faSBill Paul goto done; 858a94100faSBill Paul } 859a94100faSBill Paul 860a94100faSBill Paul /* Test that the received packet data matches what we sent. */ 861a94100faSBill Paul 862a94100faSBill Paul if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) || 863a94100faSBill Paul bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) || 864a94100faSBill Paul ntohs(eh->ether_type) != ETHERTYPE_IP) { 8656b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n"); 8666b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n", 867a94100faSBill Paul dst, ":", src, ":", ETHERTYPE_IP); 8686b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n", 869a94100faSBill Paul eh->ether_dhost, ":", eh->ether_shost, ":", 870a94100faSBill Paul ntohs(eh->ether_type)); 8716b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "You may have a defective 32-bit " 8726b9f5c94SGleb Smirnoff "NIC plugged into a 64-bit PCI slot.\n"); 8736b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "Please re-install the NIC in a " 8746b9f5c94SGleb Smirnoff "32-bit slot for proper operation.\n"); 8756b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "Read the re(4) man page for more " 8766b9f5c94SGleb Smirnoff "details.\n"); 877a94100faSBill Paul error = EIO; 878a94100faSBill Paul } 879a94100faSBill Paul 880a94100faSBill Paul done: 881a94100faSBill Paul /* Turn interface off, release resources */ 882a94100faSBill Paul 883a94100faSBill Paul sc->rl_testmode = 0; 884ed510fb0SBill Paul sc->rl_link = 0; 885a94100faSBill Paul ifp->if_flags &= ~IFF_PROMISC; 886a94100faSBill Paul re_stop(sc); 887a94100faSBill Paul if (m0 != NULL) 888a94100faSBill Paul m_freem(m0); 889a94100faSBill Paul 89097b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 89197b9d4baSJohn-Mark Gurney 892a94100faSBill Paul return (error); 893a94100faSBill Paul } 894a94100faSBill Paul 895ed510fb0SBill Paul #endif 896ed510fb0SBill Paul 897a94100faSBill Paul /* 898a94100faSBill Paul * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device 899a94100faSBill Paul * IDs against our list and return a device name if we find a match. 900a94100faSBill Paul */ 901a94100faSBill Paul static int 902a94100faSBill Paul re_probe(dev) 903a94100faSBill Paul device_t dev; 904a94100faSBill Paul { 905a94100faSBill Paul struct rl_type *t; 906a94100faSBill Paul struct rl_softc *sc; 907a94100faSBill Paul int rid; 908a94100faSBill Paul u_int32_t hwrev; 909a94100faSBill Paul 910a94100faSBill Paul t = re_devs; 911a94100faSBill Paul sc = device_get_softc(dev); 912a94100faSBill Paul 913a94100faSBill Paul while (t->rl_name != NULL) { 914a94100faSBill Paul if ((pci_get_vendor(dev) == t->rl_vid) && 915a94100faSBill Paul (pci_get_device(dev) == t->rl_did)) { 91626390635SJohn Baldwin /* 91726390635SJohn Baldwin * Only attach to rev. 3 of the Linksys EG1032 adapter. 91826390635SJohn Baldwin * Rev. 2 i supported by sk(4). 91926390635SJohn Baldwin */ 92026390635SJohn Baldwin if ((t->rl_vid == LINKSYS_VENDORID) && 92126390635SJohn Baldwin (t->rl_did == LINKSYS_DEVICEID_EG1032) && 92226390635SJohn Baldwin (pci_get_subdevice(dev) != 92326390635SJohn Baldwin LINKSYS_SUBDEVICE_EG1032_REV3)) { 92426390635SJohn Baldwin t++; 92526390635SJohn Baldwin continue; 92626390635SJohn Baldwin } 927a94100faSBill Paul 928a94100faSBill Paul /* 929a94100faSBill Paul * Temporarily map the I/O space 930a94100faSBill Paul * so we can read the chip ID register. 931a94100faSBill Paul */ 932a94100faSBill Paul rid = RL_RID; 9335f96beb9SNate Lawson sc->rl_res = bus_alloc_resource_any(dev, RL_RES, &rid, 9345f96beb9SNate Lawson RF_ACTIVE); 935a94100faSBill Paul if (sc->rl_res == NULL) { 936a94100faSBill Paul device_printf(dev, 937a94100faSBill Paul "couldn't map ports/memory\n"); 938a94100faSBill Paul return (ENXIO); 939a94100faSBill Paul } 940a94100faSBill Paul sc->rl_btag = rman_get_bustag(sc->rl_res); 941a94100faSBill Paul sc->rl_bhandle = rman_get_bushandle(sc->rl_res); 942a94100faSBill Paul hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV; 943a94100faSBill Paul bus_release_resource(dev, RL_RES, 944a94100faSBill Paul RL_RID, sc->rl_res); 945a94100faSBill Paul if (t->rl_basetype == hwrev) { 946a94100faSBill Paul device_set_desc(dev, t->rl_name); 947d2b677bbSWarner Losh return (BUS_PROBE_DEFAULT); 948a94100faSBill Paul } 949a94100faSBill Paul } 950a94100faSBill Paul t++; 951a94100faSBill Paul } 952a94100faSBill Paul 953a94100faSBill Paul return (ENXIO); 954a94100faSBill Paul } 955a94100faSBill Paul 956a94100faSBill Paul /* 957a94100faSBill Paul * This routine takes the segment list provided as the result of 958a94100faSBill Paul * a bus_dma_map_load() operation and assigns the addresses/lengths 959a94100faSBill Paul * to RealTek DMA descriptors. This can be called either by the RX 960a94100faSBill Paul * code or the TX code. In the RX case, we'll probably wind up mapping 961a94100faSBill Paul * at most one segment. For the TX case, there could be any number of 962a94100faSBill Paul * segments since TX packets may span multiple mbufs. In either case, 963a94100faSBill Paul * if the number of segments is larger than the rl_maxsegs limit 964a94100faSBill Paul * specified by the caller, we abort the mapping operation. Sadly, 965a94100faSBill Paul * whoever designed the buffer mapping API did not provide a way to 966a94100faSBill Paul * return an error from here, so we have to fake it a bit. 967a94100faSBill Paul */ 968a94100faSBill Paul 969a94100faSBill Paul static void 970a94100faSBill Paul re_dma_map_desc(arg, segs, nseg, mapsize, error) 971a94100faSBill Paul void *arg; 972a94100faSBill Paul bus_dma_segment_t *segs; 973a94100faSBill Paul int nseg; 974a94100faSBill Paul bus_size_t mapsize; 975a94100faSBill Paul int error; 976a94100faSBill Paul { 977a94100faSBill Paul struct rl_dmaload_arg *ctx; 978a94100faSBill Paul struct rl_desc *d = NULL; 979a94100faSBill Paul int i = 0, idx; 980498bd0d3SBill Paul u_int32_t cmdstat; 981498bd0d3SBill Paul int totlen = 0; 982a94100faSBill Paul 983a94100faSBill Paul if (error) 984a94100faSBill Paul return; 985a94100faSBill Paul 986a94100faSBill Paul ctx = arg; 987a94100faSBill Paul 988a94100faSBill Paul /* Signal error to caller if there's too many segments */ 989a94100faSBill Paul if (nseg > ctx->rl_maxsegs) { 990a94100faSBill Paul ctx->rl_maxsegs = 0; 991a94100faSBill Paul return; 992a94100faSBill Paul } 993a94100faSBill Paul 994a94100faSBill Paul /* 995a94100faSBill Paul * Map the segment array into descriptors. Note that we set the 996a94100faSBill Paul * start-of-frame and end-of-frame markers for either TX or RX, but 997a94100faSBill Paul * they really only have meaning in the TX case. (In the RX case, 998a94100faSBill Paul * it's the chip that tells us where packets begin and end.) 999a94100faSBill Paul * We also keep track of the end of the ring and set the 1000a94100faSBill Paul * end-of-ring bits as needed, and we set the ownership bits 1001a94100faSBill Paul * in all except the very first descriptor. (The caller will 1002a94100faSBill Paul * set this descriptor later when it start transmission or 1003a94100faSBill Paul * reception.) 1004a94100faSBill Paul */ 1005a94100faSBill Paul idx = ctx->rl_idx; 100659b5d934SBruce M Simpson for (;;) { 1007a94100faSBill Paul d = &ctx->rl_ring[idx]; 1008a94100faSBill Paul if (le32toh(d->rl_cmdstat) & RL_RDESC_STAT_OWN) { 1009a94100faSBill Paul ctx->rl_maxsegs = 0; 1010a94100faSBill Paul return; 1011a94100faSBill Paul } 1012a94100faSBill Paul cmdstat = segs[i].ds_len; 1013498bd0d3SBill Paul totlen += segs[i].ds_len; 101492825635SMarius Strobl d->rl_vlanctl = 0; 1015a94100faSBill Paul d->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr)); 1016a94100faSBill Paul d->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr)); 1017a94100faSBill Paul if (i == 0) 1018a94100faSBill Paul cmdstat |= RL_TDESC_CMD_SOF; 1019a94100faSBill Paul else 1020a94100faSBill Paul cmdstat |= RL_TDESC_CMD_OWN; 1021a94100faSBill Paul if (idx == (RL_RX_DESC_CNT - 1)) 1022a94100faSBill Paul cmdstat |= RL_TDESC_CMD_EOR; 1023a94100faSBill Paul d->rl_cmdstat = htole32(cmdstat | ctx->rl_flags); 1024a94100faSBill Paul i++; 1025a94100faSBill Paul if (i == nseg) 1026a94100faSBill Paul break; 1027a94100faSBill Paul RL_DESC_INC(idx); 1028a94100faSBill Paul } 1029a94100faSBill Paul 1030a94100faSBill Paul d->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF); 1031a94100faSBill Paul ctx->rl_maxsegs = nseg; 1032a94100faSBill Paul ctx->rl_idx = idx; 1033a94100faSBill Paul } 1034a94100faSBill Paul 1035a94100faSBill Paul /* 1036a94100faSBill Paul * Map a single buffer address. 1037a94100faSBill Paul */ 1038a94100faSBill Paul 1039a94100faSBill Paul static void 1040a94100faSBill Paul re_dma_map_addr(arg, segs, nseg, error) 1041a94100faSBill Paul void *arg; 1042a94100faSBill Paul bus_dma_segment_t *segs; 1043a94100faSBill Paul int nseg; 1044a94100faSBill Paul int error; 1045a94100faSBill Paul { 10468fd99e38SPyun YongHyeon bus_addr_t *addr; 1047a94100faSBill Paul 1048a94100faSBill Paul if (error) 1049a94100faSBill Paul return; 1050a94100faSBill Paul 1051a94100faSBill Paul KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 1052a94100faSBill Paul addr = arg; 1053a94100faSBill Paul *addr = segs->ds_addr; 1054a94100faSBill Paul } 1055a94100faSBill Paul 1056a94100faSBill Paul static int 1057a94100faSBill Paul re_allocmem(dev, sc) 1058a94100faSBill Paul device_t dev; 1059a94100faSBill Paul struct rl_softc *sc; 1060a94100faSBill Paul { 1061a94100faSBill Paul int error; 1062a94100faSBill Paul int nseg; 1063a94100faSBill Paul int i; 1064a94100faSBill Paul 1065a94100faSBill Paul /* 1066a94100faSBill Paul * Allocate map for RX mbufs. 1067a94100faSBill Paul */ 1068a94100faSBill Paul nseg = 32; 1069a94100faSBill Paul error = bus_dma_tag_create(sc->rl_parent_tag, ETHER_ALIGN, 0, 1070a94100faSBill Paul BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 10716110675fSBill Paul NULL, MCLBYTES * nseg, nseg, MCLBYTES, BUS_DMA_ALLOCNOW, 1072a94100faSBill Paul NULL, NULL, &sc->rl_ldata.rl_mtag); 1073a94100faSBill Paul if (error) { 1074a94100faSBill Paul device_printf(dev, "could not allocate dma tag\n"); 1075a94100faSBill Paul return (ENOMEM); 1076a94100faSBill Paul } 1077a94100faSBill Paul 1078a94100faSBill Paul /* 1079a94100faSBill Paul * Allocate map for TX descriptor list. 1080a94100faSBill Paul */ 1081a94100faSBill Paul error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN, 1082a94100faSBill Paul 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 10831d545c7aSMarius Strobl NULL, RL_TX_LIST_SZ, 1, RL_TX_LIST_SZ, 0, 1084a94100faSBill Paul NULL, NULL, &sc->rl_ldata.rl_tx_list_tag); 1085a94100faSBill Paul if (error) { 1086a94100faSBill Paul device_printf(dev, "could not allocate dma tag\n"); 1087a94100faSBill Paul return (ENOMEM); 1088a94100faSBill Paul } 1089a94100faSBill Paul 1090a94100faSBill Paul /* Allocate DMA'able memory for the TX ring */ 1091a94100faSBill Paul 1092a94100faSBill Paul error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag, 1093a94100faSBill Paul (void **)&sc->rl_ldata.rl_tx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO, 1094a94100faSBill Paul &sc->rl_ldata.rl_tx_list_map); 1095a94100faSBill Paul if (error) 1096a94100faSBill Paul return (ENOMEM); 1097a94100faSBill Paul 1098a94100faSBill Paul /* Load the map for the TX ring. */ 1099a94100faSBill Paul 1100a94100faSBill Paul error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag, 1101a94100faSBill Paul sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list, 1102a94100faSBill Paul RL_TX_LIST_SZ, re_dma_map_addr, 1103a94100faSBill Paul &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT); 1104a94100faSBill Paul 1105a94100faSBill Paul /* Create DMA maps for TX buffers */ 1106a94100faSBill Paul 1107a94100faSBill Paul for (i = 0; i < RL_TX_DESC_CNT; i++) { 1108a94100faSBill Paul error = bus_dmamap_create(sc->rl_ldata.rl_mtag, 0, 1109a94100faSBill Paul &sc->rl_ldata.rl_tx_dmamap[i]); 1110a94100faSBill Paul if (error) { 1111a94100faSBill Paul device_printf(dev, "can't create DMA map for TX\n"); 1112a94100faSBill Paul return (ENOMEM); 1113a94100faSBill Paul } 1114a94100faSBill Paul } 1115a94100faSBill Paul 1116a94100faSBill Paul /* 1117a94100faSBill Paul * Allocate map for RX descriptor list. 1118a94100faSBill Paul */ 1119a94100faSBill Paul error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN, 1120a94100faSBill Paul 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 11211d545c7aSMarius Strobl NULL, RL_RX_LIST_SZ, 1, RL_RX_LIST_SZ, 0, 1122a94100faSBill Paul NULL, NULL, &sc->rl_ldata.rl_rx_list_tag); 1123a94100faSBill Paul if (error) { 1124a94100faSBill Paul device_printf(dev, "could not allocate dma tag\n"); 1125a94100faSBill Paul return (ENOMEM); 1126a94100faSBill Paul } 1127a94100faSBill Paul 1128a94100faSBill Paul /* Allocate DMA'able memory for the RX ring */ 1129a94100faSBill Paul 1130a94100faSBill Paul error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag, 1131a94100faSBill Paul (void **)&sc->rl_ldata.rl_rx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO, 1132a94100faSBill Paul &sc->rl_ldata.rl_rx_list_map); 1133a94100faSBill Paul if (error) 1134a94100faSBill Paul return (ENOMEM); 1135a94100faSBill Paul 1136a94100faSBill Paul /* Load the map for the RX ring. */ 1137a94100faSBill Paul 1138a94100faSBill Paul error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag, 1139a94100faSBill Paul sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list, 114061021536SJohn-Mark Gurney RL_RX_LIST_SZ, re_dma_map_addr, 1141a94100faSBill Paul &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT); 1142a94100faSBill Paul 1143a94100faSBill Paul /* Create DMA maps for RX buffers */ 1144a94100faSBill Paul 1145a94100faSBill Paul for (i = 0; i < RL_RX_DESC_CNT; i++) { 1146a94100faSBill Paul error = bus_dmamap_create(sc->rl_ldata.rl_mtag, 0, 1147a94100faSBill Paul &sc->rl_ldata.rl_rx_dmamap[i]); 1148a94100faSBill Paul if (error) { 1149a94100faSBill Paul device_printf(dev, "can't create DMA map for RX\n"); 1150a94100faSBill Paul return (ENOMEM); 1151a94100faSBill Paul } 1152a94100faSBill Paul } 1153a94100faSBill Paul 1154a94100faSBill Paul return (0); 1155a94100faSBill Paul } 1156a94100faSBill Paul 1157a94100faSBill Paul /* 1158a94100faSBill Paul * Attach the interface. Allocate softc structures, do ifmedia 1159a94100faSBill Paul * setup and ethernet/BPF attach. 1160a94100faSBill Paul */ 1161a94100faSBill Paul static int 1162a94100faSBill Paul re_attach(dev) 1163a94100faSBill Paul device_t dev; 1164a94100faSBill Paul { 1165a94100faSBill Paul u_char eaddr[ETHER_ADDR_LEN]; 1166be099007SPyun YongHyeon u_int16_t as[ETHER_ADDR_LEN / 2]; 1167a94100faSBill Paul struct rl_softc *sc; 1168a94100faSBill Paul struct ifnet *ifp; 1169a94100faSBill Paul struct rl_hwrev *hw_rev; 1170a94100faSBill Paul int hwrev; 1171a94100faSBill Paul u_int16_t re_did = 0; 1172d1754a9bSJohn Baldwin int error = 0, rid, i; 11735774c5ffSPyun YongHyeon int msic, reg; 1174a94100faSBill Paul 1175a94100faSBill Paul sc = device_get_softc(dev); 1176ed510fb0SBill Paul sc->rl_dev = dev; 1177a94100faSBill Paul 1178a94100faSBill Paul mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 117997b9d4baSJohn-Mark Gurney MTX_DEF); 1180d1754a9bSJohn Baldwin callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0); 1181d1754a9bSJohn Baldwin 1182a94100faSBill Paul /* 1183a94100faSBill Paul * Map control/status registers. 1184a94100faSBill Paul */ 1185a94100faSBill Paul pci_enable_busmaster(dev); 1186a94100faSBill Paul 1187a94100faSBill Paul rid = RL_RID; 11885f96beb9SNate Lawson sc->rl_res = bus_alloc_resource_any(dev, RL_RES, &rid, 11895f96beb9SNate Lawson RF_ACTIVE); 1190a94100faSBill Paul 1191a94100faSBill Paul if (sc->rl_res == NULL) { 1192d1754a9bSJohn Baldwin device_printf(dev, "couldn't map ports/memory\n"); 1193a94100faSBill Paul error = ENXIO; 1194a94100faSBill Paul goto fail; 1195a94100faSBill Paul } 1196a94100faSBill Paul 1197a94100faSBill Paul sc->rl_btag = rman_get_bustag(sc->rl_res); 1198a94100faSBill Paul sc->rl_bhandle = rman_get_bushandle(sc->rl_res); 1199a94100faSBill Paul 12005774c5ffSPyun YongHyeon msic = 0; 12015774c5ffSPyun YongHyeon if (pci_find_extcap(dev, PCIY_EXPRESS, ®) == 0) { 12025774c5ffSPyun YongHyeon msic = pci_msi_count(dev); 12035774c5ffSPyun YongHyeon if (bootverbose) 12045774c5ffSPyun YongHyeon device_printf(dev, "MSI count : %d\n", msic); 12055774c5ffSPyun YongHyeon } 12065774c5ffSPyun YongHyeon if (msic == RL_MSI_MESSAGES && msi_disable == 0) { 12075774c5ffSPyun YongHyeon if (pci_alloc_msi(dev, &msic) == 0) { 12085774c5ffSPyun YongHyeon if (msic == RL_MSI_MESSAGES) { 12095774c5ffSPyun YongHyeon device_printf(dev, "Using %d MSI messages\n", 12105774c5ffSPyun YongHyeon msic); 12115774c5ffSPyun YongHyeon sc->rl_msi = 1; 12125774c5ffSPyun YongHyeon } else 12135774c5ffSPyun YongHyeon pci_release_msi(dev); 12145774c5ffSPyun YongHyeon } 12155774c5ffSPyun YongHyeon } 1216a94100faSBill Paul 12175774c5ffSPyun YongHyeon /* Allocate interrupt */ 12185774c5ffSPyun YongHyeon if (sc->rl_msi == 0) { 12195774c5ffSPyun YongHyeon rid = 0; 12205774c5ffSPyun YongHyeon sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 12215774c5ffSPyun YongHyeon RF_SHAREABLE | RF_ACTIVE); 12225774c5ffSPyun YongHyeon if (sc->rl_irq[0] == NULL) { 12235774c5ffSPyun YongHyeon device_printf(dev, "couldn't allocate IRQ resources\n"); 1224a94100faSBill Paul error = ENXIO; 1225a94100faSBill Paul goto fail; 1226a94100faSBill Paul } 12275774c5ffSPyun YongHyeon } else { 12285774c5ffSPyun YongHyeon for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) { 12295774c5ffSPyun YongHyeon sc->rl_irq[i] = bus_alloc_resource_any(dev, 12305774c5ffSPyun YongHyeon SYS_RES_IRQ, &rid, RF_ACTIVE); 12315774c5ffSPyun YongHyeon if (sc->rl_irq[i] == NULL) { 12325774c5ffSPyun YongHyeon device_printf(dev, 12335774c5ffSPyun YongHyeon "couldn't llocate IRQ resources for " 12345774c5ffSPyun YongHyeon "message %d\n", rid); 12355774c5ffSPyun YongHyeon error = ENXIO; 12365774c5ffSPyun YongHyeon goto fail; 12375774c5ffSPyun YongHyeon } 12385774c5ffSPyun YongHyeon } 12395774c5ffSPyun YongHyeon } 1240a94100faSBill Paul 1241a94100faSBill Paul /* Reset the adapter. */ 124297b9d4baSJohn-Mark Gurney RL_LOCK(sc); 1243a94100faSBill Paul re_reset(sc); 124497b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 1245abc8ff44SBill Paul 1246abc8ff44SBill Paul hw_rev = re_hwrevs; 1247abc8ff44SBill Paul hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV; 1248abc8ff44SBill Paul while (hw_rev->rl_desc != NULL) { 1249abc8ff44SBill Paul if (hw_rev->rl_rev == hwrev) { 1250abc8ff44SBill Paul sc->rl_type = hw_rev->rl_type; 1251abc8ff44SBill Paul break; 1252abc8ff44SBill Paul } 1253abc8ff44SBill Paul hw_rev++; 1254abc8ff44SBill Paul } 1255abc8ff44SBill Paul 1256141f92e7SPyun YongHyeon sc->rl_eewidth = RL_9356_ADDR_LEN; 1257ed510fb0SBill Paul re_read_eeprom(sc, (caddr_t)&re_did, 0, 1); 1258a94100faSBill Paul if (re_did != 0x8129) 1259141f92e7SPyun YongHyeon sc->rl_eewidth = RL_9346_ADDR_LEN; 1260a94100faSBill Paul 1261a94100faSBill Paul /* 1262a94100faSBill Paul * Get station address from the EEPROM. 1263a94100faSBill Paul */ 1264ed510fb0SBill Paul re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3); 1265be099007SPyun YongHyeon for (i = 0; i < ETHER_ADDR_LEN / 2; i++) 1266be099007SPyun YongHyeon as[i] = le16toh(as[i]); 1267be099007SPyun YongHyeon bcopy(as, eaddr, sizeof(eaddr)); 1268ed510fb0SBill Paul 1269ed510fb0SBill Paul if (sc->rl_type == RL_8169) { 1270ed510fb0SBill Paul /* Set RX length mask */ 1271ed510fb0SBill Paul sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN; 1272ed510fb0SBill Paul sc->rl_txstart = RL_GTXSTART; 1273ed510fb0SBill Paul } else { 1274ed510fb0SBill Paul /* Set RX length mask */ 1275ed510fb0SBill Paul sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN; 1276ed510fb0SBill Paul sc->rl_txstart = RL_TXSTART; 1277abc8ff44SBill Paul } 12789bac70b8SBill Paul 1279a94100faSBill Paul /* 1280a94100faSBill Paul * Allocate the parent bus DMA tag appropriate for PCI. 1281a94100faSBill Paul */ 1282a94100faSBill Paul #define RL_NSEG_NEW 32 12831d545c7aSMarius Strobl error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0, 12841d545c7aSMarius Strobl BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 12851d545c7aSMarius Strobl MAXBSIZE, RL_NSEG_NEW, BUS_SPACE_MAXSIZE_32BIT, 0, 12861d545c7aSMarius Strobl NULL, NULL, &sc->rl_parent_tag); 1287a94100faSBill Paul if (error) 1288a94100faSBill Paul goto fail; 1289a94100faSBill Paul 1290a94100faSBill Paul error = re_allocmem(dev, sc); 1291a94100faSBill Paul 1292a94100faSBill Paul if (error) 1293a94100faSBill Paul goto fail; 1294a94100faSBill Paul 1295cd036ec1SBrooks Davis ifp = sc->rl_ifp = if_alloc(IFT_ETHER); 1296cd036ec1SBrooks Davis if (ifp == NULL) { 1297d1754a9bSJohn Baldwin device_printf(dev, "can not if_alloc()\n"); 1298cd036ec1SBrooks Davis error = ENOSPC; 1299cd036ec1SBrooks Davis goto fail; 1300cd036ec1SBrooks Davis } 1301cd036ec1SBrooks Davis 1302a94100faSBill Paul /* Do MII setup */ 1303a94100faSBill Paul if (mii_phy_probe(dev, &sc->rl_miibus, 1304a94100faSBill Paul re_ifmedia_upd, re_ifmedia_sts)) { 1305d1754a9bSJohn Baldwin device_printf(dev, "MII without any phy!\n"); 1306a94100faSBill Paul error = ENXIO; 1307a94100faSBill Paul goto fail; 1308a94100faSBill Paul } 1309a94100faSBill Paul 1310c4aca09aSPyun YongHyeon /* Take PHY out of power down mode. */ 1311c4aca09aSPyun YongHyeon if (sc->rl_type == RL_8169) { 1312c4aca09aSPyun YongHyeon uint32_t rev; 1313c4aca09aSPyun YongHyeon 1314c4aca09aSPyun YongHyeon rev = CSR_READ_4(sc, RL_TXCFG); 1315c4aca09aSPyun YongHyeon /* HWVERID 0, 1 and 2 : bit26-30, bit23 */ 1316c4aca09aSPyun YongHyeon rev &= 0x7c800000; 1317c4aca09aSPyun YongHyeon if (rev != 0) { 1318c4aca09aSPyun YongHyeon /* RTL8169S single chip */ 1319c4aca09aSPyun YongHyeon switch (rev) { 1320c4aca09aSPyun YongHyeon case RL_HWREV_8169_8110SB: 1321c4aca09aSPyun YongHyeon case RL_HWREV_8169_8110SC: 1322c4aca09aSPyun YongHyeon case RL_HWREV_8168_SPIN2: 1323c4aca09aSPyun YongHyeon re_gmii_writereg(dev, 1, 0x1f, 0); 1324c4aca09aSPyun YongHyeon re_gmii_writereg(dev, 1, 0x0e, 0); 1325c4aca09aSPyun YongHyeon break; 1326c4aca09aSPyun YongHyeon default: 1327c4aca09aSPyun YongHyeon break; 1328c4aca09aSPyun YongHyeon } 1329c4aca09aSPyun YongHyeon } 1330c4aca09aSPyun YongHyeon } 1331c4aca09aSPyun YongHyeon 1332a94100faSBill Paul ifp->if_softc = sc; 13339bf40edeSBrooks Davis if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 1334a94100faSBill Paul ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1335a94100faSBill Paul ifp->if_ioctl = re_ioctl; 1336a94100faSBill Paul ifp->if_start = re_start; 1337f28a171cSPyun YongHyeon ifp->if_hwassist = RE_CSUM_FEATURES; 1338f28a171cSPyun YongHyeon ifp->if_capabilities = IFCAP_HWCSUM; 1339498bd0d3SBill Paul ifp->if_capenable = ifp->if_capabilities; 1340a94100faSBill Paul ifp->if_init = re_init; 134152732175SMax Laier IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN); 134252732175SMax Laier ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN; 134352732175SMax Laier IFQ_SET_READY(&ifp->if_snd); 1344a94100faSBill Paul 1345ed510fb0SBill Paul TASK_INIT(&sc->rl_txtask, 1, re_tx_task, ifp); 1346ed510fb0SBill Paul TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc); 1347ed510fb0SBill Paul 1348a94100faSBill Paul /* 1349a94100faSBill Paul * Call MI attach routine. 1350a94100faSBill Paul */ 1351a94100faSBill Paul ether_ifattach(ifp, eaddr); 1352a94100faSBill Paul 1353960fd5b3SPyun YongHyeon /* VLAN capability setup */ 1354960fd5b3SPyun YongHyeon ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING; 1355960fd5b3SPyun YongHyeon if (ifp->if_capabilities & IFCAP_HWCSUM) 1356960fd5b3SPyun YongHyeon ifp->if_capabilities |= IFCAP_VLAN_HWCSUM; 1357960fd5b3SPyun YongHyeon ifp->if_capenable = ifp->if_capabilities; 1358960fd5b3SPyun YongHyeon #ifdef DEVICE_POLLING 1359960fd5b3SPyun YongHyeon ifp->if_capabilities |= IFCAP_POLLING; 1360960fd5b3SPyun YongHyeon #endif 1361960fd5b3SPyun YongHyeon /* 1362960fd5b3SPyun YongHyeon * Tell the upper layer(s) we support long frames. 1363960fd5b3SPyun YongHyeon * Must appear after the call to ether_ifattach() because 1364960fd5b3SPyun YongHyeon * ether_ifattach() sets ifi_hdrlen to the default value. 1365960fd5b3SPyun YongHyeon */ 1366960fd5b3SPyun YongHyeon ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header); 1367960fd5b3SPyun YongHyeon 1368ed510fb0SBill Paul #ifdef RE_DIAG 1369ed510fb0SBill Paul /* 1370ed510fb0SBill Paul * Perform hardware diagnostic on the original RTL8169. 1371ed510fb0SBill Paul * Some 32-bit cards were incorrectly wired and would 1372ed510fb0SBill Paul * malfunction if plugged into a 64-bit slot. 1373ed510fb0SBill Paul */ 1374a94100faSBill Paul 1375ed510fb0SBill Paul if (hwrev == RL_HWREV_8169) { 1376ed510fb0SBill Paul error = re_diag(sc); 1377a94100faSBill Paul if (error) { 1378ed510fb0SBill Paul device_printf(dev, 1379ed510fb0SBill Paul "attach aborted due to hardware diag failure\n"); 1380a94100faSBill Paul ether_ifdetach(ifp); 1381a94100faSBill Paul goto fail; 1382a94100faSBill Paul } 1383ed510fb0SBill Paul } 1384ed510fb0SBill Paul #endif 1385a94100faSBill Paul 1386a94100faSBill Paul /* Hook interrupt last to avoid having to lock softc */ 13875774c5ffSPyun YongHyeon if (sc->rl_msi == 0) 13885774c5ffSPyun YongHyeon error = bus_setup_intr(dev, sc->rl_irq[0], 13895774c5ffSPyun YongHyeon INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc, 13905774c5ffSPyun YongHyeon &sc->rl_intrhand[0]); 13915774c5ffSPyun YongHyeon else { 13925774c5ffSPyun YongHyeon for (i = 0; i < RL_MSI_MESSAGES; i++) { 13935774c5ffSPyun YongHyeon error = bus_setup_intr(dev, sc->rl_irq[i], 13945774c5ffSPyun YongHyeon INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc, 13955774c5ffSPyun YongHyeon &sc->rl_intrhand[i]); 13965774c5ffSPyun YongHyeon if (error != 0) 13975774c5ffSPyun YongHyeon break; 13985774c5ffSPyun YongHyeon } 13995774c5ffSPyun YongHyeon } 1400a94100faSBill Paul if (error) { 1401d1754a9bSJohn Baldwin device_printf(dev, "couldn't set up irq\n"); 1402a94100faSBill Paul ether_ifdetach(ifp); 1403a94100faSBill Paul } 1404a94100faSBill Paul 1405a94100faSBill Paul fail: 1406ed510fb0SBill Paul 1407a94100faSBill Paul if (error) 1408a94100faSBill Paul re_detach(dev); 1409a94100faSBill Paul 1410a94100faSBill Paul return (error); 1411a94100faSBill Paul } 1412a94100faSBill Paul 1413a94100faSBill Paul /* 1414a94100faSBill Paul * Shutdown hardware and free up resources. This can be called any 1415a94100faSBill Paul * time after the mutex has been initialized. It is called in both 1416a94100faSBill Paul * the error case in attach and the normal detach case so it needs 1417a94100faSBill Paul * to be careful about only freeing resources that have actually been 1418a94100faSBill Paul * allocated. 1419a94100faSBill Paul */ 1420a94100faSBill Paul static int 1421a94100faSBill Paul re_detach(dev) 1422a94100faSBill Paul device_t dev; 1423a94100faSBill Paul { 1424a94100faSBill Paul struct rl_softc *sc; 1425a94100faSBill Paul struct ifnet *ifp; 14265774c5ffSPyun YongHyeon int i, rid; 1427a94100faSBill Paul 1428a94100faSBill Paul sc = device_get_softc(dev); 1429fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 1430aedd16d9SJohn-Mark Gurney KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized")); 143197b9d4baSJohn-Mark Gurney 143240929967SGleb Smirnoff #ifdef DEVICE_POLLING 143340929967SGleb Smirnoff if (ifp->if_capenable & IFCAP_POLLING) 143440929967SGleb Smirnoff ether_poll_deregister(ifp); 143540929967SGleb Smirnoff #endif 143697b9d4baSJohn-Mark Gurney /* These should only be active if attach succeeded */ 1437525e6a87SRuslan Ermilov if (device_is_attached(dev)) { 143897b9d4baSJohn-Mark Gurney RL_LOCK(sc); 143997b9d4baSJohn-Mark Gurney #if 0 144097b9d4baSJohn-Mark Gurney sc->suspended = 1; 144197b9d4baSJohn-Mark Gurney #endif 1442a94100faSBill Paul re_stop(sc); 1443525e6a87SRuslan Ermilov RL_UNLOCK(sc); 1444d1754a9bSJohn Baldwin callout_drain(&sc->rl_stat_callout); 14453d4c1b57SJohn Baldwin taskqueue_drain(taskqueue_fast, &sc->rl_inttask); 14463d4c1b57SJohn Baldwin taskqueue_drain(taskqueue_fast, &sc->rl_txtask); 1447a94100faSBill Paul /* 1448a94100faSBill Paul * Force off the IFF_UP flag here, in case someone 1449a94100faSBill Paul * still had a BPF descriptor attached to this 145097b9d4baSJohn-Mark Gurney * interface. If they do, ether_ifdetach() will cause 1451a94100faSBill Paul * the BPF code to try and clear the promisc mode 1452a94100faSBill Paul * flag, which will bubble down to re_ioctl(), 1453a94100faSBill Paul * which will try to call re_init() again. This will 1454a94100faSBill Paul * turn the NIC back on and restart the MII ticker, 1455a94100faSBill Paul * which will panic the system when the kernel tries 1456a94100faSBill Paul * to invoke the re_tick() function that isn't there 1457a94100faSBill Paul * anymore. 1458a94100faSBill Paul */ 1459a94100faSBill Paul ifp->if_flags &= ~IFF_UP; 1460525e6a87SRuslan Ermilov ether_ifdetach(ifp); 1461a94100faSBill Paul } 1462a94100faSBill Paul if (sc->rl_miibus) 1463a94100faSBill Paul device_delete_child(dev, sc->rl_miibus); 1464a94100faSBill Paul bus_generic_detach(dev); 1465a94100faSBill Paul 146697b9d4baSJohn-Mark Gurney /* 146797b9d4baSJohn-Mark Gurney * The rest is resource deallocation, so we should already be 146897b9d4baSJohn-Mark Gurney * stopped here. 146997b9d4baSJohn-Mark Gurney */ 147097b9d4baSJohn-Mark Gurney 14715774c5ffSPyun YongHyeon for (i = 0; i < RL_MSI_MESSAGES; i++) { 14725774c5ffSPyun YongHyeon if (sc->rl_intrhand[i] != NULL) { 14735774c5ffSPyun YongHyeon bus_teardown_intr(dev, sc->rl_irq[i], 14745774c5ffSPyun YongHyeon sc->rl_intrhand[i]); 14755774c5ffSPyun YongHyeon sc->rl_intrhand[i] = NULL; 14765774c5ffSPyun YongHyeon } 14775774c5ffSPyun YongHyeon } 1478ad4f426eSWarner Losh if (ifp != NULL) 1479ad4f426eSWarner Losh if_free(ifp); 14805774c5ffSPyun YongHyeon if (sc->rl_msi == 0) { 14815774c5ffSPyun YongHyeon if (sc->rl_irq[0] != NULL) { 14825774c5ffSPyun YongHyeon bus_release_resource(dev, SYS_RES_IRQ, 0, 14835774c5ffSPyun YongHyeon sc->rl_irq[0]); 14845774c5ffSPyun YongHyeon sc->rl_irq[0] = NULL; 14855774c5ffSPyun YongHyeon } 14865774c5ffSPyun YongHyeon } else { 14875774c5ffSPyun YongHyeon for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) { 14885774c5ffSPyun YongHyeon if (sc->rl_irq[i] != NULL) { 14895774c5ffSPyun YongHyeon bus_release_resource(dev, SYS_RES_IRQ, rid, 14905774c5ffSPyun YongHyeon sc->rl_irq[i]); 14915774c5ffSPyun YongHyeon sc->rl_irq[i] = NULL; 14925774c5ffSPyun YongHyeon } 14935774c5ffSPyun YongHyeon } 14945774c5ffSPyun YongHyeon pci_release_msi(dev); 14955774c5ffSPyun YongHyeon } 1496a94100faSBill Paul if (sc->rl_res) 1497a94100faSBill Paul bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res); 1498a94100faSBill Paul 1499a94100faSBill Paul /* Unload and free the RX DMA ring memory and map */ 1500a94100faSBill Paul 1501a94100faSBill Paul if (sc->rl_ldata.rl_rx_list_tag) { 1502a94100faSBill Paul bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag, 1503a94100faSBill Paul sc->rl_ldata.rl_rx_list_map); 1504a94100faSBill Paul bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag, 1505a94100faSBill Paul sc->rl_ldata.rl_rx_list, 1506a94100faSBill Paul sc->rl_ldata.rl_rx_list_map); 1507a94100faSBill Paul bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag); 1508a94100faSBill Paul } 1509a94100faSBill Paul 1510a94100faSBill Paul /* Unload and free the TX DMA ring memory and map */ 1511a94100faSBill Paul 1512a94100faSBill Paul if (sc->rl_ldata.rl_tx_list_tag) { 1513a94100faSBill Paul bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag, 1514a94100faSBill Paul sc->rl_ldata.rl_tx_list_map); 1515a94100faSBill Paul bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag, 1516a94100faSBill Paul sc->rl_ldata.rl_tx_list, 1517a94100faSBill Paul sc->rl_ldata.rl_tx_list_map); 1518a94100faSBill Paul bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag); 1519a94100faSBill Paul } 1520a94100faSBill Paul 1521a94100faSBill Paul /* Destroy all the RX and TX buffer maps */ 1522a94100faSBill Paul 1523a94100faSBill Paul if (sc->rl_ldata.rl_mtag) { 1524a94100faSBill Paul for (i = 0; i < RL_TX_DESC_CNT; i++) 1525a94100faSBill Paul bus_dmamap_destroy(sc->rl_ldata.rl_mtag, 1526a94100faSBill Paul sc->rl_ldata.rl_tx_dmamap[i]); 1527a94100faSBill Paul for (i = 0; i < RL_RX_DESC_CNT; i++) 1528a94100faSBill Paul bus_dmamap_destroy(sc->rl_ldata.rl_mtag, 1529a94100faSBill Paul sc->rl_ldata.rl_rx_dmamap[i]); 1530a94100faSBill Paul bus_dma_tag_destroy(sc->rl_ldata.rl_mtag); 1531a94100faSBill Paul } 1532a94100faSBill Paul 1533a94100faSBill Paul /* Unload and free the stats buffer and map */ 1534a94100faSBill Paul 1535a94100faSBill Paul if (sc->rl_ldata.rl_stag) { 1536a94100faSBill Paul bus_dmamap_unload(sc->rl_ldata.rl_stag, 1537a94100faSBill Paul sc->rl_ldata.rl_rx_list_map); 1538a94100faSBill Paul bus_dmamem_free(sc->rl_ldata.rl_stag, 1539a94100faSBill Paul sc->rl_ldata.rl_stats, 1540a94100faSBill Paul sc->rl_ldata.rl_smap); 1541a94100faSBill Paul bus_dma_tag_destroy(sc->rl_ldata.rl_stag); 1542a94100faSBill Paul } 1543a94100faSBill Paul 1544a94100faSBill Paul if (sc->rl_parent_tag) 1545a94100faSBill Paul bus_dma_tag_destroy(sc->rl_parent_tag); 1546a94100faSBill Paul 1547a94100faSBill Paul mtx_destroy(&sc->rl_mtx); 1548a94100faSBill Paul 1549a94100faSBill Paul return (0); 1550a94100faSBill Paul } 1551a94100faSBill Paul 1552a94100faSBill Paul static int 1553a94100faSBill Paul re_newbuf(sc, idx, m) 1554a94100faSBill Paul struct rl_softc *sc; 1555a94100faSBill Paul int idx; 1556a94100faSBill Paul struct mbuf *m; 1557a94100faSBill Paul { 1558a94100faSBill Paul struct rl_dmaload_arg arg; 1559a94100faSBill Paul struct mbuf *n = NULL; 1560a94100faSBill Paul int error; 1561a94100faSBill Paul 1562a94100faSBill Paul if (m == NULL) { 1563a94100faSBill Paul n = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1564a94100faSBill Paul if (n == NULL) 1565a94100faSBill Paul return (ENOBUFS); 1566a94100faSBill Paul m = n; 1567a94100faSBill Paul } else 1568a94100faSBill Paul m->m_data = m->m_ext.ext_buf; 1569a94100faSBill Paul 1570a94100faSBill Paul m->m_len = m->m_pkthdr.len = MCLBYTES; 157122a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX 157222a11c96SJohn-Mark Gurney /* 157322a11c96SJohn-Mark Gurney * This is part of an evil trick to deal with non-x86 platforms. 157422a11c96SJohn-Mark Gurney * The RealTek chip requires RX buffers to be aligned on 64-bit 157522a11c96SJohn-Mark Gurney * boundaries, but that will hose non-x86 machines. To get around 157622a11c96SJohn-Mark Gurney * this, we leave some empty space at the start of each buffer 157722a11c96SJohn-Mark Gurney * and for non-x86 hosts, we copy the buffer back six bytes 157822a11c96SJohn-Mark Gurney * to achieve word alignment. This is slightly more efficient 157922a11c96SJohn-Mark Gurney * than allocating a new buffer, copying the contents, and 158022a11c96SJohn-Mark Gurney * discarding the old buffer. 158122a11c96SJohn-Mark Gurney */ 158222a11c96SJohn-Mark Gurney m_adj(m, RE_ETHER_ALIGN); 158322a11c96SJohn-Mark Gurney #endif 1584a94100faSBill Paul arg.rl_idx = idx; 1585a94100faSBill Paul arg.rl_maxsegs = 1; 1586a94100faSBill Paul arg.rl_flags = 0; 1587a94100faSBill Paul arg.rl_ring = sc->rl_ldata.rl_rx_list; 1588a94100faSBill Paul 1589a94100faSBill Paul error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, 1590a94100faSBill Paul sc->rl_ldata.rl_rx_dmamap[idx], m, re_dma_map_desc, 1591a94100faSBill Paul &arg, BUS_DMA_NOWAIT); 1592a94100faSBill Paul if (error || arg.rl_maxsegs != 1) { 1593a94100faSBill Paul if (n != NULL) 1594a94100faSBill Paul m_freem(n); 1595b4b95879SMarius Strobl if (arg.rl_maxsegs == 0) 1596b4b95879SMarius Strobl bus_dmamap_unload(sc->rl_ldata.rl_mtag, 1597b4b95879SMarius Strobl sc->rl_ldata.rl_rx_dmamap[idx]); 1598a94100faSBill Paul return (ENOMEM); 1599a94100faSBill Paul } 1600a94100faSBill Paul 1601a94100faSBill Paul sc->rl_ldata.rl_rx_list[idx].rl_cmdstat |= htole32(RL_RDESC_CMD_OWN); 1602a94100faSBill Paul sc->rl_ldata.rl_rx_mbuf[idx] = m; 1603a94100faSBill Paul 1604a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_mtag, 1605a94100faSBill Paul sc->rl_ldata.rl_rx_dmamap[idx], 1606a94100faSBill Paul BUS_DMASYNC_PREREAD); 1607a94100faSBill Paul 1608a94100faSBill Paul return (0); 1609a94100faSBill Paul } 1610a94100faSBill Paul 161122a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX 161222a11c96SJohn-Mark Gurney static __inline void 161322a11c96SJohn-Mark Gurney re_fixup_rx(m) 161422a11c96SJohn-Mark Gurney struct mbuf *m; 161522a11c96SJohn-Mark Gurney { 161622a11c96SJohn-Mark Gurney int i; 161722a11c96SJohn-Mark Gurney uint16_t *src, *dst; 161822a11c96SJohn-Mark Gurney 161922a11c96SJohn-Mark Gurney src = mtod(m, uint16_t *); 162022a11c96SJohn-Mark Gurney dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src; 162122a11c96SJohn-Mark Gurney 162222a11c96SJohn-Mark Gurney for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++) 162322a11c96SJohn-Mark Gurney *dst++ = *src++; 162422a11c96SJohn-Mark Gurney 162522a11c96SJohn-Mark Gurney m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN; 162622a11c96SJohn-Mark Gurney 162722a11c96SJohn-Mark Gurney return; 162822a11c96SJohn-Mark Gurney } 162922a11c96SJohn-Mark Gurney #endif 163022a11c96SJohn-Mark Gurney 1631a94100faSBill Paul static int 1632a94100faSBill Paul re_tx_list_init(sc) 1633a94100faSBill Paul struct rl_softc *sc; 1634a94100faSBill Paul { 163597b9d4baSJohn-Mark Gurney 163697b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 163797b9d4baSJohn-Mark Gurney 1638a94100faSBill Paul bzero ((char *)sc->rl_ldata.rl_tx_list, RL_TX_LIST_SZ); 1639a94100faSBill Paul bzero ((char *)&sc->rl_ldata.rl_tx_mbuf, 1640a94100faSBill Paul (RL_TX_DESC_CNT * sizeof(struct mbuf *))); 1641a94100faSBill Paul 1642a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 1643a94100faSBill Paul sc->rl_ldata.rl_tx_list_map, BUS_DMASYNC_PREWRITE); 1644a94100faSBill Paul sc->rl_ldata.rl_tx_prodidx = 0; 1645a94100faSBill Paul sc->rl_ldata.rl_tx_considx = 0; 1646a94100faSBill Paul sc->rl_ldata.rl_tx_free = RL_TX_DESC_CNT; 1647a94100faSBill Paul 1648a94100faSBill Paul return (0); 1649a94100faSBill Paul } 1650a94100faSBill Paul 1651a94100faSBill Paul static int 1652a94100faSBill Paul re_rx_list_init(sc) 1653a94100faSBill Paul struct rl_softc *sc; 1654a94100faSBill Paul { 1655a94100faSBill Paul int i; 1656a94100faSBill Paul 1657a94100faSBill Paul bzero ((char *)sc->rl_ldata.rl_rx_list, RL_RX_LIST_SZ); 1658a94100faSBill Paul bzero ((char *)&sc->rl_ldata.rl_rx_mbuf, 1659a94100faSBill Paul (RL_RX_DESC_CNT * sizeof(struct mbuf *))); 1660a94100faSBill Paul 1661a94100faSBill Paul for (i = 0; i < RL_RX_DESC_CNT; i++) { 1662a94100faSBill Paul if (re_newbuf(sc, i, NULL) == ENOBUFS) 1663a94100faSBill Paul return (ENOBUFS); 1664a94100faSBill Paul } 1665a94100faSBill Paul 1666a94100faSBill Paul /* Flush the RX descriptors */ 1667a94100faSBill Paul 1668a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 1669a94100faSBill Paul sc->rl_ldata.rl_rx_list_map, 1670a94100faSBill Paul BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 1671a94100faSBill Paul 1672a94100faSBill Paul sc->rl_ldata.rl_rx_prodidx = 0; 1673a94100faSBill Paul sc->rl_head = sc->rl_tail = NULL; 1674a94100faSBill Paul 1675a94100faSBill Paul return (0); 1676a94100faSBill Paul } 1677a94100faSBill Paul 1678a94100faSBill Paul /* 1679a94100faSBill Paul * RX handler for C+ and 8169. For the gigE chips, we support 1680a94100faSBill Paul * the reception of jumbo frames that have been fragmented 1681a94100faSBill Paul * across multiple 2K mbuf cluster buffers. 1682a94100faSBill Paul */ 1683ed510fb0SBill Paul static int 1684a94100faSBill Paul re_rxeof(sc) 1685a94100faSBill Paul struct rl_softc *sc; 1686a94100faSBill Paul { 1687a94100faSBill Paul struct mbuf *m; 1688a94100faSBill Paul struct ifnet *ifp; 1689a94100faSBill Paul int i, total_len; 1690a94100faSBill Paul struct rl_desc *cur_rx; 1691a94100faSBill Paul u_int32_t rxstat, rxvlan; 1692ed510fb0SBill Paul int maxpkt = 16; 1693a94100faSBill Paul 16945120abbfSSam Leffler RL_LOCK_ASSERT(sc); 16955120abbfSSam Leffler 1696fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 1697a94100faSBill Paul i = sc->rl_ldata.rl_rx_prodidx; 1698a94100faSBill Paul 1699a94100faSBill Paul /* Invalidate the descriptor memory */ 1700a94100faSBill Paul 1701a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 1702a94100faSBill Paul sc->rl_ldata.rl_rx_list_map, 1703a94100faSBill Paul BUS_DMASYNC_POSTREAD); 1704a94100faSBill Paul 1705ed510fb0SBill Paul while (!RL_OWN(&sc->rl_ldata.rl_rx_list[i]) && maxpkt) { 1706a94100faSBill Paul cur_rx = &sc->rl_ldata.rl_rx_list[i]; 1707a94100faSBill Paul m = sc->rl_ldata.rl_rx_mbuf[i]; 1708a94100faSBill Paul total_len = RL_RXBYTES(cur_rx); 1709a94100faSBill Paul rxstat = le32toh(cur_rx->rl_cmdstat); 1710a94100faSBill Paul rxvlan = le32toh(cur_rx->rl_vlanctl); 1711a94100faSBill Paul 1712a94100faSBill Paul /* Invalidate the RX mbuf and unload its map */ 1713a94100faSBill Paul 1714a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_mtag, 1715a94100faSBill Paul sc->rl_ldata.rl_rx_dmamap[i], 1716a94100faSBill Paul BUS_DMASYNC_POSTWRITE); 1717a94100faSBill Paul bus_dmamap_unload(sc->rl_ldata.rl_mtag, 1718a94100faSBill Paul sc->rl_ldata.rl_rx_dmamap[i]); 1719a94100faSBill Paul 1720a94100faSBill Paul if (!(rxstat & RL_RDESC_STAT_EOF)) { 172122a11c96SJohn-Mark Gurney m->m_len = RE_RX_DESC_BUFLEN; 1722a94100faSBill Paul if (sc->rl_head == NULL) 1723a94100faSBill Paul sc->rl_head = sc->rl_tail = m; 1724a94100faSBill Paul else { 1725a94100faSBill Paul m->m_flags &= ~M_PKTHDR; 1726a94100faSBill Paul sc->rl_tail->m_next = m; 1727a94100faSBill Paul sc->rl_tail = m; 1728a94100faSBill Paul } 1729a94100faSBill Paul re_newbuf(sc, i, NULL); 1730a94100faSBill Paul RL_DESC_INC(i); 1731a94100faSBill Paul continue; 1732a94100faSBill Paul } 1733a94100faSBill Paul 1734a94100faSBill Paul /* 1735a94100faSBill Paul * NOTE: for the 8139C+, the frame length field 1736a94100faSBill Paul * is always 12 bits in size, but for the gigE chips, 1737a94100faSBill Paul * it is 13 bits (since the max RX frame length is 16K). 1738a94100faSBill Paul * Unfortunately, all 32 bits in the status word 1739a94100faSBill Paul * were already used, so to make room for the extra 1740a94100faSBill Paul * length bit, RealTek took out the 'frame alignment 1741a94100faSBill Paul * error' bit and shifted the other status bits 1742a94100faSBill Paul * over one slot. The OWN, EOR, FS and LS bits are 1743a94100faSBill Paul * still in the same places. We have already extracted 1744a94100faSBill Paul * the frame length and checked the OWN bit, so rather 1745a94100faSBill Paul * than using an alternate bit mapping, we shift the 1746a94100faSBill Paul * status bits one space to the right so we can evaluate 1747a94100faSBill Paul * them using the 8169 status as though it was in the 1748a94100faSBill Paul * same format as that of the 8139C+. 1749a94100faSBill Paul */ 1750a94100faSBill Paul if (sc->rl_type == RL_8169) 1751a94100faSBill Paul rxstat >>= 1; 1752a94100faSBill Paul 175322a11c96SJohn-Mark Gurney /* 175422a11c96SJohn-Mark Gurney * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be 175522a11c96SJohn-Mark Gurney * set, but if CRC is clear, it will still be a valid frame. 175622a11c96SJohn-Mark Gurney */ 175722a11c96SJohn-Mark Gurney if (rxstat & RL_RDESC_STAT_RXERRSUM && !(total_len > 8191 && 175822a11c96SJohn-Mark Gurney (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)) { 1759a94100faSBill Paul ifp->if_ierrors++; 1760a94100faSBill Paul /* 1761a94100faSBill Paul * If this is part of a multi-fragment packet, 1762a94100faSBill Paul * discard all the pieces. 1763a94100faSBill Paul */ 1764a94100faSBill Paul if (sc->rl_head != NULL) { 1765a94100faSBill Paul m_freem(sc->rl_head); 1766a94100faSBill Paul sc->rl_head = sc->rl_tail = NULL; 1767a94100faSBill Paul } 1768a94100faSBill Paul re_newbuf(sc, i, m); 1769a94100faSBill Paul RL_DESC_INC(i); 1770a94100faSBill Paul continue; 1771a94100faSBill Paul } 1772a94100faSBill Paul 1773a94100faSBill Paul /* 1774a94100faSBill Paul * If allocating a replacement mbuf fails, 1775a94100faSBill Paul * reload the current one. 1776a94100faSBill Paul */ 1777a94100faSBill Paul 1778a94100faSBill Paul if (re_newbuf(sc, i, NULL)) { 1779a94100faSBill Paul ifp->if_ierrors++; 1780a94100faSBill Paul if (sc->rl_head != NULL) { 1781a94100faSBill Paul m_freem(sc->rl_head); 1782a94100faSBill Paul sc->rl_head = sc->rl_tail = NULL; 1783a94100faSBill Paul } 1784a94100faSBill Paul re_newbuf(sc, i, m); 1785a94100faSBill Paul RL_DESC_INC(i); 1786a94100faSBill Paul continue; 1787a94100faSBill Paul } 1788a94100faSBill Paul 1789a94100faSBill Paul RL_DESC_INC(i); 1790a94100faSBill Paul 1791a94100faSBill Paul if (sc->rl_head != NULL) { 179222a11c96SJohn-Mark Gurney m->m_len = total_len % RE_RX_DESC_BUFLEN; 179322a11c96SJohn-Mark Gurney if (m->m_len == 0) 179422a11c96SJohn-Mark Gurney m->m_len = RE_RX_DESC_BUFLEN; 1795a94100faSBill Paul /* 1796a94100faSBill Paul * Special case: if there's 4 bytes or less 1797a94100faSBill Paul * in this buffer, the mbuf can be discarded: 1798a94100faSBill Paul * the last 4 bytes is the CRC, which we don't 1799a94100faSBill Paul * care about anyway. 1800a94100faSBill Paul */ 1801a94100faSBill Paul if (m->m_len <= ETHER_CRC_LEN) { 1802a94100faSBill Paul sc->rl_tail->m_len -= 1803a94100faSBill Paul (ETHER_CRC_LEN - m->m_len); 1804a94100faSBill Paul m_freem(m); 1805a94100faSBill Paul } else { 1806a94100faSBill Paul m->m_len -= ETHER_CRC_LEN; 1807a94100faSBill Paul m->m_flags &= ~M_PKTHDR; 1808a94100faSBill Paul sc->rl_tail->m_next = m; 1809a94100faSBill Paul } 1810a94100faSBill Paul m = sc->rl_head; 1811a94100faSBill Paul sc->rl_head = sc->rl_tail = NULL; 1812a94100faSBill Paul m->m_pkthdr.len = total_len - ETHER_CRC_LEN; 1813a94100faSBill Paul } else 1814a94100faSBill Paul m->m_pkthdr.len = m->m_len = 1815a94100faSBill Paul (total_len - ETHER_CRC_LEN); 1816a94100faSBill Paul 181722a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX 181822a11c96SJohn-Mark Gurney re_fixup_rx(m); 181922a11c96SJohn-Mark Gurney #endif 1820a94100faSBill Paul ifp->if_ipackets++; 1821a94100faSBill Paul m->m_pkthdr.rcvif = ifp; 1822a94100faSBill Paul 1823a94100faSBill Paul /* Do RX checksumming if enabled */ 1824a94100faSBill Paul 1825a94100faSBill Paul if (ifp->if_capenable & IFCAP_RXCSUM) { 1826a94100faSBill Paul 1827a94100faSBill Paul /* Check IP header checksum */ 1828a94100faSBill Paul if (rxstat & RL_RDESC_STAT_PROTOID) 1829a94100faSBill Paul m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED; 1830a94100faSBill Paul if (!(rxstat & RL_RDESC_STAT_IPSUMBAD)) 1831a94100faSBill Paul m->m_pkthdr.csum_flags |= CSUM_IP_VALID; 1832a94100faSBill Paul 1833a94100faSBill Paul /* Check TCP/UDP checksum */ 1834a94100faSBill Paul if ((RL_TCPPKT(rxstat) && 1835a94100faSBill Paul !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) || 1836a94100faSBill Paul (RL_UDPPKT(rxstat) && 1837a94100faSBill Paul !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) { 1838a94100faSBill Paul m->m_pkthdr.csum_flags |= 1839a94100faSBill Paul CSUM_DATA_VALID|CSUM_PSEUDO_HDR; 1840a94100faSBill Paul m->m_pkthdr.csum_data = 0xffff; 1841a94100faSBill Paul } 1842a94100faSBill Paul } 1843ed510fb0SBill Paul maxpkt--; 1844d147662cSGleb Smirnoff if (rxvlan & RL_RDESC_VLANCTL_TAG) { 184578ba57b9SAndre Oppermann m->m_pkthdr.ether_vtag = 184678ba57b9SAndre Oppermann ntohs((rxvlan & RL_RDESC_VLANCTL_DATA)); 184778ba57b9SAndre Oppermann m->m_flags |= M_VLANTAG; 1848d147662cSGleb Smirnoff } 18495120abbfSSam Leffler RL_UNLOCK(sc); 1850a94100faSBill Paul (*ifp->if_input)(ifp, m); 18515120abbfSSam Leffler RL_LOCK(sc); 1852a94100faSBill Paul } 1853a94100faSBill Paul 1854a94100faSBill Paul /* Flush the RX DMA ring */ 1855a94100faSBill Paul 1856a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 1857a94100faSBill Paul sc->rl_ldata.rl_rx_list_map, 1858a94100faSBill Paul BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 1859a94100faSBill Paul 1860a94100faSBill Paul sc->rl_ldata.rl_rx_prodidx = i; 1861ed510fb0SBill Paul 1862ed510fb0SBill Paul if (maxpkt) 1863ed510fb0SBill Paul return(EAGAIN); 1864ed510fb0SBill Paul 1865ed510fb0SBill Paul return(0); 1866a94100faSBill Paul } 1867a94100faSBill Paul 1868a94100faSBill Paul static void 1869a94100faSBill Paul re_txeof(sc) 1870a94100faSBill Paul struct rl_softc *sc; 1871a94100faSBill Paul { 1872a94100faSBill Paul struct ifnet *ifp; 1873a94100faSBill Paul u_int32_t txstat; 1874a94100faSBill Paul int idx; 1875a94100faSBill Paul 1876fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 1877a94100faSBill Paul idx = sc->rl_ldata.rl_tx_considx; 1878a94100faSBill Paul 1879a94100faSBill Paul /* Invalidate the TX descriptor list */ 1880a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 1881a94100faSBill Paul sc->rl_ldata.rl_tx_list_map, 1882a94100faSBill Paul BUS_DMASYNC_POSTREAD); 1883a94100faSBill Paul 1884ed510fb0SBill Paul while (sc->rl_ldata.rl_tx_free < RL_TX_DESC_CNT) { 1885a94100faSBill Paul txstat = le32toh(sc->rl_ldata.rl_tx_list[idx].rl_cmdstat); 1886a94100faSBill Paul if (txstat & RL_TDESC_CMD_OWN) 1887a94100faSBill Paul break; 1888a94100faSBill Paul 1889ed510fb0SBill Paul sc->rl_ldata.rl_tx_list[idx].rl_bufaddr_lo = 0; 1890ed510fb0SBill Paul 1891a94100faSBill Paul /* 1892a94100faSBill Paul * We only stash mbufs in the last descriptor 1893a94100faSBill Paul * in a fragment chain, which also happens to 1894a94100faSBill Paul * be the only place where the TX status bits 1895a94100faSBill Paul * are valid. 1896a94100faSBill Paul */ 1897a94100faSBill Paul if (txstat & RL_TDESC_CMD_EOF) { 1898a94100faSBill Paul m_freem(sc->rl_ldata.rl_tx_mbuf[idx]); 1899a94100faSBill Paul sc->rl_ldata.rl_tx_mbuf[idx] = NULL; 1900a94100faSBill Paul bus_dmamap_unload(sc->rl_ldata.rl_mtag, 1901a94100faSBill Paul sc->rl_ldata.rl_tx_dmamap[idx]); 1902a94100faSBill Paul if (txstat & (RL_TDESC_STAT_EXCESSCOL| 1903a94100faSBill Paul RL_TDESC_STAT_COLCNT)) 1904a94100faSBill Paul ifp->if_collisions++; 1905a94100faSBill Paul if (txstat & RL_TDESC_STAT_TXERRSUM) 1906a94100faSBill Paul ifp->if_oerrors++; 1907a94100faSBill Paul else 1908a94100faSBill Paul ifp->if_opackets++; 1909a94100faSBill Paul } 1910a94100faSBill Paul sc->rl_ldata.rl_tx_free++; 1911a94100faSBill Paul RL_DESC_INC(idx); 1912a94100faSBill Paul } 1913b4b95879SMarius Strobl sc->rl_ldata.rl_tx_considx = idx; 1914a94100faSBill Paul 1915a94100faSBill Paul /* No changes made to the TX ring, so no flush needed */ 1916a94100faSBill Paul 1917b4b95879SMarius Strobl if (sc->rl_ldata.rl_tx_free > RL_TX_DESC_THLD) 191813f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1919a94100faSBill Paul 1920b4b95879SMarius Strobl if (sc->rl_ldata.rl_tx_free < RL_TX_DESC_CNT) { 19210fc4974fSBill Paul /* 1922b4b95879SMarius Strobl * Some chips will ignore a second TX request issued 1923b4b95879SMarius Strobl * while an existing transmission is in progress. If 1924b4b95879SMarius Strobl * the transmitter goes idle but there are still 1925b4b95879SMarius Strobl * packets waiting to be sent, we need to restart the 1926b4b95879SMarius Strobl * channel here to flush them out. This only seems to 1927b4b95879SMarius Strobl * be required with the PCIe devices. 19280fc4974fSBill Paul */ 19290fc4974fSBill Paul CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 19300fc4974fSBill Paul 1931ed510fb0SBill Paul #ifdef RE_TX_MODERATION 1932a94100faSBill Paul /* 1933b4b95879SMarius Strobl * If not all descriptors have been reaped yet, reload 1934b4b95879SMarius Strobl * the timer so that we will eventually get another 1935a94100faSBill Paul * interrupt that will cause us to re-enter this routine. 1936a94100faSBill Paul * This is done in case the transmitter has gone idle. 1937a94100faSBill Paul */ 1938a94100faSBill Paul CSR_WRITE_4(sc, RL_TIMERCNT, 1); 1939ed510fb0SBill Paul #endif 1940b4b95879SMarius Strobl } else 1941b4b95879SMarius Strobl sc->rl_watchdog_timer = 0; 1942a94100faSBill Paul } 1943a94100faSBill Paul 1944a94100faSBill Paul static void 1945a94100faSBill Paul re_tick(xsc) 1946a94100faSBill Paul void *xsc; 1947a94100faSBill Paul { 1948a94100faSBill Paul struct rl_softc *sc; 1949d1754a9bSJohn Baldwin struct mii_data *mii; 1950ed510fb0SBill Paul struct ifnet *ifp; 1951a94100faSBill Paul 1952a94100faSBill Paul sc = xsc; 1953ed510fb0SBill Paul ifp = sc->rl_ifp; 195497b9d4baSJohn-Mark Gurney 195597b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 195697b9d4baSJohn-Mark Gurney 19571d545c7aSMarius Strobl re_watchdog(sc); 1958a94100faSBill Paul 19591d545c7aSMarius Strobl mii = device_get_softc(sc->rl_miibus); 1960a94100faSBill Paul mii_tick(mii); 1961ed510fb0SBill Paul if (sc->rl_link) { 1962ed510fb0SBill Paul if (!(mii->mii_media_status & IFM_ACTIVE)) 1963ed510fb0SBill Paul sc->rl_link = 0; 1964ed510fb0SBill Paul } else { 1965ed510fb0SBill Paul if (mii->mii_media_status & IFM_ACTIVE && 1966ed510fb0SBill Paul IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 1967ed510fb0SBill Paul sc->rl_link = 1; 1968ed510fb0SBill Paul if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1969ed510fb0SBill Paul taskqueue_enqueue_fast(taskqueue_fast, 1970ed510fb0SBill Paul &sc->rl_txtask); 1971ed510fb0SBill Paul } 1972ed510fb0SBill Paul } 1973a94100faSBill Paul 1974d1754a9bSJohn Baldwin callout_reset(&sc->rl_stat_callout, hz, re_tick, sc); 1975a94100faSBill Paul } 1976a94100faSBill Paul 1977a94100faSBill Paul #ifdef DEVICE_POLLING 1978a94100faSBill Paul static void 1979a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 1980a94100faSBill Paul { 1981a94100faSBill Paul struct rl_softc *sc = ifp->if_softc; 1982a94100faSBill Paul 1983a94100faSBill Paul RL_LOCK(sc); 198440929967SGleb Smirnoff if (ifp->if_drv_flags & IFF_DRV_RUNNING) 198597b9d4baSJohn-Mark Gurney re_poll_locked(ifp, cmd, count); 198697b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 198797b9d4baSJohn-Mark Gurney } 198897b9d4baSJohn-Mark Gurney 198997b9d4baSJohn-Mark Gurney static void 199097b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count) 199197b9d4baSJohn-Mark Gurney { 199297b9d4baSJohn-Mark Gurney struct rl_softc *sc = ifp->if_softc; 199397b9d4baSJohn-Mark Gurney 199497b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 199597b9d4baSJohn-Mark Gurney 1996a94100faSBill Paul sc->rxcycles = count; 1997a94100faSBill Paul re_rxeof(sc); 1998a94100faSBill Paul re_txeof(sc); 1999a94100faSBill Paul 200037652939SMax Laier if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2001ed510fb0SBill Paul taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask); 2002a94100faSBill Paul 2003a94100faSBill Paul if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */ 2004a94100faSBill Paul u_int16_t status; 2005a94100faSBill Paul 2006a94100faSBill Paul status = CSR_READ_2(sc, RL_ISR); 2007a94100faSBill Paul if (status == 0xffff) 200897b9d4baSJohn-Mark Gurney return; 2009a94100faSBill Paul if (status) 2010a94100faSBill Paul CSR_WRITE_2(sc, RL_ISR, status); 2011a94100faSBill Paul 2012a94100faSBill Paul /* 2013a94100faSBill Paul * XXX check behaviour on receiver stalls. 2014a94100faSBill Paul */ 2015a94100faSBill Paul 2016a94100faSBill Paul if (status & RL_ISR_SYSTEM_ERR) { 2017a94100faSBill Paul re_reset(sc); 201897b9d4baSJohn-Mark Gurney re_init_locked(sc); 2019a94100faSBill Paul } 2020a94100faSBill Paul } 2021a94100faSBill Paul } 2022a94100faSBill Paul #endif /* DEVICE_POLLING */ 2023a94100faSBill Paul 2024ef544f63SPaolo Pisati static int 2025a94100faSBill Paul re_intr(arg) 2026a94100faSBill Paul void *arg; 2027a94100faSBill Paul { 2028a94100faSBill Paul struct rl_softc *sc; 2029ed510fb0SBill Paul uint16_t status; 2030a94100faSBill Paul 2031a94100faSBill Paul sc = arg; 2032ed510fb0SBill Paul 2033ed510fb0SBill Paul status = CSR_READ_2(sc, RL_ISR); 2034498bd0d3SBill Paul if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0) 2035ef544f63SPaolo Pisati return (FILTER_STRAY); 2036ed510fb0SBill Paul CSR_WRITE_2(sc, RL_IMR, 0); 2037ed510fb0SBill Paul 2038ed510fb0SBill Paul taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask); 2039ed510fb0SBill Paul 2040ef544f63SPaolo Pisati return (FILTER_HANDLED); 2041ed510fb0SBill Paul } 2042ed510fb0SBill Paul 2043ed510fb0SBill Paul static void 2044ed510fb0SBill Paul re_int_task(arg, npending) 2045ed510fb0SBill Paul void *arg; 2046ed510fb0SBill Paul int npending; 2047ed510fb0SBill Paul { 2048ed510fb0SBill Paul struct rl_softc *sc; 2049ed510fb0SBill Paul struct ifnet *ifp; 2050ed510fb0SBill Paul u_int16_t status; 2051ed510fb0SBill Paul int rval = 0; 2052ed510fb0SBill Paul 2053ed510fb0SBill Paul sc = arg; 2054ed510fb0SBill Paul ifp = sc->rl_ifp; 2055a94100faSBill Paul 2056a94100faSBill Paul RL_LOCK(sc); 205797b9d4baSJohn-Mark Gurney 2058a94100faSBill Paul status = CSR_READ_2(sc, RL_ISR); 2059a94100faSBill Paul CSR_WRITE_2(sc, RL_ISR, status); 2060a94100faSBill Paul 2061ed510fb0SBill Paul if (sc->suspended || !(ifp->if_flags & IFF_UP)) { 2062ed510fb0SBill Paul RL_UNLOCK(sc); 2063ed510fb0SBill Paul return; 2064ed510fb0SBill Paul } 2065a94100faSBill Paul 2066ed510fb0SBill Paul #ifdef DEVICE_POLLING 2067ed510fb0SBill Paul if (ifp->if_capenable & IFCAP_POLLING) { 2068ed510fb0SBill Paul RL_UNLOCK(sc); 2069ed510fb0SBill Paul return; 2070ed510fb0SBill Paul } 2071ed510fb0SBill Paul #endif 2072a94100faSBill Paul 2073ed510fb0SBill Paul if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW)) 2074ed510fb0SBill Paul rval = re_rxeof(sc); 2075ed510fb0SBill Paul 2076ed510fb0SBill Paul #ifdef RE_TX_MODERATION 2077ed510fb0SBill Paul if (status & (RL_ISR_TIMEOUT_EXPIRED| 2078ed510fb0SBill Paul #else 2079ed510fb0SBill Paul if (status & (RL_ISR_TX_OK| 2080ed510fb0SBill Paul #endif 2081ed510fb0SBill Paul RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL)) 2082a94100faSBill Paul re_txeof(sc); 2083a94100faSBill Paul 2084a94100faSBill Paul if (status & RL_ISR_SYSTEM_ERR) { 2085a94100faSBill Paul re_reset(sc); 208697b9d4baSJohn-Mark Gurney re_init_locked(sc); 2087a94100faSBill Paul } 2088a94100faSBill Paul 2089a94100faSBill Paul if (status & RL_ISR_LINKCHG) { 2090d1754a9bSJohn Baldwin callout_stop(&sc->rl_stat_callout); 2091d1754a9bSJohn Baldwin re_tick(sc); 2092a94100faSBill Paul } 2093a94100faSBill Paul 209452732175SMax Laier if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2095ed510fb0SBill Paul taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask); 2096a94100faSBill Paul 2097a94100faSBill Paul RL_UNLOCK(sc); 2098ed510fb0SBill Paul 2099ed510fb0SBill Paul if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) { 2100ed510fb0SBill Paul taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask); 2101ed510fb0SBill Paul return; 2102ed510fb0SBill Paul } 2103ed510fb0SBill Paul 2104ed510fb0SBill Paul CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS); 2105ed510fb0SBill Paul 2106ed510fb0SBill Paul return; 2107a94100faSBill Paul } 2108a94100faSBill Paul 2109a94100faSBill Paul static int 2110a94100faSBill Paul re_encap(sc, m_head, idx) 2111a94100faSBill Paul struct rl_softc *sc; 211280a2a305SJohn-Mark Gurney struct mbuf **m_head; 2113a94100faSBill Paul int *idx; 2114a94100faSBill Paul { 2115a94100faSBill Paul struct mbuf *m_new = NULL; 2116a94100faSBill Paul struct rl_dmaload_arg arg; 2117a94100faSBill Paul bus_dmamap_t map; 2118a94100faSBill Paul int error; 2119a94100faSBill Paul 212097b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 212197b9d4baSJohn-Mark Gurney 2122b4b95879SMarius Strobl if (sc->rl_ldata.rl_tx_free <= RL_TX_DESC_THLD) 2123a94100faSBill Paul return (EFBIG); 2124a94100faSBill Paul 2125a94100faSBill Paul /* 2126a94100faSBill Paul * Set up checksum offload. Note: checksum offload bits must 2127a94100faSBill Paul * appear in all descriptors of a multi-descriptor transmit 212822a11c96SJohn-Mark Gurney * attempt. This is according to testing done with an 8169 212922a11c96SJohn-Mark Gurney * chip. This is a requirement. 2130a94100faSBill Paul */ 2131a94100faSBill Paul 2132a94100faSBill Paul arg.rl_flags = 0; 2133a94100faSBill Paul 2134dc74159dSPyun YongHyeon if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0) 2135dc74159dSPyun YongHyeon arg.rl_flags = RL_TDESC_CMD_LGSEND | 2136dc74159dSPyun YongHyeon ((uint32_t)(*m_head)->m_pkthdr.tso_segsz << 2137dc74159dSPyun YongHyeon RL_TDESC_CMD_MSSVAL_SHIFT); 2138dc74159dSPyun YongHyeon else { 213980a2a305SJohn-Mark Gurney if ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) 2140a94100faSBill Paul arg.rl_flags |= RL_TDESC_CMD_IPCSUM; 214180a2a305SJohn-Mark Gurney if ((*m_head)->m_pkthdr.csum_flags & CSUM_TCP) 2142a94100faSBill Paul arg.rl_flags |= RL_TDESC_CMD_TCPCSUM; 214380a2a305SJohn-Mark Gurney if ((*m_head)->m_pkthdr.csum_flags & CSUM_UDP) 2144a94100faSBill Paul arg.rl_flags |= RL_TDESC_CMD_UDPCSUM; 2145dc74159dSPyun YongHyeon } 2146a94100faSBill Paul 2147a94100faSBill Paul arg.rl_idx = *idx; 2148a94100faSBill Paul arg.rl_maxsegs = sc->rl_ldata.rl_tx_free; 2149b4b95879SMarius Strobl if (arg.rl_maxsegs > RL_TX_DESC_THLD) 2150b4b95879SMarius Strobl arg.rl_maxsegs -= RL_TX_DESC_THLD; 2151a94100faSBill Paul arg.rl_ring = sc->rl_ldata.rl_tx_list; 2152a94100faSBill Paul 2153a94100faSBill Paul map = sc->rl_ldata.rl_tx_dmamap[*idx]; 21540fc4974fSBill Paul 21550fc4974fSBill Paul /* 21560fc4974fSBill Paul * With some of the RealTek chips, using the checksum offload 21570fc4974fSBill Paul * support in conjunction with the autopadding feature results 21580fc4974fSBill Paul * in the transmission of corrupt frames. For example, if we 21590fc4974fSBill Paul * need to send a really small IP fragment that's less than 60 21600fc4974fSBill Paul * bytes in size, and IP header checksumming is enabled, the 21610fc4974fSBill Paul * resulting ethernet frame that appears on the wire will 21620fc4974fSBill Paul * have garbled payload. To work around this, if TX checksum 21630fc4974fSBill Paul * offload is enabled, we always manually pad short frames out 21640fc4974fSBill Paul * to the minimum ethernet frame size. We do this by pretending 21650fc4974fSBill Paul * the mbuf chain has too many fragments so the coalescing code 21660fc4974fSBill Paul * below can assemble the packet into a single buffer that's 21670fc4974fSBill Paul * padded out to the mininum frame size. 2168e2bcb489SBill Paul * 2169e2bcb489SBill Paul * Note: this appears unnecessary for TCP, and doing it for TCP 2170e2bcb489SBill Paul * with PCIe adapters seems to result in bad checksums. 21710fc4974fSBill Paul */ 2172e2bcb489SBill Paul 2173e2bcb489SBill Paul if (arg.rl_flags && !(arg.rl_flags & RL_TDESC_CMD_TCPCSUM) && 2174e2bcb489SBill Paul (*m_head)->m_pkthdr.len < RL_MIN_FRAMELEN) 21750fc4974fSBill Paul error = EFBIG; 21760fc4974fSBill Paul else 2177a94100faSBill Paul error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, map, 217880a2a305SJohn-Mark Gurney *m_head, re_dma_map_desc, &arg, BUS_DMA_NOWAIT); 2179a94100faSBill Paul 2180a94100faSBill Paul if (error && error != EFBIG) { 21816b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "can't map mbuf (error %d)\n", error); 2182a94100faSBill Paul return (ENOBUFS); 2183a94100faSBill Paul } 2184a94100faSBill Paul 2185a94100faSBill Paul /* Too many segments to map, coalesce into a single mbuf */ 2186a94100faSBill Paul 2187a94100faSBill Paul if (error || arg.rl_maxsegs == 0) { 2188b4b95879SMarius Strobl if (arg.rl_maxsegs == 0) 2189b4b95879SMarius Strobl bus_dmamap_unload(sc->rl_ldata.rl_mtag, map); 219080a2a305SJohn-Mark Gurney m_new = m_defrag(*m_head, M_DONTWAIT); 2191b4b95879SMarius Strobl if (m_new == NULL) { 2192b4b95879SMarius Strobl m_freem(*m_head); 2193b4b95879SMarius Strobl *m_head = NULL; 219480a2a305SJohn-Mark Gurney return (ENOBUFS); 2195b4b95879SMarius Strobl } 219680a2a305SJohn-Mark Gurney *m_head = m_new; 2197a94100faSBill Paul 21980fc4974fSBill Paul /* 21990fc4974fSBill Paul * Manually pad short frames, and zero the pad space 22000fc4974fSBill Paul * to avoid leaking data. 22010fc4974fSBill Paul */ 22020fc4974fSBill Paul if (m_new->m_pkthdr.len < RL_MIN_FRAMELEN) { 22030fc4974fSBill Paul bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, 22040fc4974fSBill Paul RL_MIN_FRAMELEN - m_new->m_pkthdr.len); 22050fc4974fSBill Paul m_new->m_pkthdr.len += RL_MIN_FRAMELEN - 22060fc4974fSBill Paul m_new->m_pkthdr.len; 22070fc4974fSBill Paul m_new->m_len = m_new->m_pkthdr.len; 22080fc4974fSBill Paul } 22090fc4974fSBill Paul 2210b4b95879SMarius Strobl /* Note that we'll run over RL_TX_DESC_THLD here. */ 2211a94100faSBill Paul arg.rl_maxsegs = sc->rl_ldata.rl_tx_free; 2212a94100faSBill Paul error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, map, 221380a2a305SJohn-Mark Gurney *m_head, re_dma_map_desc, &arg, BUS_DMA_NOWAIT); 2214b4b95879SMarius Strobl if (error || arg.rl_maxsegs == 0) { 2215b4b95879SMarius Strobl device_printf(sc->rl_dev, 2216b4b95879SMarius Strobl "can't map defragmented mbuf (error %d)\n", error); 2217b4b95879SMarius Strobl m_freem(m_new); 2218b4b95879SMarius Strobl *m_head = NULL; 2219b4b95879SMarius Strobl if (arg.rl_maxsegs == 0) 2220b4b95879SMarius Strobl bus_dmamap_unload(sc->rl_ldata.rl_mtag, map); 2221a94100faSBill Paul return (EFBIG); 2222a94100faSBill Paul } 2223a94100faSBill Paul } 2224a94100faSBill Paul 2225a94100faSBill Paul /* 2226a94100faSBill Paul * Insure that the map for this transmission 2227a94100faSBill Paul * is placed at the array index of the last descriptor 222822a11c96SJohn-Mark Gurney * in this chain. (Swap last and first dmamaps.) 2229a94100faSBill Paul */ 2230a94100faSBill Paul sc->rl_ldata.rl_tx_dmamap[*idx] = 2231a94100faSBill Paul sc->rl_ldata.rl_tx_dmamap[arg.rl_idx]; 2232a94100faSBill Paul sc->rl_ldata.rl_tx_dmamap[arg.rl_idx] = map; 2233a94100faSBill Paul 223480a2a305SJohn-Mark Gurney sc->rl_ldata.rl_tx_mbuf[arg.rl_idx] = *m_head; 2235a94100faSBill Paul sc->rl_ldata.rl_tx_free -= arg.rl_maxsegs; 2236a94100faSBill Paul 2237a94100faSBill Paul /* 2238a94100faSBill Paul * Set up hardware VLAN tagging. Note: vlan tag info must 2239a94100faSBill Paul * appear in the first descriptor of a multi-descriptor 2240a94100faSBill Paul * transmission attempt. 2241a94100faSBill Paul */ 224278ba57b9SAndre Oppermann if ((*m_head)->m_flags & M_VLANTAG) 2243a94100faSBill Paul sc->rl_ldata.rl_tx_list[*idx].rl_vlanctl = 224478ba57b9SAndre Oppermann htole32(htons((*m_head)->m_pkthdr.ether_vtag) | 224578ba57b9SAndre Oppermann RL_TDESC_VLANCTL_TAG); 2246a94100faSBill Paul 2247a94100faSBill Paul /* Transfer ownership of packet to the chip. */ 2248a94100faSBill Paul 2249a94100faSBill Paul sc->rl_ldata.rl_tx_list[arg.rl_idx].rl_cmdstat |= 2250a94100faSBill Paul htole32(RL_TDESC_CMD_OWN); 2251a94100faSBill Paul if (*idx != arg.rl_idx) 2252a94100faSBill Paul sc->rl_ldata.rl_tx_list[*idx].rl_cmdstat |= 2253a94100faSBill Paul htole32(RL_TDESC_CMD_OWN); 2254a94100faSBill Paul 2255a94100faSBill Paul RL_DESC_INC(arg.rl_idx); 2256a94100faSBill Paul *idx = arg.rl_idx; 2257a94100faSBill Paul 2258a94100faSBill Paul return (0); 2259a94100faSBill Paul } 2260a94100faSBill Paul 226197b9d4baSJohn-Mark Gurney static void 2262ed510fb0SBill Paul re_tx_task(arg, npending) 2263ed510fb0SBill Paul void *arg; 2264ed510fb0SBill Paul int npending; 226597b9d4baSJohn-Mark Gurney { 2266ed510fb0SBill Paul struct ifnet *ifp; 226797b9d4baSJohn-Mark Gurney 2268ed510fb0SBill Paul ifp = arg; 2269ed510fb0SBill Paul re_start(ifp); 2270ed510fb0SBill Paul 2271ed510fb0SBill Paul return; 227297b9d4baSJohn-Mark Gurney } 227397b9d4baSJohn-Mark Gurney 2274a94100faSBill Paul /* 2275a94100faSBill Paul * Main transmit routine for C+ and gigE NICs. 2276a94100faSBill Paul */ 2277a94100faSBill Paul static void 2278ed510fb0SBill Paul re_start(ifp) 2279a94100faSBill Paul struct ifnet *ifp; 2280a94100faSBill Paul { 2281a94100faSBill Paul struct rl_softc *sc; 2282a94100faSBill Paul struct mbuf *m_head = NULL; 228352732175SMax Laier int idx, queued = 0; 2284a94100faSBill Paul 2285a94100faSBill Paul sc = ifp->if_softc; 228697b9d4baSJohn-Mark Gurney 2287ed510fb0SBill Paul RL_LOCK(sc); 2288ed510fb0SBill Paul 2289ed510fb0SBill Paul if (!sc->rl_link || ifp->if_drv_flags & IFF_DRV_OACTIVE) { 2290ed510fb0SBill Paul RL_UNLOCK(sc); 2291ed510fb0SBill Paul return; 2292ed510fb0SBill Paul } 2293a94100faSBill Paul 2294a94100faSBill Paul idx = sc->rl_ldata.rl_tx_prodidx; 2295a94100faSBill Paul 2296a94100faSBill Paul while (sc->rl_ldata.rl_tx_mbuf[idx] == NULL) { 229752732175SMax Laier IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head); 2298a94100faSBill Paul if (m_head == NULL) 2299a94100faSBill Paul break; 2300a94100faSBill Paul 230180a2a305SJohn-Mark Gurney if (re_encap(sc, &m_head, &idx)) { 2302b4b95879SMarius Strobl if (m_head == NULL) 2303b4b95879SMarius Strobl break; 230452732175SMax Laier IFQ_DRV_PREPEND(&ifp->if_snd, m_head); 230513f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2306a94100faSBill Paul break; 2307a94100faSBill Paul } 2308a94100faSBill Paul 2309a94100faSBill Paul /* 2310a94100faSBill Paul * If there's a BPF listener, bounce a copy of this frame 2311a94100faSBill Paul * to him. 2312a94100faSBill Paul */ 231359a0d28bSChristian S.J. Peron ETHER_BPF_MTAP(ifp, m_head); 231452732175SMax Laier 231552732175SMax Laier queued++; 2316a94100faSBill Paul } 2317a94100faSBill Paul 2318ed510fb0SBill Paul if (queued == 0) { 2319ed510fb0SBill Paul #ifdef RE_TX_MODERATION 2320ed510fb0SBill Paul if (sc->rl_ldata.rl_tx_free != RL_TX_DESC_CNT) 2321ed510fb0SBill Paul CSR_WRITE_4(sc, RL_TIMERCNT, 1); 2322ed510fb0SBill Paul #endif 2323ed510fb0SBill Paul RL_UNLOCK(sc); 232452732175SMax Laier return; 2325ed510fb0SBill Paul } 232652732175SMax Laier 2327a94100faSBill Paul /* Flush the TX descriptors */ 2328a94100faSBill Paul 2329a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 2330a94100faSBill Paul sc->rl_ldata.rl_tx_list_map, 2331a94100faSBill Paul BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 2332a94100faSBill Paul 2333a94100faSBill Paul sc->rl_ldata.rl_tx_prodidx = idx; 2334a94100faSBill Paul 23350fc4974fSBill Paul CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 2336a94100faSBill Paul 2337ed510fb0SBill Paul #ifdef RE_TX_MODERATION 2338a94100faSBill Paul /* 2339a94100faSBill Paul * Use the countdown timer for interrupt moderation. 2340a94100faSBill Paul * 'TX done' interrupts are disabled. Instead, we reset the 2341a94100faSBill Paul * countdown timer, which will begin counting until it hits 2342a94100faSBill Paul * the value in the TIMERINT register, and then trigger an 2343a94100faSBill Paul * interrupt. Each time we write to the TIMERCNT register, 2344a94100faSBill Paul * the timer count is reset to 0. 2345a94100faSBill Paul */ 2346a94100faSBill Paul CSR_WRITE_4(sc, RL_TIMERCNT, 1); 2347ed510fb0SBill Paul #endif 2348a94100faSBill Paul 2349a94100faSBill Paul /* 2350a94100faSBill Paul * Set a timeout in case the chip goes out to lunch. 2351a94100faSBill Paul */ 23521d545c7aSMarius Strobl sc->rl_watchdog_timer = 5; 2353ed510fb0SBill Paul 2354ed510fb0SBill Paul RL_UNLOCK(sc); 2355ed510fb0SBill Paul 2356ed510fb0SBill Paul return; 2357a94100faSBill Paul } 2358a94100faSBill Paul 2359a94100faSBill Paul static void 2360a94100faSBill Paul re_init(xsc) 2361a94100faSBill Paul void *xsc; 2362a94100faSBill Paul { 2363a94100faSBill Paul struct rl_softc *sc = xsc; 236497b9d4baSJohn-Mark Gurney 236597b9d4baSJohn-Mark Gurney RL_LOCK(sc); 236697b9d4baSJohn-Mark Gurney re_init_locked(sc); 236797b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 236897b9d4baSJohn-Mark Gurney } 236997b9d4baSJohn-Mark Gurney 237097b9d4baSJohn-Mark Gurney static void 237197b9d4baSJohn-Mark Gurney re_init_locked(sc) 237297b9d4baSJohn-Mark Gurney struct rl_softc *sc; 237397b9d4baSJohn-Mark Gurney { 2374fc74a9f9SBrooks Davis struct ifnet *ifp = sc->rl_ifp; 2375a94100faSBill Paul struct mii_data *mii; 2376a94100faSBill Paul u_int32_t rxcfg = 0; 23774d3d7085SBernd Walter union { 23784d3d7085SBernd Walter uint32_t align_dummy; 23794d3d7085SBernd Walter u_char eaddr[ETHER_ADDR_LEN]; 23804d3d7085SBernd Walter } eaddr; 2381a94100faSBill Paul 238297b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 238397b9d4baSJohn-Mark Gurney 2384a94100faSBill Paul mii = device_get_softc(sc->rl_miibus); 2385a94100faSBill Paul 2386a94100faSBill Paul /* 2387a94100faSBill Paul * Cancel pending I/O and free all RX/TX buffers. 2388a94100faSBill Paul */ 2389a94100faSBill Paul re_stop(sc); 2390a94100faSBill Paul 2391a94100faSBill Paul /* 2392c2c6548bSBill Paul * Enable C+ RX and TX mode, as well as VLAN stripping and 2393edd03374SBill Paul * RX checksum offload. We must configure the C+ register 2394c2c6548bSBill Paul * before all others. 2395c2c6548bSBill Paul */ 2396c2c6548bSBill Paul CSR_WRITE_2(sc, RL_CPLUS_CMD, RL_CPLUSCMD_RXENB| 2397c2c6548bSBill Paul RL_CPLUSCMD_TXENB|RL_CPLUSCMD_PCI_MRW| 2398ed510fb0SBill Paul RL_CPLUSCMD_VLANSTRIP|RL_CPLUSCMD_RXCSUM_ENB); 2399c2c6548bSBill Paul 2400c2c6548bSBill Paul /* 2401a94100faSBill Paul * Init our MAC address. Even though the chipset 2402a94100faSBill Paul * documentation doesn't mention it, we need to enter "Config 2403a94100faSBill Paul * register write enable" mode to modify the ID registers. 2404a94100faSBill Paul */ 24054d3d7085SBernd Walter /* Copy MAC address on stack to align. */ 24064d3d7085SBernd Walter bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN); 2407a94100faSBill Paul CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG); 2408ed510fb0SBill Paul CSR_WRITE_4(sc, RL_IDR0, 2409ed510fb0SBill Paul htole32(*(u_int32_t *)(&eaddr.eaddr[0]))); 2410ed510fb0SBill Paul CSR_WRITE_4(sc, RL_IDR4, 2411ed510fb0SBill Paul htole32(*(u_int32_t *)(&eaddr.eaddr[4]))); 2412a94100faSBill Paul CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 2413a94100faSBill Paul 2414a94100faSBill Paul /* 2415a94100faSBill Paul * For C+ mode, initialize the RX descriptors and mbufs. 2416a94100faSBill Paul */ 2417a94100faSBill Paul re_rx_list_init(sc); 2418a94100faSBill Paul re_tx_list_init(sc); 2419a94100faSBill Paul 2420a94100faSBill Paul /* 2421d01fac16SPyun YongHyeon * Load the addresses of the RX and TX lists into the chip. 2422d01fac16SPyun YongHyeon */ 2423d01fac16SPyun YongHyeon 2424d01fac16SPyun YongHyeon CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI, 2425d01fac16SPyun YongHyeon RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr)); 2426d01fac16SPyun YongHyeon CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO, 2427d01fac16SPyun YongHyeon RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr)); 2428d01fac16SPyun YongHyeon 2429d01fac16SPyun YongHyeon CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI, 2430d01fac16SPyun YongHyeon RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr)); 2431d01fac16SPyun YongHyeon CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO, 2432d01fac16SPyun YongHyeon RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr)); 2433d01fac16SPyun YongHyeon 2434d01fac16SPyun YongHyeon /* 2435a94100faSBill Paul * Enable transmit and receive. 2436a94100faSBill Paul */ 2437a94100faSBill Paul CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB); 2438a94100faSBill Paul 2439a94100faSBill Paul /* 2440a94100faSBill Paul * Set the initial TX and RX configuration. 2441a94100faSBill Paul */ 2442abc8ff44SBill Paul if (sc->rl_testmode) { 2443abc8ff44SBill Paul if (sc->rl_type == RL_8169) 2444abc8ff44SBill Paul CSR_WRITE_4(sc, RL_TXCFG, 2445abc8ff44SBill Paul RL_TXCFG_CONFIG|RL_LOOPTEST_ON); 2446a94100faSBill Paul else 2447abc8ff44SBill Paul CSR_WRITE_4(sc, RL_TXCFG, 2448abc8ff44SBill Paul RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS); 2449abc8ff44SBill Paul } else 2450a94100faSBill Paul CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG); 2451d01fac16SPyun YongHyeon 2452d01fac16SPyun YongHyeon CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16); 2453d01fac16SPyun YongHyeon 2454a94100faSBill Paul CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG); 2455a94100faSBill Paul 2456a94100faSBill Paul /* Set the individual bit to receive frames for this host only. */ 2457a94100faSBill Paul rxcfg = CSR_READ_4(sc, RL_RXCFG); 2458a94100faSBill Paul rxcfg |= RL_RXCFG_RX_INDIV; 2459a94100faSBill Paul 2460a94100faSBill Paul /* If we want promiscuous mode, set the allframes bit. */ 246161021536SJohn-Mark Gurney if (ifp->if_flags & IFF_PROMISC) 2462a94100faSBill Paul rxcfg |= RL_RXCFG_RX_ALLPHYS; 246361021536SJohn-Mark Gurney else 2464a94100faSBill Paul rxcfg &= ~RL_RXCFG_RX_ALLPHYS; 2465a94100faSBill Paul CSR_WRITE_4(sc, RL_RXCFG, rxcfg); 2466a94100faSBill Paul 2467a94100faSBill Paul /* 2468a94100faSBill Paul * Set capture broadcast bit to capture broadcast frames. 2469a94100faSBill Paul */ 247061021536SJohn-Mark Gurney if (ifp->if_flags & IFF_BROADCAST) 2471a94100faSBill Paul rxcfg |= RL_RXCFG_RX_BROAD; 247261021536SJohn-Mark Gurney else 2473a94100faSBill Paul rxcfg &= ~RL_RXCFG_RX_BROAD; 2474a94100faSBill Paul CSR_WRITE_4(sc, RL_RXCFG, rxcfg); 2475a94100faSBill Paul 2476a94100faSBill Paul /* 2477a94100faSBill Paul * Program the multicast filter, if necessary. 2478a94100faSBill Paul */ 2479a94100faSBill Paul re_setmulti(sc); 2480a94100faSBill Paul 2481a94100faSBill Paul #ifdef DEVICE_POLLING 2482a94100faSBill Paul /* 2483a94100faSBill Paul * Disable interrupts if we are polling. 2484a94100faSBill Paul */ 248540929967SGleb Smirnoff if (ifp->if_capenable & IFCAP_POLLING) 2486a94100faSBill Paul CSR_WRITE_2(sc, RL_IMR, 0); 2487a94100faSBill Paul else /* otherwise ... */ 248840929967SGleb Smirnoff #endif 2489ed510fb0SBill Paul 2490a94100faSBill Paul /* 2491a94100faSBill Paul * Enable interrupts. 2492a94100faSBill Paul */ 2493a94100faSBill Paul if (sc->rl_testmode) 2494a94100faSBill Paul CSR_WRITE_2(sc, RL_IMR, 0); 2495a94100faSBill Paul else 2496a94100faSBill Paul CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS); 2497ed510fb0SBill Paul CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS); 2498a94100faSBill Paul 2499a94100faSBill Paul /* Set initial TX threshold */ 2500a94100faSBill Paul sc->rl_txthresh = RL_TX_THRESH_INIT; 2501a94100faSBill Paul 2502a94100faSBill Paul /* Start RX/TX process. */ 2503a94100faSBill Paul CSR_WRITE_4(sc, RL_MISSEDPKT, 0); 2504a94100faSBill Paul #ifdef notdef 2505a94100faSBill Paul /* Enable receiver and transmitter. */ 2506a94100faSBill Paul CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB); 2507a94100faSBill Paul #endif 2508a94100faSBill Paul 2509ed510fb0SBill Paul #ifdef RE_TX_MODERATION 2510a94100faSBill Paul /* 2511a94100faSBill Paul * Initialize the timer interrupt register so that 2512a94100faSBill Paul * a timer interrupt will be generated once the timer 2513a94100faSBill Paul * reaches a certain number of ticks. The timer is 2514a94100faSBill Paul * reloaded on each transmit. This gives us TX interrupt 2515a94100faSBill Paul * moderation, which dramatically improves TX frame rate. 2516a94100faSBill Paul */ 2517a94100faSBill Paul if (sc->rl_type == RL_8169) 2518a94100faSBill Paul CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800); 2519a94100faSBill Paul else 2520a94100faSBill Paul CSR_WRITE_4(sc, RL_TIMERINT, 0x400); 2521ed510fb0SBill Paul #endif 2522a94100faSBill Paul 2523a94100faSBill Paul /* 2524a94100faSBill Paul * For 8169 gigE NICs, set the max allowed RX packet 2525a94100faSBill Paul * size so we can receive jumbo frames. 2526a94100faSBill Paul */ 2527a94100faSBill Paul if (sc->rl_type == RL_8169) 2528a94100faSBill Paul CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383); 2529a94100faSBill Paul 253097b9d4baSJohn-Mark Gurney if (sc->rl_testmode) 2531a94100faSBill Paul return; 2532a94100faSBill Paul 2533a94100faSBill Paul mii_mediachg(mii); 2534a94100faSBill Paul 253519ecd231SPyun YongHyeon CSR_WRITE_1(sc, RL_CFG1, CSR_READ_1(sc, RL_CFG1) | RL_CFG1_DRVLOAD); 2536a94100faSBill Paul 253713f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 253813f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2539a94100faSBill Paul 2540ed510fb0SBill Paul sc->rl_link = 0; 25411d545c7aSMarius Strobl sc->rl_watchdog_timer = 0; 2542d1754a9bSJohn Baldwin callout_reset(&sc->rl_stat_callout, hz, re_tick, sc); 2543a94100faSBill Paul } 2544a94100faSBill Paul 2545a94100faSBill Paul /* 2546a94100faSBill Paul * Set media options. 2547a94100faSBill Paul */ 2548a94100faSBill Paul static int 2549a94100faSBill Paul re_ifmedia_upd(ifp) 2550a94100faSBill Paul struct ifnet *ifp; 2551a94100faSBill Paul { 2552a94100faSBill Paul struct rl_softc *sc; 2553a94100faSBill Paul struct mii_data *mii; 2554a94100faSBill Paul 2555a94100faSBill Paul sc = ifp->if_softc; 2556a94100faSBill Paul mii = device_get_softc(sc->rl_miibus); 2557d1754a9bSJohn Baldwin RL_LOCK(sc); 2558a94100faSBill Paul mii_mediachg(mii); 2559d1754a9bSJohn Baldwin RL_UNLOCK(sc); 2560a94100faSBill Paul 2561a94100faSBill Paul return (0); 2562a94100faSBill Paul } 2563a94100faSBill Paul 2564a94100faSBill Paul /* 2565a94100faSBill Paul * Report current media status. 2566a94100faSBill Paul */ 2567a94100faSBill Paul static void 2568a94100faSBill Paul re_ifmedia_sts(ifp, ifmr) 2569a94100faSBill Paul struct ifnet *ifp; 2570a94100faSBill Paul struct ifmediareq *ifmr; 2571a94100faSBill Paul { 2572a94100faSBill Paul struct rl_softc *sc; 2573a94100faSBill Paul struct mii_data *mii; 2574a94100faSBill Paul 2575a94100faSBill Paul sc = ifp->if_softc; 2576a94100faSBill Paul mii = device_get_softc(sc->rl_miibus); 2577a94100faSBill Paul 2578d1754a9bSJohn Baldwin RL_LOCK(sc); 2579a94100faSBill Paul mii_pollstat(mii); 2580d1754a9bSJohn Baldwin RL_UNLOCK(sc); 2581a94100faSBill Paul ifmr->ifm_active = mii->mii_media_active; 2582a94100faSBill Paul ifmr->ifm_status = mii->mii_media_status; 2583a94100faSBill Paul } 2584a94100faSBill Paul 2585a94100faSBill Paul static int 2586a94100faSBill Paul re_ioctl(ifp, command, data) 2587a94100faSBill Paul struct ifnet *ifp; 2588a94100faSBill Paul u_long command; 2589a94100faSBill Paul caddr_t data; 2590a94100faSBill Paul { 2591a94100faSBill Paul struct rl_softc *sc = ifp->if_softc; 2592a94100faSBill Paul struct ifreq *ifr = (struct ifreq *) data; 2593a94100faSBill Paul struct mii_data *mii; 259440929967SGleb Smirnoff int error = 0; 2595a94100faSBill Paul 2596a94100faSBill Paul switch (command) { 2597a94100faSBill Paul case SIOCSIFMTU: 2598d1754a9bSJohn Baldwin RL_LOCK(sc); 2599a94100faSBill Paul if (ifr->ifr_mtu > RL_JUMBO_MTU) 2600a94100faSBill Paul error = EINVAL; 2601a94100faSBill Paul ifp->if_mtu = ifr->ifr_mtu; 2602d1754a9bSJohn Baldwin RL_UNLOCK(sc); 2603a94100faSBill Paul break; 2604a94100faSBill Paul case SIOCSIFFLAGS: 260597b9d4baSJohn-Mark Gurney RL_LOCK(sc); 2606eed497bbSPyun YongHyeon if ((ifp->if_flags & IFF_UP) != 0) { 2607eed497bbSPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) { 2608eed497bbSPyun YongHyeon if (((ifp->if_flags ^ sc->rl_if_flags) 2609eed497bbSPyun YongHyeon & IFF_PROMISC) != 0) 2610eed497bbSPyun YongHyeon re_setmulti(sc); 2611eed497bbSPyun YongHyeon } else 261297b9d4baSJohn-Mark Gurney re_init_locked(sc); 2613eed497bbSPyun YongHyeon } else { 2614eed497bbSPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 2615a94100faSBill Paul re_stop(sc); 2616eed497bbSPyun YongHyeon } 2617eed497bbSPyun YongHyeon sc->rl_if_flags = ifp->if_flags; 261897b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 2619a94100faSBill Paul break; 2620a94100faSBill Paul case SIOCADDMULTI: 2621a94100faSBill Paul case SIOCDELMULTI: 262297b9d4baSJohn-Mark Gurney RL_LOCK(sc); 2623a94100faSBill Paul re_setmulti(sc); 262497b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 2625a94100faSBill Paul break; 2626a94100faSBill Paul case SIOCGIFMEDIA: 2627a94100faSBill Paul case SIOCSIFMEDIA: 2628a94100faSBill Paul mii = device_get_softc(sc->rl_miibus); 2629a94100faSBill Paul error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 2630a94100faSBill Paul break; 2631a94100faSBill Paul case SIOCSIFCAP: 263240929967SGleb Smirnoff { 2633f051cb85SGleb Smirnoff int mask, reinit; 2634f051cb85SGleb Smirnoff 2635f051cb85SGleb Smirnoff mask = ifr->ifr_reqcap ^ ifp->if_capenable; 2636f051cb85SGleb Smirnoff reinit = 0; 263740929967SGleb Smirnoff #ifdef DEVICE_POLLING 263840929967SGleb Smirnoff if (mask & IFCAP_POLLING) { 263940929967SGleb Smirnoff if (ifr->ifr_reqcap & IFCAP_POLLING) { 264040929967SGleb Smirnoff error = ether_poll_register(re_poll, ifp); 264140929967SGleb Smirnoff if (error) 264240929967SGleb Smirnoff return(error); 2643d1754a9bSJohn Baldwin RL_LOCK(sc); 264440929967SGleb Smirnoff /* Disable interrupts */ 264540929967SGleb Smirnoff CSR_WRITE_2(sc, RL_IMR, 0x0000); 264640929967SGleb Smirnoff ifp->if_capenable |= IFCAP_POLLING; 264740929967SGleb Smirnoff RL_UNLOCK(sc); 264840929967SGleb Smirnoff } else { 264940929967SGleb Smirnoff error = ether_poll_deregister(ifp); 265040929967SGleb Smirnoff /* Enable interrupts. */ 265140929967SGleb Smirnoff RL_LOCK(sc); 265240929967SGleb Smirnoff CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS); 265340929967SGleb Smirnoff ifp->if_capenable &= ~IFCAP_POLLING; 265440929967SGleb Smirnoff RL_UNLOCK(sc); 265540929967SGleb Smirnoff } 265640929967SGleb Smirnoff } 265740929967SGleb Smirnoff #endif /* DEVICE_POLLING */ 265840929967SGleb Smirnoff if (mask & IFCAP_HWCSUM) { 2659f051cb85SGleb Smirnoff ifp->if_capenable ^= IFCAP_HWCSUM; 2660a94100faSBill Paul if (ifp->if_capenable & IFCAP_TXCSUM) 2661dc74159dSPyun YongHyeon ifp->if_hwassist |= RE_CSUM_FEATURES; 2662a94100faSBill Paul else 2663b61178a9SPyun YongHyeon ifp->if_hwassist &= ~RE_CSUM_FEATURES; 2664f051cb85SGleb Smirnoff reinit = 1; 266540929967SGleb Smirnoff } 2666f051cb85SGleb Smirnoff if (mask & IFCAP_VLAN_HWTAGGING) { 2667f051cb85SGleb Smirnoff ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING; 2668f051cb85SGleb Smirnoff reinit = 1; 2669f051cb85SGleb Smirnoff } 2670dc74159dSPyun YongHyeon if (mask & IFCAP_TSO4) { 2671dc74159dSPyun YongHyeon ifp->if_capenable ^= IFCAP_TSO4; 2672dc74159dSPyun YongHyeon if ((IFCAP_TSO4 & ifp->if_capenable) && 2673dc74159dSPyun YongHyeon (IFCAP_TSO4 & ifp->if_capabilities)) 2674dc74159dSPyun YongHyeon ifp->if_hwassist |= CSUM_TSO; 2675dc74159dSPyun YongHyeon else 2676dc74159dSPyun YongHyeon ifp->if_hwassist &= ~CSUM_TSO; 2677dc74159dSPyun YongHyeon } 2678f051cb85SGleb Smirnoff if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING) 2679f051cb85SGleb Smirnoff re_init(sc); 2680960fd5b3SPyun YongHyeon VLAN_CAPABILITIES(ifp); 268140929967SGleb Smirnoff } 2682a94100faSBill Paul break; 2683a94100faSBill Paul default: 2684a94100faSBill Paul error = ether_ioctl(ifp, command, data); 2685a94100faSBill Paul break; 2686a94100faSBill Paul } 2687a94100faSBill Paul 2688a94100faSBill Paul return (error); 2689a94100faSBill Paul } 2690a94100faSBill Paul 2691a94100faSBill Paul static void 26921d545c7aSMarius Strobl re_watchdog(sc) 2693a94100faSBill Paul struct rl_softc *sc; 26941d545c7aSMarius Strobl { 2695a94100faSBill Paul 26961d545c7aSMarius Strobl RL_LOCK_ASSERT(sc); 26971d545c7aSMarius Strobl 26981d545c7aSMarius Strobl if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0) 26991d545c7aSMarius Strobl return; 27001d545c7aSMarius Strobl 27011d545c7aSMarius Strobl device_printf(sc->rl_dev, "watchdog timeout\n"); 27021d545c7aSMarius Strobl sc->rl_ifp->if_oerrors++; 2703a94100faSBill Paul 2704a94100faSBill Paul re_txeof(sc); 2705a94100faSBill Paul re_rxeof(sc); 270697b9d4baSJohn-Mark Gurney re_init_locked(sc); 2707a94100faSBill Paul } 2708a94100faSBill Paul 2709a94100faSBill Paul /* 2710a94100faSBill Paul * Stop the adapter and free any mbufs allocated to the 2711a94100faSBill Paul * RX and TX lists. 2712a94100faSBill Paul */ 2713a94100faSBill Paul static void 2714a94100faSBill Paul re_stop(sc) 2715a94100faSBill Paul struct rl_softc *sc; 2716a94100faSBill Paul { 2717a94100faSBill Paul register int i; 2718a94100faSBill Paul struct ifnet *ifp; 2719a94100faSBill Paul 272097b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 272197b9d4baSJohn-Mark Gurney 2722fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 2723a94100faSBill Paul 27241d545c7aSMarius Strobl sc->rl_watchdog_timer = 0; 2725d1754a9bSJohn Baldwin callout_stop(&sc->rl_stat_callout); 272613f4c340SRobert Watson ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2727a94100faSBill Paul 2728a94100faSBill Paul CSR_WRITE_1(sc, RL_COMMAND, 0x00); 2729a94100faSBill Paul CSR_WRITE_2(sc, RL_IMR, 0x0000); 2730ed510fb0SBill Paul CSR_WRITE_2(sc, RL_ISR, 0xFFFF); 2731a94100faSBill Paul 2732a94100faSBill Paul if (sc->rl_head != NULL) { 2733a94100faSBill Paul m_freem(sc->rl_head); 2734a94100faSBill Paul sc->rl_head = sc->rl_tail = NULL; 2735a94100faSBill Paul } 2736a94100faSBill Paul 2737a94100faSBill Paul /* Free the TX list buffers. */ 2738a94100faSBill Paul 2739a94100faSBill Paul for (i = 0; i < RL_TX_DESC_CNT; i++) { 2740a94100faSBill Paul if (sc->rl_ldata.rl_tx_mbuf[i] != NULL) { 2741a94100faSBill Paul bus_dmamap_unload(sc->rl_ldata.rl_mtag, 2742a94100faSBill Paul sc->rl_ldata.rl_tx_dmamap[i]); 2743a94100faSBill Paul m_freem(sc->rl_ldata.rl_tx_mbuf[i]); 2744a94100faSBill Paul sc->rl_ldata.rl_tx_mbuf[i] = NULL; 2745a94100faSBill Paul } 2746a94100faSBill Paul } 2747a94100faSBill Paul 2748a94100faSBill Paul /* Free the RX list buffers. */ 2749a94100faSBill Paul 2750a94100faSBill Paul for (i = 0; i < RL_RX_DESC_CNT; i++) { 2751a94100faSBill Paul if (sc->rl_ldata.rl_rx_mbuf[i] != NULL) { 2752a94100faSBill Paul bus_dmamap_unload(sc->rl_ldata.rl_mtag, 2753a94100faSBill Paul sc->rl_ldata.rl_rx_dmamap[i]); 2754a94100faSBill Paul m_freem(sc->rl_ldata.rl_rx_mbuf[i]); 2755a94100faSBill Paul sc->rl_ldata.rl_rx_mbuf[i] = NULL; 2756a94100faSBill Paul } 2757a94100faSBill Paul } 2758a94100faSBill Paul } 2759a94100faSBill Paul 2760a94100faSBill Paul /* 2761a94100faSBill Paul * Device suspend routine. Stop the interface and save some PCI 2762a94100faSBill Paul * settings in case the BIOS doesn't restore them properly on 2763a94100faSBill Paul * resume. 2764a94100faSBill Paul */ 2765a94100faSBill Paul static int 2766a94100faSBill Paul re_suspend(dev) 2767a94100faSBill Paul device_t dev; 2768a94100faSBill Paul { 2769a94100faSBill Paul struct rl_softc *sc; 2770a94100faSBill Paul 2771a94100faSBill Paul sc = device_get_softc(dev); 2772a94100faSBill Paul 277397b9d4baSJohn-Mark Gurney RL_LOCK(sc); 2774a94100faSBill Paul re_stop(sc); 2775a94100faSBill Paul sc->suspended = 1; 277697b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 2777a94100faSBill Paul 2778a94100faSBill Paul return (0); 2779a94100faSBill Paul } 2780a94100faSBill Paul 2781a94100faSBill Paul /* 2782a94100faSBill Paul * Device resume routine. Restore some PCI settings in case the BIOS 2783a94100faSBill Paul * doesn't, re-enable busmastering, and restart the interface if 2784a94100faSBill Paul * appropriate. 2785a94100faSBill Paul */ 2786a94100faSBill Paul static int 2787a94100faSBill Paul re_resume(dev) 2788a94100faSBill Paul device_t dev; 2789a94100faSBill Paul { 2790a94100faSBill Paul struct rl_softc *sc; 2791a94100faSBill Paul struct ifnet *ifp; 2792a94100faSBill Paul 2793a94100faSBill Paul sc = device_get_softc(dev); 279497b9d4baSJohn-Mark Gurney 279597b9d4baSJohn-Mark Gurney RL_LOCK(sc); 279697b9d4baSJohn-Mark Gurney 2797fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 2798a94100faSBill Paul 2799a94100faSBill Paul /* reinitialize interface if necessary */ 2800a94100faSBill Paul if (ifp->if_flags & IFF_UP) 280197b9d4baSJohn-Mark Gurney re_init_locked(sc); 2802a94100faSBill Paul 2803a94100faSBill Paul sc->suspended = 0; 280497b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 2805a94100faSBill Paul 2806a94100faSBill Paul return (0); 2807a94100faSBill Paul } 2808a94100faSBill Paul 2809a94100faSBill Paul /* 2810a94100faSBill Paul * Stop all chip I/O so that the kernel's probe routines don't 2811a94100faSBill Paul * get confused by errant DMAs when rebooting. 2812a94100faSBill Paul */ 28136a087a87SPyun YongHyeon static int 2814a94100faSBill Paul re_shutdown(dev) 2815a94100faSBill Paul device_t dev; 2816a94100faSBill Paul { 2817a94100faSBill Paul struct rl_softc *sc; 2818a94100faSBill Paul 2819a94100faSBill Paul sc = device_get_softc(dev); 2820a94100faSBill Paul 282197b9d4baSJohn-Mark Gurney RL_LOCK(sc); 2822a94100faSBill Paul re_stop(sc); 2823536fde34SMaxim Sobolev /* 2824536fde34SMaxim Sobolev * Mark interface as down since otherwise we will panic if 2825536fde34SMaxim Sobolev * interrupt comes in later on, which can happen in some 282672293673SRuslan Ermilov * cases. 2827536fde34SMaxim Sobolev */ 2828536fde34SMaxim Sobolev sc->rl_ifp->if_flags &= ~IFF_UP; 282997b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 28306a087a87SPyun YongHyeon 28316a087a87SPyun YongHyeon return (0); 2832a94100faSBill Paul } 2833