1 /*- 2 * Copyright (c) 1997, 1998-2003 3 * Bill Paul <wpaul@windriver.com>. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Bill Paul. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 /* 37 * RealTek 8139C+/8169/8169S/8110S/8168/8111/8101E PCI NIC driver 38 * 39 * Written by Bill Paul <wpaul@windriver.com> 40 * Senior Networking Software Engineer 41 * Wind River Systems 42 */ 43 44 /* 45 * This driver is designed to support RealTek's next generation of 46 * 10/100 and 10/100/1000 PCI ethernet controllers. There are currently 47 * seven devices in this family: the RTL8139C+, the RTL8169, the RTL8169S, 48 * RTL8110S, the RTL8168, the RTL8111 and the RTL8101E. 49 * 50 * The 8139C+ is a 10/100 ethernet chip. It is backwards compatible 51 * with the older 8139 family, however it also supports a special 52 * C+ mode of operation that provides several new performance enhancing 53 * features. These include: 54 * 55 * o Descriptor based DMA mechanism. Each descriptor represents 56 * a single packet fragment. Data buffers may be aligned on 57 * any byte boundary. 58 * 59 * o 64-bit DMA 60 * 61 * o TCP/IP checksum offload for both RX and TX 62 * 63 * o High and normal priority transmit DMA rings 64 * 65 * o VLAN tag insertion and extraction 66 * 67 * o TCP large send (segmentation offload) 68 * 69 * Like the 8139, the 8139C+ also has a built-in 10/100 PHY. The C+ 70 * programming API is fairly straightforward. The RX filtering, EEPROM 71 * access and PHY access is the same as it is on the older 8139 series 72 * chips. 73 * 74 * The 8169 is a 64-bit 10/100/1000 gigabit ethernet MAC. It has almost the 75 * same programming API and feature set as the 8139C+ with the following 76 * differences and additions: 77 * 78 * o 1000Mbps mode 79 * 80 * o Jumbo frames 81 * 82 * o GMII and TBI ports/registers for interfacing with copper 83 * or fiber PHYs 84 * 85 * o RX and TX DMA rings can have up to 1024 descriptors 86 * (the 8139C+ allows a maximum of 64) 87 * 88 * o Slight differences in register layout from the 8139C+ 89 * 90 * The TX start and timer interrupt registers are at different locations 91 * on the 8169 than they are on the 8139C+. Also, the status word in the 92 * RX descriptor has a slightly different bit layout. The 8169 does not 93 * have a built-in PHY. Most reference boards use a Marvell 88E1000 'Alaska' 94 * copper gigE PHY. 95 * 96 * The 8169S/8110S 10/100/1000 devices have built-in copper gigE PHYs 97 * (the 'S' stands for 'single-chip'). These devices have the same 98 * programming API as the older 8169, but also have some vendor-specific 99 * registers for the on-board PHY. The 8110S is a LAN-on-motherboard 100 * part designed to be pin-compatible with the RealTek 8100 10/100 chip. 101 * 102 * This driver takes advantage of the RX and TX checksum offload and 103 * VLAN tag insertion/extraction features. It also implements TX 104 * interrupt moderation using the timer interrupt registers, which 105 * significantly reduces TX interrupt load. There is also support 106 * for jumbo frames, however the 8169/8169S/8110S can not transmit 107 * jumbo frames larger than 7440, so the max MTU possible with this 108 * driver is 7422 bytes. 109 */ 110 111 #ifdef HAVE_KERNEL_OPTION_HEADERS 112 #include "opt_device_polling.h" 113 #endif 114 115 #include <sys/param.h> 116 #include <sys/endian.h> 117 #include <sys/systm.h> 118 #include <sys/sockio.h> 119 #include <sys/mbuf.h> 120 #include <sys/malloc.h> 121 #include <sys/module.h> 122 #include <sys/kernel.h> 123 #include <sys/socket.h> 124 #include <sys/lock.h> 125 #include <sys/mutex.h> 126 #include <sys/sysctl.h> 127 #include <sys/taskqueue.h> 128 129 #include <net/if.h> 130 #include <net/if_var.h> 131 #include <net/if_arp.h> 132 #include <net/ethernet.h> 133 #include <net/if_dl.h> 134 #include <net/if_media.h> 135 #include <net/if_types.h> 136 #include <net/if_vlan_var.h> 137 138 #include <net/bpf.h> 139 140 #include <machine/bus.h> 141 #include <machine/resource.h> 142 #include <sys/bus.h> 143 #include <sys/rman.h> 144 145 #include <dev/mii/mii.h> 146 #include <dev/mii/miivar.h> 147 148 #include <dev/pci/pcireg.h> 149 #include <dev/pci/pcivar.h> 150 151 #include <dev/rl/if_rlreg.h> 152 153 MODULE_DEPEND(re, pci, 1, 1, 1); 154 MODULE_DEPEND(re, ether, 1, 1, 1); 155 MODULE_DEPEND(re, miibus, 1, 1, 1); 156 157 /* "device miibus" required. See GENERIC if you get errors here. */ 158 #include "miibus_if.h" 159 160 /* Tunables. */ 161 static int intr_filter = 0; 162 TUNABLE_INT("hw.re.intr_filter", &intr_filter); 163 static int msi_disable = 0; 164 TUNABLE_INT("hw.re.msi_disable", &msi_disable); 165 static int msix_disable = 0; 166 TUNABLE_INT("hw.re.msix_disable", &msix_disable); 167 static int prefer_iomap = 0; 168 TUNABLE_INT("hw.re.prefer_iomap", &prefer_iomap); 169 170 #define RE_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP) 171 172 /* 173 * Various supported device vendors/types and their names. 174 */ 175 static const struct rl_type re_devs[] = { 176 { DLINK_VENDORID, DLINK_DEVICEID_528T, 0, 177 "D-Link DGE-528(T) Gigabit Ethernet Adapter" }, 178 { DLINK_VENDORID, DLINK_DEVICEID_530T_REVC, 0, 179 "D-Link DGE-530(T) Gigabit Ethernet Adapter" }, 180 { RT_VENDORID, RT_DEVICEID_8139, 0, 181 "RealTek 8139C+ 10/100BaseTX" }, 182 { RT_VENDORID, RT_DEVICEID_8101E, 0, 183 "RealTek 810xE PCIe 10/100baseTX" }, 184 { RT_VENDORID, RT_DEVICEID_8168, 0, 185 "RealTek 8168/8111 B/C/CP/D/DP/E/F/G PCIe Gigabit Ethernet" }, 186 { RT_VENDORID, RT_DEVICEID_8169, 0, 187 "RealTek 8169/8169S/8169SB(L)/8110S/8110SB(L) Gigabit Ethernet" }, 188 { RT_VENDORID, RT_DEVICEID_8169SC, 0, 189 "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" }, 190 { COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, 0, 191 "Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" }, 192 { LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, 0, 193 "Linksys EG1032 (RTL8169S) Gigabit Ethernet" }, 194 { USR_VENDORID, USR_DEVICEID_997902, 0, 195 "US Robotics 997902 (RTL8169S) Gigabit Ethernet" } 196 }; 197 198 static const struct rl_hwrev re_hwrevs[] = { 199 { RL_HWREV_8139, RL_8139, "", RL_MTU }, 200 { RL_HWREV_8139A, RL_8139, "A", RL_MTU }, 201 { RL_HWREV_8139AG, RL_8139, "A-G", RL_MTU }, 202 { RL_HWREV_8139B, RL_8139, "B", RL_MTU }, 203 { RL_HWREV_8130, RL_8139, "8130", RL_MTU }, 204 { RL_HWREV_8139C, RL_8139, "C", RL_MTU }, 205 { RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C", RL_MTU }, 206 { RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+", RL_MTU }, 207 { RL_HWREV_8168B_SPIN1, RL_8169, "8168", RL_JUMBO_MTU }, 208 { RL_HWREV_8169, RL_8169, "8169", RL_JUMBO_MTU }, 209 { RL_HWREV_8169S, RL_8169, "8169S", RL_JUMBO_MTU }, 210 { RL_HWREV_8110S, RL_8169, "8110S", RL_JUMBO_MTU }, 211 { RL_HWREV_8169_8110SB, RL_8169, "8169SB/8110SB", RL_JUMBO_MTU }, 212 { RL_HWREV_8169_8110SC, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU }, 213 { RL_HWREV_8169_8110SBL, RL_8169, "8169SBL/8110SBL", RL_JUMBO_MTU }, 214 { RL_HWREV_8169_8110SCE, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU }, 215 { RL_HWREV_8100, RL_8139, "8100", RL_MTU }, 216 { RL_HWREV_8101, RL_8139, "8101", RL_MTU }, 217 { RL_HWREV_8100E, RL_8169, "8100E", RL_MTU }, 218 { RL_HWREV_8101E, RL_8169, "8101E", RL_MTU }, 219 { RL_HWREV_8102E, RL_8169, "8102E", RL_MTU }, 220 { RL_HWREV_8102EL, RL_8169, "8102EL", RL_MTU }, 221 { RL_HWREV_8102EL_SPIN1, RL_8169, "8102EL", RL_MTU }, 222 { RL_HWREV_8103E, RL_8169, "8103E", RL_MTU }, 223 { RL_HWREV_8401E, RL_8169, "8401E", RL_MTU }, 224 { RL_HWREV_8402, RL_8169, "8402", RL_MTU }, 225 { RL_HWREV_8105E, RL_8169, "8105E", RL_MTU }, 226 { RL_HWREV_8105E_SPIN1, RL_8169, "8105E", RL_MTU }, 227 { RL_HWREV_8106E, RL_8169, "8106E", RL_MTU }, 228 { RL_HWREV_8168B_SPIN2, RL_8169, "8168", RL_JUMBO_MTU }, 229 { RL_HWREV_8168B_SPIN3, RL_8169, "8168", RL_JUMBO_MTU }, 230 { RL_HWREV_8168C, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K }, 231 { RL_HWREV_8168C_SPIN2, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K }, 232 { RL_HWREV_8168CP, RL_8169, "8168CP/8111CP", RL_JUMBO_MTU_6K }, 233 { RL_HWREV_8168D, RL_8169, "8168D/8111D", RL_JUMBO_MTU_9K }, 234 { RL_HWREV_8168DP, RL_8169, "8168DP/8111DP", RL_JUMBO_MTU_9K }, 235 { RL_HWREV_8168E, RL_8169, "8168E/8111E", RL_JUMBO_MTU_9K}, 236 { RL_HWREV_8168E_VL, RL_8169, "8168E/8111E-VL", RL_JUMBO_MTU_6K}, 237 { RL_HWREV_8168EP, RL_8169, "8168EP/8111EP", RL_JUMBO_MTU_9K}, 238 { RL_HWREV_8168F, RL_8169, "8168F/8111F", RL_JUMBO_MTU_9K}, 239 { RL_HWREV_8168G, RL_8169, "8168G/8111G", RL_JUMBO_MTU_9K}, 240 { RL_HWREV_8168GU, RL_8169, "8168GU/8111GU", RL_JUMBO_MTU_9K}, 241 { RL_HWREV_8168H, RL_8169, "8168H/8111H", RL_JUMBO_MTU_9K}, 242 { RL_HWREV_8411, RL_8169, "8411", RL_JUMBO_MTU_9K}, 243 { RL_HWREV_8411B, RL_8169, "8411B", RL_JUMBO_MTU_9K}, 244 { 0, 0, NULL, 0 } 245 }; 246 247 static int re_probe (device_t); 248 static int re_attach (device_t); 249 static int re_detach (device_t); 250 251 static int re_encap (struct rl_softc *, struct mbuf **); 252 253 static void re_dma_map_addr (void *, bus_dma_segment_t *, int, int); 254 static int re_allocmem (device_t, struct rl_softc *); 255 static __inline void re_discard_rxbuf 256 (struct rl_softc *, int); 257 static int re_newbuf (struct rl_softc *, int); 258 static int re_jumbo_newbuf (struct rl_softc *, int); 259 static int re_rx_list_init (struct rl_softc *); 260 static int re_jrx_list_init (struct rl_softc *); 261 static int re_tx_list_init (struct rl_softc *); 262 #ifdef RE_FIXUP_RX 263 static __inline void re_fixup_rx 264 (struct mbuf *); 265 #endif 266 static int re_rxeof (struct rl_softc *, int *); 267 static void re_txeof (struct rl_softc *); 268 #ifdef DEVICE_POLLING 269 static int re_poll (struct ifnet *, enum poll_cmd, int); 270 static int re_poll_locked (struct ifnet *, enum poll_cmd, int); 271 #endif 272 static int re_intr (void *); 273 static void re_intr_msi (void *); 274 static void re_tick (void *); 275 static void re_int_task (void *, int); 276 static void re_start (struct ifnet *); 277 static void re_start_locked (struct ifnet *); 278 static int re_ioctl (struct ifnet *, u_long, caddr_t); 279 static void re_init (void *); 280 static void re_init_locked (struct rl_softc *); 281 static void re_stop (struct rl_softc *); 282 static void re_watchdog (struct rl_softc *); 283 static int re_suspend (device_t); 284 static int re_resume (device_t); 285 static int re_shutdown (device_t); 286 static int re_ifmedia_upd (struct ifnet *); 287 static void re_ifmedia_sts (struct ifnet *, struct ifmediareq *); 288 289 static void re_eeprom_putbyte (struct rl_softc *, int); 290 static void re_eeprom_getword (struct rl_softc *, int, u_int16_t *); 291 static void re_read_eeprom (struct rl_softc *, caddr_t, int, int); 292 static int re_gmii_readreg (device_t, int, int); 293 static int re_gmii_writereg (device_t, int, int, int); 294 295 static int re_miibus_readreg (device_t, int, int); 296 static int re_miibus_writereg (device_t, int, int, int); 297 static void re_miibus_statchg (device_t); 298 299 static void re_set_jumbo (struct rl_softc *, int); 300 static void re_set_rxmode (struct rl_softc *); 301 static void re_reset (struct rl_softc *); 302 static void re_setwol (struct rl_softc *); 303 static void re_clrwol (struct rl_softc *); 304 static void re_set_linkspeed (struct rl_softc *); 305 306 #ifdef DEV_NETMAP /* see ixgbe.c for details */ 307 #include <dev/netmap/if_re_netmap.h> 308 MODULE_DEPEND(re, netmap, 1, 1, 1); 309 #endif /* !DEV_NETMAP */ 310 311 #ifdef RE_DIAG 312 static int re_diag (struct rl_softc *); 313 #endif 314 315 static void re_add_sysctls (struct rl_softc *); 316 static int re_sysctl_stats (SYSCTL_HANDLER_ARGS); 317 static int sysctl_int_range (SYSCTL_HANDLER_ARGS, int, int); 318 static int sysctl_hw_re_int_mod (SYSCTL_HANDLER_ARGS); 319 320 static device_method_t re_methods[] = { 321 /* Device interface */ 322 DEVMETHOD(device_probe, re_probe), 323 DEVMETHOD(device_attach, re_attach), 324 DEVMETHOD(device_detach, re_detach), 325 DEVMETHOD(device_suspend, re_suspend), 326 DEVMETHOD(device_resume, re_resume), 327 DEVMETHOD(device_shutdown, re_shutdown), 328 329 /* MII interface */ 330 DEVMETHOD(miibus_readreg, re_miibus_readreg), 331 DEVMETHOD(miibus_writereg, re_miibus_writereg), 332 DEVMETHOD(miibus_statchg, re_miibus_statchg), 333 334 DEVMETHOD_END 335 }; 336 337 static driver_t re_driver = { 338 "re", 339 re_methods, 340 sizeof(struct rl_softc) 341 }; 342 343 static devclass_t re_devclass; 344 345 DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0); 346 DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0); 347 348 #define EE_SET(x) \ 349 CSR_WRITE_1(sc, RL_EECMD, \ 350 CSR_READ_1(sc, RL_EECMD) | x) 351 352 #define EE_CLR(x) \ 353 CSR_WRITE_1(sc, RL_EECMD, \ 354 CSR_READ_1(sc, RL_EECMD) & ~x) 355 356 /* 357 * Send a read command and address to the EEPROM, check for ACK. 358 */ 359 static void 360 re_eeprom_putbyte(struct rl_softc *sc, int addr) 361 { 362 int d, i; 363 364 d = addr | (RL_9346_READ << sc->rl_eewidth); 365 366 /* 367 * Feed in each bit and strobe the clock. 368 */ 369 370 for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) { 371 if (d & i) { 372 EE_SET(RL_EE_DATAIN); 373 } else { 374 EE_CLR(RL_EE_DATAIN); 375 } 376 DELAY(100); 377 EE_SET(RL_EE_CLK); 378 DELAY(150); 379 EE_CLR(RL_EE_CLK); 380 DELAY(100); 381 } 382 } 383 384 /* 385 * Read a word of data stored in the EEPROM at address 'addr.' 386 */ 387 static void 388 re_eeprom_getword(struct rl_softc *sc, int addr, u_int16_t *dest) 389 { 390 int i; 391 u_int16_t word = 0; 392 393 /* 394 * Send address of word we want to read. 395 */ 396 re_eeprom_putbyte(sc, addr); 397 398 /* 399 * Start reading bits from EEPROM. 400 */ 401 for (i = 0x8000; i; i >>= 1) { 402 EE_SET(RL_EE_CLK); 403 DELAY(100); 404 if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT) 405 word |= i; 406 EE_CLR(RL_EE_CLK); 407 DELAY(100); 408 } 409 410 *dest = word; 411 } 412 413 /* 414 * Read a sequence of words from the EEPROM. 415 */ 416 static void 417 re_read_eeprom(struct rl_softc *sc, caddr_t dest, int off, int cnt) 418 { 419 int i; 420 u_int16_t word = 0, *ptr; 421 422 CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM); 423 424 DELAY(100); 425 426 for (i = 0; i < cnt; i++) { 427 CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL); 428 re_eeprom_getword(sc, off + i, &word); 429 CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL); 430 ptr = (u_int16_t *)(dest + (i * 2)); 431 *ptr = word; 432 } 433 434 CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM); 435 } 436 437 static int 438 re_gmii_readreg(device_t dev, int phy, int reg) 439 { 440 struct rl_softc *sc; 441 u_int32_t rval; 442 int i; 443 444 sc = device_get_softc(dev); 445 446 /* Let the rgephy driver read the GMEDIASTAT register */ 447 448 if (reg == RL_GMEDIASTAT) { 449 rval = CSR_READ_1(sc, RL_GMEDIASTAT); 450 return (rval); 451 } 452 453 CSR_WRITE_4(sc, RL_PHYAR, reg << 16); 454 455 for (i = 0; i < RL_PHY_TIMEOUT; i++) { 456 rval = CSR_READ_4(sc, RL_PHYAR); 457 if (rval & RL_PHYAR_BUSY) 458 break; 459 DELAY(25); 460 } 461 462 if (i == RL_PHY_TIMEOUT) { 463 device_printf(sc->rl_dev, "PHY read failed\n"); 464 return (0); 465 } 466 467 /* 468 * Controller requires a 20us delay to process next MDIO request. 469 */ 470 DELAY(20); 471 472 return (rval & RL_PHYAR_PHYDATA); 473 } 474 475 static int 476 re_gmii_writereg(device_t dev, int phy, int reg, int data) 477 { 478 struct rl_softc *sc; 479 u_int32_t rval; 480 int i; 481 482 sc = device_get_softc(dev); 483 484 CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) | 485 (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY); 486 487 for (i = 0; i < RL_PHY_TIMEOUT; i++) { 488 rval = CSR_READ_4(sc, RL_PHYAR); 489 if (!(rval & RL_PHYAR_BUSY)) 490 break; 491 DELAY(25); 492 } 493 494 if (i == RL_PHY_TIMEOUT) { 495 device_printf(sc->rl_dev, "PHY write failed\n"); 496 return (0); 497 } 498 499 /* 500 * Controller requires a 20us delay to process next MDIO request. 501 */ 502 DELAY(20); 503 504 return (0); 505 } 506 507 static int 508 re_miibus_readreg(device_t dev, int phy, int reg) 509 { 510 struct rl_softc *sc; 511 u_int16_t rval = 0; 512 u_int16_t re8139_reg = 0; 513 514 sc = device_get_softc(dev); 515 516 if (sc->rl_type == RL_8169) { 517 rval = re_gmii_readreg(dev, phy, reg); 518 return (rval); 519 } 520 521 switch (reg) { 522 case MII_BMCR: 523 re8139_reg = RL_BMCR; 524 break; 525 case MII_BMSR: 526 re8139_reg = RL_BMSR; 527 break; 528 case MII_ANAR: 529 re8139_reg = RL_ANAR; 530 break; 531 case MII_ANER: 532 re8139_reg = RL_ANER; 533 break; 534 case MII_ANLPAR: 535 re8139_reg = RL_LPAR; 536 break; 537 case MII_PHYIDR1: 538 case MII_PHYIDR2: 539 return (0); 540 /* 541 * Allow the rlphy driver to read the media status 542 * register. If we have a link partner which does not 543 * support NWAY, this is the register which will tell 544 * us the results of parallel detection. 545 */ 546 case RL_MEDIASTAT: 547 rval = CSR_READ_1(sc, RL_MEDIASTAT); 548 return (rval); 549 default: 550 device_printf(sc->rl_dev, "bad phy register\n"); 551 return (0); 552 } 553 rval = CSR_READ_2(sc, re8139_reg); 554 if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) { 555 /* 8139C+ has different bit layout. */ 556 rval &= ~(BMCR_LOOP | BMCR_ISO); 557 } 558 return (rval); 559 } 560 561 static int 562 re_miibus_writereg(device_t dev, int phy, int reg, int data) 563 { 564 struct rl_softc *sc; 565 u_int16_t re8139_reg = 0; 566 int rval = 0; 567 568 sc = device_get_softc(dev); 569 570 if (sc->rl_type == RL_8169) { 571 rval = re_gmii_writereg(dev, phy, reg, data); 572 return (rval); 573 } 574 575 switch (reg) { 576 case MII_BMCR: 577 re8139_reg = RL_BMCR; 578 if (sc->rl_type == RL_8139CPLUS) { 579 /* 8139C+ has different bit layout. */ 580 data &= ~(BMCR_LOOP | BMCR_ISO); 581 } 582 break; 583 case MII_BMSR: 584 re8139_reg = RL_BMSR; 585 break; 586 case MII_ANAR: 587 re8139_reg = RL_ANAR; 588 break; 589 case MII_ANER: 590 re8139_reg = RL_ANER; 591 break; 592 case MII_ANLPAR: 593 re8139_reg = RL_LPAR; 594 break; 595 case MII_PHYIDR1: 596 case MII_PHYIDR2: 597 return (0); 598 break; 599 default: 600 device_printf(sc->rl_dev, "bad phy register\n"); 601 return (0); 602 } 603 CSR_WRITE_2(sc, re8139_reg, data); 604 return (0); 605 } 606 607 static void 608 re_miibus_statchg(device_t dev) 609 { 610 struct rl_softc *sc; 611 struct ifnet *ifp; 612 struct mii_data *mii; 613 614 sc = device_get_softc(dev); 615 mii = device_get_softc(sc->rl_miibus); 616 ifp = sc->rl_ifp; 617 if (mii == NULL || ifp == NULL || 618 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 619 return; 620 621 sc->rl_flags &= ~RL_FLAG_LINK; 622 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) == 623 (IFM_ACTIVE | IFM_AVALID)) { 624 switch (IFM_SUBTYPE(mii->mii_media_active)) { 625 case IFM_10_T: 626 case IFM_100_TX: 627 sc->rl_flags |= RL_FLAG_LINK; 628 break; 629 case IFM_1000_T: 630 if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0) 631 break; 632 sc->rl_flags |= RL_FLAG_LINK; 633 break; 634 default: 635 break; 636 } 637 } 638 /* 639 * RealTek controllers does not provide any interface to 640 * Tx/Rx MACs for resolved speed, duplex and flow-control 641 * parameters. 642 */ 643 } 644 645 /* 646 * Set the RX configuration and 64-bit multicast hash filter. 647 */ 648 static void 649 re_set_rxmode(struct rl_softc *sc) 650 { 651 struct ifnet *ifp; 652 struct ifmultiaddr *ifma; 653 uint32_t hashes[2] = { 0, 0 }; 654 uint32_t h, rxfilt; 655 656 RL_LOCK_ASSERT(sc); 657 658 ifp = sc->rl_ifp; 659 660 rxfilt = RL_RXCFG_CONFIG | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_BROAD; 661 if ((sc->rl_flags & RL_FLAG_EARLYOFF) != 0) 662 rxfilt |= RL_RXCFG_EARLYOFF; 663 else if ((sc->rl_flags & RL_FLAG_EARLYOFFV2) != 0) 664 rxfilt |= RL_RXCFG_EARLYOFFV2; 665 666 if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) { 667 if (ifp->if_flags & IFF_PROMISC) 668 rxfilt |= RL_RXCFG_RX_ALLPHYS; 669 /* 670 * Unlike other hardwares, we have to explicitly set 671 * RL_RXCFG_RX_MULTI to receive multicast frames in 672 * promiscuous mode. 673 */ 674 rxfilt |= RL_RXCFG_RX_MULTI; 675 hashes[0] = hashes[1] = 0xffffffff; 676 goto done; 677 } 678 679 if_maddr_rlock(ifp); 680 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 681 if (ifma->ifma_addr->sa_family != AF_LINK) 682 continue; 683 h = ether_crc32_be(LLADDR((struct sockaddr_dl *) 684 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 685 if (h < 32) 686 hashes[0] |= (1 << h); 687 else 688 hashes[1] |= (1 << (h - 32)); 689 } 690 if_maddr_runlock(ifp); 691 692 if (hashes[0] != 0 || hashes[1] != 0) { 693 /* 694 * For some unfathomable reason, RealTek decided to 695 * reverse the order of the multicast hash registers 696 * in the PCI Express parts. This means we have to 697 * write the hash pattern in reverse order for those 698 * devices. 699 */ 700 if ((sc->rl_flags & RL_FLAG_PCIE) != 0) { 701 h = bswap32(hashes[0]); 702 hashes[0] = bswap32(hashes[1]); 703 hashes[1] = h; 704 } 705 rxfilt |= RL_RXCFG_RX_MULTI; 706 } 707 708 if (sc->rl_hwrev->rl_rev == RL_HWREV_8168F) { 709 /* Disable multicast filtering due to silicon bug. */ 710 hashes[0] = 0xffffffff; 711 hashes[1] = 0xffffffff; 712 } 713 714 done: 715 CSR_WRITE_4(sc, RL_MAR0, hashes[0]); 716 CSR_WRITE_4(sc, RL_MAR4, hashes[1]); 717 CSR_WRITE_4(sc, RL_RXCFG, rxfilt); 718 } 719 720 static void 721 re_reset(struct rl_softc *sc) 722 { 723 int i; 724 725 RL_LOCK_ASSERT(sc); 726 727 CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET); 728 729 for (i = 0; i < RL_TIMEOUT; i++) { 730 DELAY(10); 731 if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET)) 732 break; 733 } 734 if (i == RL_TIMEOUT) 735 device_printf(sc->rl_dev, "reset never completed!\n"); 736 737 if ((sc->rl_flags & RL_FLAG_MACRESET) != 0) 738 CSR_WRITE_1(sc, 0x82, 1); 739 if (sc->rl_hwrev->rl_rev == RL_HWREV_8169S) 740 re_gmii_writereg(sc->rl_dev, 1, 0x0b, 0); 741 } 742 743 #ifdef RE_DIAG 744 745 /* 746 * The following routine is designed to test for a defect on some 747 * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64# 748 * lines connected to the bus, however for a 32-bit only card, they 749 * should be pulled high. The result of this defect is that the 750 * NIC will not work right if you plug it into a 64-bit slot: DMA 751 * operations will be done with 64-bit transfers, which will fail 752 * because the 64-bit data lines aren't connected. 753 * 754 * There's no way to work around this (short of talking a soldering 755 * iron to the board), however we can detect it. The method we use 756 * here is to put the NIC into digital loopback mode, set the receiver 757 * to promiscuous mode, and then try to send a frame. We then compare 758 * the frame data we sent to what was received. If the data matches, 759 * then the NIC is working correctly, otherwise we know the user has 760 * a defective NIC which has been mistakenly plugged into a 64-bit PCI 761 * slot. In the latter case, there's no way the NIC can work correctly, 762 * so we print out a message on the console and abort the device attach. 763 */ 764 765 static int 766 re_diag(struct rl_softc *sc) 767 { 768 struct ifnet *ifp = sc->rl_ifp; 769 struct mbuf *m0; 770 struct ether_header *eh; 771 struct rl_desc *cur_rx; 772 u_int16_t status; 773 u_int32_t rxstat; 774 int total_len, i, error = 0, phyaddr; 775 u_int8_t dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' }; 776 u_int8_t src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' }; 777 778 /* Allocate a single mbuf */ 779 MGETHDR(m0, M_NOWAIT, MT_DATA); 780 if (m0 == NULL) 781 return (ENOBUFS); 782 783 RL_LOCK(sc); 784 785 /* 786 * Initialize the NIC in test mode. This sets the chip up 787 * so that it can send and receive frames, but performs the 788 * following special functions: 789 * - Puts receiver in promiscuous mode 790 * - Enables digital loopback mode 791 * - Leaves interrupts turned off 792 */ 793 794 ifp->if_flags |= IFF_PROMISC; 795 sc->rl_testmode = 1; 796 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 797 re_init_locked(sc); 798 sc->rl_flags |= RL_FLAG_LINK; 799 if (sc->rl_type == RL_8169) 800 phyaddr = 1; 801 else 802 phyaddr = 0; 803 804 re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET); 805 for (i = 0; i < RL_TIMEOUT; i++) { 806 status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR); 807 if (!(status & BMCR_RESET)) 808 break; 809 } 810 811 re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP); 812 CSR_WRITE_2(sc, RL_ISR, RL_INTRS); 813 814 DELAY(100000); 815 816 /* Put some data in the mbuf */ 817 818 eh = mtod(m0, struct ether_header *); 819 bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN); 820 bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN); 821 eh->ether_type = htons(ETHERTYPE_IP); 822 m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN; 823 824 /* 825 * Queue the packet, start transmission. 826 * Note: IF_HANDOFF() ultimately calls re_start() for us. 827 */ 828 829 CSR_WRITE_2(sc, RL_ISR, 0xFFFF); 830 RL_UNLOCK(sc); 831 /* XXX: re_diag must not be called when in ALTQ mode */ 832 IF_HANDOFF(&ifp->if_snd, m0, ifp); 833 RL_LOCK(sc); 834 m0 = NULL; 835 836 /* Wait for it to propagate through the chip */ 837 838 DELAY(100000); 839 for (i = 0; i < RL_TIMEOUT; i++) { 840 status = CSR_READ_2(sc, RL_ISR); 841 CSR_WRITE_2(sc, RL_ISR, status); 842 if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) == 843 (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) 844 break; 845 DELAY(10); 846 } 847 848 if (i == RL_TIMEOUT) { 849 device_printf(sc->rl_dev, 850 "diagnostic failed, failed to receive packet in" 851 " loopback mode\n"); 852 error = EIO; 853 goto done; 854 } 855 856 /* 857 * The packet should have been dumped into the first 858 * entry in the RX DMA ring. Grab it from there. 859 */ 860 861 bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 862 sc->rl_ldata.rl_rx_list_map, 863 BUS_DMASYNC_POSTREAD); 864 bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, 865 sc->rl_ldata.rl_rx_desc[0].rx_dmamap, 866 BUS_DMASYNC_POSTREAD); 867 bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, 868 sc->rl_ldata.rl_rx_desc[0].rx_dmamap); 869 870 m0 = sc->rl_ldata.rl_rx_desc[0].rx_m; 871 sc->rl_ldata.rl_rx_desc[0].rx_m = NULL; 872 eh = mtod(m0, struct ether_header *); 873 874 cur_rx = &sc->rl_ldata.rl_rx_list[0]; 875 total_len = RL_RXBYTES(cur_rx); 876 rxstat = le32toh(cur_rx->rl_cmdstat); 877 878 if (total_len != ETHER_MIN_LEN) { 879 device_printf(sc->rl_dev, 880 "diagnostic failed, received short packet\n"); 881 error = EIO; 882 goto done; 883 } 884 885 /* Test that the received packet data matches what we sent. */ 886 887 if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) || 888 bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) || 889 ntohs(eh->ether_type) != ETHERTYPE_IP) { 890 device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n"); 891 device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n", 892 dst, ":", src, ":", ETHERTYPE_IP); 893 device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n", 894 eh->ether_dhost, ":", eh->ether_shost, ":", 895 ntohs(eh->ether_type)); 896 device_printf(sc->rl_dev, "You may have a defective 32-bit " 897 "NIC plugged into a 64-bit PCI slot.\n"); 898 device_printf(sc->rl_dev, "Please re-install the NIC in a " 899 "32-bit slot for proper operation.\n"); 900 device_printf(sc->rl_dev, "Read the re(4) man page for more " 901 "details.\n"); 902 error = EIO; 903 } 904 905 done: 906 /* Turn interface off, release resources */ 907 908 sc->rl_testmode = 0; 909 sc->rl_flags &= ~RL_FLAG_LINK; 910 ifp->if_flags &= ~IFF_PROMISC; 911 re_stop(sc); 912 if (m0 != NULL) 913 m_freem(m0); 914 915 RL_UNLOCK(sc); 916 917 return (error); 918 } 919 920 #endif 921 922 /* 923 * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device 924 * IDs against our list and return a device name if we find a match. 925 */ 926 static int 927 re_probe(device_t dev) 928 { 929 const struct rl_type *t; 930 uint16_t devid, vendor; 931 uint16_t revid, sdevid; 932 int i; 933 934 vendor = pci_get_vendor(dev); 935 devid = pci_get_device(dev); 936 revid = pci_get_revid(dev); 937 sdevid = pci_get_subdevice(dev); 938 939 if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) { 940 if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) { 941 /* 942 * Only attach to rev. 3 of the Linksys EG1032 adapter. 943 * Rev. 2 is supported by sk(4). 944 */ 945 return (ENXIO); 946 } 947 } 948 949 if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) { 950 if (revid != 0x20) { 951 /* 8139, let rl(4) take care of this device. */ 952 return (ENXIO); 953 } 954 } 955 956 t = re_devs; 957 for (i = 0; i < sizeof(re_devs) / sizeof(re_devs[0]); i++, t++) { 958 if (vendor == t->rl_vid && devid == t->rl_did) { 959 device_set_desc(dev, t->rl_name); 960 return (BUS_PROBE_DEFAULT); 961 } 962 } 963 964 return (ENXIO); 965 } 966 967 /* 968 * Map a single buffer address. 969 */ 970 971 static void 972 re_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error) 973 { 974 bus_addr_t *addr; 975 976 if (error) 977 return; 978 979 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 980 addr = arg; 981 *addr = segs->ds_addr; 982 } 983 984 static int 985 re_allocmem(device_t dev, struct rl_softc *sc) 986 { 987 bus_addr_t lowaddr; 988 bus_size_t rx_list_size, tx_list_size; 989 int error; 990 int i; 991 992 rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc); 993 tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc); 994 995 /* 996 * Allocate the parent bus DMA tag appropriate for PCI. 997 * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD 998 * register should be set. However some RealTek chips are known 999 * to be buggy on DAC handling, therefore disable DAC by limiting 1000 * DMA address space to 32bit. PCIe variants of RealTek chips 1001 * may not have the limitation. 1002 */ 1003 lowaddr = BUS_SPACE_MAXADDR; 1004 if ((sc->rl_flags & RL_FLAG_PCIE) == 0) 1005 lowaddr = BUS_SPACE_MAXADDR_32BIT; 1006 error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0, 1007 lowaddr, BUS_SPACE_MAXADDR, NULL, NULL, 1008 BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0, 1009 NULL, NULL, &sc->rl_parent_tag); 1010 if (error) { 1011 device_printf(dev, "could not allocate parent DMA tag\n"); 1012 return (error); 1013 } 1014 1015 /* 1016 * Allocate map for TX mbufs. 1017 */ 1018 error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0, 1019 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, 1020 NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0, 1021 NULL, NULL, &sc->rl_ldata.rl_tx_mtag); 1022 if (error) { 1023 device_printf(dev, "could not allocate TX DMA tag\n"); 1024 return (error); 1025 } 1026 1027 /* 1028 * Allocate map for RX mbufs. 1029 */ 1030 1031 if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) { 1032 error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 1033 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 1034 MJUM9BYTES, 1, MJUM9BYTES, 0, NULL, NULL, 1035 &sc->rl_ldata.rl_jrx_mtag); 1036 if (error) { 1037 device_printf(dev, 1038 "could not allocate jumbo RX DMA tag\n"); 1039 return (error); 1040 } 1041 } 1042 error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0, 1043 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 1044 MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag); 1045 if (error) { 1046 device_printf(dev, "could not allocate RX DMA tag\n"); 1047 return (error); 1048 } 1049 1050 /* 1051 * Allocate map for TX descriptor list. 1052 */ 1053 error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN, 1054 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 1055 NULL, tx_list_size, 1, tx_list_size, 0, 1056 NULL, NULL, &sc->rl_ldata.rl_tx_list_tag); 1057 if (error) { 1058 device_printf(dev, "could not allocate TX DMA ring tag\n"); 1059 return (error); 1060 } 1061 1062 /* Allocate DMA'able memory for the TX ring */ 1063 1064 error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag, 1065 (void **)&sc->rl_ldata.rl_tx_list, 1066 BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, 1067 &sc->rl_ldata.rl_tx_list_map); 1068 if (error) { 1069 device_printf(dev, "could not allocate TX DMA ring\n"); 1070 return (error); 1071 } 1072 1073 /* Load the map for the TX ring. */ 1074 1075 sc->rl_ldata.rl_tx_list_addr = 0; 1076 error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag, 1077 sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list, 1078 tx_list_size, re_dma_map_addr, 1079 &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT); 1080 if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) { 1081 device_printf(dev, "could not load TX DMA ring\n"); 1082 return (ENOMEM); 1083 } 1084 1085 /* Create DMA maps for TX buffers */ 1086 1087 for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) { 1088 error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0, 1089 &sc->rl_ldata.rl_tx_desc[i].tx_dmamap); 1090 if (error) { 1091 device_printf(dev, "could not create DMA map for TX\n"); 1092 return (error); 1093 } 1094 } 1095 1096 /* 1097 * Allocate map for RX descriptor list. 1098 */ 1099 error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN, 1100 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 1101 NULL, rx_list_size, 1, rx_list_size, 0, 1102 NULL, NULL, &sc->rl_ldata.rl_rx_list_tag); 1103 if (error) { 1104 device_printf(dev, "could not create RX DMA ring tag\n"); 1105 return (error); 1106 } 1107 1108 /* Allocate DMA'able memory for the RX ring */ 1109 1110 error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag, 1111 (void **)&sc->rl_ldata.rl_rx_list, 1112 BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, 1113 &sc->rl_ldata.rl_rx_list_map); 1114 if (error) { 1115 device_printf(dev, "could not allocate RX DMA ring\n"); 1116 return (error); 1117 } 1118 1119 /* Load the map for the RX ring. */ 1120 1121 sc->rl_ldata.rl_rx_list_addr = 0; 1122 error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag, 1123 sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list, 1124 rx_list_size, re_dma_map_addr, 1125 &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT); 1126 if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) { 1127 device_printf(dev, "could not load RX DMA ring\n"); 1128 return (ENOMEM); 1129 } 1130 1131 /* Create DMA maps for RX buffers */ 1132 1133 if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) { 1134 error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0, 1135 &sc->rl_ldata.rl_jrx_sparemap); 1136 if (error) { 1137 device_printf(dev, 1138 "could not create spare DMA map for jumbo RX\n"); 1139 return (error); 1140 } 1141 for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 1142 error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0, 1143 &sc->rl_ldata.rl_jrx_desc[i].rx_dmamap); 1144 if (error) { 1145 device_printf(dev, 1146 "could not create DMA map for jumbo RX\n"); 1147 return (error); 1148 } 1149 } 1150 } 1151 error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0, 1152 &sc->rl_ldata.rl_rx_sparemap); 1153 if (error) { 1154 device_printf(dev, "could not create spare DMA map for RX\n"); 1155 return (error); 1156 } 1157 for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 1158 error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0, 1159 &sc->rl_ldata.rl_rx_desc[i].rx_dmamap); 1160 if (error) { 1161 device_printf(dev, "could not create DMA map for RX\n"); 1162 return (error); 1163 } 1164 } 1165 1166 /* Create DMA map for statistics. */ 1167 error = bus_dma_tag_create(sc->rl_parent_tag, RL_DUMP_ALIGN, 0, 1168 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 1169 sizeof(struct rl_stats), 1, sizeof(struct rl_stats), 0, NULL, NULL, 1170 &sc->rl_ldata.rl_stag); 1171 if (error) { 1172 device_printf(dev, "could not create statistics DMA tag\n"); 1173 return (error); 1174 } 1175 /* Allocate DMA'able memory for statistics. */ 1176 error = bus_dmamem_alloc(sc->rl_ldata.rl_stag, 1177 (void **)&sc->rl_ldata.rl_stats, 1178 BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, 1179 &sc->rl_ldata.rl_smap); 1180 if (error) { 1181 device_printf(dev, 1182 "could not allocate statistics DMA memory\n"); 1183 return (error); 1184 } 1185 /* Load the map for statistics. */ 1186 sc->rl_ldata.rl_stats_addr = 0; 1187 error = bus_dmamap_load(sc->rl_ldata.rl_stag, sc->rl_ldata.rl_smap, 1188 sc->rl_ldata.rl_stats, sizeof(struct rl_stats), re_dma_map_addr, 1189 &sc->rl_ldata.rl_stats_addr, BUS_DMA_NOWAIT); 1190 if (error != 0 || sc->rl_ldata.rl_stats_addr == 0) { 1191 device_printf(dev, "could not load statistics DMA memory\n"); 1192 return (ENOMEM); 1193 } 1194 1195 return (0); 1196 } 1197 1198 /* 1199 * Attach the interface. Allocate softc structures, do ifmedia 1200 * setup and ethernet/BPF attach. 1201 */ 1202 static int 1203 re_attach(device_t dev) 1204 { 1205 u_char eaddr[ETHER_ADDR_LEN]; 1206 u_int16_t as[ETHER_ADDR_LEN / 2]; 1207 struct rl_softc *sc; 1208 struct ifnet *ifp; 1209 const struct rl_hwrev *hw_rev; 1210 u_int32_t cap, ctl; 1211 int hwrev; 1212 u_int16_t devid, re_did = 0; 1213 int error = 0, i, phy, rid; 1214 int msic, msixc, reg; 1215 uint8_t cfg; 1216 1217 sc = device_get_softc(dev); 1218 sc->rl_dev = dev; 1219 1220 mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 1221 MTX_DEF); 1222 callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0); 1223 1224 /* 1225 * Map control/status registers. 1226 */ 1227 pci_enable_busmaster(dev); 1228 1229 devid = pci_get_device(dev); 1230 /* 1231 * Prefer memory space register mapping over IO space. 1232 * Because RTL8169SC does not seem to work when memory mapping 1233 * is used always activate io mapping. 1234 */ 1235 if (devid == RT_DEVICEID_8169SC) 1236 prefer_iomap = 1; 1237 if (prefer_iomap == 0) { 1238 sc->rl_res_id = PCIR_BAR(1); 1239 sc->rl_res_type = SYS_RES_MEMORY; 1240 /* RTL8168/8101E seems to use different BARs. */ 1241 if (devid == RT_DEVICEID_8168 || devid == RT_DEVICEID_8101E) 1242 sc->rl_res_id = PCIR_BAR(2); 1243 } else { 1244 sc->rl_res_id = PCIR_BAR(0); 1245 sc->rl_res_type = SYS_RES_IOPORT; 1246 } 1247 sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type, 1248 &sc->rl_res_id, RF_ACTIVE); 1249 if (sc->rl_res == NULL && prefer_iomap == 0) { 1250 sc->rl_res_id = PCIR_BAR(0); 1251 sc->rl_res_type = SYS_RES_IOPORT; 1252 sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type, 1253 &sc->rl_res_id, RF_ACTIVE); 1254 } 1255 if (sc->rl_res == NULL) { 1256 device_printf(dev, "couldn't map ports/memory\n"); 1257 error = ENXIO; 1258 goto fail; 1259 } 1260 1261 sc->rl_btag = rman_get_bustag(sc->rl_res); 1262 sc->rl_bhandle = rman_get_bushandle(sc->rl_res); 1263 1264 msic = pci_msi_count(dev); 1265 msixc = pci_msix_count(dev); 1266 if (pci_find_cap(dev, PCIY_EXPRESS, ®) == 0) { 1267 sc->rl_flags |= RL_FLAG_PCIE; 1268 sc->rl_expcap = reg; 1269 } 1270 if (bootverbose) { 1271 device_printf(dev, "MSI count : %d\n", msic); 1272 device_printf(dev, "MSI-X count : %d\n", msixc); 1273 } 1274 if (msix_disable > 0) 1275 msixc = 0; 1276 if (msi_disable > 0) 1277 msic = 0; 1278 /* Prefer MSI-X to MSI. */ 1279 if (msixc > 0) { 1280 msixc = RL_MSI_MESSAGES; 1281 rid = PCIR_BAR(4); 1282 sc->rl_res_pba = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 1283 &rid, RF_ACTIVE); 1284 if (sc->rl_res_pba == NULL) { 1285 device_printf(sc->rl_dev, 1286 "could not allocate MSI-X PBA resource\n"); 1287 } 1288 if (sc->rl_res_pba != NULL && 1289 pci_alloc_msix(dev, &msixc) == 0) { 1290 if (msixc == RL_MSI_MESSAGES) { 1291 device_printf(dev, "Using %d MSI-X message\n", 1292 msixc); 1293 sc->rl_flags |= RL_FLAG_MSIX; 1294 } else 1295 pci_release_msi(dev); 1296 } 1297 if ((sc->rl_flags & RL_FLAG_MSIX) == 0) { 1298 if (sc->rl_res_pba != NULL) 1299 bus_release_resource(dev, SYS_RES_MEMORY, rid, 1300 sc->rl_res_pba); 1301 sc->rl_res_pba = NULL; 1302 msixc = 0; 1303 } 1304 } 1305 /* Prefer MSI to INTx. */ 1306 if (msixc == 0 && msic > 0) { 1307 msic = RL_MSI_MESSAGES; 1308 if (pci_alloc_msi(dev, &msic) == 0) { 1309 if (msic == RL_MSI_MESSAGES) { 1310 device_printf(dev, "Using %d MSI message\n", 1311 msic); 1312 sc->rl_flags |= RL_FLAG_MSI; 1313 /* Explicitly set MSI enable bit. */ 1314 CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE); 1315 cfg = CSR_READ_1(sc, RL_CFG2); 1316 cfg |= RL_CFG2_MSI; 1317 CSR_WRITE_1(sc, RL_CFG2, cfg); 1318 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 1319 } else 1320 pci_release_msi(dev); 1321 } 1322 if ((sc->rl_flags & RL_FLAG_MSI) == 0) 1323 msic = 0; 1324 } 1325 1326 /* Allocate interrupt */ 1327 if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0) { 1328 rid = 0; 1329 sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 1330 RF_SHAREABLE | RF_ACTIVE); 1331 if (sc->rl_irq[0] == NULL) { 1332 device_printf(dev, "couldn't allocate IRQ resources\n"); 1333 error = ENXIO; 1334 goto fail; 1335 } 1336 } else { 1337 for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) { 1338 sc->rl_irq[i] = bus_alloc_resource_any(dev, 1339 SYS_RES_IRQ, &rid, RF_ACTIVE); 1340 if (sc->rl_irq[i] == NULL) { 1341 device_printf(dev, 1342 "couldn't allocate IRQ resources for " 1343 "message %d\n", rid); 1344 error = ENXIO; 1345 goto fail; 1346 } 1347 } 1348 } 1349 1350 if ((sc->rl_flags & RL_FLAG_MSI) == 0) { 1351 CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE); 1352 cfg = CSR_READ_1(sc, RL_CFG2); 1353 if ((cfg & RL_CFG2_MSI) != 0) { 1354 device_printf(dev, "turning off MSI enable bit.\n"); 1355 cfg &= ~RL_CFG2_MSI; 1356 CSR_WRITE_1(sc, RL_CFG2, cfg); 1357 } 1358 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 1359 } 1360 1361 /* Disable ASPM L0S/L1. */ 1362 if (sc->rl_expcap != 0) { 1363 cap = pci_read_config(dev, sc->rl_expcap + 1364 PCIER_LINK_CAP, 2); 1365 if ((cap & PCIEM_LINK_CAP_ASPM) != 0) { 1366 ctl = pci_read_config(dev, sc->rl_expcap + 1367 PCIER_LINK_CTL, 2); 1368 if ((ctl & PCIEM_LINK_CTL_ASPMC) != 0) { 1369 ctl &= ~PCIEM_LINK_CTL_ASPMC; 1370 pci_write_config(dev, sc->rl_expcap + 1371 PCIER_LINK_CTL, ctl, 2); 1372 device_printf(dev, "ASPM disabled\n"); 1373 } 1374 } else 1375 device_printf(dev, "no ASPM capability\n"); 1376 } 1377 1378 hw_rev = re_hwrevs; 1379 hwrev = CSR_READ_4(sc, RL_TXCFG); 1380 switch (hwrev & 0x70000000) { 1381 case 0x00000000: 1382 case 0x10000000: 1383 device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0xfc800000); 1384 hwrev &= (RL_TXCFG_HWREV | 0x80000000); 1385 break; 1386 default: 1387 device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0x7c800000); 1388 sc->rl_macrev = hwrev & 0x00700000; 1389 hwrev &= RL_TXCFG_HWREV; 1390 break; 1391 } 1392 device_printf(dev, "MAC rev. 0x%08x\n", sc->rl_macrev); 1393 while (hw_rev->rl_desc != NULL) { 1394 if (hw_rev->rl_rev == hwrev) { 1395 sc->rl_type = hw_rev->rl_type; 1396 sc->rl_hwrev = hw_rev; 1397 break; 1398 } 1399 hw_rev++; 1400 } 1401 if (hw_rev->rl_desc == NULL) { 1402 device_printf(dev, "Unknown H/W revision: 0x%08x\n", hwrev); 1403 error = ENXIO; 1404 goto fail; 1405 } 1406 1407 switch (hw_rev->rl_rev) { 1408 case RL_HWREV_8139CPLUS: 1409 sc->rl_flags |= RL_FLAG_FASTETHER | RL_FLAG_AUTOPAD; 1410 break; 1411 case RL_HWREV_8100E: 1412 case RL_HWREV_8101E: 1413 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_FASTETHER; 1414 break; 1415 case RL_HWREV_8102E: 1416 case RL_HWREV_8102EL: 1417 case RL_HWREV_8102EL_SPIN1: 1418 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 | 1419 RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | 1420 RL_FLAG_AUTOPAD; 1421 break; 1422 case RL_HWREV_8103E: 1423 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 | 1424 RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | 1425 RL_FLAG_AUTOPAD | RL_FLAG_MACSLEEP; 1426 break; 1427 case RL_HWREV_8401E: 1428 case RL_HWREV_8105E: 1429 case RL_HWREV_8105E_SPIN1: 1430 case RL_HWREV_8106E: 1431 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM | 1432 RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | 1433 RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD; 1434 break; 1435 case RL_HWREV_8402: 1436 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM | 1437 RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | 1438 RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | 1439 RL_FLAG_CMDSTOP_WAIT_TXQ; 1440 break; 1441 case RL_HWREV_8168B_SPIN1: 1442 case RL_HWREV_8168B_SPIN2: 1443 sc->rl_flags |= RL_FLAG_WOLRXENB; 1444 /* FALLTHROUGH */ 1445 case RL_HWREV_8168B_SPIN3: 1446 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_MACSTAT; 1447 break; 1448 case RL_HWREV_8168C_SPIN2: 1449 sc->rl_flags |= RL_FLAG_MACSLEEP; 1450 /* FALLTHROUGH */ 1451 case RL_HWREV_8168C: 1452 if (sc->rl_macrev == 0x00200000) 1453 sc->rl_flags |= RL_FLAG_MACSLEEP; 1454 /* FALLTHROUGH */ 1455 case RL_HWREV_8168CP: 1456 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | 1457 RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP | 1458 RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 | RL_FLAG_WOL_MANLINK; 1459 break; 1460 case RL_HWREV_8168D: 1461 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM | 1462 RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | 1463 RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 | 1464 RL_FLAG_WOL_MANLINK; 1465 break; 1466 case RL_HWREV_8168DP: 1467 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | 1468 RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_AUTOPAD | 1469 RL_FLAG_JUMBOV2 | RL_FLAG_WAIT_TXPOLL | RL_FLAG_WOL_MANLINK; 1470 break; 1471 case RL_HWREV_8168E: 1472 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM | 1473 RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | 1474 RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 | 1475 RL_FLAG_WOL_MANLINK; 1476 break; 1477 case RL_HWREV_8168E_VL: 1478 case RL_HWREV_8168F: 1479 sc->rl_flags |= RL_FLAG_EARLYOFF; 1480 /* FALLTHROUGH */ 1481 case RL_HWREV_8411: 1482 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | 1483 RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP | 1484 RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 | 1485 RL_FLAG_CMDSTOP_WAIT_TXQ | RL_FLAG_WOL_MANLINK; 1486 break; 1487 case RL_HWREV_8168EP: 1488 case RL_HWREV_8168G: 1489 case RL_HWREV_8168H: 1490 case RL_HWREV_8411B: 1491 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | 1492 RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP | 1493 RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 | 1494 RL_FLAG_CMDSTOP_WAIT_TXQ | RL_FLAG_WOL_MANLINK | 1495 RL_FLAG_EARLYOFFV2 | RL_FLAG_RXDV_GATED; 1496 break; 1497 case RL_HWREV_8168GU: 1498 if (pci_get_device(dev) == RT_DEVICEID_8101E) { 1499 /* RTL8106EUS */ 1500 sc->rl_flags |= RL_FLAG_FASTETHER; 1501 } else 1502 sc->rl_flags |= RL_FLAG_JUMBOV2 | RL_FLAG_WOL_MANLINK; 1503 1504 sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | 1505 RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP | 1506 RL_FLAG_AUTOPAD | RL_FLAG_CMDSTOP_WAIT_TXQ | 1507 RL_FLAG_EARLYOFFV2 | RL_FLAG_RXDV_GATED; 1508 break; 1509 case RL_HWREV_8169_8110SB: 1510 case RL_HWREV_8169_8110SBL: 1511 case RL_HWREV_8169_8110SC: 1512 case RL_HWREV_8169_8110SCE: 1513 sc->rl_flags |= RL_FLAG_PHYWAKE; 1514 /* FALLTHROUGH */ 1515 case RL_HWREV_8169: 1516 case RL_HWREV_8169S: 1517 case RL_HWREV_8110S: 1518 sc->rl_flags |= RL_FLAG_MACRESET; 1519 break; 1520 default: 1521 break; 1522 } 1523 1524 if (sc->rl_hwrev->rl_rev == RL_HWREV_8139CPLUS) { 1525 sc->rl_cfg0 = RL_8139_CFG0; 1526 sc->rl_cfg1 = RL_8139_CFG1; 1527 sc->rl_cfg2 = 0; 1528 sc->rl_cfg3 = RL_8139_CFG3; 1529 sc->rl_cfg4 = RL_8139_CFG4; 1530 sc->rl_cfg5 = RL_8139_CFG5; 1531 } else { 1532 sc->rl_cfg0 = RL_CFG0; 1533 sc->rl_cfg1 = RL_CFG1; 1534 sc->rl_cfg2 = RL_CFG2; 1535 sc->rl_cfg3 = RL_CFG3; 1536 sc->rl_cfg4 = RL_CFG4; 1537 sc->rl_cfg5 = RL_CFG5; 1538 } 1539 1540 /* Reset the adapter. */ 1541 RL_LOCK(sc); 1542 re_reset(sc); 1543 RL_UNLOCK(sc); 1544 1545 /* Enable PME. */ 1546 CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE); 1547 cfg = CSR_READ_1(sc, sc->rl_cfg1); 1548 cfg |= RL_CFG1_PME; 1549 CSR_WRITE_1(sc, sc->rl_cfg1, cfg); 1550 cfg = CSR_READ_1(sc, sc->rl_cfg5); 1551 cfg &= RL_CFG5_PME_STS; 1552 CSR_WRITE_1(sc, sc->rl_cfg5, cfg); 1553 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 1554 1555 if ((sc->rl_flags & RL_FLAG_PAR) != 0) { 1556 /* 1557 * XXX Should have a better way to extract station 1558 * address from EEPROM. 1559 */ 1560 for (i = 0; i < ETHER_ADDR_LEN; i++) 1561 eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i); 1562 } else { 1563 sc->rl_eewidth = RL_9356_ADDR_LEN; 1564 re_read_eeprom(sc, (caddr_t)&re_did, 0, 1); 1565 if (re_did != 0x8129) 1566 sc->rl_eewidth = RL_9346_ADDR_LEN; 1567 1568 /* 1569 * Get station address from the EEPROM. 1570 */ 1571 re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3); 1572 for (i = 0; i < ETHER_ADDR_LEN / 2; i++) 1573 as[i] = le16toh(as[i]); 1574 bcopy(as, eaddr, ETHER_ADDR_LEN); 1575 } 1576 1577 if (sc->rl_type == RL_8169) { 1578 /* Set RX length mask and number of descriptors. */ 1579 sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN; 1580 sc->rl_txstart = RL_GTXSTART; 1581 sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT; 1582 sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT; 1583 } else { 1584 /* Set RX length mask and number of descriptors. */ 1585 sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN; 1586 sc->rl_txstart = RL_TXSTART; 1587 sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT; 1588 sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT; 1589 } 1590 1591 error = re_allocmem(dev, sc); 1592 if (error) 1593 goto fail; 1594 re_add_sysctls(sc); 1595 1596 ifp = sc->rl_ifp = if_alloc(IFT_ETHER); 1597 if (ifp == NULL) { 1598 device_printf(dev, "can not if_alloc()\n"); 1599 error = ENOSPC; 1600 goto fail; 1601 } 1602 1603 /* Take controller out of deep sleep mode. */ 1604 if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) { 1605 if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80) 1606 CSR_WRITE_1(sc, RL_GPIO, 1607 CSR_READ_1(sc, RL_GPIO) | 0x01); 1608 else 1609 CSR_WRITE_1(sc, RL_GPIO, 1610 CSR_READ_1(sc, RL_GPIO) & ~0x01); 1611 } 1612 1613 /* Take PHY out of power down mode. */ 1614 if ((sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0) { 1615 CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) | 0x80); 1616 if (hw_rev->rl_rev == RL_HWREV_8401E) 1617 CSR_WRITE_1(sc, 0xD1, CSR_READ_1(sc, 0xD1) & ~0x08); 1618 } 1619 if ((sc->rl_flags & RL_FLAG_PHYWAKE) != 0) { 1620 re_gmii_writereg(dev, 1, 0x1f, 0); 1621 re_gmii_writereg(dev, 1, 0x0e, 0); 1622 } 1623 1624 ifp->if_softc = sc; 1625 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 1626 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1627 ifp->if_ioctl = re_ioctl; 1628 ifp->if_start = re_start; 1629 /* 1630 * RTL8168/8111C generates wrong IP checksummed frame if the 1631 * packet has IP options so disable TX checksum offloading. 1632 */ 1633 if (sc->rl_hwrev->rl_rev == RL_HWREV_8168C || 1634 sc->rl_hwrev->rl_rev == RL_HWREV_8168C_SPIN2 || 1635 sc->rl_hwrev->rl_rev == RL_HWREV_8168CP) { 1636 ifp->if_hwassist = 0; 1637 ifp->if_capabilities = IFCAP_RXCSUM | IFCAP_TSO4; 1638 } else { 1639 ifp->if_hwassist = CSUM_IP | CSUM_TCP | CSUM_UDP; 1640 ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_TSO4; 1641 } 1642 ifp->if_hwassist |= CSUM_TSO; 1643 ifp->if_capenable = ifp->if_capabilities; 1644 ifp->if_init = re_init; 1645 IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN); 1646 ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN; 1647 IFQ_SET_READY(&ifp->if_snd); 1648 1649 TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc); 1650 1651 #define RE_PHYAD_INTERNAL 0 1652 1653 /* Do MII setup. */ 1654 phy = RE_PHYAD_INTERNAL; 1655 if (sc->rl_type == RL_8169) 1656 phy = 1; 1657 error = mii_attach(dev, &sc->rl_miibus, ifp, re_ifmedia_upd, 1658 re_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, MIIF_DOPAUSE); 1659 if (error != 0) { 1660 device_printf(dev, "attaching PHYs failed\n"); 1661 goto fail; 1662 } 1663 1664 /* 1665 * Call MI attach routine. 1666 */ 1667 ether_ifattach(ifp, eaddr); 1668 1669 /* VLAN capability setup */ 1670 ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING; 1671 if (ifp->if_capabilities & IFCAP_HWCSUM) 1672 ifp->if_capabilities |= IFCAP_VLAN_HWCSUM; 1673 /* Enable WOL if PM is supported. */ 1674 if (pci_find_cap(sc->rl_dev, PCIY_PMG, ®) == 0) 1675 ifp->if_capabilities |= IFCAP_WOL; 1676 ifp->if_capenable = ifp->if_capabilities; 1677 ifp->if_capenable &= ~(IFCAP_WOL_UCAST | IFCAP_WOL_MCAST); 1678 /* 1679 * Don't enable TSO by default. It is known to generate 1680 * corrupted TCP segments(bad TCP options) under certain 1681 * circumstances. 1682 */ 1683 ifp->if_hwassist &= ~CSUM_TSO; 1684 ifp->if_capenable &= ~(IFCAP_TSO4 | IFCAP_VLAN_HWTSO); 1685 #ifdef DEVICE_POLLING 1686 ifp->if_capabilities |= IFCAP_POLLING; 1687 #endif 1688 /* 1689 * Tell the upper layer(s) we support long frames. 1690 * Must appear after the call to ether_ifattach() because 1691 * ether_ifattach() sets ifi_hdrlen to the default value. 1692 */ 1693 ifp->if_hdrlen = sizeof(struct ether_vlan_header); 1694 1695 #ifdef DEV_NETMAP 1696 re_netmap_attach(sc); 1697 #endif /* DEV_NETMAP */ 1698 #ifdef RE_DIAG 1699 /* 1700 * Perform hardware diagnostic on the original RTL8169. 1701 * Some 32-bit cards were incorrectly wired and would 1702 * malfunction if plugged into a 64-bit slot. 1703 */ 1704 1705 if (hwrev == RL_HWREV_8169) { 1706 error = re_diag(sc); 1707 if (error) { 1708 device_printf(dev, 1709 "attach aborted due to hardware diag failure\n"); 1710 ether_ifdetach(ifp); 1711 goto fail; 1712 } 1713 } 1714 #endif 1715 1716 #ifdef RE_TX_MODERATION 1717 intr_filter = 1; 1718 #endif 1719 /* Hook interrupt last to avoid having to lock softc */ 1720 if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0 && 1721 intr_filter == 0) { 1722 error = bus_setup_intr(dev, sc->rl_irq[0], 1723 INTR_TYPE_NET | INTR_MPSAFE, NULL, re_intr_msi, sc, 1724 &sc->rl_intrhand[0]); 1725 } else { 1726 error = bus_setup_intr(dev, sc->rl_irq[0], 1727 INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc, 1728 &sc->rl_intrhand[0]); 1729 } 1730 if (error) { 1731 device_printf(dev, "couldn't set up irq\n"); 1732 ether_ifdetach(ifp); 1733 } 1734 1735 fail: 1736 1737 if (error) 1738 re_detach(dev); 1739 1740 return (error); 1741 } 1742 1743 /* 1744 * Shutdown hardware and free up resources. This can be called any 1745 * time after the mutex has been initialized. It is called in both 1746 * the error case in attach and the normal detach case so it needs 1747 * to be careful about only freeing resources that have actually been 1748 * allocated. 1749 */ 1750 static int 1751 re_detach(device_t dev) 1752 { 1753 struct rl_softc *sc; 1754 struct ifnet *ifp; 1755 int i, rid; 1756 1757 sc = device_get_softc(dev); 1758 ifp = sc->rl_ifp; 1759 KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized")); 1760 1761 /* These should only be active if attach succeeded */ 1762 if (device_is_attached(dev)) { 1763 #ifdef DEVICE_POLLING 1764 if (ifp->if_capenable & IFCAP_POLLING) 1765 ether_poll_deregister(ifp); 1766 #endif 1767 RL_LOCK(sc); 1768 #if 0 1769 sc->suspended = 1; 1770 #endif 1771 re_stop(sc); 1772 RL_UNLOCK(sc); 1773 callout_drain(&sc->rl_stat_callout); 1774 taskqueue_drain(taskqueue_fast, &sc->rl_inttask); 1775 /* 1776 * Force off the IFF_UP flag here, in case someone 1777 * still had a BPF descriptor attached to this 1778 * interface. If they do, ether_ifdetach() will cause 1779 * the BPF code to try and clear the promisc mode 1780 * flag, which will bubble down to re_ioctl(), 1781 * which will try to call re_init() again. This will 1782 * turn the NIC back on and restart the MII ticker, 1783 * which will panic the system when the kernel tries 1784 * to invoke the re_tick() function that isn't there 1785 * anymore. 1786 */ 1787 ifp->if_flags &= ~IFF_UP; 1788 ether_ifdetach(ifp); 1789 } 1790 if (sc->rl_miibus) 1791 device_delete_child(dev, sc->rl_miibus); 1792 bus_generic_detach(dev); 1793 1794 /* 1795 * The rest is resource deallocation, so we should already be 1796 * stopped here. 1797 */ 1798 1799 if (sc->rl_intrhand[0] != NULL) { 1800 bus_teardown_intr(dev, sc->rl_irq[0], sc->rl_intrhand[0]); 1801 sc->rl_intrhand[0] = NULL; 1802 } 1803 if (ifp != NULL) { 1804 #ifdef DEV_NETMAP 1805 netmap_detach(ifp); 1806 #endif /* DEV_NETMAP */ 1807 if_free(ifp); 1808 } 1809 if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0) 1810 rid = 0; 1811 else 1812 rid = 1; 1813 if (sc->rl_irq[0] != NULL) { 1814 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->rl_irq[0]); 1815 sc->rl_irq[0] = NULL; 1816 } 1817 if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0) 1818 pci_release_msi(dev); 1819 if (sc->rl_res_pba) { 1820 rid = PCIR_BAR(4); 1821 bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->rl_res_pba); 1822 } 1823 if (sc->rl_res) 1824 bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id, 1825 sc->rl_res); 1826 1827 /* Unload and free the RX DMA ring memory and map */ 1828 1829 if (sc->rl_ldata.rl_rx_list_tag) { 1830 if (sc->rl_ldata.rl_rx_list_addr) 1831 bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag, 1832 sc->rl_ldata.rl_rx_list_map); 1833 if (sc->rl_ldata.rl_rx_list) 1834 bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag, 1835 sc->rl_ldata.rl_rx_list, 1836 sc->rl_ldata.rl_rx_list_map); 1837 bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag); 1838 } 1839 1840 /* Unload and free the TX DMA ring memory and map */ 1841 1842 if (sc->rl_ldata.rl_tx_list_tag) { 1843 if (sc->rl_ldata.rl_tx_list_addr) 1844 bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag, 1845 sc->rl_ldata.rl_tx_list_map); 1846 if (sc->rl_ldata.rl_tx_list) 1847 bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag, 1848 sc->rl_ldata.rl_tx_list, 1849 sc->rl_ldata.rl_tx_list_map); 1850 bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag); 1851 } 1852 1853 /* Destroy all the RX and TX buffer maps */ 1854 1855 if (sc->rl_ldata.rl_tx_mtag) { 1856 for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) { 1857 if (sc->rl_ldata.rl_tx_desc[i].tx_dmamap) 1858 bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag, 1859 sc->rl_ldata.rl_tx_desc[i].tx_dmamap); 1860 } 1861 bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag); 1862 } 1863 if (sc->rl_ldata.rl_rx_mtag) { 1864 for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 1865 if (sc->rl_ldata.rl_rx_desc[i].rx_dmamap) 1866 bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag, 1867 sc->rl_ldata.rl_rx_desc[i].rx_dmamap); 1868 } 1869 if (sc->rl_ldata.rl_rx_sparemap) 1870 bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag, 1871 sc->rl_ldata.rl_rx_sparemap); 1872 bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag); 1873 } 1874 if (sc->rl_ldata.rl_jrx_mtag) { 1875 for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 1876 if (sc->rl_ldata.rl_jrx_desc[i].rx_dmamap) 1877 bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag, 1878 sc->rl_ldata.rl_jrx_desc[i].rx_dmamap); 1879 } 1880 if (sc->rl_ldata.rl_jrx_sparemap) 1881 bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag, 1882 sc->rl_ldata.rl_jrx_sparemap); 1883 bus_dma_tag_destroy(sc->rl_ldata.rl_jrx_mtag); 1884 } 1885 /* Unload and free the stats buffer and map */ 1886 1887 if (sc->rl_ldata.rl_stag) { 1888 if (sc->rl_ldata.rl_stats_addr) 1889 bus_dmamap_unload(sc->rl_ldata.rl_stag, 1890 sc->rl_ldata.rl_smap); 1891 if (sc->rl_ldata.rl_stats) 1892 bus_dmamem_free(sc->rl_ldata.rl_stag, 1893 sc->rl_ldata.rl_stats, sc->rl_ldata.rl_smap); 1894 bus_dma_tag_destroy(sc->rl_ldata.rl_stag); 1895 } 1896 1897 if (sc->rl_parent_tag) 1898 bus_dma_tag_destroy(sc->rl_parent_tag); 1899 1900 mtx_destroy(&sc->rl_mtx); 1901 1902 return (0); 1903 } 1904 1905 static __inline void 1906 re_discard_rxbuf(struct rl_softc *sc, int idx) 1907 { 1908 struct rl_desc *desc; 1909 struct rl_rxdesc *rxd; 1910 uint32_t cmdstat; 1911 1912 if (sc->rl_ifp->if_mtu > RL_MTU && 1913 (sc->rl_flags & RL_FLAG_JUMBOV2) != 0) 1914 rxd = &sc->rl_ldata.rl_jrx_desc[idx]; 1915 else 1916 rxd = &sc->rl_ldata.rl_rx_desc[idx]; 1917 desc = &sc->rl_ldata.rl_rx_list[idx]; 1918 desc->rl_vlanctl = 0; 1919 cmdstat = rxd->rx_size; 1920 if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1) 1921 cmdstat |= RL_RDESC_CMD_EOR; 1922 desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN); 1923 } 1924 1925 static int 1926 re_newbuf(struct rl_softc *sc, int idx) 1927 { 1928 struct mbuf *m; 1929 struct rl_rxdesc *rxd; 1930 bus_dma_segment_t segs[1]; 1931 bus_dmamap_t map; 1932 struct rl_desc *desc; 1933 uint32_t cmdstat; 1934 int error, nsegs; 1935 1936 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1937 if (m == NULL) 1938 return (ENOBUFS); 1939 1940 m->m_len = m->m_pkthdr.len = MCLBYTES; 1941 #ifdef RE_FIXUP_RX 1942 /* 1943 * This is part of an evil trick to deal with non-x86 platforms. 1944 * The RealTek chip requires RX buffers to be aligned on 64-bit 1945 * boundaries, but that will hose non-x86 machines. To get around 1946 * this, we leave some empty space at the start of each buffer 1947 * and for non-x86 hosts, we copy the buffer back six bytes 1948 * to achieve word alignment. This is slightly more efficient 1949 * than allocating a new buffer, copying the contents, and 1950 * discarding the old buffer. 1951 */ 1952 m_adj(m, RE_ETHER_ALIGN); 1953 #endif 1954 error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag, 1955 sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT); 1956 if (error != 0) { 1957 m_freem(m); 1958 return (ENOBUFS); 1959 } 1960 KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs)); 1961 1962 rxd = &sc->rl_ldata.rl_rx_desc[idx]; 1963 if (rxd->rx_m != NULL) { 1964 bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap, 1965 BUS_DMASYNC_POSTREAD); 1966 bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap); 1967 } 1968 1969 rxd->rx_m = m; 1970 map = rxd->rx_dmamap; 1971 rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap; 1972 rxd->rx_size = segs[0].ds_len; 1973 sc->rl_ldata.rl_rx_sparemap = map; 1974 bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap, 1975 BUS_DMASYNC_PREREAD); 1976 1977 desc = &sc->rl_ldata.rl_rx_list[idx]; 1978 desc->rl_vlanctl = 0; 1979 desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr)); 1980 desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr)); 1981 cmdstat = segs[0].ds_len; 1982 if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1) 1983 cmdstat |= RL_RDESC_CMD_EOR; 1984 desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN); 1985 1986 return (0); 1987 } 1988 1989 static int 1990 re_jumbo_newbuf(struct rl_softc *sc, int idx) 1991 { 1992 struct mbuf *m; 1993 struct rl_rxdesc *rxd; 1994 bus_dma_segment_t segs[1]; 1995 bus_dmamap_t map; 1996 struct rl_desc *desc; 1997 uint32_t cmdstat; 1998 int error, nsegs; 1999 2000 m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES); 2001 if (m == NULL) 2002 return (ENOBUFS); 2003 m->m_len = m->m_pkthdr.len = MJUM9BYTES; 2004 #ifdef RE_FIXUP_RX 2005 m_adj(m, RE_ETHER_ALIGN); 2006 #endif 2007 error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_jrx_mtag, 2008 sc->rl_ldata.rl_jrx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT); 2009 if (error != 0) { 2010 m_freem(m); 2011 return (ENOBUFS); 2012 } 2013 KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs)); 2014 2015 rxd = &sc->rl_ldata.rl_jrx_desc[idx]; 2016 if (rxd->rx_m != NULL) { 2017 bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap, 2018 BUS_DMASYNC_POSTREAD); 2019 bus_dmamap_unload(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap); 2020 } 2021 2022 rxd->rx_m = m; 2023 map = rxd->rx_dmamap; 2024 rxd->rx_dmamap = sc->rl_ldata.rl_jrx_sparemap; 2025 rxd->rx_size = segs[0].ds_len; 2026 sc->rl_ldata.rl_jrx_sparemap = map; 2027 bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap, 2028 BUS_DMASYNC_PREREAD); 2029 2030 desc = &sc->rl_ldata.rl_rx_list[idx]; 2031 desc->rl_vlanctl = 0; 2032 desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr)); 2033 desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr)); 2034 cmdstat = segs[0].ds_len; 2035 if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1) 2036 cmdstat |= RL_RDESC_CMD_EOR; 2037 desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN); 2038 2039 return (0); 2040 } 2041 2042 #ifdef RE_FIXUP_RX 2043 static __inline void 2044 re_fixup_rx(struct mbuf *m) 2045 { 2046 int i; 2047 uint16_t *src, *dst; 2048 2049 src = mtod(m, uint16_t *); 2050 dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src; 2051 2052 for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++) 2053 *dst++ = *src++; 2054 2055 m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN; 2056 } 2057 #endif 2058 2059 static int 2060 re_tx_list_init(struct rl_softc *sc) 2061 { 2062 struct rl_desc *desc; 2063 int i; 2064 2065 RL_LOCK_ASSERT(sc); 2066 2067 bzero(sc->rl_ldata.rl_tx_list, 2068 sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc)); 2069 for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) 2070 sc->rl_ldata.rl_tx_desc[i].tx_m = NULL; 2071 #ifdef DEV_NETMAP 2072 re_netmap_tx_init(sc); 2073 #endif /* DEV_NETMAP */ 2074 /* Set EOR. */ 2075 desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1]; 2076 desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR); 2077 2078 bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 2079 sc->rl_ldata.rl_tx_list_map, 2080 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 2081 2082 sc->rl_ldata.rl_tx_prodidx = 0; 2083 sc->rl_ldata.rl_tx_considx = 0; 2084 sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt; 2085 2086 return (0); 2087 } 2088 2089 static int 2090 re_rx_list_init(struct rl_softc *sc) 2091 { 2092 int error, i; 2093 2094 bzero(sc->rl_ldata.rl_rx_list, 2095 sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc)); 2096 for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 2097 sc->rl_ldata.rl_rx_desc[i].rx_m = NULL; 2098 if ((error = re_newbuf(sc, i)) != 0) 2099 return (error); 2100 } 2101 #ifdef DEV_NETMAP 2102 re_netmap_rx_init(sc); 2103 #endif /* DEV_NETMAP */ 2104 2105 /* Flush the RX descriptors */ 2106 2107 bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 2108 sc->rl_ldata.rl_rx_list_map, 2109 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 2110 2111 sc->rl_ldata.rl_rx_prodidx = 0; 2112 sc->rl_head = sc->rl_tail = NULL; 2113 sc->rl_int_rx_act = 0; 2114 2115 return (0); 2116 } 2117 2118 static int 2119 re_jrx_list_init(struct rl_softc *sc) 2120 { 2121 int error, i; 2122 2123 bzero(sc->rl_ldata.rl_rx_list, 2124 sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc)); 2125 for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 2126 sc->rl_ldata.rl_jrx_desc[i].rx_m = NULL; 2127 if ((error = re_jumbo_newbuf(sc, i)) != 0) 2128 return (error); 2129 } 2130 2131 bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 2132 sc->rl_ldata.rl_rx_list_map, 2133 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD); 2134 2135 sc->rl_ldata.rl_rx_prodidx = 0; 2136 sc->rl_head = sc->rl_tail = NULL; 2137 sc->rl_int_rx_act = 0; 2138 2139 return (0); 2140 } 2141 2142 /* 2143 * RX handler for C+ and 8169. For the gigE chips, we support 2144 * the reception of jumbo frames that have been fragmented 2145 * across multiple 2K mbuf cluster buffers. 2146 */ 2147 static int 2148 re_rxeof(struct rl_softc *sc, int *rx_npktsp) 2149 { 2150 struct mbuf *m; 2151 struct ifnet *ifp; 2152 int i, rxerr, total_len; 2153 struct rl_desc *cur_rx; 2154 u_int32_t rxstat, rxvlan; 2155 int jumbo, maxpkt = 16, rx_npkts = 0; 2156 2157 RL_LOCK_ASSERT(sc); 2158 2159 ifp = sc->rl_ifp; 2160 #ifdef DEV_NETMAP 2161 if (netmap_rx_irq(ifp, 0, &rx_npkts)) 2162 return 0; 2163 #endif /* DEV_NETMAP */ 2164 if (ifp->if_mtu > RL_MTU && (sc->rl_flags & RL_FLAG_JUMBOV2) != 0) 2165 jumbo = 1; 2166 else 2167 jumbo = 0; 2168 2169 /* Invalidate the descriptor memory */ 2170 2171 bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 2172 sc->rl_ldata.rl_rx_list_map, 2173 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 2174 2175 for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0; 2176 i = RL_RX_DESC_NXT(sc, i)) { 2177 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 2178 break; 2179 cur_rx = &sc->rl_ldata.rl_rx_list[i]; 2180 rxstat = le32toh(cur_rx->rl_cmdstat); 2181 if ((rxstat & RL_RDESC_STAT_OWN) != 0) 2182 break; 2183 total_len = rxstat & sc->rl_rxlenmask; 2184 rxvlan = le32toh(cur_rx->rl_vlanctl); 2185 if (jumbo != 0) 2186 m = sc->rl_ldata.rl_jrx_desc[i].rx_m; 2187 else 2188 m = sc->rl_ldata.rl_rx_desc[i].rx_m; 2189 2190 if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 && 2191 (rxstat & (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) != 2192 (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) { 2193 /* 2194 * RTL8168C or later controllers do not 2195 * support multi-fragment packet. 2196 */ 2197 re_discard_rxbuf(sc, i); 2198 continue; 2199 } else if ((rxstat & RL_RDESC_STAT_EOF) == 0) { 2200 if (re_newbuf(sc, i) != 0) { 2201 /* 2202 * If this is part of a multi-fragment packet, 2203 * discard all the pieces. 2204 */ 2205 if (sc->rl_head != NULL) { 2206 m_freem(sc->rl_head); 2207 sc->rl_head = sc->rl_tail = NULL; 2208 } 2209 re_discard_rxbuf(sc, i); 2210 continue; 2211 } 2212 m->m_len = RE_RX_DESC_BUFLEN; 2213 if (sc->rl_head == NULL) 2214 sc->rl_head = sc->rl_tail = m; 2215 else { 2216 m->m_flags &= ~M_PKTHDR; 2217 sc->rl_tail->m_next = m; 2218 sc->rl_tail = m; 2219 } 2220 continue; 2221 } 2222 2223 /* 2224 * NOTE: for the 8139C+, the frame length field 2225 * is always 12 bits in size, but for the gigE chips, 2226 * it is 13 bits (since the max RX frame length is 16K). 2227 * Unfortunately, all 32 bits in the status word 2228 * were already used, so to make room for the extra 2229 * length bit, RealTek took out the 'frame alignment 2230 * error' bit and shifted the other status bits 2231 * over one slot. The OWN, EOR, FS and LS bits are 2232 * still in the same places. We have already extracted 2233 * the frame length and checked the OWN bit, so rather 2234 * than using an alternate bit mapping, we shift the 2235 * status bits one space to the right so we can evaluate 2236 * them using the 8169 status as though it was in the 2237 * same format as that of the 8139C+. 2238 */ 2239 if (sc->rl_type == RL_8169) 2240 rxstat >>= 1; 2241 2242 /* 2243 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be 2244 * set, but if CRC is clear, it will still be a valid frame. 2245 */ 2246 if ((rxstat & RL_RDESC_STAT_RXERRSUM) != 0) { 2247 rxerr = 1; 2248 if ((sc->rl_flags & RL_FLAG_JUMBOV2) == 0 && 2249 total_len > 8191 && 2250 (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT) 2251 rxerr = 0; 2252 if (rxerr != 0) { 2253 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 2254 /* 2255 * If this is part of a multi-fragment packet, 2256 * discard all the pieces. 2257 */ 2258 if (sc->rl_head != NULL) { 2259 m_freem(sc->rl_head); 2260 sc->rl_head = sc->rl_tail = NULL; 2261 } 2262 re_discard_rxbuf(sc, i); 2263 continue; 2264 } 2265 } 2266 2267 /* 2268 * If allocating a replacement mbuf fails, 2269 * reload the current one. 2270 */ 2271 if (jumbo != 0) 2272 rxerr = re_jumbo_newbuf(sc, i); 2273 else 2274 rxerr = re_newbuf(sc, i); 2275 if (rxerr != 0) { 2276 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1); 2277 if (sc->rl_head != NULL) { 2278 m_freem(sc->rl_head); 2279 sc->rl_head = sc->rl_tail = NULL; 2280 } 2281 re_discard_rxbuf(sc, i); 2282 continue; 2283 } 2284 2285 if (sc->rl_head != NULL) { 2286 if (jumbo != 0) 2287 m->m_len = total_len; 2288 else { 2289 m->m_len = total_len % RE_RX_DESC_BUFLEN; 2290 if (m->m_len == 0) 2291 m->m_len = RE_RX_DESC_BUFLEN; 2292 } 2293 /* 2294 * Special case: if there's 4 bytes or less 2295 * in this buffer, the mbuf can be discarded: 2296 * the last 4 bytes is the CRC, which we don't 2297 * care about anyway. 2298 */ 2299 if (m->m_len <= ETHER_CRC_LEN) { 2300 sc->rl_tail->m_len -= 2301 (ETHER_CRC_LEN - m->m_len); 2302 m_freem(m); 2303 } else { 2304 m->m_len -= ETHER_CRC_LEN; 2305 m->m_flags &= ~M_PKTHDR; 2306 sc->rl_tail->m_next = m; 2307 } 2308 m = sc->rl_head; 2309 sc->rl_head = sc->rl_tail = NULL; 2310 m->m_pkthdr.len = total_len - ETHER_CRC_LEN; 2311 } else 2312 m->m_pkthdr.len = m->m_len = 2313 (total_len - ETHER_CRC_LEN); 2314 2315 #ifdef RE_FIXUP_RX 2316 re_fixup_rx(m); 2317 #endif 2318 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 2319 m->m_pkthdr.rcvif = ifp; 2320 2321 /* Do RX checksumming if enabled */ 2322 2323 if (ifp->if_capenable & IFCAP_RXCSUM) { 2324 if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) { 2325 /* Check IP header checksum */ 2326 if (rxstat & RL_RDESC_STAT_PROTOID) 2327 m->m_pkthdr.csum_flags |= 2328 CSUM_IP_CHECKED; 2329 if (!(rxstat & RL_RDESC_STAT_IPSUMBAD)) 2330 m->m_pkthdr.csum_flags |= 2331 CSUM_IP_VALID; 2332 2333 /* Check TCP/UDP checksum */ 2334 if ((RL_TCPPKT(rxstat) && 2335 !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) || 2336 (RL_UDPPKT(rxstat) && 2337 !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) { 2338 m->m_pkthdr.csum_flags |= 2339 CSUM_DATA_VALID|CSUM_PSEUDO_HDR; 2340 m->m_pkthdr.csum_data = 0xffff; 2341 } 2342 } else { 2343 /* 2344 * RTL8168C/RTL816CP/RTL8111C/RTL8111CP 2345 */ 2346 if ((rxstat & RL_RDESC_STAT_PROTOID) && 2347 (rxvlan & RL_RDESC_IPV4)) 2348 m->m_pkthdr.csum_flags |= 2349 CSUM_IP_CHECKED; 2350 if (!(rxstat & RL_RDESC_STAT_IPSUMBAD) && 2351 (rxvlan & RL_RDESC_IPV4)) 2352 m->m_pkthdr.csum_flags |= 2353 CSUM_IP_VALID; 2354 if (((rxstat & RL_RDESC_STAT_TCP) && 2355 !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) || 2356 ((rxstat & RL_RDESC_STAT_UDP) && 2357 !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) { 2358 m->m_pkthdr.csum_flags |= 2359 CSUM_DATA_VALID|CSUM_PSEUDO_HDR; 2360 m->m_pkthdr.csum_data = 0xffff; 2361 } 2362 } 2363 } 2364 maxpkt--; 2365 if (rxvlan & RL_RDESC_VLANCTL_TAG) { 2366 m->m_pkthdr.ether_vtag = 2367 bswap16((rxvlan & RL_RDESC_VLANCTL_DATA)); 2368 m->m_flags |= M_VLANTAG; 2369 } 2370 RL_UNLOCK(sc); 2371 (*ifp->if_input)(ifp, m); 2372 RL_LOCK(sc); 2373 rx_npkts++; 2374 } 2375 2376 /* Flush the RX DMA ring */ 2377 2378 bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 2379 sc->rl_ldata.rl_rx_list_map, 2380 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 2381 2382 sc->rl_ldata.rl_rx_prodidx = i; 2383 2384 if (rx_npktsp != NULL) 2385 *rx_npktsp = rx_npkts; 2386 if (maxpkt) 2387 return (EAGAIN); 2388 2389 return (0); 2390 } 2391 2392 static void 2393 re_txeof(struct rl_softc *sc) 2394 { 2395 struct ifnet *ifp; 2396 struct rl_txdesc *txd; 2397 u_int32_t txstat; 2398 int cons; 2399 2400 cons = sc->rl_ldata.rl_tx_considx; 2401 if (cons == sc->rl_ldata.rl_tx_prodidx) 2402 return; 2403 2404 ifp = sc->rl_ifp; 2405 #ifdef DEV_NETMAP 2406 if (netmap_tx_irq(ifp, 0)) 2407 return; 2408 #endif /* DEV_NETMAP */ 2409 /* Invalidate the TX descriptor list */ 2410 bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 2411 sc->rl_ldata.rl_tx_list_map, 2412 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 2413 2414 for (; cons != sc->rl_ldata.rl_tx_prodidx; 2415 cons = RL_TX_DESC_NXT(sc, cons)) { 2416 txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat); 2417 if (txstat & RL_TDESC_STAT_OWN) 2418 break; 2419 /* 2420 * We only stash mbufs in the last descriptor 2421 * in a fragment chain, which also happens to 2422 * be the only place where the TX status bits 2423 * are valid. 2424 */ 2425 if (txstat & RL_TDESC_CMD_EOF) { 2426 txd = &sc->rl_ldata.rl_tx_desc[cons]; 2427 bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, 2428 txd->tx_dmamap, BUS_DMASYNC_POSTWRITE); 2429 bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, 2430 txd->tx_dmamap); 2431 KASSERT(txd->tx_m != NULL, 2432 ("%s: freeing NULL mbufs!", __func__)); 2433 m_freem(txd->tx_m); 2434 txd->tx_m = NULL; 2435 if (txstat & (RL_TDESC_STAT_EXCESSCOL| 2436 RL_TDESC_STAT_COLCNT)) 2437 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1); 2438 if (txstat & RL_TDESC_STAT_TXERRSUM) 2439 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 2440 else 2441 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 2442 } 2443 sc->rl_ldata.rl_tx_free++; 2444 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2445 } 2446 sc->rl_ldata.rl_tx_considx = cons; 2447 2448 /* No changes made to the TX ring, so no flush needed */ 2449 2450 if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) { 2451 #ifdef RE_TX_MODERATION 2452 /* 2453 * If not all descriptors have been reaped yet, reload 2454 * the timer so that we will eventually get another 2455 * interrupt that will cause us to re-enter this routine. 2456 * This is done in case the transmitter has gone idle. 2457 */ 2458 CSR_WRITE_4(sc, RL_TIMERCNT, 1); 2459 #endif 2460 } else 2461 sc->rl_watchdog_timer = 0; 2462 } 2463 2464 static void 2465 re_tick(void *xsc) 2466 { 2467 struct rl_softc *sc; 2468 struct mii_data *mii; 2469 2470 sc = xsc; 2471 2472 RL_LOCK_ASSERT(sc); 2473 2474 mii = device_get_softc(sc->rl_miibus); 2475 mii_tick(mii); 2476 if ((sc->rl_flags & RL_FLAG_LINK) == 0) 2477 re_miibus_statchg(sc->rl_dev); 2478 /* 2479 * Reclaim transmitted frames here. Technically it is not 2480 * necessary to do here but it ensures periodic reclamation 2481 * regardless of Tx completion interrupt which seems to be 2482 * lost on PCIe based controllers under certain situations. 2483 */ 2484 re_txeof(sc); 2485 re_watchdog(sc); 2486 callout_reset(&sc->rl_stat_callout, hz, re_tick, sc); 2487 } 2488 2489 #ifdef DEVICE_POLLING 2490 static int 2491 re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 2492 { 2493 struct rl_softc *sc = ifp->if_softc; 2494 int rx_npkts = 0; 2495 2496 RL_LOCK(sc); 2497 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2498 rx_npkts = re_poll_locked(ifp, cmd, count); 2499 RL_UNLOCK(sc); 2500 return (rx_npkts); 2501 } 2502 2503 static int 2504 re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count) 2505 { 2506 struct rl_softc *sc = ifp->if_softc; 2507 int rx_npkts; 2508 2509 RL_LOCK_ASSERT(sc); 2510 2511 sc->rxcycles = count; 2512 re_rxeof(sc, &rx_npkts); 2513 re_txeof(sc); 2514 2515 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2516 re_start_locked(ifp); 2517 2518 if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */ 2519 u_int16_t status; 2520 2521 status = CSR_READ_2(sc, RL_ISR); 2522 if (status == 0xffff) 2523 return (rx_npkts); 2524 if (status) 2525 CSR_WRITE_2(sc, RL_ISR, status); 2526 if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) && 2527 (sc->rl_flags & RL_FLAG_PCIE)) 2528 CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 2529 2530 /* 2531 * XXX check behaviour on receiver stalls. 2532 */ 2533 2534 if (status & RL_ISR_SYSTEM_ERR) { 2535 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2536 re_init_locked(sc); 2537 } 2538 } 2539 return (rx_npkts); 2540 } 2541 #endif /* DEVICE_POLLING */ 2542 2543 static int 2544 re_intr(void *arg) 2545 { 2546 struct rl_softc *sc; 2547 uint16_t status; 2548 2549 sc = arg; 2550 2551 status = CSR_READ_2(sc, RL_ISR); 2552 if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0) 2553 return (FILTER_STRAY); 2554 CSR_WRITE_2(sc, RL_IMR, 0); 2555 2556 taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask); 2557 2558 return (FILTER_HANDLED); 2559 } 2560 2561 static void 2562 re_int_task(void *arg, int npending) 2563 { 2564 struct rl_softc *sc; 2565 struct ifnet *ifp; 2566 u_int16_t status; 2567 int rval = 0; 2568 2569 sc = arg; 2570 ifp = sc->rl_ifp; 2571 2572 RL_LOCK(sc); 2573 2574 status = CSR_READ_2(sc, RL_ISR); 2575 CSR_WRITE_2(sc, RL_ISR, status); 2576 2577 if (sc->suspended || 2578 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 2579 RL_UNLOCK(sc); 2580 return; 2581 } 2582 2583 #ifdef DEVICE_POLLING 2584 if (ifp->if_capenable & IFCAP_POLLING) { 2585 RL_UNLOCK(sc); 2586 return; 2587 } 2588 #endif 2589 2590 if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW)) 2591 rval = re_rxeof(sc, NULL); 2592 2593 /* 2594 * Some chips will ignore a second TX request issued 2595 * while an existing transmission is in progress. If 2596 * the transmitter goes idle but there are still 2597 * packets waiting to be sent, we need to restart the 2598 * channel here to flush them out. This only seems to 2599 * be required with the PCIe devices. 2600 */ 2601 if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) && 2602 (sc->rl_flags & RL_FLAG_PCIE)) 2603 CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 2604 if (status & ( 2605 #ifdef RE_TX_MODERATION 2606 RL_ISR_TIMEOUT_EXPIRED| 2607 #else 2608 RL_ISR_TX_OK| 2609 #endif 2610 RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL)) 2611 re_txeof(sc); 2612 2613 if (status & RL_ISR_SYSTEM_ERR) { 2614 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2615 re_init_locked(sc); 2616 } 2617 2618 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2619 re_start_locked(ifp); 2620 2621 RL_UNLOCK(sc); 2622 2623 if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) { 2624 taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask); 2625 return; 2626 } 2627 2628 CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS); 2629 } 2630 2631 static void 2632 re_intr_msi(void *xsc) 2633 { 2634 struct rl_softc *sc; 2635 struct ifnet *ifp; 2636 uint16_t intrs, status; 2637 2638 sc = xsc; 2639 RL_LOCK(sc); 2640 2641 ifp = sc->rl_ifp; 2642 #ifdef DEVICE_POLLING 2643 if (ifp->if_capenable & IFCAP_POLLING) { 2644 RL_UNLOCK(sc); 2645 return; 2646 } 2647 #endif 2648 /* Disable interrupts. */ 2649 CSR_WRITE_2(sc, RL_IMR, 0); 2650 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 2651 RL_UNLOCK(sc); 2652 return; 2653 } 2654 2655 intrs = RL_INTRS_CPLUS; 2656 status = CSR_READ_2(sc, RL_ISR); 2657 CSR_WRITE_2(sc, RL_ISR, status); 2658 if (sc->rl_int_rx_act > 0) { 2659 intrs &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | RL_ISR_FIFO_OFLOW | 2660 RL_ISR_RX_OVERRUN); 2661 status &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | RL_ISR_FIFO_OFLOW | 2662 RL_ISR_RX_OVERRUN); 2663 } 2664 2665 if (status & (RL_ISR_TIMEOUT_EXPIRED | RL_ISR_RX_OK | RL_ISR_RX_ERR | 2666 RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN)) { 2667 re_rxeof(sc, NULL); 2668 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) { 2669 if (sc->rl_int_rx_mod != 0 && 2670 (status & (RL_ISR_RX_OK | RL_ISR_RX_ERR | 2671 RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN)) != 0) { 2672 /* Rearm one-shot timer. */ 2673 CSR_WRITE_4(sc, RL_TIMERCNT, 1); 2674 intrs &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | 2675 RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN); 2676 sc->rl_int_rx_act = 1; 2677 } else { 2678 intrs |= RL_ISR_RX_OK | RL_ISR_RX_ERR | 2679 RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN; 2680 sc->rl_int_rx_act = 0; 2681 } 2682 } 2683 } 2684 2685 /* 2686 * Some chips will ignore a second TX request issued 2687 * while an existing transmission is in progress. If 2688 * the transmitter goes idle but there are still 2689 * packets waiting to be sent, we need to restart the 2690 * channel here to flush them out. This only seems to 2691 * be required with the PCIe devices. 2692 */ 2693 if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) && 2694 (sc->rl_flags & RL_FLAG_PCIE)) 2695 CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 2696 if (status & (RL_ISR_TX_OK | RL_ISR_TX_ERR | RL_ISR_TX_DESC_UNAVAIL)) 2697 re_txeof(sc); 2698 2699 if (status & RL_ISR_SYSTEM_ERR) { 2700 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2701 re_init_locked(sc); 2702 } 2703 2704 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) { 2705 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2706 re_start_locked(ifp); 2707 CSR_WRITE_2(sc, RL_IMR, intrs); 2708 } 2709 RL_UNLOCK(sc); 2710 } 2711 2712 static int 2713 re_encap(struct rl_softc *sc, struct mbuf **m_head) 2714 { 2715 struct rl_txdesc *txd, *txd_last; 2716 bus_dma_segment_t segs[RL_NTXSEGS]; 2717 bus_dmamap_t map; 2718 struct mbuf *m_new; 2719 struct rl_desc *desc; 2720 int nsegs, prod; 2721 int i, error, ei, si; 2722 int padlen; 2723 uint32_t cmdstat, csum_flags, vlanctl; 2724 2725 RL_LOCK_ASSERT(sc); 2726 M_ASSERTPKTHDR((*m_head)); 2727 2728 /* 2729 * With some of the RealTek chips, using the checksum offload 2730 * support in conjunction with the autopadding feature results 2731 * in the transmission of corrupt frames. For example, if we 2732 * need to send a really small IP fragment that's less than 60 2733 * bytes in size, and IP header checksumming is enabled, the 2734 * resulting ethernet frame that appears on the wire will 2735 * have garbled payload. To work around this, if TX IP checksum 2736 * offload is enabled, we always manually pad short frames out 2737 * to the minimum ethernet frame size. 2738 */ 2739 if ((sc->rl_flags & RL_FLAG_AUTOPAD) == 0 && 2740 (*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN && 2741 ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) { 2742 padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len; 2743 if (M_WRITABLE(*m_head) == 0) { 2744 /* Get a writable copy. */ 2745 m_new = m_dup(*m_head, M_NOWAIT); 2746 m_freem(*m_head); 2747 if (m_new == NULL) { 2748 *m_head = NULL; 2749 return (ENOBUFS); 2750 } 2751 *m_head = m_new; 2752 } 2753 if ((*m_head)->m_next != NULL || 2754 M_TRAILINGSPACE(*m_head) < padlen) { 2755 m_new = m_defrag(*m_head, M_NOWAIT); 2756 if (m_new == NULL) { 2757 m_freem(*m_head); 2758 *m_head = NULL; 2759 return (ENOBUFS); 2760 } 2761 } else 2762 m_new = *m_head; 2763 2764 /* 2765 * Manually pad short frames, and zero the pad space 2766 * to avoid leaking data. 2767 */ 2768 bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen); 2769 m_new->m_pkthdr.len += padlen; 2770 m_new->m_len = m_new->m_pkthdr.len; 2771 *m_head = m_new; 2772 } 2773 2774 prod = sc->rl_ldata.rl_tx_prodidx; 2775 txd = &sc->rl_ldata.rl_tx_desc[prod]; 2776 error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap, 2777 *m_head, segs, &nsegs, BUS_DMA_NOWAIT); 2778 if (error == EFBIG) { 2779 m_new = m_collapse(*m_head, M_NOWAIT, RL_NTXSEGS); 2780 if (m_new == NULL) { 2781 m_freem(*m_head); 2782 *m_head = NULL; 2783 return (ENOBUFS); 2784 } 2785 *m_head = m_new; 2786 error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, 2787 txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT); 2788 if (error != 0) { 2789 m_freem(*m_head); 2790 *m_head = NULL; 2791 return (error); 2792 } 2793 } else if (error != 0) 2794 return (error); 2795 if (nsegs == 0) { 2796 m_freem(*m_head); 2797 *m_head = NULL; 2798 return (EIO); 2799 } 2800 2801 /* Check for number of available descriptors. */ 2802 if (sc->rl_ldata.rl_tx_free - nsegs <= 1) { 2803 bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap); 2804 return (ENOBUFS); 2805 } 2806 2807 bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap, 2808 BUS_DMASYNC_PREWRITE); 2809 2810 /* 2811 * Set up checksum offload. Note: checksum offload bits must 2812 * appear in all descriptors of a multi-descriptor transmit 2813 * attempt. This is according to testing done with an 8169 2814 * chip. This is a requirement. 2815 */ 2816 vlanctl = 0; 2817 csum_flags = 0; 2818 if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0) { 2819 if ((sc->rl_flags & RL_FLAG_DESCV2) != 0) { 2820 csum_flags |= RL_TDESC_CMD_LGSEND; 2821 vlanctl |= ((uint32_t)(*m_head)->m_pkthdr.tso_segsz << 2822 RL_TDESC_CMD_MSSVALV2_SHIFT); 2823 } else { 2824 csum_flags |= RL_TDESC_CMD_LGSEND | 2825 ((uint32_t)(*m_head)->m_pkthdr.tso_segsz << 2826 RL_TDESC_CMD_MSSVAL_SHIFT); 2827 } 2828 } else { 2829 /* 2830 * Unconditionally enable IP checksum if TCP or UDP 2831 * checksum is required. Otherwise, TCP/UDP checksum 2832 * doesn't make effects. 2833 */ 2834 if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) { 2835 if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) { 2836 csum_flags |= RL_TDESC_CMD_IPCSUM; 2837 if (((*m_head)->m_pkthdr.csum_flags & 2838 CSUM_TCP) != 0) 2839 csum_flags |= RL_TDESC_CMD_TCPCSUM; 2840 if (((*m_head)->m_pkthdr.csum_flags & 2841 CSUM_UDP) != 0) 2842 csum_flags |= RL_TDESC_CMD_UDPCSUM; 2843 } else { 2844 vlanctl |= RL_TDESC_CMD_IPCSUMV2; 2845 if (((*m_head)->m_pkthdr.csum_flags & 2846 CSUM_TCP) != 0) 2847 vlanctl |= RL_TDESC_CMD_TCPCSUMV2; 2848 if (((*m_head)->m_pkthdr.csum_flags & 2849 CSUM_UDP) != 0) 2850 vlanctl |= RL_TDESC_CMD_UDPCSUMV2; 2851 } 2852 } 2853 } 2854 2855 /* 2856 * Set up hardware VLAN tagging. Note: vlan tag info must 2857 * appear in all descriptors of a multi-descriptor 2858 * transmission attempt. 2859 */ 2860 if ((*m_head)->m_flags & M_VLANTAG) 2861 vlanctl |= bswap16((*m_head)->m_pkthdr.ether_vtag) | 2862 RL_TDESC_VLANCTL_TAG; 2863 2864 si = prod; 2865 for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) { 2866 desc = &sc->rl_ldata.rl_tx_list[prod]; 2867 desc->rl_vlanctl = htole32(vlanctl); 2868 desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr)); 2869 desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr)); 2870 cmdstat = segs[i].ds_len; 2871 if (i != 0) 2872 cmdstat |= RL_TDESC_CMD_OWN; 2873 if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1) 2874 cmdstat |= RL_TDESC_CMD_EOR; 2875 desc->rl_cmdstat = htole32(cmdstat | csum_flags); 2876 sc->rl_ldata.rl_tx_free--; 2877 } 2878 /* Update producer index. */ 2879 sc->rl_ldata.rl_tx_prodidx = prod; 2880 2881 /* Set EOF on the last descriptor. */ 2882 ei = RL_TX_DESC_PRV(sc, prod); 2883 desc = &sc->rl_ldata.rl_tx_list[ei]; 2884 desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF); 2885 2886 desc = &sc->rl_ldata.rl_tx_list[si]; 2887 /* Set SOF and transfer ownership of packet to the chip. */ 2888 desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF); 2889 2890 /* 2891 * Insure that the map for this transmission 2892 * is placed at the array index of the last descriptor 2893 * in this chain. (Swap last and first dmamaps.) 2894 */ 2895 txd_last = &sc->rl_ldata.rl_tx_desc[ei]; 2896 map = txd->tx_dmamap; 2897 txd->tx_dmamap = txd_last->tx_dmamap; 2898 txd_last->tx_dmamap = map; 2899 txd_last->tx_m = *m_head; 2900 2901 return (0); 2902 } 2903 2904 static void 2905 re_start(struct ifnet *ifp) 2906 { 2907 struct rl_softc *sc; 2908 2909 sc = ifp->if_softc; 2910 RL_LOCK(sc); 2911 re_start_locked(ifp); 2912 RL_UNLOCK(sc); 2913 } 2914 2915 /* 2916 * Main transmit routine for C+ and gigE NICs. 2917 */ 2918 static void 2919 re_start_locked(struct ifnet *ifp) 2920 { 2921 struct rl_softc *sc; 2922 struct mbuf *m_head; 2923 int queued; 2924 2925 sc = ifp->if_softc; 2926 2927 #ifdef DEV_NETMAP 2928 /* XXX is this necessary ? */ 2929 if (ifp->if_capenable & IFCAP_NETMAP) { 2930 struct netmap_kring *kring = &NA(ifp)->tx_rings[0]; 2931 if (sc->rl_ldata.rl_tx_prodidx != kring->nr_hwcur) { 2932 /* kick the tx unit */ 2933 CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 2934 #ifdef RE_TX_MODERATION 2935 CSR_WRITE_4(sc, RL_TIMERCNT, 1); 2936 #endif 2937 sc->rl_watchdog_timer = 5; 2938 } 2939 return; 2940 } 2941 #endif /* DEV_NETMAP */ 2942 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 2943 IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0) 2944 return; 2945 2946 for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) && 2947 sc->rl_ldata.rl_tx_free > 1;) { 2948 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head); 2949 if (m_head == NULL) 2950 break; 2951 2952 if (re_encap(sc, &m_head) != 0) { 2953 if (m_head == NULL) 2954 break; 2955 IFQ_DRV_PREPEND(&ifp->if_snd, m_head); 2956 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2957 break; 2958 } 2959 2960 /* 2961 * If there's a BPF listener, bounce a copy of this frame 2962 * to him. 2963 */ 2964 ETHER_BPF_MTAP(ifp, m_head); 2965 2966 queued++; 2967 } 2968 2969 if (queued == 0) { 2970 #ifdef RE_TX_MODERATION 2971 if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) 2972 CSR_WRITE_4(sc, RL_TIMERCNT, 1); 2973 #endif 2974 return; 2975 } 2976 2977 /* Flush the TX descriptors */ 2978 2979 bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 2980 sc->rl_ldata.rl_tx_list_map, 2981 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 2982 2983 CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 2984 2985 #ifdef RE_TX_MODERATION 2986 /* 2987 * Use the countdown timer for interrupt moderation. 2988 * 'TX done' interrupts are disabled. Instead, we reset the 2989 * countdown timer, which will begin counting until it hits 2990 * the value in the TIMERINT register, and then trigger an 2991 * interrupt. Each time we write to the TIMERCNT register, 2992 * the timer count is reset to 0. 2993 */ 2994 CSR_WRITE_4(sc, RL_TIMERCNT, 1); 2995 #endif 2996 2997 /* 2998 * Set a timeout in case the chip goes out to lunch. 2999 */ 3000 sc->rl_watchdog_timer = 5; 3001 } 3002 3003 static void 3004 re_set_jumbo(struct rl_softc *sc, int jumbo) 3005 { 3006 3007 if (sc->rl_hwrev->rl_rev == RL_HWREV_8168E_VL) { 3008 pci_set_max_read_req(sc->rl_dev, 4096); 3009 return; 3010 } 3011 3012 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG); 3013 if (jumbo != 0) { 3014 CSR_WRITE_1(sc, sc->rl_cfg3, CSR_READ_1(sc, sc->rl_cfg3) | 3015 RL_CFG3_JUMBO_EN0); 3016 switch (sc->rl_hwrev->rl_rev) { 3017 case RL_HWREV_8168DP: 3018 break; 3019 case RL_HWREV_8168E: 3020 CSR_WRITE_1(sc, sc->rl_cfg4, 3021 CSR_READ_1(sc, sc->rl_cfg4) | 0x01); 3022 break; 3023 default: 3024 CSR_WRITE_1(sc, sc->rl_cfg4, 3025 CSR_READ_1(sc, sc->rl_cfg4) | RL_CFG4_JUMBO_EN1); 3026 } 3027 } else { 3028 CSR_WRITE_1(sc, sc->rl_cfg3, CSR_READ_1(sc, sc->rl_cfg3) & 3029 ~RL_CFG3_JUMBO_EN0); 3030 switch (sc->rl_hwrev->rl_rev) { 3031 case RL_HWREV_8168DP: 3032 break; 3033 case RL_HWREV_8168E: 3034 CSR_WRITE_1(sc, sc->rl_cfg4, 3035 CSR_READ_1(sc, sc->rl_cfg4) & ~0x01); 3036 break; 3037 default: 3038 CSR_WRITE_1(sc, sc->rl_cfg4, 3039 CSR_READ_1(sc, sc->rl_cfg4) & ~RL_CFG4_JUMBO_EN1); 3040 } 3041 } 3042 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 3043 3044 switch (sc->rl_hwrev->rl_rev) { 3045 case RL_HWREV_8168DP: 3046 pci_set_max_read_req(sc->rl_dev, 4096); 3047 break; 3048 default: 3049 if (jumbo != 0) 3050 pci_set_max_read_req(sc->rl_dev, 512); 3051 else 3052 pci_set_max_read_req(sc->rl_dev, 4096); 3053 } 3054 } 3055 3056 static void 3057 re_init(void *xsc) 3058 { 3059 struct rl_softc *sc = xsc; 3060 3061 RL_LOCK(sc); 3062 re_init_locked(sc); 3063 RL_UNLOCK(sc); 3064 } 3065 3066 static void 3067 re_init_locked(struct rl_softc *sc) 3068 { 3069 struct ifnet *ifp = sc->rl_ifp; 3070 struct mii_data *mii; 3071 uint32_t reg; 3072 uint16_t cfg; 3073 union { 3074 uint32_t align_dummy; 3075 u_char eaddr[ETHER_ADDR_LEN]; 3076 } eaddr; 3077 3078 RL_LOCK_ASSERT(sc); 3079 3080 mii = device_get_softc(sc->rl_miibus); 3081 3082 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 3083 return; 3084 3085 /* 3086 * Cancel pending I/O and free all RX/TX buffers. 3087 */ 3088 re_stop(sc); 3089 3090 /* Put controller into known state. */ 3091 re_reset(sc); 3092 3093 /* 3094 * For C+ mode, initialize the RX descriptors and mbufs. 3095 */ 3096 if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) { 3097 if (ifp->if_mtu > RL_MTU) { 3098 if (re_jrx_list_init(sc) != 0) { 3099 device_printf(sc->rl_dev, 3100 "no memory for jumbo RX buffers\n"); 3101 re_stop(sc); 3102 return; 3103 } 3104 /* Disable checksum offloading for jumbo frames. */ 3105 ifp->if_capenable &= ~(IFCAP_HWCSUM | IFCAP_TSO4); 3106 ifp->if_hwassist &= ~(RE_CSUM_FEATURES | CSUM_TSO); 3107 } else { 3108 if (re_rx_list_init(sc) != 0) { 3109 device_printf(sc->rl_dev, 3110 "no memory for RX buffers\n"); 3111 re_stop(sc); 3112 return; 3113 } 3114 } 3115 re_set_jumbo(sc, ifp->if_mtu > RL_MTU); 3116 } else { 3117 if (re_rx_list_init(sc) != 0) { 3118 device_printf(sc->rl_dev, "no memory for RX buffers\n"); 3119 re_stop(sc); 3120 return; 3121 } 3122 if ((sc->rl_flags & RL_FLAG_PCIE) != 0 && 3123 pci_get_device(sc->rl_dev) != RT_DEVICEID_8101E) { 3124 if (ifp->if_mtu > RL_MTU) 3125 pci_set_max_read_req(sc->rl_dev, 512); 3126 else 3127 pci_set_max_read_req(sc->rl_dev, 4096); 3128 } 3129 } 3130 re_tx_list_init(sc); 3131 3132 /* 3133 * Enable C+ RX and TX mode, as well as VLAN stripping and 3134 * RX checksum offload. We must configure the C+ register 3135 * before all others. 3136 */ 3137 cfg = RL_CPLUSCMD_PCI_MRW; 3138 if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) 3139 cfg |= RL_CPLUSCMD_RXCSUM_ENB; 3140 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) 3141 cfg |= RL_CPLUSCMD_VLANSTRIP; 3142 if ((sc->rl_flags & RL_FLAG_MACSTAT) != 0) { 3143 cfg |= RL_CPLUSCMD_MACSTAT_DIS; 3144 /* XXX magic. */ 3145 cfg |= 0x0001; 3146 } else 3147 cfg |= RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB; 3148 CSR_WRITE_2(sc, RL_CPLUS_CMD, cfg); 3149 if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SC || 3150 sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE) { 3151 reg = 0x000fff00; 3152 if ((CSR_READ_1(sc, sc->rl_cfg2) & RL_CFG2_PCI66MHZ) != 0) 3153 reg |= 0x000000ff; 3154 if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE) 3155 reg |= 0x00f00000; 3156 CSR_WRITE_4(sc, 0x7c, reg); 3157 /* Disable interrupt mitigation. */ 3158 CSR_WRITE_2(sc, 0xe2, 0); 3159 } 3160 /* 3161 * Disable TSO if interface MTU size is greater than MSS 3162 * allowed in controller. 3163 */ 3164 if (ifp->if_mtu > RL_TSO_MTU && (ifp->if_capenable & IFCAP_TSO4) != 0) { 3165 ifp->if_capenable &= ~IFCAP_TSO4; 3166 ifp->if_hwassist &= ~CSUM_TSO; 3167 } 3168 3169 /* 3170 * Init our MAC address. Even though the chipset 3171 * documentation doesn't mention it, we need to enter "Config 3172 * register write enable" mode to modify the ID registers. 3173 */ 3174 /* Copy MAC address on stack to align. */ 3175 bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN); 3176 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG); 3177 CSR_WRITE_4(sc, RL_IDR0, 3178 htole32(*(u_int32_t *)(&eaddr.eaddr[0]))); 3179 CSR_WRITE_4(sc, RL_IDR4, 3180 htole32(*(u_int32_t *)(&eaddr.eaddr[4]))); 3181 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 3182 3183 /* 3184 * Load the addresses of the RX and TX lists into the chip. 3185 */ 3186 3187 CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI, 3188 RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr)); 3189 CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO, 3190 RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr)); 3191 3192 CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI, 3193 RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr)); 3194 CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO, 3195 RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr)); 3196 3197 if ((sc->rl_flags & RL_FLAG_RXDV_GATED) != 0) 3198 CSR_WRITE_4(sc, RL_MISC, CSR_READ_4(sc, RL_MISC) & 3199 ~0x00080000); 3200 3201 /* 3202 * Set the initial TX configuration. 3203 */ 3204 if (sc->rl_testmode) { 3205 if (sc->rl_type == RL_8169) 3206 CSR_WRITE_4(sc, RL_TXCFG, 3207 RL_TXCFG_CONFIG|RL_LOOPTEST_ON); 3208 else 3209 CSR_WRITE_4(sc, RL_TXCFG, 3210 RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS); 3211 } else 3212 CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG); 3213 3214 CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16); 3215 3216 /* 3217 * Set the initial RX configuration. 3218 */ 3219 re_set_rxmode(sc); 3220 3221 /* Configure interrupt moderation. */ 3222 if (sc->rl_type == RL_8169) { 3223 /* Magic from vendor. */ 3224 CSR_WRITE_2(sc, RL_INTRMOD, 0x5100); 3225 } 3226 3227 /* 3228 * Enable transmit and receive. 3229 */ 3230 CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB | RL_CMD_RX_ENB); 3231 3232 #ifdef DEVICE_POLLING 3233 /* 3234 * Disable interrupts if we are polling. 3235 */ 3236 if (ifp->if_capenable & IFCAP_POLLING) 3237 CSR_WRITE_2(sc, RL_IMR, 0); 3238 else /* otherwise ... */ 3239 #endif 3240 3241 /* 3242 * Enable interrupts. 3243 */ 3244 if (sc->rl_testmode) 3245 CSR_WRITE_2(sc, RL_IMR, 0); 3246 else 3247 CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS); 3248 CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS); 3249 3250 /* Set initial TX threshold */ 3251 sc->rl_txthresh = RL_TX_THRESH_INIT; 3252 3253 /* Start RX/TX process. */ 3254 CSR_WRITE_4(sc, RL_MISSEDPKT, 0); 3255 3256 /* 3257 * Initialize the timer interrupt register so that 3258 * a timer interrupt will be generated once the timer 3259 * reaches a certain number of ticks. The timer is 3260 * reloaded on each transmit. 3261 */ 3262 #ifdef RE_TX_MODERATION 3263 /* 3264 * Use timer interrupt register to moderate TX interrupt 3265 * moderation, which dramatically improves TX frame rate. 3266 */ 3267 if (sc->rl_type == RL_8169) 3268 CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800); 3269 else 3270 CSR_WRITE_4(sc, RL_TIMERINT, 0x400); 3271 #else 3272 /* 3273 * Use timer interrupt register to moderate RX interrupt 3274 * moderation. 3275 */ 3276 if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0 && 3277 intr_filter == 0) { 3278 if (sc->rl_type == RL_8169) 3279 CSR_WRITE_4(sc, RL_TIMERINT_8169, 3280 RL_USECS(sc->rl_int_rx_mod)); 3281 } else { 3282 if (sc->rl_type == RL_8169) 3283 CSR_WRITE_4(sc, RL_TIMERINT_8169, RL_USECS(0)); 3284 } 3285 #endif 3286 3287 /* 3288 * For 8169 gigE NICs, set the max allowed RX packet 3289 * size so we can receive jumbo frames. 3290 */ 3291 if (sc->rl_type == RL_8169) { 3292 if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) { 3293 /* 3294 * For controllers that use new jumbo frame scheme, 3295 * set maximum size of jumbo frame depending on 3296 * controller revisions. 3297 */ 3298 if (ifp->if_mtu > RL_MTU) 3299 CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 3300 sc->rl_hwrev->rl_max_mtu + 3301 ETHER_VLAN_ENCAP_LEN + ETHER_HDR_LEN + 3302 ETHER_CRC_LEN); 3303 else 3304 CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 3305 RE_RX_DESC_BUFLEN); 3306 } else if ((sc->rl_flags & RL_FLAG_PCIE) != 0 && 3307 sc->rl_hwrev->rl_max_mtu == RL_MTU) { 3308 /* RTL810x has no jumbo frame support. */ 3309 CSR_WRITE_2(sc, RL_MAXRXPKTLEN, RE_RX_DESC_BUFLEN); 3310 } else 3311 CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383); 3312 } 3313 3314 if (sc->rl_testmode) 3315 return; 3316 3317 CSR_WRITE_1(sc, sc->rl_cfg1, CSR_READ_1(sc, sc->rl_cfg1) | 3318 RL_CFG1_DRVLOAD); 3319 3320 ifp->if_drv_flags |= IFF_DRV_RUNNING; 3321 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 3322 3323 sc->rl_flags &= ~RL_FLAG_LINK; 3324 mii_mediachg(mii); 3325 3326 sc->rl_watchdog_timer = 0; 3327 callout_reset(&sc->rl_stat_callout, hz, re_tick, sc); 3328 } 3329 3330 /* 3331 * Set media options. 3332 */ 3333 static int 3334 re_ifmedia_upd(struct ifnet *ifp) 3335 { 3336 struct rl_softc *sc; 3337 struct mii_data *mii; 3338 int error; 3339 3340 sc = ifp->if_softc; 3341 mii = device_get_softc(sc->rl_miibus); 3342 RL_LOCK(sc); 3343 error = mii_mediachg(mii); 3344 RL_UNLOCK(sc); 3345 3346 return (error); 3347 } 3348 3349 /* 3350 * Report current media status. 3351 */ 3352 static void 3353 re_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 3354 { 3355 struct rl_softc *sc; 3356 struct mii_data *mii; 3357 3358 sc = ifp->if_softc; 3359 mii = device_get_softc(sc->rl_miibus); 3360 3361 RL_LOCK(sc); 3362 mii_pollstat(mii); 3363 ifmr->ifm_active = mii->mii_media_active; 3364 ifmr->ifm_status = mii->mii_media_status; 3365 RL_UNLOCK(sc); 3366 } 3367 3368 static int 3369 re_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 3370 { 3371 struct rl_softc *sc = ifp->if_softc; 3372 struct ifreq *ifr = (struct ifreq *) data; 3373 struct mii_data *mii; 3374 int error = 0; 3375 3376 switch (command) { 3377 case SIOCSIFMTU: 3378 if (ifr->ifr_mtu < ETHERMIN || 3379 ifr->ifr_mtu > sc->rl_hwrev->rl_max_mtu || 3380 ((sc->rl_flags & RL_FLAG_FASTETHER) != 0 && 3381 ifr->ifr_mtu > RL_MTU)) { 3382 error = EINVAL; 3383 break; 3384 } 3385 RL_LOCK(sc); 3386 if (ifp->if_mtu != ifr->ifr_mtu) { 3387 ifp->if_mtu = ifr->ifr_mtu; 3388 if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 && 3389 (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) { 3390 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 3391 re_init_locked(sc); 3392 } 3393 if (ifp->if_mtu > RL_TSO_MTU && 3394 (ifp->if_capenable & IFCAP_TSO4) != 0) { 3395 ifp->if_capenable &= ~(IFCAP_TSO4 | 3396 IFCAP_VLAN_HWTSO); 3397 ifp->if_hwassist &= ~CSUM_TSO; 3398 } 3399 VLAN_CAPABILITIES(ifp); 3400 } 3401 RL_UNLOCK(sc); 3402 break; 3403 case SIOCSIFFLAGS: 3404 RL_LOCK(sc); 3405 if ((ifp->if_flags & IFF_UP) != 0) { 3406 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) { 3407 if (((ifp->if_flags ^ sc->rl_if_flags) 3408 & (IFF_PROMISC | IFF_ALLMULTI)) != 0) 3409 re_set_rxmode(sc); 3410 } else 3411 re_init_locked(sc); 3412 } else { 3413 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 3414 re_stop(sc); 3415 } 3416 sc->rl_if_flags = ifp->if_flags; 3417 RL_UNLOCK(sc); 3418 break; 3419 case SIOCADDMULTI: 3420 case SIOCDELMULTI: 3421 RL_LOCK(sc); 3422 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 3423 re_set_rxmode(sc); 3424 RL_UNLOCK(sc); 3425 break; 3426 case SIOCGIFMEDIA: 3427 case SIOCSIFMEDIA: 3428 mii = device_get_softc(sc->rl_miibus); 3429 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 3430 break; 3431 case SIOCSIFCAP: 3432 { 3433 int mask, reinit; 3434 3435 mask = ifr->ifr_reqcap ^ ifp->if_capenable; 3436 reinit = 0; 3437 #ifdef DEVICE_POLLING 3438 if (mask & IFCAP_POLLING) { 3439 if (ifr->ifr_reqcap & IFCAP_POLLING) { 3440 error = ether_poll_register(re_poll, ifp); 3441 if (error) 3442 return (error); 3443 RL_LOCK(sc); 3444 /* Disable interrupts */ 3445 CSR_WRITE_2(sc, RL_IMR, 0x0000); 3446 ifp->if_capenable |= IFCAP_POLLING; 3447 RL_UNLOCK(sc); 3448 } else { 3449 error = ether_poll_deregister(ifp); 3450 /* Enable interrupts. */ 3451 RL_LOCK(sc); 3452 CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS); 3453 ifp->if_capenable &= ~IFCAP_POLLING; 3454 RL_UNLOCK(sc); 3455 } 3456 } 3457 #endif /* DEVICE_POLLING */ 3458 RL_LOCK(sc); 3459 if ((mask & IFCAP_TXCSUM) != 0 && 3460 (ifp->if_capabilities & IFCAP_TXCSUM) != 0) { 3461 ifp->if_capenable ^= IFCAP_TXCSUM; 3462 if ((ifp->if_capenable & IFCAP_TXCSUM) != 0) 3463 ifp->if_hwassist |= RE_CSUM_FEATURES; 3464 else 3465 ifp->if_hwassist &= ~RE_CSUM_FEATURES; 3466 reinit = 1; 3467 } 3468 if ((mask & IFCAP_RXCSUM) != 0 && 3469 (ifp->if_capabilities & IFCAP_RXCSUM) != 0) { 3470 ifp->if_capenable ^= IFCAP_RXCSUM; 3471 reinit = 1; 3472 } 3473 if ((mask & IFCAP_TSO4) != 0 && 3474 (ifp->if_capabilities & IFCAP_TSO4) != 0) { 3475 ifp->if_capenable ^= IFCAP_TSO4; 3476 if ((IFCAP_TSO4 & ifp->if_capenable) != 0) 3477 ifp->if_hwassist |= CSUM_TSO; 3478 else 3479 ifp->if_hwassist &= ~CSUM_TSO; 3480 if (ifp->if_mtu > RL_TSO_MTU && 3481 (ifp->if_capenable & IFCAP_TSO4) != 0) { 3482 ifp->if_capenable &= ~IFCAP_TSO4; 3483 ifp->if_hwassist &= ~CSUM_TSO; 3484 } 3485 } 3486 if ((mask & IFCAP_VLAN_HWTSO) != 0 && 3487 (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0) 3488 ifp->if_capenable ^= IFCAP_VLAN_HWTSO; 3489 if ((mask & IFCAP_VLAN_HWTAGGING) != 0 && 3490 (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) { 3491 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING; 3492 /* TSO over VLAN requires VLAN hardware tagging. */ 3493 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) 3494 ifp->if_capenable &= ~IFCAP_VLAN_HWTSO; 3495 reinit = 1; 3496 } 3497 if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 && 3498 (mask & (IFCAP_HWCSUM | IFCAP_TSO4 | 3499 IFCAP_VLAN_HWTSO)) != 0) 3500 reinit = 1; 3501 if ((mask & IFCAP_WOL) != 0 && 3502 (ifp->if_capabilities & IFCAP_WOL) != 0) { 3503 if ((mask & IFCAP_WOL_UCAST) != 0) 3504 ifp->if_capenable ^= IFCAP_WOL_UCAST; 3505 if ((mask & IFCAP_WOL_MCAST) != 0) 3506 ifp->if_capenable ^= IFCAP_WOL_MCAST; 3507 if ((mask & IFCAP_WOL_MAGIC) != 0) 3508 ifp->if_capenable ^= IFCAP_WOL_MAGIC; 3509 } 3510 if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING) { 3511 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 3512 re_init_locked(sc); 3513 } 3514 RL_UNLOCK(sc); 3515 VLAN_CAPABILITIES(ifp); 3516 } 3517 break; 3518 default: 3519 error = ether_ioctl(ifp, command, data); 3520 break; 3521 } 3522 3523 return (error); 3524 } 3525 3526 static void 3527 re_watchdog(struct rl_softc *sc) 3528 { 3529 struct ifnet *ifp; 3530 3531 RL_LOCK_ASSERT(sc); 3532 3533 if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0) 3534 return; 3535 3536 ifp = sc->rl_ifp; 3537 re_txeof(sc); 3538 if (sc->rl_ldata.rl_tx_free == sc->rl_ldata.rl_tx_desc_cnt) { 3539 if_printf(ifp, "watchdog timeout (missed Tx interrupts) " 3540 "-- recovering\n"); 3541 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 3542 re_start_locked(ifp); 3543 return; 3544 } 3545 3546 if_printf(ifp, "watchdog timeout\n"); 3547 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 3548 3549 re_rxeof(sc, NULL); 3550 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 3551 re_init_locked(sc); 3552 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 3553 re_start_locked(ifp); 3554 } 3555 3556 /* 3557 * Stop the adapter and free any mbufs allocated to the 3558 * RX and TX lists. 3559 */ 3560 static void 3561 re_stop(struct rl_softc *sc) 3562 { 3563 int i; 3564 struct ifnet *ifp; 3565 struct rl_txdesc *txd; 3566 struct rl_rxdesc *rxd; 3567 3568 RL_LOCK_ASSERT(sc); 3569 3570 ifp = sc->rl_ifp; 3571 3572 sc->rl_watchdog_timer = 0; 3573 callout_stop(&sc->rl_stat_callout); 3574 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 3575 3576 /* 3577 * Disable accepting frames to put RX MAC into idle state. 3578 * Otherwise it's possible to get frames while stop command 3579 * execution is in progress and controller can DMA the frame 3580 * to already freed RX buffer during that period. 3581 */ 3582 CSR_WRITE_4(sc, RL_RXCFG, CSR_READ_4(sc, RL_RXCFG) & 3583 ~(RL_RXCFG_RX_ALLPHYS | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_MULTI | 3584 RL_RXCFG_RX_BROAD)); 3585 3586 if ((sc->rl_flags & RL_FLAG_WAIT_TXPOLL) != 0) { 3587 for (i = RL_TIMEOUT; i > 0; i--) { 3588 if ((CSR_READ_1(sc, sc->rl_txstart) & 3589 RL_TXSTART_START) == 0) 3590 break; 3591 DELAY(20); 3592 } 3593 if (i == 0) 3594 device_printf(sc->rl_dev, 3595 "stopping TX poll timed out!\n"); 3596 CSR_WRITE_1(sc, RL_COMMAND, 0x00); 3597 } else if ((sc->rl_flags & RL_FLAG_CMDSTOP) != 0) { 3598 CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_STOPREQ | RL_CMD_TX_ENB | 3599 RL_CMD_RX_ENB); 3600 if ((sc->rl_flags & RL_FLAG_CMDSTOP_WAIT_TXQ) != 0) { 3601 for (i = RL_TIMEOUT; i > 0; i--) { 3602 if ((CSR_READ_4(sc, RL_TXCFG) & 3603 RL_TXCFG_QUEUE_EMPTY) != 0) 3604 break; 3605 DELAY(100); 3606 } 3607 if (i == 0) 3608 device_printf(sc->rl_dev, 3609 "stopping TXQ timed out!\n"); 3610 } 3611 } else 3612 CSR_WRITE_1(sc, RL_COMMAND, 0x00); 3613 DELAY(1000); 3614 CSR_WRITE_2(sc, RL_IMR, 0x0000); 3615 CSR_WRITE_2(sc, RL_ISR, 0xFFFF); 3616 3617 if (sc->rl_head != NULL) { 3618 m_freem(sc->rl_head); 3619 sc->rl_head = sc->rl_tail = NULL; 3620 } 3621 3622 /* Free the TX list buffers. */ 3623 for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) { 3624 txd = &sc->rl_ldata.rl_tx_desc[i]; 3625 if (txd->tx_m != NULL) { 3626 bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, 3627 txd->tx_dmamap, BUS_DMASYNC_POSTWRITE); 3628 bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, 3629 txd->tx_dmamap); 3630 m_freem(txd->tx_m); 3631 txd->tx_m = NULL; 3632 } 3633 } 3634 3635 /* Free the RX list buffers. */ 3636 for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 3637 rxd = &sc->rl_ldata.rl_rx_desc[i]; 3638 if (rxd->rx_m != NULL) { 3639 bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, 3640 rxd->rx_dmamap, BUS_DMASYNC_POSTREAD); 3641 bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, 3642 rxd->rx_dmamap); 3643 m_freem(rxd->rx_m); 3644 rxd->rx_m = NULL; 3645 } 3646 } 3647 3648 if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) { 3649 for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) { 3650 rxd = &sc->rl_ldata.rl_jrx_desc[i]; 3651 if (rxd->rx_m != NULL) { 3652 bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, 3653 rxd->rx_dmamap, BUS_DMASYNC_POSTREAD); 3654 bus_dmamap_unload(sc->rl_ldata.rl_jrx_mtag, 3655 rxd->rx_dmamap); 3656 m_freem(rxd->rx_m); 3657 rxd->rx_m = NULL; 3658 } 3659 } 3660 } 3661 } 3662 3663 /* 3664 * Device suspend routine. Stop the interface and save some PCI 3665 * settings in case the BIOS doesn't restore them properly on 3666 * resume. 3667 */ 3668 static int 3669 re_suspend(device_t dev) 3670 { 3671 struct rl_softc *sc; 3672 3673 sc = device_get_softc(dev); 3674 3675 RL_LOCK(sc); 3676 re_stop(sc); 3677 re_setwol(sc); 3678 sc->suspended = 1; 3679 RL_UNLOCK(sc); 3680 3681 return (0); 3682 } 3683 3684 /* 3685 * Device resume routine. Restore some PCI settings in case the BIOS 3686 * doesn't, re-enable busmastering, and restart the interface if 3687 * appropriate. 3688 */ 3689 static int 3690 re_resume(device_t dev) 3691 { 3692 struct rl_softc *sc; 3693 struct ifnet *ifp; 3694 3695 sc = device_get_softc(dev); 3696 3697 RL_LOCK(sc); 3698 3699 ifp = sc->rl_ifp; 3700 /* Take controller out of sleep mode. */ 3701 if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) { 3702 if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80) 3703 CSR_WRITE_1(sc, RL_GPIO, 3704 CSR_READ_1(sc, RL_GPIO) | 0x01); 3705 } 3706 3707 /* 3708 * Clear WOL matching such that normal Rx filtering 3709 * wouldn't interfere with WOL patterns. 3710 */ 3711 re_clrwol(sc); 3712 3713 /* reinitialize interface if necessary */ 3714 if (ifp->if_flags & IFF_UP) 3715 re_init_locked(sc); 3716 3717 sc->suspended = 0; 3718 RL_UNLOCK(sc); 3719 3720 return (0); 3721 } 3722 3723 /* 3724 * Stop all chip I/O so that the kernel's probe routines don't 3725 * get confused by errant DMAs when rebooting. 3726 */ 3727 static int 3728 re_shutdown(device_t dev) 3729 { 3730 struct rl_softc *sc; 3731 3732 sc = device_get_softc(dev); 3733 3734 RL_LOCK(sc); 3735 re_stop(sc); 3736 /* 3737 * Mark interface as down since otherwise we will panic if 3738 * interrupt comes in later on, which can happen in some 3739 * cases. 3740 */ 3741 sc->rl_ifp->if_flags &= ~IFF_UP; 3742 re_setwol(sc); 3743 RL_UNLOCK(sc); 3744 3745 return (0); 3746 } 3747 3748 static void 3749 re_set_linkspeed(struct rl_softc *sc) 3750 { 3751 struct mii_softc *miisc; 3752 struct mii_data *mii; 3753 int aneg, i, phyno; 3754 3755 RL_LOCK_ASSERT(sc); 3756 3757 mii = device_get_softc(sc->rl_miibus); 3758 mii_pollstat(mii); 3759 aneg = 0; 3760 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) == 3761 (IFM_ACTIVE | IFM_AVALID)) { 3762 switch IFM_SUBTYPE(mii->mii_media_active) { 3763 case IFM_10_T: 3764 case IFM_100_TX: 3765 return; 3766 case IFM_1000_T: 3767 aneg++; 3768 break; 3769 default: 3770 break; 3771 } 3772 } 3773 miisc = LIST_FIRST(&mii->mii_phys); 3774 phyno = miisc->mii_phy; 3775 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 3776 PHY_RESET(miisc); 3777 re_miibus_writereg(sc->rl_dev, phyno, MII_100T2CR, 0); 3778 re_miibus_writereg(sc->rl_dev, phyno, 3779 MII_ANAR, ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA); 3780 re_miibus_writereg(sc->rl_dev, phyno, 3781 MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG); 3782 DELAY(1000); 3783 if (aneg != 0) { 3784 /* 3785 * Poll link state until re(4) get a 10/100Mbps link. 3786 */ 3787 for (i = 0; i < MII_ANEGTICKS_GIGE; i++) { 3788 mii_pollstat(mii); 3789 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) 3790 == (IFM_ACTIVE | IFM_AVALID)) { 3791 switch (IFM_SUBTYPE(mii->mii_media_active)) { 3792 case IFM_10_T: 3793 case IFM_100_TX: 3794 return; 3795 default: 3796 break; 3797 } 3798 } 3799 RL_UNLOCK(sc); 3800 pause("relnk", hz); 3801 RL_LOCK(sc); 3802 } 3803 if (i == MII_ANEGTICKS_GIGE) 3804 device_printf(sc->rl_dev, 3805 "establishing a link failed, WOL may not work!"); 3806 } 3807 /* 3808 * No link, force MAC to have 100Mbps, full-duplex link. 3809 * MAC does not require reprogramming on resolved speed/duplex, 3810 * so this is just for completeness. 3811 */ 3812 mii->mii_media_status = IFM_AVALID | IFM_ACTIVE; 3813 mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX; 3814 } 3815 3816 static void 3817 re_setwol(struct rl_softc *sc) 3818 { 3819 struct ifnet *ifp; 3820 int pmc; 3821 uint16_t pmstat; 3822 uint8_t v; 3823 3824 RL_LOCK_ASSERT(sc); 3825 3826 if (pci_find_cap(sc->rl_dev, PCIY_PMG, &pmc) != 0) 3827 return; 3828 3829 ifp = sc->rl_ifp; 3830 /* Put controller into sleep mode. */ 3831 if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) { 3832 if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80) 3833 CSR_WRITE_1(sc, RL_GPIO, 3834 CSR_READ_1(sc, RL_GPIO) & ~0x01); 3835 } 3836 if ((ifp->if_capenable & IFCAP_WOL) != 0) { 3837 re_set_rxmode(sc); 3838 if ((sc->rl_flags & RL_FLAG_WOL_MANLINK) != 0) 3839 re_set_linkspeed(sc); 3840 if ((sc->rl_flags & RL_FLAG_WOLRXENB) != 0) 3841 CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RX_ENB); 3842 } 3843 /* Enable config register write. */ 3844 CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE); 3845 3846 /* Enable PME. */ 3847 v = CSR_READ_1(sc, sc->rl_cfg1); 3848 v &= ~RL_CFG1_PME; 3849 if ((ifp->if_capenable & IFCAP_WOL) != 0) 3850 v |= RL_CFG1_PME; 3851 CSR_WRITE_1(sc, sc->rl_cfg1, v); 3852 3853 v = CSR_READ_1(sc, sc->rl_cfg3); 3854 v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC); 3855 if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0) 3856 v |= RL_CFG3_WOL_MAGIC; 3857 CSR_WRITE_1(sc, sc->rl_cfg3, v); 3858 3859 v = CSR_READ_1(sc, sc->rl_cfg5); 3860 v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST | 3861 RL_CFG5_WOL_LANWAKE); 3862 if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0) 3863 v |= RL_CFG5_WOL_UCAST; 3864 if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0) 3865 v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST; 3866 if ((ifp->if_capenable & IFCAP_WOL) != 0) 3867 v |= RL_CFG5_WOL_LANWAKE; 3868 CSR_WRITE_1(sc, sc->rl_cfg5, v); 3869 3870 /* Config register write done. */ 3871 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 3872 3873 if ((ifp->if_capenable & IFCAP_WOL) == 0 && 3874 (sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0) 3875 CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) & ~0x80); 3876 /* 3877 * It seems that hardware resets its link speed to 100Mbps in 3878 * power down mode so switching to 100Mbps in driver is not 3879 * needed. 3880 */ 3881 3882 /* Request PME if WOL is requested. */ 3883 pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2); 3884 pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE); 3885 if ((ifp->if_capenable & IFCAP_WOL) != 0) 3886 pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE; 3887 pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2); 3888 } 3889 3890 static void 3891 re_clrwol(struct rl_softc *sc) 3892 { 3893 int pmc; 3894 uint8_t v; 3895 3896 RL_LOCK_ASSERT(sc); 3897 3898 if (pci_find_cap(sc->rl_dev, PCIY_PMG, &pmc) != 0) 3899 return; 3900 3901 /* Enable config register write. */ 3902 CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE); 3903 3904 v = CSR_READ_1(sc, sc->rl_cfg3); 3905 v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC); 3906 CSR_WRITE_1(sc, sc->rl_cfg3, v); 3907 3908 /* Config register write done. */ 3909 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 3910 3911 v = CSR_READ_1(sc, sc->rl_cfg5); 3912 v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST); 3913 v &= ~RL_CFG5_WOL_LANWAKE; 3914 CSR_WRITE_1(sc, sc->rl_cfg5, v); 3915 } 3916 3917 static void 3918 re_add_sysctls(struct rl_softc *sc) 3919 { 3920 struct sysctl_ctx_list *ctx; 3921 struct sysctl_oid_list *children; 3922 int error; 3923 3924 ctx = device_get_sysctl_ctx(sc->rl_dev); 3925 children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->rl_dev)); 3926 3927 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "stats", 3928 CTLTYPE_INT | CTLFLAG_RW, sc, 0, re_sysctl_stats, "I", 3929 "Statistics Information"); 3930 if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0) 3931 return; 3932 3933 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "int_rx_mod", 3934 CTLTYPE_INT | CTLFLAG_RW, &sc->rl_int_rx_mod, 0, 3935 sysctl_hw_re_int_mod, "I", "re RX interrupt moderation"); 3936 /* Pull in device tunables. */ 3937 sc->rl_int_rx_mod = RL_TIMER_DEFAULT; 3938 error = resource_int_value(device_get_name(sc->rl_dev), 3939 device_get_unit(sc->rl_dev), "int_rx_mod", &sc->rl_int_rx_mod); 3940 if (error == 0) { 3941 if (sc->rl_int_rx_mod < RL_TIMER_MIN || 3942 sc->rl_int_rx_mod > RL_TIMER_MAX) { 3943 device_printf(sc->rl_dev, "int_rx_mod value out of " 3944 "range; using default: %d\n", 3945 RL_TIMER_DEFAULT); 3946 sc->rl_int_rx_mod = RL_TIMER_DEFAULT; 3947 } 3948 } 3949 3950 } 3951 3952 static int 3953 re_sysctl_stats(SYSCTL_HANDLER_ARGS) 3954 { 3955 struct rl_softc *sc; 3956 struct rl_stats *stats; 3957 int error, i, result; 3958 3959 result = -1; 3960 error = sysctl_handle_int(oidp, &result, 0, req); 3961 if (error || req->newptr == NULL) 3962 return (error); 3963 3964 if (result == 1) { 3965 sc = (struct rl_softc *)arg1; 3966 RL_LOCK(sc); 3967 if ((sc->rl_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 3968 RL_UNLOCK(sc); 3969 goto done; 3970 } 3971 bus_dmamap_sync(sc->rl_ldata.rl_stag, 3972 sc->rl_ldata.rl_smap, BUS_DMASYNC_PREREAD); 3973 CSR_WRITE_4(sc, RL_DUMPSTATS_HI, 3974 RL_ADDR_HI(sc->rl_ldata.rl_stats_addr)); 3975 CSR_WRITE_4(sc, RL_DUMPSTATS_LO, 3976 RL_ADDR_LO(sc->rl_ldata.rl_stats_addr)); 3977 CSR_WRITE_4(sc, RL_DUMPSTATS_LO, 3978 RL_ADDR_LO(sc->rl_ldata.rl_stats_addr | 3979 RL_DUMPSTATS_START)); 3980 for (i = RL_TIMEOUT; i > 0; i--) { 3981 if ((CSR_READ_4(sc, RL_DUMPSTATS_LO) & 3982 RL_DUMPSTATS_START) == 0) 3983 break; 3984 DELAY(1000); 3985 } 3986 bus_dmamap_sync(sc->rl_ldata.rl_stag, 3987 sc->rl_ldata.rl_smap, BUS_DMASYNC_POSTREAD); 3988 RL_UNLOCK(sc); 3989 if (i == 0) { 3990 device_printf(sc->rl_dev, 3991 "DUMP statistics request timed out\n"); 3992 return (ETIMEDOUT); 3993 } 3994 done: 3995 stats = sc->rl_ldata.rl_stats; 3996 printf("%s statistics:\n", device_get_nameunit(sc->rl_dev)); 3997 printf("Tx frames : %ju\n", 3998 (uintmax_t)le64toh(stats->rl_tx_pkts)); 3999 printf("Rx frames : %ju\n", 4000 (uintmax_t)le64toh(stats->rl_rx_pkts)); 4001 printf("Tx errors : %ju\n", 4002 (uintmax_t)le64toh(stats->rl_tx_errs)); 4003 printf("Rx errors : %u\n", 4004 le32toh(stats->rl_rx_errs)); 4005 printf("Rx missed frames : %u\n", 4006 (uint32_t)le16toh(stats->rl_missed_pkts)); 4007 printf("Rx frame alignment errs : %u\n", 4008 (uint32_t)le16toh(stats->rl_rx_framealign_errs)); 4009 printf("Tx single collisions : %u\n", 4010 le32toh(stats->rl_tx_onecoll)); 4011 printf("Tx multiple collisions : %u\n", 4012 le32toh(stats->rl_tx_multicolls)); 4013 printf("Rx unicast frames : %ju\n", 4014 (uintmax_t)le64toh(stats->rl_rx_ucasts)); 4015 printf("Rx broadcast frames : %ju\n", 4016 (uintmax_t)le64toh(stats->rl_rx_bcasts)); 4017 printf("Rx multicast frames : %u\n", 4018 le32toh(stats->rl_rx_mcasts)); 4019 printf("Tx aborts : %u\n", 4020 (uint32_t)le16toh(stats->rl_tx_aborts)); 4021 printf("Tx underruns : %u\n", 4022 (uint32_t)le16toh(stats->rl_rx_underruns)); 4023 } 4024 4025 return (error); 4026 } 4027 4028 static int 4029 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high) 4030 { 4031 int error, value; 4032 4033 if (arg1 == NULL) 4034 return (EINVAL); 4035 value = *(int *)arg1; 4036 error = sysctl_handle_int(oidp, &value, 0, req); 4037 if (error || req->newptr == NULL) 4038 return (error); 4039 if (value < low || value > high) 4040 return (EINVAL); 4041 *(int *)arg1 = value; 4042 4043 return (0); 4044 } 4045 4046 static int 4047 sysctl_hw_re_int_mod(SYSCTL_HANDLER_ARGS) 4048 { 4049 4050 return (sysctl_int_range(oidp, arg1, arg2, req, RL_TIMER_MIN, 4051 RL_TIMER_MAX)); 4052 } 4053