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 149d65abd66SPyun YongHyeon #include <pci/if_rlreg.h> 150d65abd66SPyun YongHyeon 151a94100faSBill Paul MODULE_DEPEND(re, pci, 1, 1, 1); 152a94100faSBill Paul MODULE_DEPEND(re, ether, 1, 1, 1); 153a94100faSBill Paul MODULE_DEPEND(re, miibus, 1, 1, 1); 154a94100faSBill Paul 155298bfdf3SWarner Losh /* "device miibus" required. See GENERIC if you get errors here. */ 156a94100faSBill Paul #include "miibus_if.h" 157a94100faSBill Paul 1585774c5ffSPyun YongHyeon /* Tunables. */ 1592000cf6cSPyun YongHyeon static int msi_disable = 1; 1605774c5ffSPyun YongHyeon TUNABLE_INT("hw.re.msi_disable", &msi_disable); 1615774c5ffSPyun YongHyeon 162a94100faSBill Paul #define RE_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP) 163a94100faSBill Paul 164a94100faSBill Paul /* 165a94100faSBill Paul * Various supported device vendors/types and their names. 166a94100faSBill Paul */ 167a94100faSBill Paul static struct rl_type re_devs[] = { 1689dfcacbeSPyun YongHyeon { DLINK_VENDORID, DLINK_DEVICEID_528T, 0, 16932aa5f0eSAnton Berezin "D-Link DGE-528(T) Gigabit Ethernet Adapter" }, 1709dfcacbeSPyun YongHyeon { RT_VENDORID, RT_DEVICEID_8139, 0, 171a94100faSBill Paul "RealTek 8139C+ 10/100BaseTX" }, 1729dfcacbeSPyun YongHyeon { RT_VENDORID, RT_DEVICEID_8101E, 0, 173ed510fb0SBill Paul "RealTek 8101E PCIe 10/100baseTX" }, 1749dfcacbeSPyun YongHyeon { RT_VENDORID, RT_DEVICEID_8168, 0, 1759dfcacbeSPyun YongHyeon "RealTek 8168/8168B/8111B PCIe Gigabit Ethernet" }, 1769dfcacbeSPyun YongHyeon { RT_VENDORID, RT_DEVICEID_8169, 0, 1779dfcacbeSPyun YongHyeon "RealTek 8169/8169S/8169SB/8110S/8110SB Gigabit Ethernet" }, 1789dfcacbeSPyun YongHyeon { RT_VENDORID, RT_DEVICEID_8169SC, 0, 1792ee2c3b4SRemko Lodder "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" }, 1809dfcacbeSPyun YongHyeon { COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, 0, 181ea263191SMIHIRA Sanpei Yoshiro "Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" }, 1829dfcacbeSPyun YongHyeon { LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, 0, 18326390635SJohn Baldwin "Linksys EG1032 (RTL8169S) Gigabit Ethernet" }, 1849dfcacbeSPyun YongHyeon { USR_VENDORID, USR_DEVICEID_997902, 0, 185dfdb409eSPyun YongHyeon "US Robotics 997902 (RTL8169S) Gigabit Ethernet" } 186a94100faSBill Paul }; 187a94100faSBill Paul 188a94100faSBill Paul static struct rl_hwrev re_hwrevs[] = { 189a94100faSBill Paul { RL_HWREV_8139, RL_8139, "" }, 190a94100faSBill Paul { RL_HWREV_8139A, RL_8139, "A" }, 191a94100faSBill Paul { RL_HWREV_8139AG, RL_8139, "A-G" }, 192a94100faSBill Paul { RL_HWREV_8139B, RL_8139, "B" }, 193a94100faSBill Paul { RL_HWREV_8130, RL_8139, "8130" }, 194a94100faSBill Paul { RL_HWREV_8139C, RL_8139, "C" }, 195a94100faSBill Paul { RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C" }, 196a94100faSBill Paul { RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+"}, 197498bd0d3SBill Paul { RL_HWREV_8168_SPIN1, RL_8169, "8168"}, 198a94100faSBill Paul { RL_HWREV_8169, RL_8169, "8169"}, 19969a6b7fbSBill Paul { RL_HWREV_8169S, RL_8169, "8169S"}, 20069a6b7fbSBill Paul { RL_HWREV_8110S, RL_8169, "8110S"}, 201ed510fb0SBill Paul { RL_HWREV_8169_8110SB, RL_8169, "8169SB"}, 202ed510fb0SBill Paul { RL_HWREV_8169_8110SC, RL_8169, "8169SC"}, 203a94100faSBill Paul { RL_HWREV_8100, RL_8139, "8100"}, 204a94100faSBill Paul { RL_HWREV_8101, RL_8139, "8101"}, 205ed510fb0SBill Paul { RL_HWREV_8100E, RL_8169, "8100E"}, 206ed510fb0SBill Paul { RL_HWREV_8101E, RL_8169, "8101E"}, 207498bd0d3SBill Paul { RL_HWREV_8168_SPIN2, RL_8169, "8168"}, 2081acbb78aSPyun YongHyeon { RL_HWREV_8168_SPIN3, RL_8169, "8168"}, 209a94100faSBill Paul { 0, 0, NULL } 210a94100faSBill Paul }; 211a94100faSBill Paul 212a94100faSBill Paul static int re_probe (device_t); 213a94100faSBill Paul static int re_attach (device_t); 214a94100faSBill Paul static int re_detach (device_t); 215a94100faSBill Paul 216d65abd66SPyun YongHyeon static int re_encap (struct rl_softc *, struct mbuf **); 217a94100faSBill Paul 218a94100faSBill Paul static void re_dma_map_addr (void *, bus_dma_segment_t *, int, int); 219a94100faSBill Paul static int re_allocmem (device_t, struct rl_softc *); 220d65abd66SPyun YongHyeon static __inline void re_discard_rxbuf 221d65abd66SPyun YongHyeon (struct rl_softc *, int); 222d65abd66SPyun YongHyeon static int re_newbuf (struct rl_softc *, int); 223a94100faSBill Paul static int re_rx_list_init (struct rl_softc *); 224a94100faSBill Paul static int re_tx_list_init (struct rl_softc *); 22522a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX 22622a11c96SJohn-Mark Gurney static __inline void re_fixup_rx 22722a11c96SJohn-Mark Gurney (struct mbuf *); 22822a11c96SJohn-Mark Gurney #endif 229ed510fb0SBill Paul static int re_rxeof (struct rl_softc *); 230a94100faSBill Paul static void re_txeof (struct rl_softc *); 23197b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING 2320187838bSRuslan Ermilov static void re_poll (struct ifnet *, enum poll_cmd, int); 2330187838bSRuslan Ermilov static void re_poll_locked (struct ifnet *, enum poll_cmd, int); 23497b9d4baSJohn-Mark Gurney #endif 235ef544f63SPaolo Pisati static int re_intr (void *); 236a94100faSBill Paul static void re_tick (void *); 237ed510fb0SBill Paul static void re_tx_task (void *, int); 238ed510fb0SBill Paul static void re_int_task (void *, int); 239a94100faSBill Paul static void re_start (struct ifnet *); 240a94100faSBill Paul static int re_ioctl (struct ifnet *, u_long, caddr_t); 241a94100faSBill Paul static void re_init (void *); 24297b9d4baSJohn-Mark Gurney static void re_init_locked (struct rl_softc *); 243a94100faSBill Paul static void re_stop (struct rl_softc *); 2441d545c7aSMarius Strobl static void re_watchdog (struct rl_softc *); 245a94100faSBill Paul static int re_suspend (device_t); 246a94100faSBill Paul static int re_resume (device_t); 2476a087a87SPyun YongHyeon static int re_shutdown (device_t); 248a94100faSBill Paul static int re_ifmedia_upd (struct ifnet *); 249a94100faSBill Paul static void re_ifmedia_sts (struct ifnet *, struct ifmediareq *); 250a94100faSBill Paul 251a94100faSBill Paul static void re_eeprom_putbyte (struct rl_softc *, int); 252a94100faSBill Paul static void re_eeprom_getword (struct rl_softc *, int, u_int16_t *); 253ed510fb0SBill Paul static void re_read_eeprom (struct rl_softc *, caddr_t, int, int); 254a94100faSBill Paul static int re_gmii_readreg (device_t, int, int); 255a94100faSBill Paul static int re_gmii_writereg (device_t, int, int, int); 256a94100faSBill Paul 257a94100faSBill Paul static int re_miibus_readreg (device_t, int, int); 258a94100faSBill Paul static int re_miibus_writereg (device_t, int, int, int); 259a94100faSBill Paul static void re_miibus_statchg (device_t); 260a94100faSBill Paul 261a94100faSBill Paul static void re_setmulti (struct rl_softc *); 262a94100faSBill Paul static void re_reset (struct rl_softc *); 2637467bd53SPyun YongHyeon static void re_setwol (struct rl_softc *); 2647467bd53SPyun YongHyeon static void re_clrwol (struct rl_softc *); 265a94100faSBill Paul 266ed510fb0SBill Paul #ifdef RE_DIAG 267a94100faSBill Paul static int re_diag (struct rl_softc *); 268ed510fb0SBill Paul #endif 269a94100faSBill Paul 270a94100faSBill Paul static device_method_t re_methods[] = { 271a94100faSBill Paul /* Device interface */ 272a94100faSBill Paul DEVMETHOD(device_probe, re_probe), 273a94100faSBill Paul DEVMETHOD(device_attach, re_attach), 274a94100faSBill Paul DEVMETHOD(device_detach, re_detach), 275a94100faSBill Paul DEVMETHOD(device_suspend, re_suspend), 276a94100faSBill Paul DEVMETHOD(device_resume, re_resume), 277a94100faSBill Paul DEVMETHOD(device_shutdown, re_shutdown), 278a94100faSBill Paul 279a94100faSBill Paul /* bus interface */ 280a94100faSBill Paul DEVMETHOD(bus_print_child, bus_generic_print_child), 281a94100faSBill Paul DEVMETHOD(bus_driver_added, bus_generic_driver_added), 282a94100faSBill Paul 283a94100faSBill Paul /* MII interface */ 284a94100faSBill Paul DEVMETHOD(miibus_readreg, re_miibus_readreg), 285a94100faSBill Paul DEVMETHOD(miibus_writereg, re_miibus_writereg), 286a94100faSBill Paul DEVMETHOD(miibus_statchg, re_miibus_statchg), 287a94100faSBill Paul 288a94100faSBill Paul { 0, 0 } 289a94100faSBill Paul }; 290a94100faSBill Paul 291a94100faSBill Paul static driver_t re_driver = { 292a94100faSBill Paul "re", 293a94100faSBill Paul re_methods, 294a94100faSBill Paul sizeof(struct rl_softc) 295a94100faSBill Paul }; 296a94100faSBill Paul 297a94100faSBill Paul static devclass_t re_devclass; 298a94100faSBill Paul 299a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0); 300347934faSWarner Losh DRIVER_MODULE(re, cardbus, re_driver, re_devclass, 0, 0); 301a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0); 302a94100faSBill Paul 303a94100faSBill Paul #define EE_SET(x) \ 304a94100faSBill Paul CSR_WRITE_1(sc, RL_EECMD, \ 305a94100faSBill Paul CSR_READ_1(sc, RL_EECMD) | x) 306a94100faSBill Paul 307a94100faSBill Paul #define EE_CLR(x) \ 308a94100faSBill Paul CSR_WRITE_1(sc, RL_EECMD, \ 309a94100faSBill Paul CSR_READ_1(sc, RL_EECMD) & ~x) 310a94100faSBill Paul 311a94100faSBill Paul /* 312a94100faSBill Paul * Send a read command and address to the EEPROM, check for ACK. 313a94100faSBill Paul */ 314a94100faSBill Paul static void 315a94100faSBill Paul re_eeprom_putbyte(sc, addr) 316a94100faSBill Paul struct rl_softc *sc; 317a94100faSBill Paul int addr; 318a94100faSBill Paul { 319a94100faSBill Paul register int d, i; 320a94100faSBill Paul 321ed510fb0SBill Paul d = addr | (RL_9346_READ << sc->rl_eewidth); 322a94100faSBill Paul 323a94100faSBill Paul /* 324a94100faSBill Paul * Feed in each bit and strobe the clock. 325a94100faSBill Paul */ 326ed510fb0SBill Paul 327ed510fb0SBill Paul for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) { 328a94100faSBill Paul if (d & i) { 329a94100faSBill Paul EE_SET(RL_EE_DATAIN); 330a94100faSBill Paul } else { 331a94100faSBill Paul EE_CLR(RL_EE_DATAIN); 332a94100faSBill Paul } 333a94100faSBill Paul DELAY(100); 334a94100faSBill Paul EE_SET(RL_EE_CLK); 335a94100faSBill Paul DELAY(150); 336a94100faSBill Paul EE_CLR(RL_EE_CLK); 337a94100faSBill Paul DELAY(100); 338a94100faSBill Paul } 339ed510fb0SBill Paul 340ed510fb0SBill Paul return; 341a94100faSBill Paul } 342a94100faSBill Paul 343a94100faSBill Paul /* 344a94100faSBill Paul * Read a word of data stored in the EEPROM at address 'addr.' 345a94100faSBill Paul */ 346a94100faSBill Paul static void 347a94100faSBill Paul re_eeprom_getword(sc, addr, dest) 348a94100faSBill Paul struct rl_softc *sc; 349a94100faSBill Paul int addr; 350a94100faSBill Paul u_int16_t *dest; 351a94100faSBill Paul { 352a94100faSBill Paul register int i; 353a94100faSBill Paul u_int16_t word = 0; 354a94100faSBill Paul 355a94100faSBill Paul /* 356a94100faSBill Paul * Send address of word we want to read. 357a94100faSBill Paul */ 358a94100faSBill Paul re_eeprom_putbyte(sc, addr); 359a94100faSBill Paul 360a94100faSBill Paul /* 361a94100faSBill Paul * Start reading bits from EEPROM. 362a94100faSBill Paul */ 363a94100faSBill Paul for (i = 0x8000; i; i >>= 1) { 364a94100faSBill Paul EE_SET(RL_EE_CLK); 365a94100faSBill Paul DELAY(100); 366a94100faSBill Paul if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT) 367a94100faSBill Paul word |= i; 368a94100faSBill Paul EE_CLR(RL_EE_CLK); 369a94100faSBill Paul DELAY(100); 370a94100faSBill Paul } 371a94100faSBill Paul 372a94100faSBill Paul *dest = word; 373ed510fb0SBill Paul 374ed510fb0SBill Paul return; 375a94100faSBill Paul } 376a94100faSBill Paul 377a94100faSBill Paul /* 378a94100faSBill Paul * Read a sequence of words from the EEPROM. 379a94100faSBill Paul */ 380a94100faSBill Paul static void 381ed510fb0SBill Paul re_read_eeprom(sc, dest, off, cnt) 382a94100faSBill Paul struct rl_softc *sc; 383a94100faSBill Paul caddr_t dest; 384a94100faSBill Paul int off; 385a94100faSBill Paul int cnt; 386a94100faSBill Paul { 387a94100faSBill Paul int i; 388a94100faSBill Paul u_int16_t word = 0, *ptr; 389a94100faSBill Paul 390ed510fb0SBill Paul CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM); 391ed510fb0SBill Paul 392ed510fb0SBill Paul DELAY(100); 393ed510fb0SBill Paul 394a94100faSBill Paul for (i = 0; i < cnt; i++) { 395ed510fb0SBill Paul CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL); 396a94100faSBill Paul re_eeprom_getword(sc, off + i, &word); 397ed510fb0SBill Paul CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL); 398a94100faSBill Paul ptr = (u_int16_t *)(dest + (i * 2)); 399be099007SPyun YongHyeon *ptr = word; 400a94100faSBill Paul } 401ed510fb0SBill Paul 402ed510fb0SBill Paul CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM); 403ed510fb0SBill Paul 404ed510fb0SBill Paul return; 405a94100faSBill Paul } 406a94100faSBill Paul 407a94100faSBill Paul static int 408a94100faSBill Paul re_gmii_readreg(dev, phy, reg) 409a94100faSBill Paul device_t dev; 410a94100faSBill Paul int phy, reg; 411a94100faSBill Paul { 412a94100faSBill Paul struct rl_softc *sc; 413a94100faSBill Paul u_int32_t rval; 414a94100faSBill Paul int i; 415a94100faSBill Paul 416a94100faSBill Paul if (phy != 1) 417a94100faSBill Paul return (0); 418a94100faSBill Paul 419a94100faSBill Paul sc = device_get_softc(dev); 420a94100faSBill Paul 4219bac70b8SBill Paul /* Let the rgephy driver read the GMEDIASTAT register */ 4229bac70b8SBill Paul 4239bac70b8SBill Paul if (reg == RL_GMEDIASTAT) { 4249bac70b8SBill Paul rval = CSR_READ_1(sc, RL_GMEDIASTAT); 4259bac70b8SBill Paul return (rval); 4269bac70b8SBill Paul } 4279bac70b8SBill Paul 428a94100faSBill Paul CSR_WRITE_4(sc, RL_PHYAR, reg << 16); 429a94100faSBill Paul DELAY(1000); 430a94100faSBill Paul 431a94100faSBill Paul for (i = 0; i < RL_TIMEOUT; i++) { 432a94100faSBill Paul rval = CSR_READ_4(sc, RL_PHYAR); 433a94100faSBill Paul if (rval & RL_PHYAR_BUSY) 434a94100faSBill Paul break; 435a94100faSBill Paul DELAY(100); 436a94100faSBill Paul } 437a94100faSBill Paul 438a94100faSBill Paul if (i == RL_TIMEOUT) { 4396b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "PHY read failed\n"); 440a94100faSBill Paul return (0); 441a94100faSBill Paul } 442a94100faSBill Paul 443a94100faSBill Paul return (rval & RL_PHYAR_PHYDATA); 444a94100faSBill Paul } 445a94100faSBill Paul 446a94100faSBill Paul static int 447a94100faSBill Paul re_gmii_writereg(dev, phy, reg, data) 448a94100faSBill Paul device_t dev; 449a94100faSBill Paul int phy, reg, data; 450a94100faSBill Paul { 451a94100faSBill Paul struct rl_softc *sc; 452a94100faSBill Paul u_int32_t rval; 453a94100faSBill Paul int i; 454a94100faSBill Paul 455a94100faSBill Paul sc = device_get_softc(dev); 456a94100faSBill Paul 457a94100faSBill Paul CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) | 4589bac70b8SBill Paul (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY); 459a94100faSBill Paul DELAY(1000); 460a94100faSBill Paul 461a94100faSBill Paul for (i = 0; i < RL_TIMEOUT; i++) { 462a94100faSBill Paul rval = CSR_READ_4(sc, RL_PHYAR); 463a94100faSBill Paul if (!(rval & RL_PHYAR_BUSY)) 464a94100faSBill Paul break; 465a94100faSBill Paul DELAY(100); 466a94100faSBill Paul } 467a94100faSBill Paul 468a94100faSBill Paul if (i == RL_TIMEOUT) { 4696b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "PHY write failed\n"); 470a94100faSBill Paul return (0); 471a94100faSBill Paul } 472a94100faSBill Paul 473a94100faSBill Paul return (0); 474a94100faSBill Paul } 475a94100faSBill Paul 476a94100faSBill Paul static int 477a94100faSBill Paul re_miibus_readreg(dev, phy, reg) 478a94100faSBill Paul device_t dev; 479a94100faSBill Paul int phy, reg; 480a94100faSBill Paul { 481a94100faSBill Paul struct rl_softc *sc; 482a94100faSBill Paul u_int16_t rval = 0; 483a94100faSBill Paul u_int16_t re8139_reg = 0; 484a94100faSBill Paul 485a94100faSBill Paul sc = device_get_softc(dev); 486a94100faSBill Paul 487a94100faSBill Paul if (sc->rl_type == RL_8169) { 488a94100faSBill Paul rval = re_gmii_readreg(dev, phy, reg); 489a94100faSBill Paul return (rval); 490a94100faSBill Paul } 491a94100faSBill Paul 492a94100faSBill Paul /* Pretend the internal PHY is only at address 0 */ 493a94100faSBill Paul if (phy) { 494a94100faSBill Paul return (0); 495a94100faSBill Paul } 496a94100faSBill Paul switch (reg) { 497a94100faSBill Paul case MII_BMCR: 498a94100faSBill Paul re8139_reg = RL_BMCR; 499a94100faSBill Paul break; 500a94100faSBill Paul case MII_BMSR: 501a94100faSBill Paul re8139_reg = RL_BMSR; 502a94100faSBill Paul break; 503a94100faSBill Paul case MII_ANAR: 504a94100faSBill Paul re8139_reg = RL_ANAR; 505a94100faSBill Paul break; 506a94100faSBill Paul case MII_ANER: 507a94100faSBill Paul re8139_reg = RL_ANER; 508a94100faSBill Paul break; 509a94100faSBill Paul case MII_ANLPAR: 510a94100faSBill Paul re8139_reg = RL_LPAR; 511a94100faSBill Paul break; 512a94100faSBill Paul case MII_PHYIDR1: 513a94100faSBill Paul case MII_PHYIDR2: 514a94100faSBill Paul return (0); 515a94100faSBill Paul /* 516a94100faSBill Paul * Allow the rlphy driver to read the media status 517a94100faSBill Paul * register. If we have a link partner which does not 518a94100faSBill Paul * support NWAY, this is the register which will tell 519a94100faSBill Paul * us the results of parallel detection. 520a94100faSBill Paul */ 521a94100faSBill Paul case RL_MEDIASTAT: 522a94100faSBill Paul rval = CSR_READ_1(sc, RL_MEDIASTAT); 523a94100faSBill Paul return (rval); 524a94100faSBill Paul default: 5256b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "bad phy register\n"); 526a94100faSBill Paul return (0); 527a94100faSBill Paul } 528a94100faSBill Paul rval = CSR_READ_2(sc, re8139_reg); 529baa12772SPyun YongHyeon if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) { 530baa12772SPyun YongHyeon /* 8139C+ has different bit layout. */ 531baa12772SPyun YongHyeon rval &= ~(BMCR_LOOP | BMCR_ISO); 532baa12772SPyun YongHyeon } 533a94100faSBill Paul return (rval); 534a94100faSBill Paul } 535a94100faSBill Paul 536a94100faSBill Paul static int 537a94100faSBill Paul re_miibus_writereg(dev, phy, reg, data) 538a94100faSBill Paul device_t dev; 539a94100faSBill Paul int phy, reg, data; 540a94100faSBill Paul { 541a94100faSBill Paul struct rl_softc *sc; 542a94100faSBill Paul u_int16_t re8139_reg = 0; 543a94100faSBill Paul int rval = 0; 544a94100faSBill Paul 545a94100faSBill Paul sc = device_get_softc(dev); 546a94100faSBill Paul 547a94100faSBill Paul if (sc->rl_type == RL_8169) { 548a94100faSBill Paul rval = re_gmii_writereg(dev, phy, reg, data); 549a94100faSBill Paul return (rval); 550a94100faSBill Paul } 551a94100faSBill Paul 552a94100faSBill Paul /* Pretend the internal PHY is only at address 0 */ 55397b9d4baSJohn-Mark Gurney if (phy) 554a94100faSBill Paul return (0); 55597b9d4baSJohn-Mark Gurney 556a94100faSBill Paul switch (reg) { 557a94100faSBill Paul case MII_BMCR: 558a94100faSBill Paul re8139_reg = RL_BMCR; 559baa12772SPyun YongHyeon if (sc->rl_type == RL_8139CPLUS) { 560baa12772SPyun YongHyeon /* 8139C+ has different bit layout. */ 561baa12772SPyun YongHyeon data &= ~(BMCR_LOOP | BMCR_ISO); 562baa12772SPyun YongHyeon } 563a94100faSBill Paul break; 564a94100faSBill Paul case MII_BMSR: 565a94100faSBill Paul re8139_reg = RL_BMSR; 566a94100faSBill Paul break; 567a94100faSBill Paul case MII_ANAR: 568a94100faSBill Paul re8139_reg = RL_ANAR; 569a94100faSBill Paul break; 570a94100faSBill Paul case MII_ANER: 571a94100faSBill Paul re8139_reg = RL_ANER; 572a94100faSBill Paul break; 573a94100faSBill Paul case MII_ANLPAR: 574a94100faSBill Paul re8139_reg = RL_LPAR; 575a94100faSBill Paul break; 576a94100faSBill Paul case MII_PHYIDR1: 577a94100faSBill Paul case MII_PHYIDR2: 578a94100faSBill Paul return (0); 579a94100faSBill Paul break; 580a94100faSBill Paul default: 5816b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "bad phy register\n"); 582a94100faSBill Paul return (0); 583a94100faSBill Paul } 584a94100faSBill Paul CSR_WRITE_2(sc, re8139_reg, data); 585a94100faSBill Paul return (0); 586a94100faSBill Paul } 587a94100faSBill Paul 588a94100faSBill Paul static void 589a94100faSBill Paul re_miibus_statchg(dev) 590a94100faSBill Paul device_t dev; 591a94100faSBill Paul { 592a11e2f18SBruce M Simpson 593a94100faSBill Paul } 594a94100faSBill Paul 595a94100faSBill Paul /* 596a94100faSBill Paul * Program the 64-bit multicast hash filter. 597a94100faSBill Paul */ 598a94100faSBill Paul static void 599a94100faSBill Paul re_setmulti(sc) 600a94100faSBill Paul struct rl_softc *sc; 601a94100faSBill Paul { 602a94100faSBill Paul struct ifnet *ifp; 603a94100faSBill Paul int h = 0; 604a94100faSBill Paul u_int32_t hashes[2] = { 0, 0 }; 605a94100faSBill Paul struct ifmultiaddr *ifma; 606a94100faSBill Paul u_int32_t rxfilt; 607a94100faSBill Paul int mcnt = 0; 608a94100faSBill Paul 60997b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 61097b9d4baSJohn-Mark Gurney 611fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 612a94100faSBill Paul 613a94100faSBill Paul 6147c103000SPyun YongHyeon rxfilt = CSR_READ_4(sc, RL_RXCFG); 6157c103000SPyun YongHyeon rxfilt &= ~(RL_RXCFG_RX_ALLPHYS | RL_RXCFG_RX_MULTI); 616a94100faSBill Paul if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 6177c103000SPyun YongHyeon if (ifp->if_flags & IFF_PROMISC) 6187c103000SPyun YongHyeon rxfilt |= RL_RXCFG_RX_ALLPHYS; 619a0637caaSPyun YongHyeon /* 620a0637caaSPyun YongHyeon * Unlike other hardwares, we have to explicitly set 621a0637caaSPyun YongHyeon * RL_RXCFG_RX_MULTI to receive multicast frames in 622a0637caaSPyun YongHyeon * promiscuous mode. 623a0637caaSPyun YongHyeon */ 624a94100faSBill Paul rxfilt |= RL_RXCFG_RX_MULTI; 625a94100faSBill Paul CSR_WRITE_4(sc, RL_RXCFG, rxfilt); 626a94100faSBill Paul CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF); 627a94100faSBill Paul CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF); 628a94100faSBill Paul return; 629a94100faSBill Paul } 630a94100faSBill Paul 631a94100faSBill Paul /* first, zot all the existing hash bits */ 632a94100faSBill Paul CSR_WRITE_4(sc, RL_MAR0, 0); 633a94100faSBill Paul CSR_WRITE_4(sc, RL_MAR4, 0); 634a94100faSBill Paul 635a94100faSBill Paul /* now program new ones */ 63613b203d0SRobert Watson IF_ADDR_LOCK(ifp); 637a94100faSBill Paul TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 638a94100faSBill Paul if (ifma->ifma_addr->sa_family != AF_LINK) 639a94100faSBill Paul continue; 6400e939c0cSChristian Weisgerber h = ether_crc32_be(LLADDR((struct sockaddr_dl *) 6410e939c0cSChristian Weisgerber ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 642a94100faSBill Paul if (h < 32) 643a94100faSBill Paul hashes[0] |= (1 << h); 644a94100faSBill Paul else 645a94100faSBill Paul hashes[1] |= (1 << (h - 32)); 646a94100faSBill Paul mcnt++; 647a94100faSBill Paul } 64813b203d0SRobert Watson IF_ADDR_UNLOCK(ifp); 649a94100faSBill Paul 650a94100faSBill Paul if (mcnt) 651a94100faSBill Paul rxfilt |= RL_RXCFG_RX_MULTI; 652a94100faSBill Paul else 653a94100faSBill Paul rxfilt &= ~RL_RXCFG_RX_MULTI; 654a94100faSBill Paul 655a94100faSBill Paul CSR_WRITE_4(sc, RL_RXCFG, rxfilt); 656bb7dfefbSBill Paul 657bb7dfefbSBill Paul /* 658bb7dfefbSBill Paul * For some unfathomable reason, RealTek decided to reverse 659bb7dfefbSBill Paul * the order of the multicast hash registers in the PCI Express 660bb7dfefbSBill Paul * parts. This means we have to write the hash pattern in reverse 661bb7dfefbSBill Paul * order for those devices. 662bb7dfefbSBill Paul */ 663bb7dfefbSBill Paul 664351a76f9SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_INVMAR) != 0) { 665bb7dfefbSBill Paul CSR_WRITE_4(sc, RL_MAR0, bswap32(hashes[1])); 666bb7dfefbSBill Paul CSR_WRITE_4(sc, RL_MAR4, bswap32(hashes[0])); 667351a76f9SPyun YongHyeon } else { 668a94100faSBill Paul CSR_WRITE_4(sc, RL_MAR0, hashes[0]); 669a94100faSBill Paul CSR_WRITE_4(sc, RL_MAR4, hashes[1]); 670a94100faSBill Paul } 671bb7dfefbSBill Paul } 672a94100faSBill Paul 673a94100faSBill Paul static void 674a94100faSBill Paul re_reset(sc) 675a94100faSBill Paul struct rl_softc *sc; 676a94100faSBill Paul { 677a94100faSBill Paul register int i; 678a94100faSBill Paul 67997b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 68097b9d4baSJohn-Mark Gurney 681a94100faSBill Paul CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET); 682a94100faSBill Paul 683a94100faSBill Paul for (i = 0; i < RL_TIMEOUT; i++) { 684a94100faSBill Paul DELAY(10); 685a94100faSBill Paul if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET)) 686a94100faSBill Paul break; 687a94100faSBill Paul } 688a94100faSBill Paul if (i == RL_TIMEOUT) 6896b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "reset never completed!\n"); 690a94100faSBill Paul 691a94100faSBill Paul CSR_WRITE_1(sc, 0x82, 1); 692a94100faSBill Paul } 693a94100faSBill Paul 694ed510fb0SBill Paul #ifdef RE_DIAG 695ed510fb0SBill Paul 696a94100faSBill Paul /* 697a94100faSBill Paul * The following routine is designed to test for a defect on some 698a94100faSBill Paul * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64# 699a94100faSBill Paul * lines connected to the bus, however for a 32-bit only card, they 700a94100faSBill Paul * should be pulled high. The result of this defect is that the 701a94100faSBill Paul * NIC will not work right if you plug it into a 64-bit slot: DMA 702a94100faSBill Paul * operations will be done with 64-bit transfers, which will fail 703a94100faSBill Paul * because the 64-bit data lines aren't connected. 704a94100faSBill Paul * 705a94100faSBill Paul * There's no way to work around this (short of talking a soldering 706a94100faSBill Paul * iron to the board), however we can detect it. The method we use 707a94100faSBill Paul * here is to put the NIC into digital loopback mode, set the receiver 708a94100faSBill Paul * to promiscuous mode, and then try to send a frame. We then compare 709a94100faSBill Paul * the frame data we sent to what was received. If the data matches, 710a94100faSBill Paul * then the NIC is working correctly, otherwise we know the user has 711a94100faSBill Paul * a defective NIC which has been mistakenly plugged into a 64-bit PCI 712a94100faSBill Paul * slot. In the latter case, there's no way the NIC can work correctly, 713a94100faSBill Paul * so we print out a message on the console and abort the device attach. 714a94100faSBill Paul */ 715a94100faSBill Paul 716a94100faSBill Paul static int 717a94100faSBill Paul re_diag(sc) 718a94100faSBill Paul struct rl_softc *sc; 719a94100faSBill Paul { 720fc74a9f9SBrooks Davis struct ifnet *ifp = sc->rl_ifp; 721a94100faSBill Paul struct mbuf *m0; 722a94100faSBill Paul struct ether_header *eh; 723a94100faSBill Paul struct rl_desc *cur_rx; 724a94100faSBill Paul u_int16_t status; 725a94100faSBill Paul u_int32_t rxstat; 726ed510fb0SBill Paul int total_len, i, error = 0, phyaddr; 727a94100faSBill Paul u_int8_t dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' }; 728a94100faSBill Paul u_int8_t src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' }; 729a94100faSBill Paul 730a94100faSBill Paul /* Allocate a single mbuf */ 731a94100faSBill Paul MGETHDR(m0, M_DONTWAIT, MT_DATA); 732a94100faSBill Paul if (m0 == NULL) 733a94100faSBill Paul return (ENOBUFS); 734a94100faSBill Paul 73597b9d4baSJohn-Mark Gurney RL_LOCK(sc); 73697b9d4baSJohn-Mark Gurney 737a94100faSBill Paul /* 738a94100faSBill Paul * Initialize the NIC in test mode. This sets the chip up 739a94100faSBill Paul * so that it can send and receive frames, but performs the 740a94100faSBill Paul * following special functions: 741a94100faSBill Paul * - Puts receiver in promiscuous mode 742a94100faSBill Paul * - Enables digital loopback mode 743a94100faSBill Paul * - Leaves interrupts turned off 744a94100faSBill Paul */ 745a94100faSBill Paul 746a94100faSBill Paul ifp->if_flags |= IFF_PROMISC; 747a94100faSBill Paul sc->rl_testmode = 1; 748ed510fb0SBill Paul re_reset(sc); 74997b9d4baSJohn-Mark Gurney re_init_locked(sc); 750351a76f9SPyun YongHyeon sc->rl_flags |= RL_FLAG_LINK; 751ed510fb0SBill Paul if (sc->rl_type == RL_8169) 752ed510fb0SBill Paul phyaddr = 1; 753ed510fb0SBill Paul else 754ed510fb0SBill Paul phyaddr = 0; 755ed510fb0SBill Paul 756ed510fb0SBill Paul re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET); 757ed510fb0SBill Paul for (i = 0; i < RL_TIMEOUT; i++) { 758ed510fb0SBill Paul status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR); 759ed510fb0SBill Paul if (!(status & BMCR_RESET)) 760ed510fb0SBill Paul break; 761ed510fb0SBill Paul } 762ed510fb0SBill Paul 763ed510fb0SBill Paul re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP); 764ed510fb0SBill Paul CSR_WRITE_2(sc, RL_ISR, RL_INTRS); 765ed510fb0SBill Paul 766804af9a1SBill Paul DELAY(100000); 767a94100faSBill Paul 768a94100faSBill Paul /* Put some data in the mbuf */ 769a94100faSBill Paul 770a94100faSBill Paul eh = mtod(m0, struct ether_header *); 771a94100faSBill Paul bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN); 772a94100faSBill Paul bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN); 773a94100faSBill Paul eh->ether_type = htons(ETHERTYPE_IP); 774a94100faSBill Paul m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN; 775a94100faSBill Paul 7767cae6651SBill Paul /* 7777cae6651SBill Paul * Queue the packet, start transmission. 7787cae6651SBill Paul * Note: IF_HANDOFF() ultimately calls re_start() for us. 7797cae6651SBill Paul */ 780a94100faSBill Paul 781abc8ff44SBill Paul CSR_WRITE_2(sc, RL_ISR, 0xFFFF); 78297b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 78352732175SMax Laier /* XXX: re_diag must not be called when in ALTQ mode */ 7847cae6651SBill Paul IF_HANDOFF(&ifp->if_snd, m0, ifp); 78597b9d4baSJohn-Mark Gurney RL_LOCK(sc); 786a94100faSBill Paul m0 = NULL; 787a94100faSBill Paul 788a94100faSBill Paul /* Wait for it to propagate through the chip */ 789a94100faSBill Paul 790abc8ff44SBill Paul DELAY(100000); 791a94100faSBill Paul for (i = 0; i < RL_TIMEOUT; i++) { 792a94100faSBill Paul status = CSR_READ_2(sc, RL_ISR); 793ed510fb0SBill Paul CSR_WRITE_2(sc, RL_ISR, status); 794abc8ff44SBill Paul if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) == 795abc8ff44SBill Paul (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) 796a94100faSBill Paul break; 797a94100faSBill Paul DELAY(10); 798a94100faSBill Paul } 799a94100faSBill Paul 800a94100faSBill Paul if (i == RL_TIMEOUT) { 8016b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, 8026b9f5c94SGleb Smirnoff "diagnostic failed, failed to receive packet in" 8036b9f5c94SGleb Smirnoff " loopback mode\n"); 804a94100faSBill Paul error = EIO; 805a94100faSBill Paul goto done; 806a94100faSBill Paul } 807a94100faSBill Paul 808a94100faSBill Paul /* 809a94100faSBill Paul * The packet should have been dumped into the first 810a94100faSBill Paul * entry in the RX DMA ring. Grab it from there. 811a94100faSBill Paul */ 812a94100faSBill Paul 813a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 814a94100faSBill Paul sc->rl_ldata.rl_rx_list_map, 815a94100faSBill Paul BUS_DMASYNC_POSTREAD); 816d65abd66SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, 817d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc[0].rx_dmamap, 818d65abd66SPyun YongHyeon BUS_DMASYNC_POSTREAD); 819d65abd66SPyun YongHyeon bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, 820d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc[0].rx_dmamap); 821a94100faSBill Paul 822d65abd66SPyun YongHyeon m0 = sc->rl_ldata.rl_rx_desc[0].rx_m; 823d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc[0].rx_m = NULL; 824a94100faSBill Paul eh = mtod(m0, struct ether_header *); 825a94100faSBill Paul 826a94100faSBill Paul cur_rx = &sc->rl_ldata.rl_rx_list[0]; 827a94100faSBill Paul total_len = RL_RXBYTES(cur_rx); 828a94100faSBill Paul rxstat = le32toh(cur_rx->rl_cmdstat); 829a94100faSBill Paul 830a94100faSBill Paul if (total_len != ETHER_MIN_LEN) { 8316b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, 8326b9f5c94SGleb Smirnoff "diagnostic failed, received short packet\n"); 833a94100faSBill Paul error = EIO; 834a94100faSBill Paul goto done; 835a94100faSBill Paul } 836a94100faSBill Paul 837a94100faSBill Paul /* Test that the received packet data matches what we sent. */ 838a94100faSBill Paul 839a94100faSBill Paul if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) || 840a94100faSBill Paul bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) || 841a94100faSBill Paul ntohs(eh->ether_type) != ETHERTYPE_IP) { 8426b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n"); 8436b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n", 844a94100faSBill Paul dst, ":", src, ":", ETHERTYPE_IP); 8456b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n", 846a94100faSBill Paul eh->ether_dhost, ":", eh->ether_shost, ":", 847a94100faSBill Paul ntohs(eh->ether_type)); 8486b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "You may have a defective 32-bit " 8496b9f5c94SGleb Smirnoff "NIC plugged into a 64-bit PCI slot.\n"); 8506b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "Please re-install the NIC in a " 8516b9f5c94SGleb Smirnoff "32-bit slot for proper operation.\n"); 8526b9f5c94SGleb Smirnoff device_printf(sc->rl_dev, "Read the re(4) man page for more " 8536b9f5c94SGleb Smirnoff "details.\n"); 854a94100faSBill Paul error = EIO; 855a94100faSBill Paul } 856a94100faSBill Paul 857a94100faSBill Paul done: 858a94100faSBill Paul /* Turn interface off, release resources */ 859a94100faSBill Paul 860a94100faSBill Paul sc->rl_testmode = 0; 861351a76f9SPyun YongHyeon sc->rl_flags &= ~RL_FLAG_LINK; 862a94100faSBill Paul ifp->if_flags &= ~IFF_PROMISC; 863a94100faSBill Paul re_stop(sc); 864a94100faSBill Paul if (m0 != NULL) 865a94100faSBill Paul m_freem(m0); 866a94100faSBill Paul 86797b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 86897b9d4baSJohn-Mark Gurney 869a94100faSBill Paul return (error); 870a94100faSBill Paul } 871a94100faSBill Paul 872ed510fb0SBill Paul #endif 873ed510fb0SBill Paul 874a94100faSBill Paul /* 875a94100faSBill Paul * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device 876a94100faSBill Paul * IDs against our list and return a device name if we find a match. 877a94100faSBill Paul */ 878a94100faSBill Paul static int 879a94100faSBill Paul re_probe(dev) 880a94100faSBill Paul device_t dev; 881a94100faSBill Paul { 882a94100faSBill Paul struct rl_type *t; 883dfdb409eSPyun YongHyeon uint16_t devid, vendor; 884dfdb409eSPyun YongHyeon uint16_t revid, sdevid; 885dfdb409eSPyun YongHyeon int i; 886a94100faSBill Paul 887dfdb409eSPyun YongHyeon vendor = pci_get_vendor(dev); 888dfdb409eSPyun YongHyeon devid = pci_get_device(dev); 889dfdb409eSPyun YongHyeon revid = pci_get_revid(dev); 890dfdb409eSPyun YongHyeon sdevid = pci_get_subdevice(dev); 891a94100faSBill Paul 892dfdb409eSPyun YongHyeon if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) { 893dfdb409eSPyun YongHyeon if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) { 89426390635SJohn Baldwin /* 89526390635SJohn Baldwin * Only attach to rev. 3 of the Linksys EG1032 adapter. 896dfdb409eSPyun YongHyeon * Rev. 2 is supported by sk(4). 89726390635SJohn Baldwin */ 898a94100faSBill Paul return (ENXIO); 899a94100faSBill Paul } 900dfdb409eSPyun YongHyeon } 901dfdb409eSPyun YongHyeon 902dfdb409eSPyun YongHyeon if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) { 903dfdb409eSPyun YongHyeon if (revid != 0x20) { 904dfdb409eSPyun YongHyeon /* 8139, let rl(4) take care of this device. */ 905dfdb409eSPyun YongHyeon return (ENXIO); 906dfdb409eSPyun YongHyeon } 907dfdb409eSPyun YongHyeon } 908dfdb409eSPyun YongHyeon 909dfdb409eSPyun YongHyeon t = re_devs; 910dfdb409eSPyun YongHyeon for (i = 0; i < sizeof(re_devs) / sizeof(re_devs[0]); i++, t++) { 911dfdb409eSPyun YongHyeon if (vendor == t->rl_vid && devid == t->rl_did) { 912a94100faSBill Paul device_set_desc(dev, t->rl_name); 913d2b677bbSWarner Losh return (BUS_PROBE_DEFAULT); 914a94100faSBill Paul } 915a94100faSBill Paul } 916a94100faSBill Paul 917a94100faSBill Paul return (ENXIO); 918a94100faSBill Paul } 919a94100faSBill Paul 920a94100faSBill Paul /* 921a94100faSBill Paul * Map a single buffer address. 922a94100faSBill Paul */ 923a94100faSBill Paul 924a94100faSBill Paul static void 925a94100faSBill Paul re_dma_map_addr(arg, segs, nseg, error) 926a94100faSBill Paul void *arg; 927a94100faSBill Paul bus_dma_segment_t *segs; 928a94100faSBill Paul int nseg; 929a94100faSBill Paul int error; 930a94100faSBill Paul { 9318fd99e38SPyun YongHyeon bus_addr_t *addr; 932a94100faSBill Paul 933a94100faSBill Paul if (error) 934a94100faSBill Paul return; 935a94100faSBill Paul 936a94100faSBill Paul KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 937a94100faSBill Paul addr = arg; 938a94100faSBill Paul *addr = segs->ds_addr; 939a94100faSBill Paul } 940a94100faSBill Paul 941a94100faSBill Paul static int 942a94100faSBill Paul re_allocmem(dev, sc) 943a94100faSBill Paul device_t dev; 944a94100faSBill Paul struct rl_softc *sc; 945a94100faSBill Paul { 946d65abd66SPyun YongHyeon bus_size_t rx_list_size, tx_list_size; 947a94100faSBill Paul int error; 948a94100faSBill Paul int i; 949a94100faSBill Paul 950d65abd66SPyun YongHyeon rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc); 951d65abd66SPyun YongHyeon tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc); 952d65abd66SPyun YongHyeon 953d65abd66SPyun YongHyeon /* 954d65abd66SPyun YongHyeon * Allocate the parent bus DMA tag appropriate for PCI. 955ce628393SPyun YongHyeon * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD 956ce628393SPyun YongHyeon * register should be set. However some RealTek chips are known 957ce628393SPyun YongHyeon * to be buggy on DAC handling, therefore disable DAC by limiting 958ce628393SPyun YongHyeon * DMA address space to 32bit. PCIe variants of RealTek chips 959ce628393SPyun YongHyeon * may not have the limitation but I took safer path. 960d65abd66SPyun YongHyeon */ 961d65abd66SPyun YongHyeon error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0, 962ce628393SPyun YongHyeon BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 963d65abd66SPyun YongHyeon BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0, 964d65abd66SPyun YongHyeon NULL, NULL, &sc->rl_parent_tag); 965d65abd66SPyun YongHyeon if (error) { 966d65abd66SPyun YongHyeon device_printf(dev, "could not allocate parent DMA tag\n"); 967d65abd66SPyun YongHyeon return (error); 968d65abd66SPyun YongHyeon } 969d65abd66SPyun YongHyeon 970d65abd66SPyun YongHyeon /* 971d65abd66SPyun YongHyeon * Allocate map for TX mbufs. 972d65abd66SPyun YongHyeon */ 973d65abd66SPyun YongHyeon error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0, 974d65abd66SPyun YongHyeon BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, 975d65abd66SPyun YongHyeon NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0, 976d65abd66SPyun YongHyeon NULL, NULL, &sc->rl_ldata.rl_tx_mtag); 977d65abd66SPyun YongHyeon if (error) { 978d65abd66SPyun YongHyeon device_printf(dev, "could not allocate TX DMA tag\n"); 979d65abd66SPyun YongHyeon return (error); 980d65abd66SPyun YongHyeon } 981d65abd66SPyun YongHyeon 982a94100faSBill Paul /* 983a94100faSBill Paul * Allocate map for RX mbufs. 984a94100faSBill Paul */ 985d65abd66SPyun YongHyeon 986d65abd66SPyun YongHyeon error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0, 987d65abd66SPyun YongHyeon BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 988d65abd66SPyun YongHyeon MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag); 989a94100faSBill Paul if (error) { 990d65abd66SPyun YongHyeon device_printf(dev, "could not allocate RX DMA tag\n"); 991d65abd66SPyun YongHyeon return (error); 992a94100faSBill Paul } 993a94100faSBill Paul 994a94100faSBill Paul /* 995a94100faSBill Paul * Allocate map for TX descriptor list. 996a94100faSBill Paul */ 997a94100faSBill Paul error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN, 998a94100faSBill Paul 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 999d65abd66SPyun YongHyeon NULL, tx_list_size, 1, tx_list_size, 0, 1000a94100faSBill Paul NULL, NULL, &sc->rl_ldata.rl_tx_list_tag); 1001a94100faSBill Paul if (error) { 1002d65abd66SPyun YongHyeon device_printf(dev, "could not allocate TX DMA ring tag\n"); 1003d65abd66SPyun YongHyeon return (error); 1004a94100faSBill Paul } 1005a94100faSBill Paul 1006a94100faSBill Paul /* Allocate DMA'able memory for the TX ring */ 1007a94100faSBill Paul 1008a94100faSBill Paul error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag, 1009d65abd66SPyun YongHyeon (void **)&sc->rl_ldata.rl_tx_list, 1010d65abd66SPyun YongHyeon BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, 1011a94100faSBill Paul &sc->rl_ldata.rl_tx_list_map); 1012d65abd66SPyun YongHyeon if (error) { 1013d65abd66SPyun YongHyeon device_printf(dev, "could not allocate TX DMA ring\n"); 1014d65abd66SPyun YongHyeon return (error); 1015d65abd66SPyun YongHyeon } 1016a94100faSBill Paul 1017a94100faSBill Paul /* Load the map for the TX ring. */ 1018a94100faSBill Paul 1019d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_list_addr = 0; 1020a94100faSBill Paul error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag, 1021a94100faSBill Paul sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list, 1022d65abd66SPyun YongHyeon tx_list_size, re_dma_map_addr, 1023a94100faSBill Paul &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT); 1024d65abd66SPyun YongHyeon if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) { 1025d65abd66SPyun YongHyeon device_printf(dev, "could not load TX DMA ring\n"); 1026d65abd66SPyun YongHyeon return (ENOMEM); 1027d65abd66SPyun YongHyeon } 1028a94100faSBill Paul 1029a94100faSBill Paul /* Create DMA maps for TX buffers */ 1030a94100faSBill Paul 1031d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) { 1032d65abd66SPyun YongHyeon error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0, 1033d65abd66SPyun YongHyeon &sc->rl_ldata.rl_tx_desc[i].tx_dmamap); 1034a94100faSBill Paul if (error) { 1035d65abd66SPyun YongHyeon device_printf(dev, "could not create DMA map for TX\n"); 1036d65abd66SPyun YongHyeon return (error); 1037a94100faSBill Paul } 1038a94100faSBill Paul } 1039a94100faSBill Paul 1040a94100faSBill Paul /* 1041a94100faSBill Paul * Allocate map for RX descriptor list. 1042a94100faSBill Paul */ 1043a94100faSBill Paul error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN, 1044a94100faSBill Paul 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 1045d65abd66SPyun YongHyeon NULL, rx_list_size, 1, rx_list_size, 0, 1046a94100faSBill Paul NULL, NULL, &sc->rl_ldata.rl_rx_list_tag); 1047a94100faSBill Paul if (error) { 1048d65abd66SPyun YongHyeon device_printf(dev, "could not create RX DMA ring tag\n"); 1049d65abd66SPyun YongHyeon return (error); 1050a94100faSBill Paul } 1051a94100faSBill Paul 1052a94100faSBill Paul /* Allocate DMA'able memory for the RX ring */ 1053a94100faSBill Paul 1054a94100faSBill Paul error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag, 1055d65abd66SPyun YongHyeon (void **)&sc->rl_ldata.rl_rx_list, 1056d65abd66SPyun YongHyeon BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, 1057a94100faSBill Paul &sc->rl_ldata.rl_rx_list_map); 1058d65abd66SPyun YongHyeon if (error) { 1059d65abd66SPyun YongHyeon device_printf(dev, "could not allocate RX DMA ring\n"); 1060d65abd66SPyun YongHyeon return (error); 1061d65abd66SPyun YongHyeon } 1062a94100faSBill Paul 1063a94100faSBill Paul /* Load the map for the RX ring. */ 1064a94100faSBill Paul 1065d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_list_addr = 0; 1066a94100faSBill Paul error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag, 1067a94100faSBill Paul sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list, 1068d65abd66SPyun YongHyeon rx_list_size, re_dma_map_addr, 1069a94100faSBill Paul &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT); 1070d65abd66SPyun YongHyeon if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) { 1071d65abd66SPyun YongHyeon device_printf(dev, "could not load RX DMA ring\n"); 1072d65abd66SPyun YongHyeon return (ENOMEM); 1073d65abd66SPyun YongHyeon } 1074a94100faSBill Paul 1075a94100faSBill Paul /* Create DMA maps for RX buffers */ 1076a94100faSBill Paul 1077d65abd66SPyun YongHyeon error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0, 1078d65abd66SPyun YongHyeon &sc->rl_ldata.rl_rx_sparemap); 1079a94100faSBill Paul if (error) { 1080d65abd66SPyun YongHyeon device_printf(dev, "could not create spare DMA map for RX\n"); 1081d65abd66SPyun YongHyeon return (error); 1082d65abd66SPyun YongHyeon } 1083d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 1084d65abd66SPyun YongHyeon error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0, 1085d65abd66SPyun YongHyeon &sc->rl_ldata.rl_rx_desc[i].rx_dmamap); 1086d65abd66SPyun YongHyeon if (error) { 1087d65abd66SPyun YongHyeon device_printf(dev, "could not create DMA map for RX\n"); 1088d65abd66SPyun YongHyeon return (error); 1089a94100faSBill Paul } 1090a94100faSBill Paul } 1091a94100faSBill Paul 1092a94100faSBill Paul return (0); 1093a94100faSBill Paul } 1094a94100faSBill Paul 1095a94100faSBill Paul /* 1096a94100faSBill Paul * Attach the interface. Allocate softc structures, do ifmedia 1097a94100faSBill Paul * setup and ethernet/BPF attach. 1098a94100faSBill Paul */ 1099a94100faSBill Paul static int 1100a94100faSBill Paul re_attach(dev) 1101a94100faSBill Paul device_t dev; 1102a94100faSBill Paul { 1103a94100faSBill Paul u_char eaddr[ETHER_ADDR_LEN]; 1104be099007SPyun YongHyeon u_int16_t as[ETHER_ADDR_LEN / 2]; 1105a94100faSBill Paul struct rl_softc *sc; 1106a94100faSBill Paul struct ifnet *ifp; 1107a94100faSBill Paul struct rl_hwrev *hw_rev; 1108a94100faSBill Paul int hwrev; 1109ace7ed5dSPyun YongHyeon u_int16_t devid, re_did = 0; 1110d1754a9bSJohn Baldwin int error = 0, rid, i; 11115774c5ffSPyun YongHyeon int msic, reg; 111203ca7ae8SPyun YongHyeon uint8_t cfg; 1113a94100faSBill Paul 1114a94100faSBill Paul sc = device_get_softc(dev); 1115ed510fb0SBill Paul sc->rl_dev = dev; 1116a94100faSBill Paul 1117a94100faSBill Paul mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 111897b9d4baSJohn-Mark Gurney MTX_DEF); 1119d1754a9bSJohn Baldwin callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0); 1120d1754a9bSJohn Baldwin 1121a94100faSBill Paul /* 1122a94100faSBill Paul * Map control/status registers. 1123a94100faSBill Paul */ 1124a94100faSBill Paul pci_enable_busmaster(dev); 1125a94100faSBill Paul 1126ace7ed5dSPyun YongHyeon devid = pci_get_device(dev); 1127ace7ed5dSPyun YongHyeon /* Prefer memory space register mapping over IO space. */ 1128ace7ed5dSPyun YongHyeon sc->rl_res_id = PCIR_BAR(1); 1129ace7ed5dSPyun YongHyeon sc->rl_res_type = SYS_RES_MEMORY; 1130ace7ed5dSPyun YongHyeon /* RTL8168/8101E seems to use different BARs. */ 1131ace7ed5dSPyun YongHyeon if (devid == RT_DEVICEID_8168 || devid == RT_DEVICEID_8101E) 1132ace7ed5dSPyun YongHyeon sc->rl_res_id = PCIR_BAR(2); 1133ace7ed5dSPyun YongHyeon sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type, 1134ace7ed5dSPyun YongHyeon &sc->rl_res_id, RF_ACTIVE); 1135a94100faSBill Paul 1136a94100faSBill Paul if (sc->rl_res == NULL) { 1137ace7ed5dSPyun YongHyeon sc->rl_res_id = PCIR_BAR(0); 1138ace7ed5dSPyun YongHyeon sc->rl_res_type = SYS_RES_IOPORT; 1139ace7ed5dSPyun YongHyeon sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type, 1140ace7ed5dSPyun YongHyeon &sc->rl_res_id, RF_ACTIVE); 1141ace7ed5dSPyun YongHyeon if (sc->rl_res == NULL) { 1142d1754a9bSJohn Baldwin device_printf(dev, "couldn't map ports/memory\n"); 1143a94100faSBill Paul error = ENXIO; 1144a94100faSBill Paul goto fail; 1145a94100faSBill Paul } 1146ace7ed5dSPyun YongHyeon } 1147a94100faSBill Paul 1148a94100faSBill Paul sc->rl_btag = rman_get_bustag(sc->rl_res); 1149a94100faSBill Paul sc->rl_bhandle = rman_get_bushandle(sc->rl_res); 1150a94100faSBill Paul 11515774c5ffSPyun YongHyeon msic = 0; 11525774c5ffSPyun YongHyeon if (pci_find_extcap(dev, PCIY_EXPRESS, ®) == 0) { 11535774c5ffSPyun YongHyeon msic = pci_msi_count(dev); 11545774c5ffSPyun YongHyeon if (bootverbose) 11555774c5ffSPyun YongHyeon device_printf(dev, "MSI count : %d\n", msic); 11565774c5ffSPyun YongHyeon } 11575774c5ffSPyun YongHyeon if (msic == RL_MSI_MESSAGES && msi_disable == 0) { 11585774c5ffSPyun YongHyeon if (pci_alloc_msi(dev, &msic) == 0) { 11595774c5ffSPyun YongHyeon if (msic == RL_MSI_MESSAGES) { 11605774c5ffSPyun YongHyeon device_printf(dev, "Using %d MSI messages\n", 11615774c5ffSPyun YongHyeon msic); 1162351a76f9SPyun YongHyeon sc->rl_flags |= RL_FLAG_MSI; 1163339a44fbSPyun YongHyeon /* Explicitly set MSI enable bit. */ 1164339a44fbSPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE); 1165339a44fbSPyun YongHyeon cfg = CSR_READ_1(sc, RL_CFG2); 1166339a44fbSPyun YongHyeon cfg |= RL_CFG2_MSI; 1167339a44fbSPyun YongHyeon CSR_WRITE_1(sc, RL_CFG2, cfg); 1168f98dd8cfSPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 11695774c5ffSPyun YongHyeon } else 11705774c5ffSPyun YongHyeon pci_release_msi(dev); 11715774c5ffSPyun YongHyeon } 11725774c5ffSPyun YongHyeon } 1173a94100faSBill Paul 11745774c5ffSPyun YongHyeon /* Allocate interrupt */ 1175351a76f9SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_MSI) == 0) { 11765774c5ffSPyun YongHyeon rid = 0; 11775774c5ffSPyun YongHyeon sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 11785774c5ffSPyun YongHyeon RF_SHAREABLE | RF_ACTIVE); 11795774c5ffSPyun YongHyeon if (sc->rl_irq[0] == NULL) { 11805774c5ffSPyun YongHyeon device_printf(dev, "couldn't allocate IRQ resources\n"); 1181a94100faSBill Paul error = ENXIO; 1182a94100faSBill Paul goto fail; 1183a94100faSBill Paul } 11845774c5ffSPyun YongHyeon } else { 11855774c5ffSPyun YongHyeon for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) { 11865774c5ffSPyun YongHyeon sc->rl_irq[i] = bus_alloc_resource_any(dev, 11875774c5ffSPyun YongHyeon SYS_RES_IRQ, &rid, RF_ACTIVE); 11885774c5ffSPyun YongHyeon if (sc->rl_irq[i] == NULL) { 11895774c5ffSPyun YongHyeon device_printf(dev, 11905774c5ffSPyun YongHyeon "couldn't llocate IRQ resources for " 11915774c5ffSPyun YongHyeon "message %d\n", rid); 11925774c5ffSPyun YongHyeon error = ENXIO; 11935774c5ffSPyun YongHyeon goto fail; 11945774c5ffSPyun YongHyeon } 11955774c5ffSPyun YongHyeon } 11965774c5ffSPyun YongHyeon } 1197a94100faSBill Paul 11984d2bf239SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_MSI) == 0) { 11994d2bf239SPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE); 12004d2bf239SPyun YongHyeon cfg = CSR_READ_1(sc, RL_CFG2); 12014d2bf239SPyun YongHyeon if ((cfg & RL_CFG2_MSI) != 0) { 12024d2bf239SPyun YongHyeon device_printf(dev, "turning off MSI enable bit.\n"); 12034d2bf239SPyun YongHyeon cfg &= ~RL_CFG2_MSI; 12044d2bf239SPyun YongHyeon CSR_WRITE_1(sc, RL_CFG2, cfg); 12054d2bf239SPyun YongHyeon } 12064d2bf239SPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 12074d2bf239SPyun YongHyeon } 12084d2bf239SPyun YongHyeon 1209a94100faSBill Paul /* Reset the adapter. */ 121097b9d4baSJohn-Mark Gurney RL_LOCK(sc); 1211a94100faSBill Paul re_reset(sc); 121297b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 1213abc8ff44SBill Paul 1214abc8ff44SBill Paul hw_rev = re_hwrevs; 1215a810fc83SPyun YongHyeon hwrev = CSR_READ_4(sc, RL_TXCFG); 1216a810fc83SPyun YongHyeon device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0x7c800000); 1217a810fc83SPyun YongHyeon device_printf(dev, "MAC rev. 0x%08x\n", hwrev & 0x00700000); 1218a810fc83SPyun YongHyeon hwrev &= RL_TXCFG_HWREV; 1219abc8ff44SBill Paul while (hw_rev->rl_desc != NULL) { 1220abc8ff44SBill Paul if (hw_rev->rl_rev == hwrev) { 1221abc8ff44SBill Paul sc->rl_type = hw_rev->rl_type; 1222abc8ff44SBill Paul break; 1223abc8ff44SBill Paul } 1224abc8ff44SBill Paul hw_rev++; 1225abc8ff44SBill Paul } 1226d65abd66SPyun YongHyeon if (hw_rev->rl_desc == NULL) { 1227a810fc83SPyun YongHyeon device_printf(dev, "Unknown H/W revision: 0x%08x\n", hwrev); 1228d65abd66SPyun YongHyeon error = ENXIO; 1229d65abd66SPyun YongHyeon goto fail; 1230d65abd66SPyun YongHyeon } 1231abc8ff44SBill Paul 1232351a76f9SPyun YongHyeon switch (hw_rev->rl_rev) { 1233351a76f9SPyun YongHyeon case RL_HWREV_8139CPLUS: 1234351a76f9SPyun YongHyeon sc->rl_flags |= RL_FLAG_NOJUMBO; 1235351a76f9SPyun YongHyeon break; 1236351a76f9SPyun YongHyeon case RL_HWREV_8100E: 1237351a76f9SPyun YongHyeon case RL_HWREV_8101E: 1238351a76f9SPyun YongHyeon sc->rl_flags |= RL_FLAG_INVMAR | RL_FLAG_PHYWAKE; 1239351a76f9SPyun YongHyeon break; 1240351a76f9SPyun YongHyeon case RL_HWREV_8168_SPIN1: 1241351a76f9SPyun YongHyeon case RL_HWREV_8168_SPIN2: 1242351a76f9SPyun YongHyeon case RL_HWREV_8168_SPIN3: 1243351a76f9SPyun YongHyeon sc->rl_flags |= RL_FLAG_INVMAR | RL_FLAG_PHYWAKE; 1244351a76f9SPyun YongHyeon break; 1245351a76f9SPyun YongHyeon case RL_HWREV_8169_8110SB: 1246351a76f9SPyun YongHyeon case RL_HWREV_8169_8110SC: 1247351a76f9SPyun YongHyeon sc->rl_flags |= RL_FLAG_PHYWAKE; 1248351a76f9SPyun YongHyeon break; 1249351a76f9SPyun YongHyeon default: 1250351a76f9SPyun YongHyeon break; 1251351a76f9SPyun YongHyeon } 1252351a76f9SPyun YongHyeon 1253141f92e7SPyun YongHyeon sc->rl_eewidth = RL_9356_ADDR_LEN; 1254ed510fb0SBill Paul re_read_eeprom(sc, (caddr_t)&re_did, 0, 1); 1255a94100faSBill Paul if (re_did != 0x8129) 1256141f92e7SPyun YongHyeon sc->rl_eewidth = RL_9346_ADDR_LEN; 1257a94100faSBill Paul 1258a94100faSBill Paul /* 1259a94100faSBill Paul * Get station address from the EEPROM. 1260a94100faSBill Paul */ 1261ed510fb0SBill Paul re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3); 1262be099007SPyun YongHyeon for (i = 0; i < ETHER_ADDR_LEN / 2; i++) 1263be099007SPyun YongHyeon as[i] = le16toh(as[i]); 1264be099007SPyun YongHyeon bcopy(as, eaddr, sizeof(eaddr)); 1265ed510fb0SBill Paul 1266ed510fb0SBill Paul if (sc->rl_type == RL_8169) { 1267d65abd66SPyun YongHyeon /* Set RX length mask and number of descriptors. */ 1268ed510fb0SBill Paul sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN; 1269ed510fb0SBill Paul sc->rl_txstart = RL_GTXSTART; 1270d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT; 1271d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT; 1272ed510fb0SBill Paul } else { 1273d65abd66SPyun YongHyeon /* Set RX length mask and number of descriptors. */ 1274ed510fb0SBill Paul sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN; 1275ed510fb0SBill Paul sc->rl_txstart = RL_TXSTART; 1276d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT; 1277d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT; 1278abc8ff44SBill Paul } 12799bac70b8SBill Paul 1280a94100faSBill Paul error = re_allocmem(dev, sc); 1281a94100faSBill Paul if (error) 1282a94100faSBill Paul goto fail; 1283a94100faSBill Paul 1284cd036ec1SBrooks Davis ifp = sc->rl_ifp = if_alloc(IFT_ETHER); 1285cd036ec1SBrooks Davis if (ifp == NULL) { 1286d1754a9bSJohn Baldwin device_printf(dev, "can not if_alloc()\n"); 1287cd036ec1SBrooks Davis error = ENOSPC; 1288cd036ec1SBrooks Davis goto fail; 1289cd036ec1SBrooks Davis } 1290cd036ec1SBrooks Davis 1291351a76f9SPyun YongHyeon /* Take PHY out of power down mode. */ 1292351a76f9SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_PHYWAKE) != 0) { 1293351a76f9SPyun YongHyeon re_gmii_writereg(dev, 1, 0x1f, 0); 1294351a76f9SPyun YongHyeon re_gmii_writereg(dev, 1, 0x0e, 0); 1295351a76f9SPyun YongHyeon } 1296351a76f9SPyun YongHyeon 1297a94100faSBill Paul /* Do MII setup */ 1298a94100faSBill Paul if (mii_phy_probe(dev, &sc->rl_miibus, 1299a94100faSBill Paul re_ifmedia_upd, re_ifmedia_sts)) { 1300d1754a9bSJohn Baldwin device_printf(dev, "MII without any phy!\n"); 1301a94100faSBill Paul error = ENXIO; 1302a94100faSBill Paul goto fail; 1303a94100faSBill Paul } 1304a94100faSBill Paul 1305a94100faSBill Paul ifp->if_softc = sc; 13069bf40edeSBrooks Davis if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 1307a94100faSBill Paul ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1308a94100faSBill Paul ifp->if_ioctl = re_ioctl; 1309a94100faSBill Paul ifp->if_start = re_start; 1310d65abd66SPyun YongHyeon ifp->if_hwassist = RE_CSUM_FEATURES | CSUM_TSO; 1311d65abd66SPyun YongHyeon ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_TSO4; 1312498bd0d3SBill Paul ifp->if_capenable = ifp->if_capabilities; 1313a94100faSBill Paul ifp->if_init = re_init; 131452732175SMax Laier IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN); 131552732175SMax Laier ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN; 131652732175SMax Laier IFQ_SET_READY(&ifp->if_snd); 1317a94100faSBill Paul 1318ed510fb0SBill Paul TASK_INIT(&sc->rl_txtask, 1, re_tx_task, ifp); 1319ed510fb0SBill Paul TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc); 1320ed510fb0SBill Paul 1321a94100faSBill Paul /* 1322a94100faSBill Paul * Call MI attach routine. 1323a94100faSBill Paul */ 1324a94100faSBill Paul ether_ifattach(ifp, eaddr); 1325a94100faSBill Paul 1326960fd5b3SPyun YongHyeon /* VLAN capability setup */ 1327960fd5b3SPyun YongHyeon ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING; 1328960fd5b3SPyun YongHyeon if (ifp->if_capabilities & IFCAP_HWCSUM) 1329960fd5b3SPyun YongHyeon ifp->if_capabilities |= IFCAP_VLAN_HWCSUM; 13307467bd53SPyun YongHyeon /* Enable WOL if PM is supported. */ 13317467bd53SPyun YongHyeon if (pci_find_extcap(sc->rl_dev, PCIY_PMG, ®) == 0) 13327467bd53SPyun YongHyeon ifp->if_capabilities |= IFCAP_WOL; 1333960fd5b3SPyun YongHyeon ifp->if_capenable = ifp->if_capabilities; 1334960fd5b3SPyun YongHyeon #ifdef DEVICE_POLLING 1335960fd5b3SPyun YongHyeon ifp->if_capabilities |= IFCAP_POLLING; 1336960fd5b3SPyun YongHyeon #endif 1337960fd5b3SPyun YongHyeon /* 1338960fd5b3SPyun YongHyeon * Tell the upper layer(s) we support long frames. 1339960fd5b3SPyun YongHyeon * Must appear after the call to ether_ifattach() because 1340960fd5b3SPyun YongHyeon * ether_ifattach() sets ifi_hdrlen to the default value. 1341960fd5b3SPyun YongHyeon */ 1342960fd5b3SPyun YongHyeon ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header); 1343960fd5b3SPyun YongHyeon 1344ed510fb0SBill Paul #ifdef RE_DIAG 1345ed510fb0SBill Paul /* 1346ed510fb0SBill Paul * Perform hardware diagnostic on the original RTL8169. 1347ed510fb0SBill Paul * Some 32-bit cards were incorrectly wired and would 1348ed510fb0SBill Paul * malfunction if plugged into a 64-bit slot. 1349ed510fb0SBill Paul */ 1350a94100faSBill Paul 1351ed510fb0SBill Paul if (hwrev == RL_HWREV_8169) { 1352ed510fb0SBill Paul error = re_diag(sc); 1353a94100faSBill Paul if (error) { 1354ed510fb0SBill Paul device_printf(dev, 1355ed510fb0SBill Paul "attach aborted due to hardware diag failure\n"); 1356a94100faSBill Paul ether_ifdetach(ifp); 1357a94100faSBill Paul goto fail; 1358a94100faSBill Paul } 1359ed510fb0SBill Paul } 1360ed510fb0SBill Paul #endif 1361a94100faSBill Paul 1362a94100faSBill Paul /* Hook interrupt last to avoid having to lock softc */ 1363351a76f9SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_MSI) == 0) 13645774c5ffSPyun YongHyeon error = bus_setup_intr(dev, sc->rl_irq[0], 13655774c5ffSPyun YongHyeon INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc, 13665774c5ffSPyun YongHyeon &sc->rl_intrhand[0]); 13675774c5ffSPyun YongHyeon else { 13685774c5ffSPyun YongHyeon for (i = 0; i < RL_MSI_MESSAGES; i++) { 13695774c5ffSPyun YongHyeon error = bus_setup_intr(dev, sc->rl_irq[i], 13705774c5ffSPyun YongHyeon INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc, 13715774c5ffSPyun YongHyeon &sc->rl_intrhand[i]); 13725774c5ffSPyun YongHyeon if (error != 0) 13735774c5ffSPyun YongHyeon break; 13745774c5ffSPyun YongHyeon } 13755774c5ffSPyun YongHyeon } 1376a94100faSBill Paul if (error) { 1377d1754a9bSJohn Baldwin device_printf(dev, "couldn't set up irq\n"); 1378a94100faSBill Paul ether_ifdetach(ifp); 1379a94100faSBill Paul } 1380a94100faSBill Paul 1381a94100faSBill Paul fail: 1382ed510fb0SBill Paul 1383a94100faSBill Paul if (error) 1384a94100faSBill Paul re_detach(dev); 1385a94100faSBill Paul 1386a94100faSBill Paul return (error); 1387a94100faSBill Paul } 1388a94100faSBill Paul 1389a94100faSBill Paul /* 1390a94100faSBill Paul * Shutdown hardware and free up resources. This can be called any 1391a94100faSBill Paul * time after the mutex has been initialized. It is called in both 1392a94100faSBill Paul * the error case in attach and the normal detach case so it needs 1393a94100faSBill Paul * to be careful about only freeing resources that have actually been 1394a94100faSBill Paul * allocated. 1395a94100faSBill Paul */ 1396a94100faSBill Paul static int 1397a94100faSBill Paul re_detach(dev) 1398a94100faSBill Paul device_t dev; 1399a94100faSBill Paul { 1400a94100faSBill Paul struct rl_softc *sc; 1401a94100faSBill Paul struct ifnet *ifp; 14025774c5ffSPyun YongHyeon int i, rid; 1403a94100faSBill Paul 1404a94100faSBill Paul sc = device_get_softc(dev); 1405fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 1406aedd16d9SJohn-Mark Gurney KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized")); 140797b9d4baSJohn-Mark Gurney 140881cf2eb6SPyun YongHyeon /* These should only be active if attach succeeded */ 140981cf2eb6SPyun YongHyeon if (device_is_attached(dev)) { 141040929967SGleb Smirnoff #ifdef DEVICE_POLLING 141140929967SGleb Smirnoff if (ifp->if_capenable & IFCAP_POLLING) 141240929967SGleb Smirnoff ether_poll_deregister(ifp); 141340929967SGleb Smirnoff #endif 141497b9d4baSJohn-Mark Gurney RL_LOCK(sc); 141597b9d4baSJohn-Mark Gurney #if 0 141697b9d4baSJohn-Mark Gurney sc->suspended = 1; 141797b9d4baSJohn-Mark Gurney #endif 1418a94100faSBill Paul re_stop(sc); 1419525e6a87SRuslan Ermilov RL_UNLOCK(sc); 1420d1754a9bSJohn Baldwin callout_drain(&sc->rl_stat_callout); 14213d4c1b57SJohn Baldwin taskqueue_drain(taskqueue_fast, &sc->rl_inttask); 14223d4c1b57SJohn Baldwin taskqueue_drain(taskqueue_fast, &sc->rl_txtask); 1423a94100faSBill Paul /* 1424a94100faSBill Paul * Force off the IFF_UP flag here, in case someone 1425a94100faSBill Paul * still had a BPF descriptor attached to this 142697b9d4baSJohn-Mark Gurney * interface. If they do, ether_ifdetach() will cause 1427a94100faSBill Paul * the BPF code to try and clear the promisc mode 1428a94100faSBill Paul * flag, which will bubble down to re_ioctl(), 1429a94100faSBill Paul * which will try to call re_init() again. This will 1430a94100faSBill Paul * turn the NIC back on and restart the MII ticker, 1431a94100faSBill Paul * which will panic the system when the kernel tries 1432a94100faSBill Paul * to invoke the re_tick() function that isn't there 1433a94100faSBill Paul * anymore. 1434a94100faSBill Paul */ 1435a94100faSBill Paul ifp->if_flags &= ~IFF_UP; 1436525e6a87SRuslan Ermilov ether_ifdetach(ifp); 1437a94100faSBill Paul } 1438a94100faSBill Paul if (sc->rl_miibus) 1439a94100faSBill Paul device_delete_child(dev, sc->rl_miibus); 1440a94100faSBill Paul bus_generic_detach(dev); 1441a94100faSBill Paul 144297b9d4baSJohn-Mark Gurney /* 144397b9d4baSJohn-Mark Gurney * The rest is resource deallocation, so we should already be 144497b9d4baSJohn-Mark Gurney * stopped here. 144597b9d4baSJohn-Mark Gurney */ 144697b9d4baSJohn-Mark Gurney 14475774c5ffSPyun YongHyeon for (i = 0; i < RL_MSI_MESSAGES; i++) { 14485774c5ffSPyun YongHyeon if (sc->rl_intrhand[i] != NULL) { 14495774c5ffSPyun YongHyeon bus_teardown_intr(dev, sc->rl_irq[i], 14505774c5ffSPyun YongHyeon sc->rl_intrhand[i]); 14515774c5ffSPyun YongHyeon sc->rl_intrhand[i] = NULL; 14525774c5ffSPyun YongHyeon } 14535774c5ffSPyun YongHyeon } 1454ad4f426eSWarner Losh if (ifp != NULL) 1455ad4f426eSWarner Losh if_free(ifp); 1456351a76f9SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_MSI) == 0) { 14575774c5ffSPyun YongHyeon if (sc->rl_irq[0] != NULL) { 14585774c5ffSPyun YongHyeon bus_release_resource(dev, SYS_RES_IRQ, 0, 14595774c5ffSPyun YongHyeon sc->rl_irq[0]); 14605774c5ffSPyun YongHyeon sc->rl_irq[0] = NULL; 14615774c5ffSPyun YongHyeon } 14625774c5ffSPyun YongHyeon } else { 14635774c5ffSPyun YongHyeon for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) { 14645774c5ffSPyun YongHyeon if (sc->rl_irq[i] != NULL) { 14655774c5ffSPyun YongHyeon bus_release_resource(dev, SYS_RES_IRQ, rid, 14665774c5ffSPyun YongHyeon sc->rl_irq[i]); 14675774c5ffSPyun YongHyeon sc->rl_irq[i] = NULL; 14685774c5ffSPyun YongHyeon } 14695774c5ffSPyun YongHyeon } 14705774c5ffSPyun YongHyeon pci_release_msi(dev); 14715774c5ffSPyun YongHyeon } 1472a94100faSBill Paul if (sc->rl_res) 1473ace7ed5dSPyun YongHyeon bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id, 1474ace7ed5dSPyun YongHyeon sc->rl_res); 1475a94100faSBill Paul 1476a94100faSBill Paul /* Unload and free the RX DMA ring memory and map */ 1477a94100faSBill Paul 1478a94100faSBill Paul if (sc->rl_ldata.rl_rx_list_tag) { 1479a94100faSBill Paul bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag, 1480a94100faSBill Paul sc->rl_ldata.rl_rx_list_map); 1481a94100faSBill Paul bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag, 1482a94100faSBill Paul sc->rl_ldata.rl_rx_list, 1483a94100faSBill Paul sc->rl_ldata.rl_rx_list_map); 1484a94100faSBill Paul bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag); 1485a94100faSBill Paul } 1486a94100faSBill Paul 1487a94100faSBill Paul /* Unload and free the TX DMA ring memory and map */ 1488a94100faSBill Paul 1489a94100faSBill Paul if (sc->rl_ldata.rl_tx_list_tag) { 1490a94100faSBill Paul bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag, 1491a94100faSBill Paul sc->rl_ldata.rl_tx_list_map); 1492a94100faSBill Paul bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag, 1493a94100faSBill Paul sc->rl_ldata.rl_tx_list, 1494a94100faSBill Paul sc->rl_ldata.rl_tx_list_map); 1495a94100faSBill Paul bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag); 1496a94100faSBill Paul } 1497a94100faSBill Paul 1498a94100faSBill Paul /* Destroy all the RX and TX buffer maps */ 1499a94100faSBill Paul 1500d65abd66SPyun YongHyeon if (sc->rl_ldata.rl_tx_mtag) { 1501d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) 1502d65abd66SPyun YongHyeon bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag, 1503d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_desc[i].tx_dmamap); 1504d65abd66SPyun YongHyeon bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag); 1505d65abd66SPyun YongHyeon } 1506d65abd66SPyun YongHyeon if (sc->rl_ldata.rl_rx_mtag) { 1507d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) 1508d65abd66SPyun YongHyeon bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag, 1509d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc[i].rx_dmamap); 1510d65abd66SPyun YongHyeon if (sc->rl_ldata.rl_rx_sparemap) 1511d65abd66SPyun YongHyeon bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag, 1512d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_sparemap); 1513d65abd66SPyun YongHyeon bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag); 1514a94100faSBill Paul } 1515a94100faSBill Paul 1516a94100faSBill Paul /* Unload and free the stats buffer and map */ 1517a94100faSBill Paul 1518a94100faSBill Paul if (sc->rl_ldata.rl_stag) { 1519a94100faSBill Paul bus_dmamap_unload(sc->rl_ldata.rl_stag, 1520a94100faSBill Paul sc->rl_ldata.rl_rx_list_map); 1521a94100faSBill Paul bus_dmamem_free(sc->rl_ldata.rl_stag, 1522a94100faSBill Paul sc->rl_ldata.rl_stats, 1523a94100faSBill Paul sc->rl_ldata.rl_smap); 1524a94100faSBill Paul bus_dma_tag_destroy(sc->rl_ldata.rl_stag); 1525a94100faSBill Paul } 1526a94100faSBill Paul 1527a94100faSBill Paul if (sc->rl_parent_tag) 1528a94100faSBill Paul bus_dma_tag_destroy(sc->rl_parent_tag); 1529a94100faSBill Paul 1530a94100faSBill Paul mtx_destroy(&sc->rl_mtx); 1531a94100faSBill Paul 1532a94100faSBill Paul return (0); 1533a94100faSBill Paul } 1534a94100faSBill Paul 1535d65abd66SPyun YongHyeon static __inline void 1536d65abd66SPyun YongHyeon re_discard_rxbuf(sc, idx) 1537a94100faSBill Paul struct rl_softc *sc; 1538a94100faSBill Paul int idx; 1539a94100faSBill Paul { 1540d65abd66SPyun YongHyeon struct rl_desc *desc; 1541d65abd66SPyun YongHyeon struct rl_rxdesc *rxd; 1542d65abd66SPyun YongHyeon uint32_t cmdstat; 1543a94100faSBill Paul 1544d65abd66SPyun YongHyeon rxd = &sc->rl_ldata.rl_rx_desc[idx]; 1545d65abd66SPyun YongHyeon desc = &sc->rl_ldata.rl_rx_list[idx]; 1546d65abd66SPyun YongHyeon desc->rl_vlanctl = 0; 1547d65abd66SPyun YongHyeon cmdstat = rxd->rx_size; 1548d65abd66SPyun YongHyeon if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1) 1549d65abd66SPyun YongHyeon cmdstat |= RL_RDESC_CMD_EOR; 1550d65abd66SPyun YongHyeon desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN); 1551d65abd66SPyun YongHyeon } 1552d65abd66SPyun YongHyeon 1553d65abd66SPyun YongHyeon static int 1554d65abd66SPyun YongHyeon re_newbuf(sc, idx) 1555d65abd66SPyun YongHyeon struct rl_softc *sc; 1556d65abd66SPyun YongHyeon int idx; 1557d65abd66SPyun YongHyeon { 1558d65abd66SPyun YongHyeon struct mbuf *m; 1559d65abd66SPyun YongHyeon struct rl_rxdesc *rxd; 1560d65abd66SPyun YongHyeon bus_dma_segment_t segs[1]; 1561d65abd66SPyun YongHyeon bus_dmamap_t map; 1562d65abd66SPyun YongHyeon struct rl_desc *desc; 1563d65abd66SPyun YongHyeon uint32_t cmdstat; 1564d65abd66SPyun YongHyeon int error, nsegs; 1565d65abd66SPyun YongHyeon 1566d65abd66SPyun YongHyeon m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1567d65abd66SPyun YongHyeon if (m == NULL) 1568a94100faSBill Paul return (ENOBUFS); 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 1584d65abd66SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag, 1585d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT); 1586d65abd66SPyun YongHyeon if (error != 0) { 1587d65abd66SPyun YongHyeon m_freem(m); 1588d65abd66SPyun YongHyeon return (ENOBUFS); 1589d65abd66SPyun YongHyeon } 1590d65abd66SPyun YongHyeon KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs)); 1591a94100faSBill Paul 1592d65abd66SPyun YongHyeon rxd = &sc->rl_ldata.rl_rx_desc[idx]; 1593d65abd66SPyun YongHyeon if (rxd->rx_m != NULL) { 1594d65abd66SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap, 1595d65abd66SPyun YongHyeon BUS_DMASYNC_POSTREAD); 1596d65abd66SPyun YongHyeon bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap); 1597a94100faSBill Paul } 1598a94100faSBill Paul 1599d65abd66SPyun YongHyeon rxd->rx_m = m; 1600d65abd66SPyun YongHyeon map = rxd->rx_dmamap; 1601d65abd66SPyun YongHyeon rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap; 1602d65abd66SPyun YongHyeon rxd->rx_size = segs[0].ds_len; 1603d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_sparemap = map; 1604d65abd66SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap, 1605a94100faSBill Paul BUS_DMASYNC_PREREAD); 1606a94100faSBill Paul 1607d65abd66SPyun YongHyeon desc = &sc->rl_ldata.rl_rx_list[idx]; 1608d65abd66SPyun YongHyeon desc->rl_vlanctl = 0; 1609d65abd66SPyun YongHyeon desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr)); 1610d65abd66SPyun YongHyeon desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr)); 1611d65abd66SPyun YongHyeon cmdstat = segs[0].ds_len; 1612d65abd66SPyun YongHyeon if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1) 1613d65abd66SPyun YongHyeon cmdstat |= RL_RDESC_CMD_EOR; 1614d65abd66SPyun YongHyeon desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN); 1615d65abd66SPyun YongHyeon 1616a94100faSBill Paul return (0); 1617a94100faSBill Paul } 1618a94100faSBill Paul 161922a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX 162022a11c96SJohn-Mark Gurney static __inline void 162122a11c96SJohn-Mark Gurney re_fixup_rx(m) 162222a11c96SJohn-Mark Gurney struct mbuf *m; 162322a11c96SJohn-Mark Gurney { 162422a11c96SJohn-Mark Gurney int i; 162522a11c96SJohn-Mark Gurney uint16_t *src, *dst; 162622a11c96SJohn-Mark Gurney 162722a11c96SJohn-Mark Gurney src = mtod(m, uint16_t *); 162822a11c96SJohn-Mark Gurney dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src; 162922a11c96SJohn-Mark Gurney 163022a11c96SJohn-Mark Gurney for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++) 163122a11c96SJohn-Mark Gurney *dst++ = *src++; 163222a11c96SJohn-Mark Gurney 163322a11c96SJohn-Mark Gurney m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN; 163422a11c96SJohn-Mark Gurney 163522a11c96SJohn-Mark Gurney return; 163622a11c96SJohn-Mark Gurney } 163722a11c96SJohn-Mark Gurney #endif 163822a11c96SJohn-Mark Gurney 1639a94100faSBill Paul static int 1640a94100faSBill Paul re_tx_list_init(sc) 1641a94100faSBill Paul struct rl_softc *sc; 1642a94100faSBill Paul { 1643d65abd66SPyun YongHyeon struct rl_desc *desc; 1644d65abd66SPyun YongHyeon int i; 164597b9d4baSJohn-Mark Gurney 164697b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 164797b9d4baSJohn-Mark Gurney 1648d65abd66SPyun YongHyeon bzero(sc->rl_ldata.rl_tx_list, 1649d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc)); 1650d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) 1651d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_desc[i].tx_m = NULL; 1652d65abd66SPyun YongHyeon /* Set EOR. */ 1653d65abd66SPyun YongHyeon desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1]; 1654d65abd66SPyun YongHyeon desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR); 1655a94100faSBill Paul 1656a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 1657d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_list_map, 1658d65abd66SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1659d65abd66SPyun YongHyeon 1660a94100faSBill Paul sc->rl_ldata.rl_tx_prodidx = 0; 1661a94100faSBill Paul sc->rl_ldata.rl_tx_considx = 0; 1662d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt; 1663a94100faSBill Paul 1664a94100faSBill Paul return (0); 1665a94100faSBill Paul } 1666a94100faSBill Paul 1667a94100faSBill Paul static int 1668a94100faSBill Paul re_rx_list_init(sc) 1669a94100faSBill Paul struct rl_softc *sc; 1670a94100faSBill Paul { 1671d65abd66SPyun YongHyeon int error, i; 1672a94100faSBill Paul 1673d65abd66SPyun YongHyeon bzero(sc->rl_ldata.rl_rx_list, 1674d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc)); 1675d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 1676d65abd66SPyun YongHyeon sc->rl_ldata.rl_rx_desc[i].rx_m = NULL; 1677d65abd66SPyun YongHyeon if ((error = re_newbuf(sc, i)) != 0) 1678d65abd66SPyun YongHyeon return (error); 1679a94100faSBill Paul } 1680a94100faSBill Paul 1681a94100faSBill Paul /* Flush the RX descriptors */ 1682a94100faSBill Paul 1683a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 1684a94100faSBill Paul sc->rl_ldata.rl_rx_list_map, 1685a94100faSBill Paul BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 1686a94100faSBill Paul 1687a94100faSBill Paul sc->rl_ldata.rl_rx_prodidx = 0; 1688a94100faSBill Paul sc->rl_head = sc->rl_tail = NULL; 1689a94100faSBill Paul 1690a94100faSBill Paul return (0); 1691a94100faSBill Paul } 1692a94100faSBill Paul 1693a94100faSBill Paul /* 1694a94100faSBill Paul * RX handler for C+ and 8169. For the gigE chips, we support 1695a94100faSBill Paul * the reception of jumbo frames that have been fragmented 1696a94100faSBill Paul * across multiple 2K mbuf cluster buffers. 1697a94100faSBill Paul */ 1698ed510fb0SBill Paul static int 1699a94100faSBill Paul re_rxeof(sc) 1700a94100faSBill Paul struct rl_softc *sc; 1701a94100faSBill Paul { 1702a94100faSBill Paul struct mbuf *m; 1703a94100faSBill Paul struct ifnet *ifp; 1704a94100faSBill Paul int i, total_len; 1705a94100faSBill Paul struct rl_desc *cur_rx; 1706a94100faSBill Paul u_int32_t rxstat, rxvlan; 1707ed510fb0SBill Paul int maxpkt = 16; 1708a94100faSBill Paul 17095120abbfSSam Leffler RL_LOCK_ASSERT(sc); 17105120abbfSSam Leffler 1711fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 1712a94100faSBill Paul 1713a94100faSBill Paul /* Invalidate the descriptor memory */ 1714a94100faSBill Paul 1715a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 1716a94100faSBill Paul sc->rl_ldata.rl_rx_list_map, 1717d65abd66SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 1718a94100faSBill Paul 1719d65abd66SPyun YongHyeon for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0; 1720d65abd66SPyun YongHyeon i = RL_RX_DESC_NXT(sc, i)) { 1721a94100faSBill Paul cur_rx = &sc->rl_ldata.rl_rx_list[i]; 1722a94100faSBill Paul rxstat = le32toh(cur_rx->rl_cmdstat); 1723d65abd66SPyun YongHyeon if ((rxstat & RL_RDESC_STAT_OWN) != 0) 1724d65abd66SPyun YongHyeon break; 1725d65abd66SPyun YongHyeon total_len = rxstat & sc->rl_rxlenmask; 1726a94100faSBill Paul rxvlan = le32toh(cur_rx->rl_vlanctl); 1727d65abd66SPyun YongHyeon m = sc->rl_ldata.rl_rx_desc[i].rx_m; 1728a94100faSBill Paul 1729a94100faSBill Paul if (!(rxstat & RL_RDESC_STAT_EOF)) { 1730d65abd66SPyun YongHyeon if (re_newbuf(sc, i) != 0) { 1731d65abd66SPyun YongHyeon /* 1732d65abd66SPyun YongHyeon * If this is part of a multi-fragment packet, 1733d65abd66SPyun YongHyeon * discard all the pieces. 1734d65abd66SPyun YongHyeon */ 1735d65abd66SPyun YongHyeon if (sc->rl_head != NULL) { 1736d65abd66SPyun YongHyeon m_freem(sc->rl_head); 1737d65abd66SPyun YongHyeon sc->rl_head = sc->rl_tail = NULL; 1738d65abd66SPyun YongHyeon } 1739d65abd66SPyun YongHyeon re_discard_rxbuf(sc, i); 1740d65abd66SPyun YongHyeon continue; 1741d65abd66SPyun YongHyeon } 174222a11c96SJohn-Mark Gurney m->m_len = RE_RX_DESC_BUFLEN; 1743a94100faSBill Paul if (sc->rl_head == NULL) 1744a94100faSBill Paul sc->rl_head = sc->rl_tail = m; 1745a94100faSBill Paul else { 1746a94100faSBill Paul m->m_flags &= ~M_PKTHDR; 1747a94100faSBill Paul sc->rl_tail->m_next = m; 1748a94100faSBill Paul sc->rl_tail = m; 1749a94100faSBill Paul } 1750a94100faSBill Paul continue; 1751a94100faSBill Paul } 1752a94100faSBill Paul 1753a94100faSBill Paul /* 1754a94100faSBill Paul * NOTE: for the 8139C+, the frame length field 1755a94100faSBill Paul * is always 12 bits in size, but for the gigE chips, 1756a94100faSBill Paul * it is 13 bits (since the max RX frame length is 16K). 1757a94100faSBill Paul * Unfortunately, all 32 bits in the status word 1758a94100faSBill Paul * were already used, so to make room for the extra 1759a94100faSBill Paul * length bit, RealTek took out the 'frame alignment 1760a94100faSBill Paul * error' bit and shifted the other status bits 1761a94100faSBill Paul * over one slot. The OWN, EOR, FS and LS bits are 1762a94100faSBill Paul * still in the same places. We have already extracted 1763a94100faSBill Paul * the frame length and checked the OWN bit, so rather 1764a94100faSBill Paul * than using an alternate bit mapping, we shift the 1765a94100faSBill Paul * status bits one space to the right so we can evaluate 1766a94100faSBill Paul * them using the 8169 status as though it was in the 1767a94100faSBill Paul * same format as that of the 8139C+. 1768a94100faSBill Paul */ 1769a94100faSBill Paul if (sc->rl_type == RL_8169) 1770a94100faSBill Paul rxstat >>= 1; 1771a94100faSBill Paul 177222a11c96SJohn-Mark Gurney /* 177322a11c96SJohn-Mark Gurney * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be 177422a11c96SJohn-Mark Gurney * set, but if CRC is clear, it will still be a valid frame. 177522a11c96SJohn-Mark Gurney */ 177622a11c96SJohn-Mark Gurney if (rxstat & RL_RDESC_STAT_RXERRSUM && !(total_len > 8191 && 177722a11c96SJohn-Mark Gurney (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)) { 1778a94100faSBill Paul ifp->if_ierrors++; 1779a94100faSBill Paul /* 1780a94100faSBill Paul * If this is part of a multi-fragment packet, 1781a94100faSBill Paul * discard all the pieces. 1782a94100faSBill Paul */ 1783a94100faSBill Paul if (sc->rl_head != NULL) { 1784a94100faSBill Paul m_freem(sc->rl_head); 1785a94100faSBill Paul sc->rl_head = sc->rl_tail = NULL; 1786a94100faSBill Paul } 1787d65abd66SPyun YongHyeon re_discard_rxbuf(sc, i); 1788a94100faSBill Paul continue; 1789a94100faSBill Paul } 1790a94100faSBill Paul 1791a94100faSBill Paul /* 1792a94100faSBill Paul * If allocating a replacement mbuf fails, 1793a94100faSBill Paul * reload the current one. 1794a94100faSBill Paul */ 1795a94100faSBill Paul 1796d65abd66SPyun YongHyeon if (re_newbuf(sc, i) != 0) { 1797d65abd66SPyun YongHyeon ifp->if_iqdrops++; 1798a94100faSBill Paul if (sc->rl_head != NULL) { 1799a94100faSBill Paul m_freem(sc->rl_head); 1800a94100faSBill Paul sc->rl_head = sc->rl_tail = NULL; 1801a94100faSBill Paul } 1802d65abd66SPyun YongHyeon re_discard_rxbuf(sc, i); 1803a94100faSBill Paul continue; 1804a94100faSBill Paul } 1805a94100faSBill Paul 1806a94100faSBill Paul if (sc->rl_head != NULL) { 180722a11c96SJohn-Mark Gurney m->m_len = total_len % RE_RX_DESC_BUFLEN; 180822a11c96SJohn-Mark Gurney if (m->m_len == 0) 180922a11c96SJohn-Mark Gurney m->m_len = RE_RX_DESC_BUFLEN; 1810a94100faSBill Paul /* 1811a94100faSBill Paul * Special case: if there's 4 bytes or less 1812a94100faSBill Paul * in this buffer, the mbuf can be discarded: 1813a94100faSBill Paul * the last 4 bytes is the CRC, which we don't 1814a94100faSBill Paul * care about anyway. 1815a94100faSBill Paul */ 1816a94100faSBill Paul if (m->m_len <= ETHER_CRC_LEN) { 1817a94100faSBill Paul sc->rl_tail->m_len -= 1818a94100faSBill Paul (ETHER_CRC_LEN - m->m_len); 1819a94100faSBill Paul m_freem(m); 1820a94100faSBill Paul } else { 1821a94100faSBill Paul m->m_len -= ETHER_CRC_LEN; 1822a94100faSBill Paul m->m_flags &= ~M_PKTHDR; 1823a94100faSBill Paul sc->rl_tail->m_next = m; 1824a94100faSBill Paul } 1825a94100faSBill Paul m = sc->rl_head; 1826a94100faSBill Paul sc->rl_head = sc->rl_tail = NULL; 1827a94100faSBill Paul m->m_pkthdr.len = total_len - ETHER_CRC_LEN; 1828a94100faSBill Paul } else 1829a94100faSBill Paul m->m_pkthdr.len = m->m_len = 1830a94100faSBill Paul (total_len - ETHER_CRC_LEN); 1831a94100faSBill Paul 183222a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX 183322a11c96SJohn-Mark Gurney re_fixup_rx(m); 183422a11c96SJohn-Mark Gurney #endif 1835a94100faSBill Paul ifp->if_ipackets++; 1836a94100faSBill Paul m->m_pkthdr.rcvif = ifp; 1837a94100faSBill Paul 1838a94100faSBill Paul /* Do RX checksumming if enabled */ 1839a94100faSBill Paul 1840a94100faSBill Paul if (ifp->if_capenable & IFCAP_RXCSUM) { 1841a94100faSBill Paul 1842a94100faSBill Paul /* Check IP header checksum */ 1843a94100faSBill Paul if (rxstat & RL_RDESC_STAT_PROTOID) 1844a94100faSBill Paul m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED; 1845a94100faSBill Paul if (!(rxstat & RL_RDESC_STAT_IPSUMBAD)) 1846a94100faSBill Paul m->m_pkthdr.csum_flags |= CSUM_IP_VALID; 1847a94100faSBill Paul 1848a94100faSBill Paul /* Check TCP/UDP checksum */ 1849a94100faSBill Paul if ((RL_TCPPKT(rxstat) && 1850a94100faSBill Paul !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) || 1851a94100faSBill Paul (RL_UDPPKT(rxstat) && 1852a94100faSBill Paul !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) { 1853a94100faSBill Paul m->m_pkthdr.csum_flags |= 1854a94100faSBill Paul CSUM_DATA_VALID|CSUM_PSEUDO_HDR; 1855a94100faSBill Paul m->m_pkthdr.csum_data = 0xffff; 1856a94100faSBill Paul } 1857a94100faSBill Paul } 1858ed510fb0SBill Paul maxpkt--; 1859d147662cSGleb Smirnoff if (rxvlan & RL_RDESC_VLANCTL_TAG) { 186078ba57b9SAndre Oppermann m->m_pkthdr.ether_vtag = 186178ba57b9SAndre Oppermann ntohs((rxvlan & RL_RDESC_VLANCTL_DATA)); 186278ba57b9SAndre Oppermann m->m_flags |= M_VLANTAG; 1863d147662cSGleb Smirnoff } 18645120abbfSSam Leffler RL_UNLOCK(sc); 1865a94100faSBill Paul (*ifp->if_input)(ifp, m); 18665120abbfSSam Leffler RL_LOCK(sc); 1867a94100faSBill Paul } 1868a94100faSBill Paul 1869a94100faSBill Paul /* Flush the RX DMA ring */ 1870a94100faSBill Paul 1871a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 1872a94100faSBill Paul sc->rl_ldata.rl_rx_list_map, 1873a94100faSBill Paul BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 1874a94100faSBill Paul 1875a94100faSBill Paul sc->rl_ldata.rl_rx_prodidx = i; 1876ed510fb0SBill Paul 1877ed510fb0SBill Paul if (maxpkt) 1878ed510fb0SBill Paul return(EAGAIN); 1879ed510fb0SBill Paul 1880ed510fb0SBill Paul return(0); 1881a94100faSBill Paul } 1882a94100faSBill Paul 1883a94100faSBill Paul static void 1884a94100faSBill Paul re_txeof(sc) 1885a94100faSBill Paul struct rl_softc *sc; 1886a94100faSBill Paul { 1887a94100faSBill Paul struct ifnet *ifp; 1888d65abd66SPyun YongHyeon struct rl_txdesc *txd; 1889a94100faSBill Paul u_int32_t txstat; 1890d65abd66SPyun YongHyeon int cons; 1891d65abd66SPyun YongHyeon 1892d65abd66SPyun YongHyeon cons = sc->rl_ldata.rl_tx_considx; 1893d65abd66SPyun YongHyeon if (cons == sc->rl_ldata.rl_tx_prodidx) 1894d65abd66SPyun YongHyeon return; 1895a94100faSBill Paul 1896fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 1897a94100faSBill Paul /* Invalidate the TX descriptor list */ 1898a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 1899a94100faSBill Paul sc->rl_ldata.rl_tx_list_map, 1900d65abd66SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 1901a94100faSBill Paul 1902d65abd66SPyun YongHyeon for (; cons != sc->rl_ldata.rl_tx_prodidx; 1903d65abd66SPyun YongHyeon cons = RL_TX_DESC_NXT(sc, cons)) { 1904d65abd66SPyun YongHyeon txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat); 1905d65abd66SPyun YongHyeon if (txstat & RL_TDESC_STAT_OWN) 1906a94100faSBill Paul break; 1907a94100faSBill Paul /* 1908a94100faSBill Paul * We only stash mbufs in the last descriptor 1909a94100faSBill Paul * in a fragment chain, which also happens to 1910a94100faSBill Paul * be the only place where the TX status bits 1911a94100faSBill Paul * are valid. 1912a94100faSBill Paul */ 1913a94100faSBill Paul if (txstat & RL_TDESC_CMD_EOF) { 1914d65abd66SPyun YongHyeon txd = &sc->rl_ldata.rl_tx_desc[cons]; 1915d65abd66SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, 1916d65abd66SPyun YongHyeon txd->tx_dmamap, BUS_DMASYNC_POSTWRITE); 1917d65abd66SPyun YongHyeon bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, 1918d65abd66SPyun YongHyeon txd->tx_dmamap); 1919d65abd66SPyun YongHyeon KASSERT(txd->tx_m != NULL, 1920d65abd66SPyun YongHyeon ("%s: freeing NULL mbufs!", __func__)); 1921d65abd66SPyun YongHyeon m_freem(txd->tx_m); 1922d65abd66SPyun YongHyeon txd->tx_m = NULL; 1923a94100faSBill Paul if (txstat & (RL_TDESC_STAT_EXCESSCOL| 1924a94100faSBill Paul RL_TDESC_STAT_COLCNT)) 1925a94100faSBill Paul ifp->if_collisions++; 1926a94100faSBill Paul if (txstat & RL_TDESC_STAT_TXERRSUM) 1927a94100faSBill Paul ifp->if_oerrors++; 1928a94100faSBill Paul else 1929a94100faSBill Paul ifp->if_opackets++; 1930a94100faSBill Paul } 1931a94100faSBill Paul sc->rl_ldata.rl_tx_free++; 1932d65abd66SPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1933a94100faSBill Paul } 1934d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_considx = cons; 1935a94100faSBill Paul 1936a94100faSBill Paul /* No changes made to the TX ring, so no flush needed */ 1937a94100faSBill Paul 1938d65abd66SPyun YongHyeon if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) { 19390fc4974fSBill Paul /* 1940b4b95879SMarius Strobl * Some chips will ignore a second TX request issued 1941b4b95879SMarius Strobl * while an existing transmission is in progress. If 1942b4b95879SMarius Strobl * the transmitter goes idle but there are still 1943b4b95879SMarius Strobl * packets waiting to be sent, we need to restart the 1944b4b95879SMarius Strobl * channel here to flush them out. This only seems to 1945b4b95879SMarius Strobl * be required with the PCIe devices. 19460fc4974fSBill Paul */ 19470fc4974fSBill Paul CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 19480fc4974fSBill Paul 1949ed510fb0SBill Paul #ifdef RE_TX_MODERATION 1950a94100faSBill Paul /* 1951b4b95879SMarius Strobl * If not all descriptors have been reaped yet, reload 1952b4b95879SMarius Strobl * the timer so that we will eventually get another 1953a94100faSBill Paul * interrupt that will cause us to re-enter this routine. 1954a94100faSBill Paul * This is done in case the transmitter has gone idle. 1955a94100faSBill Paul */ 1956a94100faSBill Paul CSR_WRITE_4(sc, RL_TIMERCNT, 1); 1957ed510fb0SBill Paul #endif 1958b4b95879SMarius Strobl } else 1959b4b95879SMarius Strobl sc->rl_watchdog_timer = 0; 1960a94100faSBill Paul } 1961a94100faSBill Paul 1962a94100faSBill Paul static void 1963a94100faSBill Paul re_tick(xsc) 1964a94100faSBill Paul void *xsc; 1965a94100faSBill Paul { 1966a94100faSBill Paul struct rl_softc *sc; 1967d1754a9bSJohn Baldwin struct mii_data *mii; 1968ed510fb0SBill Paul struct ifnet *ifp; 1969a94100faSBill Paul 1970a94100faSBill Paul sc = xsc; 1971ed510fb0SBill Paul ifp = sc->rl_ifp; 197297b9d4baSJohn-Mark Gurney 197397b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 197497b9d4baSJohn-Mark Gurney 19751d545c7aSMarius Strobl re_watchdog(sc); 1976a94100faSBill Paul 19771d545c7aSMarius Strobl mii = device_get_softc(sc->rl_miibus); 1978a94100faSBill Paul mii_tick(mii); 1979351a76f9SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_LINK) != 0) { 1980ed510fb0SBill Paul if (!(mii->mii_media_status & IFM_ACTIVE)) 1981351a76f9SPyun YongHyeon sc->rl_flags &= ~RL_FLAG_LINK; 1982ed510fb0SBill Paul } else { 1983ed510fb0SBill Paul if (mii->mii_media_status & IFM_ACTIVE && 1984ed510fb0SBill Paul IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 1985351a76f9SPyun YongHyeon sc->rl_flags |= RL_FLAG_LINK; 1986ed510fb0SBill Paul if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1987ed510fb0SBill Paul taskqueue_enqueue_fast(taskqueue_fast, 1988ed510fb0SBill Paul &sc->rl_txtask); 1989ed510fb0SBill Paul } 1990ed510fb0SBill Paul } 1991a94100faSBill Paul 1992d1754a9bSJohn Baldwin callout_reset(&sc->rl_stat_callout, hz, re_tick, sc); 1993a94100faSBill Paul } 1994a94100faSBill Paul 1995a94100faSBill Paul #ifdef DEVICE_POLLING 1996a94100faSBill Paul static void 1997a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 1998a94100faSBill Paul { 1999a94100faSBill Paul struct rl_softc *sc = ifp->if_softc; 2000a94100faSBill Paul 2001a94100faSBill Paul RL_LOCK(sc); 200240929967SGleb Smirnoff if (ifp->if_drv_flags & IFF_DRV_RUNNING) 200397b9d4baSJohn-Mark Gurney re_poll_locked(ifp, cmd, count); 200497b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 200597b9d4baSJohn-Mark Gurney } 200697b9d4baSJohn-Mark Gurney 200797b9d4baSJohn-Mark Gurney static void 200897b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count) 200997b9d4baSJohn-Mark Gurney { 201097b9d4baSJohn-Mark Gurney struct rl_softc *sc = ifp->if_softc; 201197b9d4baSJohn-Mark Gurney 201297b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 201397b9d4baSJohn-Mark Gurney 2014a94100faSBill Paul sc->rxcycles = count; 2015a94100faSBill Paul re_rxeof(sc); 2016a94100faSBill Paul re_txeof(sc); 2017a94100faSBill Paul 201837652939SMax Laier if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2019ed510fb0SBill Paul taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask); 2020a94100faSBill Paul 2021a94100faSBill Paul if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */ 2022a94100faSBill Paul u_int16_t status; 2023a94100faSBill Paul 2024a94100faSBill Paul status = CSR_READ_2(sc, RL_ISR); 2025a94100faSBill Paul if (status == 0xffff) 202697b9d4baSJohn-Mark Gurney return; 2027a94100faSBill Paul if (status) 2028a94100faSBill Paul CSR_WRITE_2(sc, RL_ISR, status); 2029a94100faSBill Paul 2030a94100faSBill Paul /* 2031a94100faSBill Paul * XXX check behaviour on receiver stalls. 2032a94100faSBill Paul */ 2033a94100faSBill Paul 2034a94100faSBill Paul if (status & RL_ISR_SYSTEM_ERR) { 2035a94100faSBill Paul re_reset(sc); 203697b9d4baSJohn-Mark Gurney re_init_locked(sc); 2037a94100faSBill Paul } 2038a94100faSBill Paul } 2039a94100faSBill Paul } 2040a94100faSBill Paul #endif /* DEVICE_POLLING */ 2041a94100faSBill Paul 2042ef544f63SPaolo Pisati static int 2043a94100faSBill Paul re_intr(arg) 2044a94100faSBill Paul void *arg; 2045a94100faSBill Paul { 2046a94100faSBill Paul struct rl_softc *sc; 2047ed510fb0SBill Paul uint16_t status; 2048a94100faSBill Paul 2049a94100faSBill Paul sc = arg; 2050ed510fb0SBill Paul 2051ed510fb0SBill Paul status = CSR_READ_2(sc, RL_ISR); 2052498bd0d3SBill Paul if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0) 2053ef544f63SPaolo Pisati return (FILTER_STRAY); 2054ed510fb0SBill Paul CSR_WRITE_2(sc, RL_IMR, 0); 2055ed510fb0SBill Paul 2056ed510fb0SBill Paul taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask); 2057ed510fb0SBill Paul 2058ef544f63SPaolo Pisati return (FILTER_HANDLED); 2059ed510fb0SBill Paul } 2060ed510fb0SBill Paul 2061ed510fb0SBill Paul static void 2062ed510fb0SBill Paul re_int_task(arg, npending) 2063ed510fb0SBill Paul void *arg; 2064ed510fb0SBill Paul int npending; 2065ed510fb0SBill Paul { 2066ed510fb0SBill Paul struct rl_softc *sc; 2067ed510fb0SBill Paul struct ifnet *ifp; 2068ed510fb0SBill Paul u_int16_t status; 2069ed510fb0SBill Paul int rval = 0; 2070ed510fb0SBill Paul 2071ed510fb0SBill Paul sc = arg; 2072ed510fb0SBill Paul ifp = sc->rl_ifp; 2073a94100faSBill Paul 2074a94100faSBill Paul RL_LOCK(sc); 207597b9d4baSJohn-Mark Gurney 2076a94100faSBill Paul status = CSR_READ_2(sc, RL_ISR); 2077a94100faSBill Paul CSR_WRITE_2(sc, RL_ISR, status); 2078a94100faSBill Paul 2079d65abd66SPyun YongHyeon if (sc->suspended || 2080d65abd66SPyun YongHyeon (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 2081ed510fb0SBill Paul RL_UNLOCK(sc); 2082ed510fb0SBill Paul return; 2083ed510fb0SBill Paul } 2084a94100faSBill Paul 2085ed510fb0SBill Paul #ifdef DEVICE_POLLING 2086ed510fb0SBill Paul if (ifp->if_capenable & IFCAP_POLLING) { 2087ed510fb0SBill Paul RL_UNLOCK(sc); 2088ed510fb0SBill Paul return; 2089ed510fb0SBill Paul } 2090ed510fb0SBill Paul #endif 2091a94100faSBill Paul 2092ed510fb0SBill Paul if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW)) 2093ed510fb0SBill Paul rval = re_rxeof(sc); 2094ed510fb0SBill Paul 2095ed510fb0SBill Paul #ifdef RE_TX_MODERATION 2096ed510fb0SBill Paul if (status & (RL_ISR_TIMEOUT_EXPIRED| 2097ed510fb0SBill Paul #else 2098ed510fb0SBill Paul if (status & (RL_ISR_TX_OK| 2099ed510fb0SBill Paul #endif 2100ed510fb0SBill Paul RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL)) 2101a94100faSBill Paul re_txeof(sc); 2102a94100faSBill Paul 2103a94100faSBill Paul if (status & RL_ISR_SYSTEM_ERR) { 2104a94100faSBill Paul re_reset(sc); 210597b9d4baSJohn-Mark Gurney re_init_locked(sc); 2106a94100faSBill Paul } 2107a94100faSBill Paul 2108a94100faSBill Paul if (status & RL_ISR_LINKCHG) { 2109d1754a9bSJohn Baldwin callout_stop(&sc->rl_stat_callout); 2110d1754a9bSJohn Baldwin re_tick(sc); 2111a94100faSBill Paul } 2112a94100faSBill Paul 211352732175SMax Laier if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2114ed510fb0SBill Paul taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask); 2115a94100faSBill Paul 2116a94100faSBill Paul RL_UNLOCK(sc); 2117ed510fb0SBill Paul 2118ed510fb0SBill Paul if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) { 2119ed510fb0SBill Paul taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask); 2120ed510fb0SBill Paul return; 2121ed510fb0SBill Paul } 2122ed510fb0SBill Paul 2123ed510fb0SBill Paul CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS); 2124ed510fb0SBill Paul 2125ed510fb0SBill Paul return; 2126a94100faSBill Paul } 2127a94100faSBill Paul 2128d65abd66SPyun YongHyeon static int 2129d65abd66SPyun YongHyeon re_encap(sc, m_head) 2130d65abd66SPyun YongHyeon struct rl_softc *sc; 2131d65abd66SPyun YongHyeon struct mbuf **m_head; 2132d65abd66SPyun YongHyeon { 2133d65abd66SPyun YongHyeon struct rl_txdesc *txd, *txd_last; 2134d65abd66SPyun YongHyeon bus_dma_segment_t segs[RL_NTXSEGS]; 2135d65abd66SPyun YongHyeon bus_dmamap_t map; 2136d65abd66SPyun YongHyeon struct mbuf *m_new; 2137d65abd66SPyun YongHyeon struct rl_desc *desc; 2138d65abd66SPyun YongHyeon int nsegs, prod; 2139d65abd66SPyun YongHyeon int i, error, ei, si; 2140d65abd66SPyun YongHyeon int padlen; 2141ccf34c81SPyun YongHyeon uint32_t cmdstat, csum_flags, vlanctl; 2142a94100faSBill Paul 2143d65abd66SPyun YongHyeon RL_LOCK_ASSERT(sc); 2144738489d1SPyun YongHyeon M_ASSERTPKTHDR((*m_head)); 21450fc4974fSBill Paul 21460fc4974fSBill Paul /* 21470fc4974fSBill Paul * With some of the RealTek chips, using the checksum offload 21480fc4974fSBill Paul * support in conjunction with the autopadding feature results 21490fc4974fSBill Paul * in the transmission of corrupt frames. For example, if we 21500fc4974fSBill Paul * need to send a really small IP fragment that's less than 60 21510fc4974fSBill Paul * bytes in size, and IP header checksumming is enabled, the 21520fc4974fSBill Paul * resulting ethernet frame that appears on the wire will 215399c8ae87SPyun YongHyeon * have garbled payload. To work around this, if TX IP checksum 21540fc4974fSBill Paul * offload is enabled, we always manually pad short frames out 2155d65abd66SPyun YongHyeon * to the minimum ethernet frame size. 21560fc4974fSBill Paul */ 2157a4148af5SPyun YongHyeon if ((*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN && 215899c8ae87SPyun YongHyeon ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) { 2159d65abd66SPyun YongHyeon padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len; 2160d65abd66SPyun YongHyeon if (M_WRITABLE(*m_head) == 0) { 2161d65abd66SPyun YongHyeon /* Get a writable copy. */ 2162d65abd66SPyun YongHyeon m_new = m_dup(*m_head, M_DONTWAIT); 2163d65abd66SPyun YongHyeon m_freem(*m_head); 2164d65abd66SPyun YongHyeon if (m_new == NULL) { 2165d65abd66SPyun YongHyeon *m_head = NULL; 2166a94100faSBill Paul return (ENOBUFS); 2167a94100faSBill Paul } 2168d65abd66SPyun YongHyeon *m_head = m_new; 2169d65abd66SPyun YongHyeon } 2170d65abd66SPyun YongHyeon if ((*m_head)->m_next != NULL || 2171d65abd66SPyun YongHyeon M_TRAILINGSPACE(*m_head) < padlen) { 217280a2a305SJohn-Mark Gurney m_new = m_defrag(*m_head, M_DONTWAIT); 2173b4b95879SMarius Strobl if (m_new == NULL) { 2174b4b95879SMarius Strobl m_freem(*m_head); 2175b4b95879SMarius Strobl *m_head = NULL; 217680a2a305SJohn-Mark Gurney return (ENOBUFS); 2177b4b95879SMarius Strobl } 2178d65abd66SPyun YongHyeon } else 2179d65abd66SPyun YongHyeon m_new = *m_head; 2180a94100faSBill Paul 21810fc4974fSBill Paul /* 21820fc4974fSBill Paul * Manually pad short frames, and zero the pad space 21830fc4974fSBill Paul * to avoid leaking data. 21840fc4974fSBill Paul */ 2185d65abd66SPyun YongHyeon bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen); 2186d65abd66SPyun YongHyeon m_new->m_pkthdr.len += padlen; 21870fc4974fSBill Paul m_new->m_len = m_new->m_pkthdr.len; 2188d65abd66SPyun YongHyeon *m_head = m_new; 21890fc4974fSBill Paul } 21900fc4974fSBill Paul 2191d65abd66SPyun YongHyeon prod = sc->rl_ldata.rl_tx_prodidx; 2192d65abd66SPyun YongHyeon txd = &sc->rl_ldata.rl_tx_desc[prod]; 2193d65abd66SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap, 2194d65abd66SPyun YongHyeon *m_head, segs, &nsegs, BUS_DMA_NOWAIT); 2195d65abd66SPyun YongHyeon if (error == EFBIG) { 2196304a4c6fSJohn Baldwin m_new = m_collapse(*m_head, M_DONTWAIT, RL_NTXSEGS); 2197d65abd66SPyun YongHyeon if (m_new == NULL) { 2198d65abd66SPyun YongHyeon m_freem(*m_head); 2199b4b95879SMarius Strobl *m_head = NULL; 2200d65abd66SPyun YongHyeon return (ENOBUFS); 2201a94100faSBill Paul } 2202d65abd66SPyun YongHyeon *m_head = m_new; 2203d65abd66SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, 2204d65abd66SPyun YongHyeon txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT); 2205d65abd66SPyun YongHyeon if (error != 0) { 2206d65abd66SPyun YongHyeon m_freem(*m_head); 2207d65abd66SPyun YongHyeon *m_head = NULL; 2208d65abd66SPyun YongHyeon return (error); 2209a94100faSBill Paul } 2210d65abd66SPyun YongHyeon } else if (error != 0) 2211d65abd66SPyun YongHyeon return (error); 2212d65abd66SPyun YongHyeon if (nsegs == 0) { 2213d65abd66SPyun YongHyeon m_freem(*m_head); 2214d65abd66SPyun YongHyeon *m_head = NULL; 2215d65abd66SPyun YongHyeon return (EIO); 2216d65abd66SPyun YongHyeon } 2217d65abd66SPyun YongHyeon 2218d65abd66SPyun YongHyeon /* Check for number of available descriptors. */ 2219d65abd66SPyun YongHyeon if (sc->rl_ldata.rl_tx_free - nsegs <= 1) { 2220d65abd66SPyun YongHyeon bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap); 2221d65abd66SPyun YongHyeon return (ENOBUFS); 2222d65abd66SPyun YongHyeon } 2223d65abd66SPyun YongHyeon 2224d65abd66SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap, 2225d65abd66SPyun YongHyeon BUS_DMASYNC_PREWRITE); 2226a94100faSBill Paul 2227a94100faSBill Paul /* 2228d65abd66SPyun YongHyeon * Set up checksum offload. Note: checksum offload bits must 2229d65abd66SPyun YongHyeon * appear in all descriptors of a multi-descriptor transmit 2230d65abd66SPyun YongHyeon * attempt. This is according to testing done with an 8169 2231d65abd66SPyun YongHyeon * chip. This is a requirement. 2232a94100faSBill Paul */ 2233d65abd66SPyun YongHyeon csum_flags = 0; 2234d65abd66SPyun YongHyeon if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0) 2235d65abd66SPyun YongHyeon csum_flags = RL_TDESC_CMD_LGSEND | 2236d65abd66SPyun YongHyeon ((uint32_t)(*m_head)->m_pkthdr.tso_segsz << 2237d65abd66SPyun YongHyeon RL_TDESC_CMD_MSSVAL_SHIFT); 2238d65abd66SPyun YongHyeon else { 223999c8ae87SPyun YongHyeon /* 224099c8ae87SPyun YongHyeon * Unconditionally enable IP checksum if TCP or UDP 224199c8ae87SPyun YongHyeon * checksum is required. Otherwise, TCP/UDP checksum 224299c8ae87SPyun YongHyeon * does't make effects. 224399c8ae87SPyun YongHyeon */ 224499c8ae87SPyun YongHyeon if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) { 2245d65abd66SPyun YongHyeon csum_flags |= RL_TDESC_CMD_IPCSUM; 224699c8ae87SPyun YongHyeon if (((*m_head)->m_pkthdr.csum_flags & CSUM_TCP) != 0) 2247d65abd66SPyun YongHyeon csum_flags |= RL_TDESC_CMD_TCPCSUM; 224899c8ae87SPyun YongHyeon if (((*m_head)->m_pkthdr.csum_flags & CSUM_UDP) != 0) 2249d65abd66SPyun YongHyeon csum_flags |= RL_TDESC_CMD_UDPCSUM; 2250d65abd66SPyun YongHyeon } 225199c8ae87SPyun YongHyeon } 2252a94100faSBill Paul 2253ccf34c81SPyun YongHyeon /* 2254ccf34c81SPyun YongHyeon * Set up hardware VLAN tagging. Note: vlan tag info must 2255ccf34c81SPyun YongHyeon * appear in all descriptors of a multi-descriptor 2256ccf34c81SPyun YongHyeon * transmission attempt. 2257ccf34c81SPyun YongHyeon */ 2258ccf34c81SPyun YongHyeon vlanctl = 0; 2259ccf34c81SPyun YongHyeon if ((*m_head)->m_flags & M_VLANTAG) 2260ccf34c81SPyun YongHyeon vlanctl = 2261ccf34c81SPyun YongHyeon htole32(htons((*m_head)->m_pkthdr.ether_vtag) | 2262ccf34c81SPyun YongHyeon RL_TDESC_VLANCTL_TAG); 2263ccf34c81SPyun YongHyeon 2264d65abd66SPyun YongHyeon si = prod; 2265d65abd66SPyun YongHyeon for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) { 2266d65abd66SPyun YongHyeon desc = &sc->rl_ldata.rl_tx_list[prod]; 2267ccf34c81SPyun YongHyeon desc->rl_vlanctl = vlanctl; 2268d65abd66SPyun YongHyeon desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr)); 2269d65abd66SPyun YongHyeon desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr)); 2270d65abd66SPyun YongHyeon cmdstat = segs[i].ds_len; 2271d65abd66SPyun YongHyeon if (i != 0) 2272d65abd66SPyun YongHyeon cmdstat |= RL_TDESC_CMD_OWN; 2273d65abd66SPyun YongHyeon if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1) 2274d65abd66SPyun YongHyeon cmdstat |= RL_TDESC_CMD_EOR; 2275d65abd66SPyun YongHyeon desc->rl_cmdstat = htole32(cmdstat | csum_flags); 2276d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_free--; 2277d65abd66SPyun YongHyeon } 2278d65abd66SPyun YongHyeon /* Update producer index. */ 2279d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_prodidx = prod; 2280a94100faSBill Paul 2281d65abd66SPyun YongHyeon /* Set EOF on the last descriptor. */ 2282d65abd66SPyun YongHyeon ei = RL_TX_DESC_PRV(sc, prod); 2283d65abd66SPyun YongHyeon desc = &sc->rl_ldata.rl_tx_list[ei]; 2284d65abd66SPyun YongHyeon desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF); 2285d65abd66SPyun YongHyeon 2286d65abd66SPyun YongHyeon desc = &sc->rl_ldata.rl_tx_list[si]; 2287d65abd66SPyun YongHyeon /* Set SOF and transfer ownership of packet to the chip. */ 2288d65abd66SPyun YongHyeon desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF); 2289a94100faSBill Paul 2290d65abd66SPyun YongHyeon /* 2291d65abd66SPyun YongHyeon * Insure that the map for this transmission 2292d65abd66SPyun YongHyeon * is placed at the array index of the last descriptor 2293d65abd66SPyun YongHyeon * in this chain. (Swap last and first dmamaps.) 2294d65abd66SPyun YongHyeon */ 2295d65abd66SPyun YongHyeon txd_last = &sc->rl_ldata.rl_tx_desc[ei]; 2296d65abd66SPyun YongHyeon map = txd->tx_dmamap; 2297d65abd66SPyun YongHyeon txd->tx_dmamap = txd_last->tx_dmamap; 2298d65abd66SPyun YongHyeon txd_last->tx_dmamap = map; 2299d65abd66SPyun YongHyeon txd_last->tx_m = *m_head; 2300a94100faSBill Paul 2301a94100faSBill Paul return (0); 2302a94100faSBill Paul } 2303a94100faSBill Paul 230497b9d4baSJohn-Mark Gurney static void 2305ed510fb0SBill Paul re_tx_task(arg, npending) 2306ed510fb0SBill Paul void *arg; 2307ed510fb0SBill Paul int npending; 230897b9d4baSJohn-Mark Gurney { 2309ed510fb0SBill Paul struct ifnet *ifp; 231097b9d4baSJohn-Mark Gurney 2311ed510fb0SBill Paul ifp = arg; 2312ed510fb0SBill Paul re_start(ifp); 2313ed510fb0SBill Paul 2314ed510fb0SBill Paul return; 231597b9d4baSJohn-Mark Gurney } 231697b9d4baSJohn-Mark Gurney 2317a94100faSBill Paul /* 2318a94100faSBill Paul * Main transmit routine for C+ and gigE NICs. 2319a94100faSBill Paul */ 2320a94100faSBill Paul static void 2321ed510fb0SBill Paul re_start(ifp) 2322a94100faSBill Paul struct ifnet *ifp; 2323a94100faSBill Paul { 2324a94100faSBill Paul struct rl_softc *sc; 2325d65abd66SPyun YongHyeon struct mbuf *m_head; 2326d65abd66SPyun YongHyeon int queued; 2327a94100faSBill Paul 2328a94100faSBill Paul sc = ifp->if_softc; 232997b9d4baSJohn-Mark Gurney 2330ed510fb0SBill Paul RL_LOCK(sc); 2331ed510fb0SBill Paul 2332d65abd66SPyun YongHyeon if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 2333351a76f9SPyun YongHyeon IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0) { 2334ed510fb0SBill Paul RL_UNLOCK(sc); 2335ed510fb0SBill Paul return; 2336ed510fb0SBill Paul } 2337a94100faSBill Paul 2338d65abd66SPyun YongHyeon for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) && 2339d65abd66SPyun YongHyeon sc->rl_ldata.rl_tx_free > 1;) { 234052732175SMax Laier IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head); 2341a94100faSBill Paul if (m_head == NULL) 2342a94100faSBill Paul break; 2343a94100faSBill Paul 2344d65abd66SPyun YongHyeon if (re_encap(sc, &m_head) != 0) { 2345b4b95879SMarius Strobl if (m_head == NULL) 2346b4b95879SMarius Strobl break; 234752732175SMax Laier IFQ_DRV_PREPEND(&ifp->if_snd, m_head); 234813f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2349a94100faSBill Paul break; 2350a94100faSBill Paul } 2351a94100faSBill Paul 2352a94100faSBill Paul /* 2353a94100faSBill Paul * If there's a BPF listener, bounce a copy of this frame 2354a94100faSBill Paul * to him. 2355a94100faSBill Paul */ 235659a0d28bSChristian S.J. Peron ETHER_BPF_MTAP(ifp, m_head); 235752732175SMax Laier 235852732175SMax Laier queued++; 2359a94100faSBill Paul } 2360a94100faSBill Paul 2361ed510fb0SBill Paul if (queued == 0) { 2362ed510fb0SBill Paul #ifdef RE_TX_MODERATION 2363d65abd66SPyun YongHyeon if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) 2364ed510fb0SBill Paul CSR_WRITE_4(sc, RL_TIMERCNT, 1); 2365ed510fb0SBill Paul #endif 2366ed510fb0SBill Paul RL_UNLOCK(sc); 236752732175SMax Laier return; 2368ed510fb0SBill Paul } 236952732175SMax Laier 2370a94100faSBill Paul /* Flush the TX descriptors */ 2371a94100faSBill Paul 2372a94100faSBill Paul bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 2373a94100faSBill Paul sc->rl_ldata.rl_tx_list_map, 2374a94100faSBill Paul BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 2375a94100faSBill Paul 23760fc4974fSBill Paul CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 2377a94100faSBill Paul 2378ed510fb0SBill Paul #ifdef RE_TX_MODERATION 2379a94100faSBill Paul /* 2380a94100faSBill Paul * Use the countdown timer for interrupt moderation. 2381a94100faSBill Paul * 'TX done' interrupts are disabled. Instead, we reset the 2382a94100faSBill Paul * countdown timer, which will begin counting until it hits 2383a94100faSBill Paul * the value in the TIMERINT register, and then trigger an 2384a94100faSBill Paul * interrupt. Each time we write to the TIMERCNT register, 2385a94100faSBill Paul * the timer count is reset to 0. 2386a94100faSBill Paul */ 2387a94100faSBill Paul CSR_WRITE_4(sc, RL_TIMERCNT, 1); 2388ed510fb0SBill Paul #endif 2389a94100faSBill Paul 2390a94100faSBill Paul /* 2391a94100faSBill Paul * Set a timeout in case the chip goes out to lunch. 2392a94100faSBill Paul */ 23931d545c7aSMarius Strobl sc->rl_watchdog_timer = 5; 2394ed510fb0SBill Paul 2395ed510fb0SBill Paul RL_UNLOCK(sc); 2396ed510fb0SBill Paul 2397ed510fb0SBill Paul return; 2398a94100faSBill Paul } 2399a94100faSBill Paul 2400a94100faSBill Paul static void 2401a94100faSBill Paul re_init(xsc) 2402a94100faSBill Paul void *xsc; 2403a94100faSBill Paul { 2404a94100faSBill Paul struct rl_softc *sc = xsc; 240597b9d4baSJohn-Mark Gurney 240697b9d4baSJohn-Mark Gurney RL_LOCK(sc); 240797b9d4baSJohn-Mark Gurney re_init_locked(sc); 240897b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 240997b9d4baSJohn-Mark Gurney } 241097b9d4baSJohn-Mark Gurney 241197b9d4baSJohn-Mark Gurney static void 241297b9d4baSJohn-Mark Gurney re_init_locked(sc) 241397b9d4baSJohn-Mark Gurney struct rl_softc *sc; 241497b9d4baSJohn-Mark Gurney { 2415fc74a9f9SBrooks Davis struct ifnet *ifp = sc->rl_ifp; 2416a94100faSBill Paul struct mii_data *mii; 2417a94100faSBill Paul u_int32_t rxcfg = 0; 241870acaecfSPyun YongHyeon uint16_t cfg; 24194d3d7085SBernd Walter union { 24204d3d7085SBernd Walter uint32_t align_dummy; 24214d3d7085SBernd Walter u_char eaddr[ETHER_ADDR_LEN]; 24224d3d7085SBernd Walter } eaddr; 2423a94100faSBill Paul 242497b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 242597b9d4baSJohn-Mark Gurney 2426a94100faSBill Paul mii = device_get_softc(sc->rl_miibus); 2427a94100faSBill Paul 2428a94100faSBill Paul /* 2429a94100faSBill Paul * Cancel pending I/O and free all RX/TX buffers. 2430a94100faSBill Paul */ 2431a94100faSBill Paul re_stop(sc); 2432a94100faSBill Paul 2433a94100faSBill Paul /* 2434c2c6548bSBill Paul * Enable C+ RX and TX mode, as well as VLAN stripping and 2435edd03374SBill Paul * RX checksum offload. We must configure the C+ register 2436c2c6548bSBill Paul * before all others. 2437c2c6548bSBill Paul */ 243870acaecfSPyun YongHyeon cfg = RL_CPLUSCMD_PCI_MRW; 243970acaecfSPyun YongHyeon if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) 244070acaecfSPyun YongHyeon cfg |= RL_CPLUSCMD_RXCSUM_ENB; 244170acaecfSPyun YongHyeon if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) 244270acaecfSPyun YongHyeon cfg |= RL_CPLUSCMD_VLANSTRIP; 244370acaecfSPyun YongHyeon CSR_WRITE_2(sc, RL_CPLUS_CMD, 244470acaecfSPyun YongHyeon cfg | RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB); 2445c2c6548bSBill Paul 2446c2c6548bSBill Paul /* 2447a94100faSBill Paul * Init our MAC address. Even though the chipset 2448a94100faSBill Paul * documentation doesn't mention it, we need to enter "Config 2449a94100faSBill Paul * register write enable" mode to modify the ID registers. 2450a94100faSBill Paul */ 24514d3d7085SBernd Walter /* Copy MAC address on stack to align. */ 24524d3d7085SBernd Walter bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN); 2453a94100faSBill Paul CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG); 2454ed510fb0SBill Paul CSR_WRITE_4(sc, RL_IDR0, 2455ed510fb0SBill Paul htole32(*(u_int32_t *)(&eaddr.eaddr[0]))); 2456ed510fb0SBill Paul CSR_WRITE_4(sc, RL_IDR4, 2457ed510fb0SBill Paul htole32(*(u_int32_t *)(&eaddr.eaddr[4]))); 2458a94100faSBill Paul CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 2459a94100faSBill Paul 2460a94100faSBill Paul /* 2461a94100faSBill Paul * For C+ mode, initialize the RX descriptors and mbufs. 2462a94100faSBill Paul */ 2463a94100faSBill Paul re_rx_list_init(sc); 2464a94100faSBill Paul re_tx_list_init(sc); 2465a94100faSBill Paul 2466a94100faSBill Paul /* 2467d01fac16SPyun YongHyeon * Load the addresses of the RX and TX lists into the chip. 2468d01fac16SPyun YongHyeon */ 2469d01fac16SPyun YongHyeon 2470d01fac16SPyun YongHyeon CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI, 2471d01fac16SPyun YongHyeon RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr)); 2472d01fac16SPyun YongHyeon CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO, 2473d01fac16SPyun YongHyeon RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr)); 2474d01fac16SPyun YongHyeon 2475d01fac16SPyun YongHyeon CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI, 2476d01fac16SPyun YongHyeon RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr)); 2477d01fac16SPyun YongHyeon CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO, 2478d01fac16SPyun YongHyeon RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr)); 2479d01fac16SPyun YongHyeon 2480d01fac16SPyun YongHyeon /* 2481a94100faSBill Paul * Enable transmit and receive. 2482a94100faSBill Paul */ 2483a94100faSBill Paul CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB); 2484a94100faSBill Paul 2485a94100faSBill Paul /* 2486a94100faSBill Paul * Set the initial TX and RX configuration. 2487a94100faSBill Paul */ 2488abc8ff44SBill Paul if (sc->rl_testmode) { 2489abc8ff44SBill Paul if (sc->rl_type == RL_8169) 2490abc8ff44SBill Paul CSR_WRITE_4(sc, RL_TXCFG, 2491abc8ff44SBill Paul RL_TXCFG_CONFIG|RL_LOOPTEST_ON); 2492a94100faSBill Paul else 2493abc8ff44SBill Paul CSR_WRITE_4(sc, RL_TXCFG, 2494abc8ff44SBill Paul RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS); 2495abc8ff44SBill Paul } else 2496a94100faSBill Paul CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG); 2497d01fac16SPyun YongHyeon 2498d01fac16SPyun YongHyeon CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16); 2499d01fac16SPyun YongHyeon 2500a94100faSBill Paul CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG); 2501a94100faSBill Paul 2502a94100faSBill Paul /* Set the individual bit to receive frames for this host only. */ 2503a94100faSBill Paul rxcfg = CSR_READ_4(sc, RL_RXCFG); 2504a94100faSBill Paul rxcfg |= RL_RXCFG_RX_INDIV; 2505a94100faSBill Paul 2506a94100faSBill Paul /* If we want promiscuous mode, set the allframes bit. */ 250761021536SJohn-Mark Gurney if (ifp->if_flags & IFF_PROMISC) 2508a94100faSBill Paul rxcfg |= RL_RXCFG_RX_ALLPHYS; 250961021536SJohn-Mark Gurney else 2510a94100faSBill Paul rxcfg &= ~RL_RXCFG_RX_ALLPHYS; 2511a94100faSBill Paul CSR_WRITE_4(sc, RL_RXCFG, rxcfg); 2512a94100faSBill Paul 2513a94100faSBill Paul /* 2514a94100faSBill Paul * Set capture broadcast bit to capture broadcast frames. 2515a94100faSBill Paul */ 251661021536SJohn-Mark Gurney if (ifp->if_flags & IFF_BROADCAST) 2517a94100faSBill Paul rxcfg |= RL_RXCFG_RX_BROAD; 251861021536SJohn-Mark Gurney else 2519a94100faSBill Paul rxcfg &= ~RL_RXCFG_RX_BROAD; 2520a94100faSBill Paul CSR_WRITE_4(sc, RL_RXCFG, rxcfg); 2521a94100faSBill Paul 2522a94100faSBill Paul /* 2523a94100faSBill Paul * Program the multicast filter, if necessary. 2524a94100faSBill Paul */ 2525a94100faSBill Paul re_setmulti(sc); 2526a94100faSBill Paul 2527a94100faSBill Paul #ifdef DEVICE_POLLING 2528a94100faSBill Paul /* 2529a94100faSBill Paul * Disable interrupts if we are polling. 2530a94100faSBill Paul */ 253140929967SGleb Smirnoff if (ifp->if_capenable & IFCAP_POLLING) 2532a94100faSBill Paul CSR_WRITE_2(sc, RL_IMR, 0); 2533a94100faSBill Paul else /* otherwise ... */ 253440929967SGleb Smirnoff #endif 2535ed510fb0SBill Paul 2536a94100faSBill Paul /* 2537a94100faSBill Paul * Enable interrupts. 2538a94100faSBill Paul */ 2539a94100faSBill Paul if (sc->rl_testmode) 2540a94100faSBill Paul CSR_WRITE_2(sc, RL_IMR, 0); 2541a94100faSBill Paul else 2542a94100faSBill Paul CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS); 2543ed510fb0SBill Paul CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS); 2544a94100faSBill Paul 2545a94100faSBill Paul /* Set initial TX threshold */ 2546a94100faSBill Paul sc->rl_txthresh = RL_TX_THRESH_INIT; 2547a94100faSBill Paul 2548a94100faSBill Paul /* Start RX/TX process. */ 2549a94100faSBill Paul CSR_WRITE_4(sc, RL_MISSEDPKT, 0); 2550a94100faSBill Paul #ifdef notdef 2551a94100faSBill Paul /* Enable receiver and transmitter. */ 2552a94100faSBill Paul CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB); 2553a94100faSBill Paul #endif 2554a94100faSBill Paul 2555ed510fb0SBill Paul #ifdef RE_TX_MODERATION 2556a94100faSBill Paul /* 2557a94100faSBill Paul * Initialize the timer interrupt register so that 2558a94100faSBill Paul * a timer interrupt will be generated once the timer 2559a94100faSBill Paul * reaches a certain number of ticks. The timer is 2560a94100faSBill Paul * reloaded on each transmit. This gives us TX interrupt 2561a94100faSBill Paul * moderation, which dramatically improves TX frame rate. 2562a94100faSBill Paul */ 2563a94100faSBill Paul if (sc->rl_type == RL_8169) 2564a94100faSBill Paul CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800); 2565a94100faSBill Paul else 2566a94100faSBill Paul CSR_WRITE_4(sc, RL_TIMERINT, 0x400); 2567ed510fb0SBill Paul #endif 2568a94100faSBill Paul 2569a94100faSBill Paul /* 2570a94100faSBill Paul * For 8169 gigE NICs, set the max allowed RX packet 2571a94100faSBill Paul * size so we can receive jumbo frames. 2572a94100faSBill Paul */ 2573a94100faSBill Paul if (sc->rl_type == RL_8169) 2574a94100faSBill Paul CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383); 2575a94100faSBill Paul 257697b9d4baSJohn-Mark Gurney if (sc->rl_testmode) 2577a94100faSBill Paul return; 2578a94100faSBill Paul 2579a94100faSBill Paul mii_mediachg(mii); 2580a94100faSBill Paul 258119ecd231SPyun YongHyeon CSR_WRITE_1(sc, RL_CFG1, CSR_READ_1(sc, RL_CFG1) | RL_CFG1_DRVLOAD); 2582a94100faSBill Paul 258313f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 258413f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2585a94100faSBill Paul 2586351a76f9SPyun YongHyeon sc->rl_flags &= ~RL_FLAG_LINK; 25871d545c7aSMarius Strobl sc->rl_watchdog_timer = 0; 2588d1754a9bSJohn Baldwin callout_reset(&sc->rl_stat_callout, hz, re_tick, sc); 2589a94100faSBill Paul } 2590a94100faSBill Paul 2591a94100faSBill Paul /* 2592a94100faSBill Paul * Set media options. 2593a94100faSBill Paul */ 2594a94100faSBill Paul static int 2595a94100faSBill Paul re_ifmedia_upd(ifp) 2596a94100faSBill Paul struct ifnet *ifp; 2597a94100faSBill Paul { 2598a94100faSBill Paul struct rl_softc *sc; 2599a94100faSBill Paul struct mii_data *mii; 2600a94100faSBill Paul 2601a94100faSBill Paul sc = ifp->if_softc; 2602a94100faSBill Paul mii = device_get_softc(sc->rl_miibus); 2603d1754a9bSJohn Baldwin RL_LOCK(sc); 2604a94100faSBill Paul mii_mediachg(mii); 2605d1754a9bSJohn Baldwin RL_UNLOCK(sc); 2606a94100faSBill Paul 2607a94100faSBill Paul return (0); 2608a94100faSBill Paul } 2609a94100faSBill Paul 2610a94100faSBill Paul /* 2611a94100faSBill Paul * Report current media status. 2612a94100faSBill Paul */ 2613a94100faSBill Paul static void 2614a94100faSBill Paul re_ifmedia_sts(ifp, ifmr) 2615a94100faSBill Paul struct ifnet *ifp; 2616a94100faSBill Paul struct ifmediareq *ifmr; 2617a94100faSBill Paul { 2618a94100faSBill Paul struct rl_softc *sc; 2619a94100faSBill Paul struct mii_data *mii; 2620a94100faSBill Paul 2621a94100faSBill Paul sc = ifp->if_softc; 2622a94100faSBill Paul mii = device_get_softc(sc->rl_miibus); 2623a94100faSBill Paul 2624d1754a9bSJohn Baldwin RL_LOCK(sc); 2625a94100faSBill Paul mii_pollstat(mii); 2626d1754a9bSJohn Baldwin RL_UNLOCK(sc); 2627a94100faSBill Paul ifmr->ifm_active = mii->mii_media_active; 2628a94100faSBill Paul ifmr->ifm_status = mii->mii_media_status; 2629a94100faSBill Paul } 2630a94100faSBill Paul 2631a94100faSBill Paul static int 2632a94100faSBill Paul re_ioctl(ifp, command, data) 2633a94100faSBill Paul struct ifnet *ifp; 2634a94100faSBill Paul u_long command; 2635a94100faSBill Paul caddr_t data; 2636a94100faSBill Paul { 2637a94100faSBill Paul struct rl_softc *sc = ifp->if_softc; 2638a94100faSBill Paul struct ifreq *ifr = (struct ifreq *) data; 2639a94100faSBill Paul struct mii_data *mii; 264040929967SGleb Smirnoff int error = 0; 2641a94100faSBill Paul 2642a94100faSBill Paul switch (command) { 2643a94100faSBill Paul case SIOCSIFMTU: 2644c1d0b573SPyun YongHyeon if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > RL_JUMBO_MTU) { 2645a94100faSBill Paul error = EINVAL; 2646c1d0b573SPyun YongHyeon break; 2647c1d0b573SPyun YongHyeon } 2648351a76f9SPyun YongHyeon if ((sc->rl_flags & RL_FLAG_NOJUMBO) != 0 && 2649c1d0b573SPyun YongHyeon ifr->ifr_mtu > RL_MAX_FRAMELEN) { 2650c1d0b573SPyun YongHyeon error = EINVAL; 2651c1d0b573SPyun YongHyeon break; 2652c1d0b573SPyun YongHyeon } 2653c1d0b573SPyun YongHyeon RL_LOCK(sc); 2654c1d0b573SPyun YongHyeon if (ifp->if_mtu != ifr->ifr_mtu) 2655a94100faSBill Paul ifp->if_mtu = ifr->ifr_mtu; 2656d1754a9bSJohn Baldwin RL_UNLOCK(sc); 2657a94100faSBill Paul break; 2658a94100faSBill Paul case SIOCSIFFLAGS: 265997b9d4baSJohn-Mark Gurney RL_LOCK(sc); 2660eed497bbSPyun YongHyeon if ((ifp->if_flags & IFF_UP) != 0) { 2661eed497bbSPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) { 2662eed497bbSPyun YongHyeon if (((ifp->if_flags ^ sc->rl_if_flags) 26633021aef8SPyun YongHyeon & (IFF_PROMISC | IFF_ALLMULTI)) != 0) 2664eed497bbSPyun YongHyeon re_setmulti(sc); 2665eed497bbSPyun YongHyeon } else 266697b9d4baSJohn-Mark Gurney re_init_locked(sc); 2667eed497bbSPyun YongHyeon } else { 2668eed497bbSPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 2669a94100faSBill Paul re_stop(sc); 2670eed497bbSPyun YongHyeon } 2671eed497bbSPyun YongHyeon sc->rl_if_flags = ifp->if_flags; 267297b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 2673a94100faSBill Paul break; 2674a94100faSBill Paul case SIOCADDMULTI: 2675a94100faSBill Paul case SIOCDELMULTI: 267697b9d4baSJohn-Mark Gurney RL_LOCK(sc); 2677a94100faSBill Paul re_setmulti(sc); 267897b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 2679a94100faSBill Paul break; 2680a94100faSBill Paul case SIOCGIFMEDIA: 2681a94100faSBill Paul case SIOCSIFMEDIA: 2682a94100faSBill Paul mii = device_get_softc(sc->rl_miibus); 2683a94100faSBill Paul error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 2684a94100faSBill Paul break; 2685a94100faSBill Paul case SIOCSIFCAP: 268640929967SGleb Smirnoff { 2687f051cb85SGleb Smirnoff int mask, reinit; 2688f051cb85SGleb Smirnoff 2689f051cb85SGleb Smirnoff mask = ifr->ifr_reqcap ^ ifp->if_capenable; 2690f051cb85SGleb Smirnoff reinit = 0; 269140929967SGleb Smirnoff #ifdef DEVICE_POLLING 269240929967SGleb Smirnoff if (mask & IFCAP_POLLING) { 269340929967SGleb Smirnoff if (ifr->ifr_reqcap & IFCAP_POLLING) { 269440929967SGleb Smirnoff error = ether_poll_register(re_poll, ifp); 269540929967SGleb Smirnoff if (error) 269640929967SGleb Smirnoff return(error); 2697d1754a9bSJohn Baldwin RL_LOCK(sc); 269840929967SGleb Smirnoff /* Disable interrupts */ 269940929967SGleb Smirnoff CSR_WRITE_2(sc, RL_IMR, 0x0000); 270040929967SGleb Smirnoff ifp->if_capenable |= IFCAP_POLLING; 270140929967SGleb Smirnoff RL_UNLOCK(sc); 270240929967SGleb Smirnoff } else { 270340929967SGleb Smirnoff error = ether_poll_deregister(ifp); 270440929967SGleb Smirnoff /* Enable interrupts. */ 270540929967SGleb Smirnoff RL_LOCK(sc); 270640929967SGleb Smirnoff CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS); 270740929967SGleb Smirnoff ifp->if_capenable &= ~IFCAP_POLLING; 270840929967SGleb Smirnoff RL_UNLOCK(sc); 270940929967SGleb Smirnoff } 271040929967SGleb Smirnoff } 271140929967SGleb Smirnoff #endif /* DEVICE_POLLING */ 271240929967SGleb Smirnoff if (mask & IFCAP_HWCSUM) { 2713f051cb85SGleb Smirnoff ifp->if_capenable ^= IFCAP_HWCSUM; 2714a94100faSBill Paul if (ifp->if_capenable & IFCAP_TXCSUM) 2715dc74159dSPyun YongHyeon ifp->if_hwassist |= RE_CSUM_FEATURES; 2716a94100faSBill Paul else 2717b61178a9SPyun YongHyeon ifp->if_hwassist &= ~RE_CSUM_FEATURES; 2718f051cb85SGleb Smirnoff reinit = 1; 271940929967SGleb Smirnoff } 2720f051cb85SGleb Smirnoff if (mask & IFCAP_VLAN_HWTAGGING) { 2721f051cb85SGleb Smirnoff ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING; 2722f051cb85SGleb Smirnoff reinit = 1; 2723f051cb85SGleb Smirnoff } 2724dc74159dSPyun YongHyeon if (mask & IFCAP_TSO4) { 2725dc74159dSPyun YongHyeon ifp->if_capenable ^= IFCAP_TSO4; 2726dc74159dSPyun YongHyeon if ((IFCAP_TSO4 & ifp->if_capenable) && 2727dc74159dSPyun YongHyeon (IFCAP_TSO4 & ifp->if_capabilities)) 2728dc74159dSPyun YongHyeon ifp->if_hwassist |= CSUM_TSO; 2729dc74159dSPyun YongHyeon else 2730dc74159dSPyun YongHyeon ifp->if_hwassist &= ~CSUM_TSO; 2731dc74159dSPyun YongHyeon } 27327467bd53SPyun YongHyeon if ((mask & IFCAP_WOL) != 0 && 27337467bd53SPyun YongHyeon (ifp->if_capabilities & IFCAP_WOL) != 0) { 27347467bd53SPyun YongHyeon if ((mask & IFCAP_WOL_UCAST) != 0) 27357467bd53SPyun YongHyeon ifp->if_capenable ^= IFCAP_WOL_UCAST; 27367467bd53SPyun YongHyeon if ((mask & IFCAP_WOL_MCAST) != 0) 27377467bd53SPyun YongHyeon ifp->if_capenable ^= IFCAP_WOL_MCAST; 27387467bd53SPyun YongHyeon if ((mask & IFCAP_WOL_MAGIC) != 0) 27397467bd53SPyun YongHyeon ifp->if_capenable ^= IFCAP_WOL_MAGIC; 27407467bd53SPyun YongHyeon } 2741f051cb85SGleb Smirnoff if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING) 2742f051cb85SGleb Smirnoff re_init(sc); 2743960fd5b3SPyun YongHyeon VLAN_CAPABILITIES(ifp); 274440929967SGleb Smirnoff } 2745a94100faSBill Paul break; 2746a94100faSBill Paul default: 2747a94100faSBill Paul error = ether_ioctl(ifp, command, data); 2748a94100faSBill Paul break; 2749a94100faSBill Paul } 2750a94100faSBill Paul 2751a94100faSBill Paul return (error); 2752a94100faSBill Paul } 2753a94100faSBill Paul 2754a94100faSBill Paul static void 27551d545c7aSMarius Strobl re_watchdog(sc) 2756a94100faSBill Paul struct rl_softc *sc; 27571d545c7aSMarius Strobl { 2758a94100faSBill Paul 27591d545c7aSMarius Strobl RL_LOCK_ASSERT(sc); 27601d545c7aSMarius Strobl 27611d545c7aSMarius Strobl if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0) 27621d545c7aSMarius Strobl return; 27631d545c7aSMarius Strobl 27641d545c7aSMarius Strobl device_printf(sc->rl_dev, "watchdog timeout\n"); 27651d545c7aSMarius Strobl sc->rl_ifp->if_oerrors++; 2766a94100faSBill Paul 2767a94100faSBill Paul re_txeof(sc); 2768a94100faSBill Paul re_rxeof(sc); 276997b9d4baSJohn-Mark Gurney re_init_locked(sc); 2770a94100faSBill Paul } 2771a94100faSBill Paul 2772a94100faSBill Paul /* 2773a94100faSBill Paul * Stop the adapter and free any mbufs allocated to the 2774a94100faSBill Paul * RX and TX lists. 2775a94100faSBill Paul */ 2776a94100faSBill Paul static void 2777a94100faSBill Paul re_stop(sc) 2778a94100faSBill Paul struct rl_softc *sc; 2779a94100faSBill Paul { 2780a94100faSBill Paul register int i; 2781a94100faSBill Paul struct ifnet *ifp; 2782d65abd66SPyun YongHyeon struct rl_txdesc *txd; 2783d65abd66SPyun YongHyeon struct rl_rxdesc *rxd; 2784a94100faSBill Paul 278597b9d4baSJohn-Mark Gurney RL_LOCK_ASSERT(sc); 278697b9d4baSJohn-Mark Gurney 2787fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 2788a94100faSBill Paul 27891d545c7aSMarius Strobl sc->rl_watchdog_timer = 0; 2790d1754a9bSJohn Baldwin callout_stop(&sc->rl_stat_callout); 279113f4c340SRobert Watson ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2792a94100faSBill Paul 2793a94100faSBill Paul CSR_WRITE_1(sc, RL_COMMAND, 0x00); 2794a94100faSBill Paul CSR_WRITE_2(sc, RL_IMR, 0x0000); 2795ed510fb0SBill Paul CSR_WRITE_2(sc, RL_ISR, 0xFFFF); 2796a94100faSBill Paul 2797a94100faSBill Paul if (sc->rl_head != NULL) { 2798a94100faSBill Paul m_freem(sc->rl_head); 2799a94100faSBill Paul sc->rl_head = sc->rl_tail = NULL; 2800a94100faSBill Paul } 2801a94100faSBill Paul 2802a94100faSBill Paul /* Free the TX list buffers. */ 2803a94100faSBill Paul 2804d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) { 2805d65abd66SPyun YongHyeon txd = &sc->rl_ldata.rl_tx_desc[i]; 2806d65abd66SPyun YongHyeon if (txd->tx_m != NULL) { 2807d65abd66SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, 2808d65abd66SPyun YongHyeon txd->tx_dmamap, BUS_DMASYNC_POSTWRITE); 2809d65abd66SPyun YongHyeon bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, 2810d65abd66SPyun YongHyeon txd->tx_dmamap); 2811d65abd66SPyun YongHyeon m_freem(txd->tx_m); 2812d65abd66SPyun YongHyeon txd->tx_m = NULL; 2813a94100faSBill Paul } 2814a94100faSBill Paul } 2815a94100faSBill Paul 2816a94100faSBill Paul /* Free the RX list buffers. */ 2817a94100faSBill Paul 2818d65abd66SPyun YongHyeon for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 2819d65abd66SPyun YongHyeon rxd = &sc->rl_ldata.rl_rx_desc[i]; 2820d65abd66SPyun YongHyeon if (rxd->rx_m != NULL) { 2821d65abd66SPyun YongHyeon bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, 2822d65abd66SPyun YongHyeon rxd->rx_dmamap, BUS_DMASYNC_POSTREAD); 2823d65abd66SPyun YongHyeon bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, 2824d65abd66SPyun YongHyeon rxd->rx_dmamap); 2825d65abd66SPyun YongHyeon m_freem(rxd->rx_m); 2826d65abd66SPyun YongHyeon rxd->rx_m = NULL; 2827a94100faSBill Paul } 2828a94100faSBill Paul } 2829a94100faSBill Paul } 2830a94100faSBill Paul 2831a94100faSBill Paul /* 2832a94100faSBill Paul * Device suspend routine. Stop the interface and save some PCI 2833a94100faSBill Paul * settings in case the BIOS doesn't restore them properly on 2834a94100faSBill Paul * resume. 2835a94100faSBill Paul */ 2836a94100faSBill Paul static int 2837a94100faSBill Paul re_suspend(dev) 2838a94100faSBill Paul device_t dev; 2839a94100faSBill Paul { 2840a94100faSBill Paul struct rl_softc *sc; 2841a94100faSBill Paul 2842a94100faSBill Paul sc = device_get_softc(dev); 2843a94100faSBill Paul 284497b9d4baSJohn-Mark Gurney RL_LOCK(sc); 2845a94100faSBill Paul re_stop(sc); 28467467bd53SPyun YongHyeon re_setwol(sc); 2847a94100faSBill Paul sc->suspended = 1; 284897b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 2849a94100faSBill Paul 2850a94100faSBill Paul return (0); 2851a94100faSBill Paul } 2852a94100faSBill Paul 2853a94100faSBill Paul /* 2854a94100faSBill Paul * Device resume routine. Restore some PCI settings in case the BIOS 2855a94100faSBill Paul * doesn't, re-enable busmastering, and restart the interface if 2856a94100faSBill Paul * appropriate. 2857a94100faSBill Paul */ 2858a94100faSBill Paul static int 2859a94100faSBill Paul re_resume(dev) 2860a94100faSBill Paul device_t dev; 2861a94100faSBill Paul { 2862a94100faSBill Paul struct rl_softc *sc; 2863a94100faSBill Paul struct ifnet *ifp; 2864a94100faSBill Paul 2865a94100faSBill Paul sc = device_get_softc(dev); 286697b9d4baSJohn-Mark Gurney 286797b9d4baSJohn-Mark Gurney RL_LOCK(sc); 286897b9d4baSJohn-Mark Gurney 2869fc74a9f9SBrooks Davis ifp = sc->rl_ifp; 2870a94100faSBill Paul 2871a94100faSBill Paul /* reinitialize interface if necessary */ 2872a94100faSBill Paul if (ifp->if_flags & IFF_UP) 287397b9d4baSJohn-Mark Gurney re_init_locked(sc); 2874a94100faSBill Paul 28757467bd53SPyun YongHyeon /* 28767467bd53SPyun YongHyeon * Clear WOL matching such that normal Rx filtering 28777467bd53SPyun YongHyeon * wouldn't interfere with WOL patterns. 28787467bd53SPyun YongHyeon */ 28797467bd53SPyun YongHyeon re_clrwol(sc); 2880a94100faSBill Paul sc->suspended = 0; 288197b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 2882a94100faSBill Paul 2883a94100faSBill Paul return (0); 2884a94100faSBill Paul } 2885a94100faSBill Paul 2886a94100faSBill Paul /* 2887a94100faSBill Paul * Stop all chip I/O so that the kernel's probe routines don't 2888a94100faSBill Paul * get confused by errant DMAs when rebooting. 2889a94100faSBill Paul */ 28906a087a87SPyun YongHyeon static int 2891a94100faSBill Paul re_shutdown(dev) 2892a94100faSBill Paul device_t dev; 2893a94100faSBill Paul { 2894a94100faSBill Paul struct rl_softc *sc; 2895a94100faSBill Paul 2896a94100faSBill Paul sc = device_get_softc(dev); 2897a94100faSBill Paul 289897b9d4baSJohn-Mark Gurney RL_LOCK(sc); 2899a94100faSBill Paul re_stop(sc); 2900536fde34SMaxim Sobolev /* 2901536fde34SMaxim Sobolev * Mark interface as down since otherwise we will panic if 2902536fde34SMaxim Sobolev * interrupt comes in later on, which can happen in some 290372293673SRuslan Ermilov * cases. 2904536fde34SMaxim Sobolev */ 2905536fde34SMaxim Sobolev sc->rl_ifp->if_flags &= ~IFF_UP; 29067467bd53SPyun YongHyeon re_setwol(sc); 290797b9d4baSJohn-Mark Gurney RL_UNLOCK(sc); 29086a087a87SPyun YongHyeon 29096a087a87SPyun YongHyeon return (0); 2910a94100faSBill Paul } 29117467bd53SPyun YongHyeon 29127467bd53SPyun YongHyeon static void 29137467bd53SPyun YongHyeon re_setwol(sc) 29147467bd53SPyun YongHyeon struct rl_softc *sc; 29157467bd53SPyun YongHyeon { 29167467bd53SPyun YongHyeon struct ifnet *ifp; 29177467bd53SPyun YongHyeon int pmc; 29187467bd53SPyun YongHyeon uint16_t pmstat; 29197467bd53SPyun YongHyeon uint8_t v; 29207467bd53SPyun YongHyeon 29217467bd53SPyun YongHyeon RL_LOCK_ASSERT(sc); 29227467bd53SPyun YongHyeon 29237467bd53SPyun YongHyeon if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0) 29247467bd53SPyun YongHyeon return; 29257467bd53SPyun YongHyeon 29267467bd53SPyun YongHyeon ifp = sc->rl_ifp; 29277467bd53SPyun YongHyeon /* Enable config register write. */ 29287467bd53SPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE); 29297467bd53SPyun YongHyeon 29307467bd53SPyun YongHyeon /* Enable PME. */ 29317467bd53SPyun YongHyeon v = CSR_READ_1(sc, RL_CFG1); 29327467bd53SPyun YongHyeon v &= ~RL_CFG1_PME; 29337467bd53SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL) != 0) 29347467bd53SPyun YongHyeon v |= RL_CFG1_PME; 29357467bd53SPyun YongHyeon CSR_WRITE_1(sc, RL_CFG1, v); 29367467bd53SPyun YongHyeon 29377467bd53SPyun YongHyeon v = CSR_READ_1(sc, RL_CFG3); 29387467bd53SPyun YongHyeon v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC); 29397467bd53SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0) 29407467bd53SPyun YongHyeon v |= RL_CFG3_WOL_MAGIC; 29417467bd53SPyun YongHyeon CSR_WRITE_1(sc, RL_CFG3, v); 29427467bd53SPyun YongHyeon 29437467bd53SPyun YongHyeon /* Config register write done. */ 2944f98dd8cfSPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 29457467bd53SPyun YongHyeon 29467467bd53SPyun YongHyeon v = CSR_READ_1(sc, RL_CFG5); 29477467bd53SPyun YongHyeon v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST); 29487467bd53SPyun YongHyeon v &= ~RL_CFG5_WOL_LANWAKE; 29497467bd53SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0) 29507467bd53SPyun YongHyeon v |= RL_CFG5_WOL_UCAST; 29517467bd53SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0) 29527467bd53SPyun YongHyeon v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST; 29537467bd53SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL) != 0) 29547467bd53SPyun YongHyeon v |= RL_CFG5_WOL_LANWAKE; 29557467bd53SPyun YongHyeon CSR_WRITE_1(sc, RL_CFG5, v); 29567467bd53SPyun YongHyeon 29577467bd53SPyun YongHyeon /* 29587467bd53SPyun YongHyeon * It seems that hardware resets its link speed to 100Mbps in 29597467bd53SPyun YongHyeon * power down mode so switching to 100Mbps in driver is not 29607467bd53SPyun YongHyeon * needed. 29617467bd53SPyun YongHyeon */ 29627467bd53SPyun YongHyeon 29637467bd53SPyun YongHyeon /* Request PME if WOL is requested. */ 29647467bd53SPyun YongHyeon pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2); 29657467bd53SPyun YongHyeon pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE); 29667467bd53SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL) != 0) 29677467bd53SPyun YongHyeon pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE; 29687467bd53SPyun YongHyeon pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2); 29697467bd53SPyun YongHyeon } 29707467bd53SPyun YongHyeon 29717467bd53SPyun YongHyeon static void 29727467bd53SPyun YongHyeon re_clrwol(sc) 29737467bd53SPyun YongHyeon struct rl_softc *sc; 29747467bd53SPyun YongHyeon { 29757467bd53SPyun YongHyeon int pmc; 29767467bd53SPyun YongHyeon uint8_t v; 29777467bd53SPyun YongHyeon 29787467bd53SPyun YongHyeon RL_LOCK_ASSERT(sc); 29797467bd53SPyun YongHyeon 29807467bd53SPyun YongHyeon if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0) 29817467bd53SPyun YongHyeon return; 29827467bd53SPyun YongHyeon 29837467bd53SPyun YongHyeon /* Enable config register write. */ 29847467bd53SPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE); 29857467bd53SPyun YongHyeon 29867467bd53SPyun YongHyeon v = CSR_READ_1(sc, RL_CFG3); 29877467bd53SPyun YongHyeon v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC); 29887467bd53SPyun YongHyeon CSR_WRITE_1(sc, RL_CFG3, v); 29897467bd53SPyun YongHyeon 29907467bd53SPyun YongHyeon /* Config register write done. */ 2991f98dd8cfSPyun YongHyeon CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 29927467bd53SPyun YongHyeon 29937467bd53SPyun YongHyeon v = CSR_READ_1(sc, RL_CFG5); 29947467bd53SPyun YongHyeon v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST); 29957467bd53SPyun YongHyeon v &= ~RL_CFG5_WOL_LANWAKE; 29967467bd53SPyun YongHyeon CSR_WRITE_1(sc, RL_CFG5, v); 29977467bd53SPyun YongHyeon } 2998