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/taskqueue.h> 127 128 #include <net/if.h> 129 #include <net/if_arp.h> 130 #include <net/ethernet.h> 131 #include <net/if_dl.h> 132 #include <net/if_media.h> 133 #include <net/if_types.h> 134 #include <net/if_vlan_var.h> 135 136 #include <net/bpf.h> 137 138 #include <machine/bus.h> 139 #include <machine/resource.h> 140 #include <sys/bus.h> 141 #include <sys/rman.h> 142 143 #include <dev/mii/mii.h> 144 #include <dev/mii/miivar.h> 145 146 #include <dev/pci/pcireg.h> 147 #include <dev/pci/pcivar.h> 148 149 MODULE_DEPEND(re, pci, 1, 1, 1); 150 MODULE_DEPEND(re, ether, 1, 1, 1); 151 MODULE_DEPEND(re, miibus, 1, 1, 1); 152 153 /* "device miibus" required. See GENERIC if you get errors here. */ 154 #include "miibus_if.h" 155 156 /* 157 * Default to using PIO access for this driver. 158 */ 159 #define RE_USEIOSPACE 160 161 #include <pci/if_rlreg.h> 162 163 #define RE_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP) 164 165 /* 166 * Various supported device vendors/types and their names. 167 */ 168 static struct rl_type re_devs[] = { 169 { DLINK_VENDORID, DLINK_DEVICEID_528T, RL_HWREV_8169S, 170 "D-Link DGE-528(T) Gigabit Ethernet Adapter" }, 171 { RT_VENDORID, RT_DEVICEID_8139, RL_HWREV_8139CPLUS, 172 "RealTek 8139C+ 10/100BaseTX" }, 173 { RT_VENDORID, RT_DEVICEID_8101E, RL_HWREV_8101E, 174 "RealTek 8101E PCIe 10/100baseTX" }, 175 { RT_VENDORID, RT_DEVICEID_8168, RL_HWREV_8168_SPIN1, 176 "RealTek 8168/8111B PCIe Gigabit Ethernet" }, 177 { RT_VENDORID, RT_DEVICEID_8168, RL_HWREV_8168_SPIN2, 178 "RealTek 8168/8111B PCIe Gigabit Ethernet" }, 179 { RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169, 180 "RealTek 8169 Gigabit Ethernet" }, 181 { RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169S, 182 "RealTek 8169S Single-chip Gigabit Ethernet" }, 183 { RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169_8110SB, 184 "RealTek 8169SB/8110SB Single-chip Gigabit Ethernet" }, 185 { RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169_8110SC, 186 "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" }, 187 { RT_VENDORID, RT_DEVICEID_8169SC, RL_HWREV_8169_8110SC, 188 "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" }, 189 { RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8110S, 190 "RealTek 8110S Single-chip Gigabit Ethernet" }, 191 { COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, RL_HWREV_8169S, 192 "Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" }, 193 { LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, RL_HWREV_8169S, 194 "Linksys EG1032 (RTL8169S) Gigabit Ethernet" }, 195 { USR_VENDORID, USR_DEVICEID_997902, RL_HWREV_8169S, 196 "US Robotics 997902 (RTL8169S) Gigabit Ethernet" }, 197 { 0, 0, 0, NULL } 198 }; 199 200 static struct rl_hwrev re_hwrevs[] = { 201 { RL_HWREV_8139, RL_8139, "" }, 202 { RL_HWREV_8139A, RL_8139, "A" }, 203 { RL_HWREV_8139AG, RL_8139, "A-G" }, 204 { RL_HWREV_8139B, RL_8139, "B" }, 205 { RL_HWREV_8130, RL_8139, "8130" }, 206 { RL_HWREV_8139C, RL_8139, "C" }, 207 { RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C" }, 208 { RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+"}, 209 { RL_HWREV_8168_SPIN1, RL_8169, "8168"}, 210 { RL_HWREV_8169, RL_8169, "8169"}, 211 { RL_HWREV_8169S, RL_8169, "8169S"}, 212 { RL_HWREV_8110S, RL_8169, "8110S"}, 213 { RL_HWREV_8169_8110SB, RL_8169, "8169SB"}, 214 { RL_HWREV_8169_8110SC, RL_8169, "8169SC"}, 215 { RL_HWREV_8100, RL_8139, "8100"}, 216 { RL_HWREV_8101, RL_8139, "8101"}, 217 { RL_HWREV_8100E, RL_8169, "8100E"}, 218 { RL_HWREV_8101E, RL_8169, "8101E"}, 219 { RL_HWREV_8168_SPIN2, RL_8169, "8168"}, 220 { 0, 0, NULL } 221 }; 222 223 static int re_probe (device_t); 224 static int re_attach (device_t); 225 static int re_detach (device_t); 226 227 static int re_encap (struct rl_softc *, struct mbuf **, int *); 228 229 static void re_dma_map_addr (void *, bus_dma_segment_t *, int, int); 230 static void re_dma_map_desc (void *, bus_dma_segment_t *, int, 231 bus_size_t, int); 232 static int re_allocmem (device_t, struct rl_softc *); 233 static int re_newbuf (struct rl_softc *, int, struct mbuf *); 234 static int re_rx_list_init (struct rl_softc *); 235 static int re_tx_list_init (struct rl_softc *); 236 #ifdef RE_FIXUP_RX 237 static __inline void re_fixup_rx 238 (struct mbuf *); 239 #endif 240 static int re_rxeof (struct rl_softc *); 241 static void re_txeof (struct rl_softc *); 242 #ifdef DEVICE_POLLING 243 static void re_poll (struct ifnet *, enum poll_cmd, int); 244 static void re_poll_locked (struct ifnet *, enum poll_cmd, int); 245 #endif 246 static int re_intr (void *); 247 static void re_tick (void *); 248 static void re_tx_task (void *, int); 249 static void re_int_task (void *, int); 250 static void re_start (struct ifnet *); 251 static int re_ioctl (struct ifnet *, u_long, caddr_t); 252 static void re_init (void *); 253 static void re_init_locked (struct rl_softc *); 254 static void re_stop (struct rl_softc *); 255 static void re_watchdog (struct rl_softc *); 256 static int re_suspend (device_t); 257 static int re_resume (device_t); 258 static void re_shutdown (device_t); 259 static int re_ifmedia_upd (struct ifnet *); 260 static void re_ifmedia_sts (struct ifnet *, struct ifmediareq *); 261 262 static void re_eeprom_putbyte (struct rl_softc *, int); 263 static void re_eeprom_getword (struct rl_softc *, int, u_int16_t *); 264 static void re_read_eeprom (struct rl_softc *, caddr_t, int, int); 265 static int re_gmii_readreg (device_t, int, int); 266 static int re_gmii_writereg (device_t, int, int, int); 267 268 static int re_miibus_readreg (device_t, int, int); 269 static int re_miibus_writereg (device_t, int, int, int); 270 static void re_miibus_statchg (device_t); 271 272 static void re_setmulti (struct rl_softc *); 273 static void re_reset (struct rl_softc *); 274 275 #ifdef RE_DIAG 276 static int re_diag (struct rl_softc *); 277 #endif 278 279 #ifdef RE_USEIOSPACE 280 #define RL_RES SYS_RES_IOPORT 281 #define RL_RID RL_PCI_LOIO 282 #else 283 #define RL_RES SYS_RES_MEMORY 284 #define RL_RID RL_PCI_LOMEM 285 #endif 286 287 static device_method_t re_methods[] = { 288 /* Device interface */ 289 DEVMETHOD(device_probe, re_probe), 290 DEVMETHOD(device_attach, re_attach), 291 DEVMETHOD(device_detach, re_detach), 292 DEVMETHOD(device_suspend, re_suspend), 293 DEVMETHOD(device_resume, re_resume), 294 DEVMETHOD(device_shutdown, re_shutdown), 295 296 /* bus interface */ 297 DEVMETHOD(bus_print_child, bus_generic_print_child), 298 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 299 300 /* MII interface */ 301 DEVMETHOD(miibus_readreg, re_miibus_readreg), 302 DEVMETHOD(miibus_writereg, re_miibus_writereg), 303 DEVMETHOD(miibus_statchg, re_miibus_statchg), 304 305 { 0, 0 } 306 }; 307 308 static driver_t re_driver = { 309 "re", 310 re_methods, 311 sizeof(struct rl_softc) 312 }; 313 314 static devclass_t re_devclass; 315 316 DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0); 317 DRIVER_MODULE(re, cardbus, re_driver, re_devclass, 0, 0); 318 DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0); 319 320 #define EE_SET(x) \ 321 CSR_WRITE_1(sc, RL_EECMD, \ 322 CSR_READ_1(sc, RL_EECMD) | x) 323 324 #define EE_CLR(x) \ 325 CSR_WRITE_1(sc, RL_EECMD, \ 326 CSR_READ_1(sc, RL_EECMD) & ~x) 327 328 /* 329 * Send a read command and address to the EEPROM, check for ACK. 330 */ 331 static void 332 re_eeprom_putbyte(sc, addr) 333 struct rl_softc *sc; 334 int addr; 335 { 336 register int d, i; 337 338 d = addr | (RL_9346_READ << sc->rl_eewidth); 339 340 /* 341 * Feed in each bit and strobe the clock. 342 */ 343 344 for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) { 345 if (d & i) { 346 EE_SET(RL_EE_DATAIN); 347 } else { 348 EE_CLR(RL_EE_DATAIN); 349 } 350 DELAY(100); 351 EE_SET(RL_EE_CLK); 352 DELAY(150); 353 EE_CLR(RL_EE_CLK); 354 DELAY(100); 355 } 356 357 return; 358 } 359 360 /* 361 * Read a word of data stored in the EEPROM at address 'addr.' 362 */ 363 static void 364 re_eeprom_getword(sc, addr, dest) 365 struct rl_softc *sc; 366 int addr; 367 u_int16_t *dest; 368 { 369 register int i; 370 u_int16_t word = 0; 371 372 /* 373 * Send address of word we want to read. 374 */ 375 re_eeprom_putbyte(sc, addr); 376 377 /* 378 * Start reading bits from EEPROM. 379 */ 380 for (i = 0x8000; i; i >>= 1) { 381 EE_SET(RL_EE_CLK); 382 DELAY(100); 383 if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT) 384 word |= i; 385 EE_CLR(RL_EE_CLK); 386 DELAY(100); 387 } 388 389 *dest = word; 390 391 return; 392 } 393 394 /* 395 * Read a sequence of words from the EEPROM. 396 */ 397 static void 398 re_read_eeprom(sc, dest, off, cnt) 399 struct rl_softc *sc; 400 caddr_t dest; 401 int off; 402 int cnt; 403 { 404 int i; 405 u_int16_t word = 0, *ptr; 406 407 CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM); 408 409 DELAY(100); 410 411 for (i = 0; i < cnt; i++) { 412 CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL); 413 re_eeprom_getword(sc, off + i, &word); 414 CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL); 415 ptr = (u_int16_t *)(dest + (i * 2)); 416 *ptr = word; 417 } 418 419 CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM); 420 421 return; 422 } 423 424 static int 425 re_gmii_readreg(dev, phy, reg) 426 device_t dev; 427 int phy, reg; 428 { 429 struct rl_softc *sc; 430 u_int32_t rval; 431 int i; 432 433 if (phy != 1) 434 return (0); 435 436 sc = device_get_softc(dev); 437 438 /* Let the rgephy driver read the GMEDIASTAT register */ 439 440 if (reg == RL_GMEDIASTAT) { 441 rval = CSR_READ_1(sc, RL_GMEDIASTAT); 442 return (rval); 443 } 444 445 CSR_WRITE_4(sc, RL_PHYAR, reg << 16); 446 DELAY(1000); 447 448 for (i = 0; i < RL_TIMEOUT; i++) { 449 rval = CSR_READ_4(sc, RL_PHYAR); 450 if (rval & RL_PHYAR_BUSY) 451 break; 452 DELAY(100); 453 } 454 455 if (i == RL_TIMEOUT) { 456 device_printf(sc->rl_dev, "PHY read failed\n"); 457 return (0); 458 } 459 460 return (rval & RL_PHYAR_PHYDATA); 461 } 462 463 static int 464 re_gmii_writereg(dev, phy, reg, data) 465 device_t dev; 466 int phy, reg, data; 467 { 468 struct rl_softc *sc; 469 u_int32_t rval; 470 int i; 471 472 sc = device_get_softc(dev); 473 474 CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) | 475 (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY); 476 DELAY(1000); 477 478 for (i = 0; i < RL_TIMEOUT; i++) { 479 rval = CSR_READ_4(sc, RL_PHYAR); 480 if (!(rval & RL_PHYAR_BUSY)) 481 break; 482 DELAY(100); 483 } 484 485 if (i == RL_TIMEOUT) { 486 device_printf(sc->rl_dev, "PHY write failed\n"); 487 return (0); 488 } 489 490 return (0); 491 } 492 493 static int 494 re_miibus_readreg(dev, phy, reg) 495 device_t dev; 496 int phy, reg; 497 { 498 struct rl_softc *sc; 499 u_int16_t rval = 0; 500 u_int16_t re8139_reg = 0; 501 502 sc = device_get_softc(dev); 503 504 if (sc->rl_type == RL_8169) { 505 rval = re_gmii_readreg(dev, phy, reg); 506 return (rval); 507 } 508 509 /* Pretend the internal PHY is only at address 0 */ 510 if (phy) { 511 return (0); 512 } 513 switch (reg) { 514 case MII_BMCR: 515 re8139_reg = RL_BMCR; 516 break; 517 case MII_BMSR: 518 re8139_reg = RL_BMSR; 519 break; 520 case MII_ANAR: 521 re8139_reg = RL_ANAR; 522 break; 523 case MII_ANER: 524 re8139_reg = RL_ANER; 525 break; 526 case MII_ANLPAR: 527 re8139_reg = RL_LPAR; 528 break; 529 case MII_PHYIDR1: 530 case MII_PHYIDR2: 531 return (0); 532 /* 533 * Allow the rlphy driver to read the media status 534 * register. If we have a link partner which does not 535 * support NWAY, this is the register which will tell 536 * us the results of parallel detection. 537 */ 538 case RL_MEDIASTAT: 539 rval = CSR_READ_1(sc, RL_MEDIASTAT); 540 return (rval); 541 default: 542 device_printf(sc->rl_dev, "bad phy register\n"); 543 return (0); 544 } 545 rval = CSR_READ_2(sc, re8139_reg); 546 if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) { 547 /* 8139C+ has different bit layout. */ 548 rval &= ~(BMCR_LOOP | BMCR_ISO); 549 } 550 return (rval); 551 } 552 553 static int 554 re_miibus_writereg(dev, phy, reg, data) 555 device_t dev; 556 int phy, reg, data; 557 { 558 struct rl_softc *sc; 559 u_int16_t re8139_reg = 0; 560 int rval = 0; 561 562 sc = device_get_softc(dev); 563 564 if (sc->rl_type == RL_8169) { 565 rval = re_gmii_writereg(dev, phy, reg, data); 566 return (rval); 567 } 568 569 /* Pretend the internal PHY is only at address 0 */ 570 if (phy) 571 return (0); 572 573 switch (reg) { 574 case MII_BMCR: 575 re8139_reg = RL_BMCR; 576 if (sc->rl_type == RL_8139CPLUS) { 577 /* 8139C+ has different bit layout. */ 578 data &= ~(BMCR_LOOP | BMCR_ISO); 579 } 580 break; 581 case MII_BMSR: 582 re8139_reg = RL_BMSR; 583 break; 584 case MII_ANAR: 585 re8139_reg = RL_ANAR; 586 break; 587 case MII_ANER: 588 re8139_reg = RL_ANER; 589 break; 590 case MII_ANLPAR: 591 re8139_reg = RL_LPAR; 592 break; 593 case MII_PHYIDR1: 594 case MII_PHYIDR2: 595 return (0); 596 break; 597 default: 598 device_printf(sc->rl_dev, "bad phy register\n"); 599 return (0); 600 } 601 CSR_WRITE_2(sc, re8139_reg, data); 602 return (0); 603 } 604 605 static void 606 re_miibus_statchg(dev) 607 device_t dev; 608 { 609 610 } 611 612 /* 613 * Program the 64-bit multicast hash filter. 614 */ 615 static void 616 re_setmulti(sc) 617 struct rl_softc *sc; 618 { 619 struct ifnet *ifp; 620 int h = 0; 621 u_int32_t hashes[2] = { 0, 0 }; 622 struct ifmultiaddr *ifma; 623 u_int32_t rxfilt; 624 int mcnt = 0; 625 u_int32_t hwrev; 626 627 RL_LOCK_ASSERT(sc); 628 629 ifp = sc->rl_ifp; 630 631 rxfilt = CSR_READ_4(sc, RL_RXCFG); 632 633 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 634 rxfilt |= RL_RXCFG_RX_MULTI; 635 CSR_WRITE_4(sc, RL_RXCFG, rxfilt); 636 CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF); 637 CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF); 638 return; 639 } 640 641 /* first, zot all the existing hash bits */ 642 CSR_WRITE_4(sc, RL_MAR0, 0); 643 CSR_WRITE_4(sc, RL_MAR4, 0); 644 645 /* now program new ones */ 646 IF_ADDR_LOCK(ifp); 647 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 648 if (ifma->ifma_addr->sa_family != AF_LINK) 649 continue; 650 h = ether_crc32_be(LLADDR((struct sockaddr_dl *) 651 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 652 if (h < 32) 653 hashes[0] |= (1 << h); 654 else 655 hashes[1] |= (1 << (h - 32)); 656 mcnt++; 657 } 658 IF_ADDR_UNLOCK(ifp); 659 660 if (mcnt) 661 rxfilt |= RL_RXCFG_RX_MULTI; 662 else 663 rxfilt &= ~RL_RXCFG_RX_MULTI; 664 665 CSR_WRITE_4(sc, RL_RXCFG, rxfilt); 666 667 /* 668 * For some unfathomable reason, RealTek decided to reverse 669 * the order of the multicast hash registers in the PCI Express 670 * parts. This means we have to write the hash pattern in reverse 671 * order for those devices. 672 */ 673 674 hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV; 675 676 if (hwrev == RL_HWREV_8100E || hwrev == RL_HWREV_8101E || 677 hwrev == RL_HWREV_8168_SPIN1 || hwrev == RL_HWREV_8168_SPIN2) { 678 CSR_WRITE_4(sc, RL_MAR0, bswap32(hashes[1])); 679 CSR_WRITE_4(sc, RL_MAR4, bswap32(hashes[0])); 680 } else { 681 CSR_WRITE_4(sc, RL_MAR0, hashes[0]); 682 CSR_WRITE_4(sc, RL_MAR4, hashes[1]); 683 } 684 } 685 686 static void 687 re_reset(sc) 688 struct rl_softc *sc; 689 { 690 register int i; 691 692 RL_LOCK_ASSERT(sc); 693 694 CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET); 695 696 for (i = 0; i < RL_TIMEOUT; i++) { 697 DELAY(10); 698 if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET)) 699 break; 700 } 701 if (i == RL_TIMEOUT) 702 device_printf(sc->rl_dev, "reset never completed!\n"); 703 704 CSR_WRITE_1(sc, 0x82, 1); 705 } 706 707 #ifdef RE_DIAG 708 709 /* 710 * The following routine is designed to test for a defect on some 711 * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64# 712 * lines connected to the bus, however for a 32-bit only card, they 713 * should be pulled high. The result of this defect is that the 714 * NIC will not work right if you plug it into a 64-bit slot: DMA 715 * operations will be done with 64-bit transfers, which will fail 716 * because the 64-bit data lines aren't connected. 717 * 718 * There's no way to work around this (short of talking a soldering 719 * iron to the board), however we can detect it. The method we use 720 * here is to put the NIC into digital loopback mode, set the receiver 721 * to promiscuous mode, and then try to send a frame. We then compare 722 * the frame data we sent to what was received. If the data matches, 723 * then the NIC is working correctly, otherwise we know the user has 724 * a defective NIC which has been mistakenly plugged into a 64-bit PCI 725 * slot. In the latter case, there's no way the NIC can work correctly, 726 * so we print out a message on the console and abort the device attach. 727 */ 728 729 static int 730 re_diag(sc) 731 struct rl_softc *sc; 732 { 733 struct ifnet *ifp = sc->rl_ifp; 734 struct mbuf *m0; 735 struct ether_header *eh; 736 struct rl_desc *cur_rx; 737 u_int16_t status; 738 u_int32_t rxstat; 739 int total_len, i, error = 0, phyaddr; 740 u_int8_t dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' }; 741 u_int8_t src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' }; 742 743 /* Allocate a single mbuf */ 744 MGETHDR(m0, M_DONTWAIT, MT_DATA); 745 if (m0 == NULL) 746 return (ENOBUFS); 747 748 RL_LOCK(sc); 749 750 /* 751 * Initialize the NIC in test mode. This sets the chip up 752 * so that it can send and receive frames, but performs the 753 * following special functions: 754 * - Puts receiver in promiscuous mode 755 * - Enables digital loopback mode 756 * - Leaves interrupts turned off 757 */ 758 759 ifp->if_flags |= IFF_PROMISC; 760 sc->rl_testmode = 1; 761 re_reset(sc); 762 re_init_locked(sc); 763 sc->rl_link = 1; 764 if (sc->rl_type == RL_8169) 765 phyaddr = 1; 766 else 767 phyaddr = 0; 768 769 re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET); 770 for (i = 0; i < RL_TIMEOUT; i++) { 771 status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR); 772 if (!(status & BMCR_RESET)) 773 break; 774 } 775 776 re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP); 777 CSR_WRITE_2(sc, RL_ISR, RL_INTRS); 778 779 DELAY(100000); 780 781 /* Put some data in the mbuf */ 782 783 eh = mtod(m0, struct ether_header *); 784 bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN); 785 bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN); 786 eh->ether_type = htons(ETHERTYPE_IP); 787 m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN; 788 789 /* 790 * Queue the packet, start transmission. 791 * Note: IF_HANDOFF() ultimately calls re_start() for us. 792 */ 793 794 CSR_WRITE_2(sc, RL_ISR, 0xFFFF); 795 RL_UNLOCK(sc); 796 /* XXX: re_diag must not be called when in ALTQ mode */ 797 IF_HANDOFF(&ifp->if_snd, m0, ifp); 798 RL_LOCK(sc); 799 m0 = NULL; 800 801 /* Wait for it to propagate through the chip */ 802 803 DELAY(100000); 804 for (i = 0; i < RL_TIMEOUT; i++) { 805 status = CSR_READ_2(sc, RL_ISR); 806 CSR_WRITE_2(sc, RL_ISR, status); 807 if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) == 808 (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) 809 break; 810 DELAY(10); 811 } 812 813 if (i == RL_TIMEOUT) { 814 device_printf(sc->rl_dev, 815 "diagnostic failed, failed to receive packet in" 816 " loopback mode\n"); 817 error = EIO; 818 goto done; 819 } 820 821 /* 822 * The packet should have been dumped into the first 823 * entry in the RX DMA ring. Grab it from there. 824 */ 825 826 bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 827 sc->rl_ldata.rl_rx_list_map, 828 BUS_DMASYNC_POSTREAD); 829 bus_dmamap_sync(sc->rl_ldata.rl_mtag, 830 sc->rl_ldata.rl_rx_dmamap[0], 831 BUS_DMASYNC_POSTWRITE); 832 bus_dmamap_unload(sc->rl_ldata.rl_mtag, 833 sc->rl_ldata.rl_rx_dmamap[0]); 834 835 m0 = sc->rl_ldata.rl_rx_mbuf[0]; 836 sc->rl_ldata.rl_rx_mbuf[0] = NULL; 837 eh = mtod(m0, struct ether_header *); 838 839 cur_rx = &sc->rl_ldata.rl_rx_list[0]; 840 total_len = RL_RXBYTES(cur_rx); 841 rxstat = le32toh(cur_rx->rl_cmdstat); 842 843 if (total_len != ETHER_MIN_LEN) { 844 device_printf(sc->rl_dev, 845 "diagnostic failed, received short packet\n"); 846 error = EIO; 847 goto done; 848 } 849 850 /* Test that the received packet data matches what we sent. */ 851 852 if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) || 853 bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) || 854 ntohs(eh->ether_type) != ETHERTYPE_IP) { 855 device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n"); 856 device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n", 857 dst, ":", src, ":", ETHERTYPE_IP); 858 device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n", 859 eh->ether_dhost, ":", eh->ether_shost, ":", 860 ntohs(eh->ether_type)); 861 device_printf(sc->rl_dev, "You may have a defective 32-bit " 862 "NIC plugged into a 64-bit PCI slot.\n"); 863 device_printf(sc->rl_dev, "Please re-install the NIC in a " 864 "32-bit slot for proper operation.\n"); 865 device_printf(sc->rl_dev, "Read the re(4) man page for more " 866 "details.\n"); 867 error = EIO; 868 } 869 870 done: 871 /* Turn interface off, release resources */ 872 873 sc->rl_testmode = 0; 874 sc->rl_link = 0; 875 ifp->if_flags &= ~IFF_PROMISC; 876 re_stop(sc); 877 if (m0 != NULL) 878 m_freem(m0); 879 880 RL_UNLOCK(sc); 881 882 return (error); 883 } 884 885 #endif 886 887 /* 888 * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device 889 * IDs against our list and return a device name if we find a match. 890 */ 891 static int 892 re_probe(dev) 893 device_t dev; 894 { 895 struct rl_type *t; 896 struct rl_softc *sc; 897 int rid; 898 u_int32_t hwrev; 899 900 t = re_devs; 901 sc = device_get_softc(dev); 902 903 while (t->rl_name != NULL) { 904 if ((pci_get_vendor(dev) == t->rl_vid) && 905 (pci_get_device(dev) == t->rl_did)) { 906 /* 907 * Only attach to rev. 3 of the Linksys EG1032 adapter. 908 * Rev. 2 i supported by sk(4). 909 */ 910 if ((t->rl_vid == LINKSYS_VENDORID) && 911 (t->rl_did == LINKSYS_DEVICEID_EG1032) && 912 (pci_get_subdevice(dev) != 913 LINKSYS_SUBDEVICE_EG1032_REV3)) { 914 t++; 915 continue; 916 } 917 918 /* 919 * Temporarily map the I/O space 920 * so we can read the chip ID register. 921 */ 922 rid = RL_RID; 923 sc->rl_res = bus_alloc_resource_any(dev, RL_RES, &rid, 924 RF_ACTIVE); 925 if (sc->rl_res == NULL) { 926 device_printf(dev, 927 "couldn't map ports/memory\n"); 928 return (ENXIO); 929 } 930 sc->rl_btag = rman_get_bustag(sc->rl_res); 931 sc->rl_bhandle = rman_get_bushandle(sc->rl_res); 932 hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV; 933 bus_release_resource(dev, RL_RES, 934 RL_RID, sc->rl_res); 935 if (t->rl_basetype == hwrev) { 936 device_set_desc(dev, t->rl_name); 937 return (BUS_PROBE_DEFAULT); 938 } 939 } 940 t++; 941 } 942 943 return (ENXIO); 944 } 945 946 /* 947 * This routine takes the segment list provided as the result of 948 * a bus_dma_map_load() operation and assigns the addresses/lengths 949 * to RealTek DMA descriptors. This can be called either by the RX 950 * code or the TX code. In the RX case, we'll probably wind up mapping 951 * at most one segment. For the TX case, there could be any number of 952 * segments since TX packets may span multiple mbufs. In either case, 953 * if the number of segments is larger than the rl_maxsegs limit 954 * specified by the caller, we abort the mapping operation. Sadly, 955 * whoever designed the buffer mapping API did not provide a way to 956 * return an error from here, so we have to fake it a bit. 957 */ 958 959 static void 960 re_dma_map_desc(arg, segs, nseg, mapsize, error) 961 void *arg; 962 bus_dma_segment_t *segs; 963 int nseg; 964 bus_size_t mapsize; 965 int error; 966 { 967 struct rl_dmaload_arg *ctx; 968 struct rl_desc *d = NULL; 969 int i = 0, idx; 970 u_int32_t cmdstat; 971 int totlen = 0; 972 973 if (error) 974 return; 975 976 ctx = arg; 977 978 /* Signal error to caller if there's too many segments */ 979 if (nseg > ctx->rl_maxsegs) { 980 ctx->rl_maxsegs = 0; 981 return; 982 } 983 984 /* 985 * Map the segment array into descriptors. Note that we set the 986 * start-of-frame and end-of-frame markers for either TX or RX, but 987 * they really only have meaning in the TX case. (In the RX case, 988 * it's the chip that tells us where packets begin and end.) 989 * We also keep track of the end of the ring and set the 990 * end-of-ring bits as needed, and we set the ownership bits 991 * in all except the very first descriptor. (The caller will 992 * set this descriptor later when it start transmission or 993 * reception.) 994 */ 995 idx = ctx->rl_idx; 996 for (;;) { 997 d = &ctx->rl_ring[idx]; 998 if (le32toh(d->rl_cmdstat) & RL_RDESC_STAT_OWN) { 999 ctx->rl_maxsegs = 0; 1000 return; 1001 } 1002 cmdstat = segs[i].ds_len; 1003 totlen += segs[i].ds_len; 1004 d->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr)); 1005 d->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr)); 1006 if (i == 0) 1007 cmdstat |= RL_TDESC_CMD_SOF; 1008 else 1009 cmdstat |= RL_TDESC_CMD_OWN; 1010 if (idx == (RL_RX_DESC_CNT - 1)) 1011 cmdstat |= RL_TDESC_CMD_EOR; 1012 d->rl_cmdstat = htole32(cmdstat | ctx->rl_flags); 1013 i++; 1014 if (i == nseg) 1015 break; 1016 RL_DESC_INC(idx); 1017 } 1018 1019 d->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF); 1020 ctx->rl_maxsegs = nseg; 1021 ctx->rl_idx = idx; 1022 } 1023 1024 /* 1025 * Map a single buffer address. 1026 */ 1027 1028 static void 1029 re_dma_map_addr(arg, segs, nseg, error) 1030 void *arg; 1031 bus_dma_segment_t *segs; 1032 int nseg; 1033 int error; 1034 { 1035 bus_addr_t *addr; 1036 1037 if (error) 1038 return; 1039 1040 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 1041 addr = arg; 1042 *addr = segs->ds_addr; 1043 } 1044 1045 static int 1046 re_allocmem(dev, sc) 1047 device_t dev; 1048 struct rl_softc *sc; 1049 { 1050 int error; 1051 int nseg; 1052 int i; 1053 1054 /* 1055 * Allocate map for RX mbufs. 1056 */ 1057 nseg = 32; 1058 error = bus_dma_tag_create(sc->rl_parent_tag, ETHER_ALIGN, 0, 1059 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 1060 NULL, MCLBYTES * nseg, nseg, MCLBYTES, BUS_DMA_ALLOCNOW, 1061 NULL, NULL, &sc->rl_ldata.rl_mtag); 1062 if (error) { 1063 device_printf(dev, "could not allocate dma tag\n"); 1064 return (ENOMEM); 1065 } 1066 1067 /* 1068 * Allocate map for TX descriptor list. 1069 */ 1070 error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN, 1071 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 1072 NULL, RL_TX_LIST_SZ, 1, RL_TX_LIST_SZ, 0, 1073 NULL, NULL, &sc->rl_ldata.rl_tx_list_tag); 1074 if (error) { 1075 device_printf(dev, "could not allocate dma tag\n"); 1076 return (ENOMEM); 1077 } 1078 1079 /* Allocate DMA'able memory for the TX ring */ 1080 1081 error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag, 1082 (void **)&sc->rl_ldata.rl_tx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO, 1083 &sc->rl_ldata.rl_tx_list_map); 1084 if (error) 1085 return (ENOMEM); 1086 1087 /* Load the map for the TX ring. */ 1088 1089 error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag, 1090 sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list, 1091 RL_TX_LIST_SZ, re_dma_map_addr, 1092 &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT); 1093 1094 /* Create DMA maps for TX buffers */ 1095 1096 for (i = 0; i < RL_TX_DESC_CNT; i++) { 1097 error = bus_dmamap_create(sc->rl_ldata.rl_mtag, 0, 1098 &sc->rl_ldata.rl_tx_dmamap[i]); 1099 if (error) { 1100 device_printf(dev, "can't create DMA map for TX\n"); 1101 return (ENOMEM); 1102 } 1103 } 1104 1105 /* 1106 * Allocate map for RX descriptor list. 1107 */ 1108 error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN, 1109 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, 1110 NULL, RL_RX_LIST_SZ, 1, RL_RX_LIST_SZ, 0, 1111 NULL, NULL, &sc->rl_ldata.rl_rx_list_tag); 1112 if (error) { 1113 device_printf(dev, "could not allocate dma tag\n"); 1114 return (ENOMEM); 1115 } 1116 1117 /* Allocate DMA'able memory for the RX ring */ 1118 1119 error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag, 1120 (void **)&sc->rl_ldata.rl_rx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO, 1121 &sc->rl_ldata.rl_rx_list_map); 1122 if (error) 1123 return (ENOMEM); 1124 1125 /* Load the map for the RX ring. */ 1126 1127 error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag, 1128 sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list, 1129 RL_RX_LIST_SZ, re_dma_map_addr, 1130 &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT); 1131 1132 /* Create DMA maps for RX buffers */ 1133 1134 for (i = 0; i < RL_RX_DESC_CNT; i++) { 1135 error = bus_dmamap_create(sc->rl_ldata.rl_mtag, 0, 1136 &sc->rl_ldata.rl_rx_dmamap[i]); 1137 if (error) { 1138 device_printf(dev, "can't create DMA map for RX\n"); 1139 return (ENOMEM); 1140 } 1141 } 1142 1143 return (0); 1144 } 1145 1146 /* 1147 * Attach the interface. Allocate softc structures, do ifmedia 1148 * setup and ethernet/BPF attach. 1149 */ 1150 static int 1151 re_attach(dev) 1152 device_t dev; 1153 { 1154 u_char eaddr[ETHER_ADDR_LEN]; 1155 u_int16_t as[ETHER_ADDR_LEN / 2]; 1156 struct rl_softc *sc; 1157 struct ifnet *ifp; 1158 struct rl_hwrev *hw_rev; 1159 int hwrev; 1160 u_int16_t re_did = 0; 1161 int error = 0, rid, i; 1162 1163 sc = device_get_softc(dev); 1164 sc->rl_dev = dev; 1165 1166 mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 1167 MTX_DEF); 1168 callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0); 1169 1170 /* 1171 * Map control/status registers. 1172 */ 1173 pci_enable_busmaster(dev); 1174 1175 rid = RL_RID; 1176 sc->rl_res = bus_alloc_resource_any(dev, RL_RES, &rid, 1177 RF_ACTIVE); 1178 1179 if (sc->rl_res == NULL) { 1180 device_printf(dev, "couldn't map ports/memory\n"); 1181 error = ENXIO; 1182 goto fail; 1183 } 1184 1185 sc->rl_btag = rman_get_bustag(sc->rl_res); 1186 sc->rl_bhandle = rman_get_bushandle(sc->rl_res); 1187 1188 /* Allocate interrupt */ 1189 rid = 0; 1190 sc->rl_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 1191 RF_SHAREABLE | RF_ACTIVE); 1192 1193 if (sc->rl_irq == NULL) { 1194 device_printf(dev, "couldn't map interrupt\n"); 1195 error = ENXIO; 1196 goto fail; 1197 } 1198 1199 /* Reset the adapter. */ 1200 RL_LOCK(sc); 1201 re_reset(sc); 1202 RL_UNLOCK(sc); 1203 1204 hw_rev = re_hwrevs; 1205 hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV; 1206 while (hw_rev->rl_desc != NULL) { 1207 if (hw_rev->rl_rev == hwrev) { 1208 sc->rl_type = hw_rev->rl_type; 1209 break; 1210 } 1211 hw_rev++; 1212 } 1213 1214 sc->rl_eewidth = 6; 1215 re_read_eeprom(sc, (caddr_t)&re_did, 0, 1); 1216 if (re_did != 0x8129) 1217 sc->rl_eewidth = 8; 1218 1219 /* 1220 * Get station address from the EEPROM. 1221 */ 1222 re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3); 1223 for (i = 0; i < ETHER_ADDR_LEN / 2; i++) 1224 as[i] = le16toh(as[i]); 1225 bcopy(as, eaddr, sizeof(eaddr)); 1226 1227 if (sc->rl_type == RL_8169) { 1228 /* Set RX length mask */ 1229 sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN; 1230 sc->rl_txstart = RL_GTXSTART; 1231 } else { 1232 /* Set RX length mask */ 1233 sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN; 1234 sc->rl_txstart = RL_TXSTART; 1235 } 1236 1237 /* 1238 * Allocate the parent bus DMA tag appropriate for PCI. 1239 */ 1240 #define RL_NSEG_NEW 32 1241 error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0, 1242 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 1243 MAXBSIZE, RL_NSEG_NEW, BUS_SPACE_MAXSIZE_32BIT, 0, 1244 NULL, NULL, &sc->rl_parent_tag); 1245 if (error) 1246 goto fail; 1247 1248 error = re_allocmem(dev, sc); 1249 1250 if (error) 1251 goto fail; 1252 1253 ifp = sc->rl_ifp = if_alloc(IFT_ETHER); 1254 if (ifp == NULL) { 1255 device_printf(dev, "can not if_alloc()\n"); 1256 error = ENOSPC; 1257 goto fail; 1258 } 1259 1260 /* Do MII setup */ 1261 if (mii_phy_probe(dev, &sc->rl_miibus, 1262 re_ifmedia_upd, re_ifmedia_sts)) { 1263 device_printf(dev, "MII without any phy!\n"); 1264 error = ENXIO; 1265 goto fail; 1266 } 1267 1268 ifp->if_softc = sc; 1269 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 1270 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1271 ifp->if_ioctl = re_ioctl; 1272 ifp->if_start = re_start; 1273 ifp->if_hwassist = RE_CSUM_FEATURES | CSUM_TSO; 1274 ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_TSO4; 1275 ifp->if_capenable = ifp->if_capabilities; 1276 ifp->if_init = re_init; 1277 IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN); 1278 ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN; 1279 IFQ_SET_READY(&ifp->if_snd); 1280 1281 TASK_INIT(&sc->rl_txtask, 1, re_tx_task, ifp); 1282 TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc); 1283 1284 /* 1285 * Call MI attach routine. 1286 */ 1287 ether_ifattach(ifp, eaddr); 1288 1289 /* VLAN capability setup */ 1290 ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING; 1291 if (ifp->if_capabilities & IFCAP_HWCSUM) 1292 ifp->if_capabilities |= IFCAP_VLAN_HWCSUM; 1293 ifp->if_capenable = ifp->if_capabilities; 1294 #ifdef DEVICE_POLLING 1295 ifp->if_capabilities |= IFCAP_POLLING; 1296 #endif 1297 /* 1298 * Tell the upper layer(s) we support long frames. 1299 * Must appear after the call to ether_ifattach() because 1300 * ether_ifattach() sets ifi_hdrlen to the default value. 1301 */ 1302 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header); 1303 1304 #ifdef RE_DIAG 1305 /* 1306 * Perform hardware diagnostic on the original RTL8169. 1307 * Some 32-bit cards were incorrectly wired and would 1308 * malfunction if plugged into a 64-bit slot. 1309 */ 1310 1311 if (hwrev == RL_HWREV_8169) { 1312 error = re_diag(sc); 1313 if (error) { 1314 device_printf(dev, 1315 "attach aborted due to hardware diag failure\n"); 1316 ether_ifdetach(ifp); 1317 goto fail; 1318 } 1319 } 1320 #endif 1321 1322 /* Hook interrupt last to avoid having to lock softc */ 1323 error = bus_setup_intr(dev, sc->rl_irq, INTR_TYPE_NET | INTR_MPSAFE, 1324 re_intr, NULL, sc, &sc->rl_intrhand); 1325 if (error) { 1326 device_printf(dev, "couldn't set up irq\n"); 1327 ether_ifdetach(ifp); 1328 } 1329 1330 fail: 1331 1332 if (error) 1333 re_detach(dev); 1334 1335 return (error); 1336 } 1337 1338 /* 1339 * Shutdown hardware and free up resources. This can be called any 1340 * time after the mutex has been initialized. It is called in both 1341 * the error case in attach and the normal detach case so it needs 1342 * to be careful about only freeing resources that have actually been 1343 * allocated. 1344 */ 1345 static int 1346 re_detach(dev) 1347 device_t dev; 1348 { 1349 struct rl_softc *sc; 1350 struct ifnet *ifp; 1351 int i; 1352 1353 sc = device_get_softc(dev); 1354 ifp = sc->rl_ifp; 1355 KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized")); 1356 1357 #ifdef DEVICE_POLLING 1358 if (ifp->if_capenable & IFCAP_POLLING) 1359 ether_poll_deregister(ifp); 1360 #endif 1361 /* These should only be active if attach succeeded */ 1362 if (device_is_attached(dev)) { 1363 RL_LOCK(sc); 1364 #if 0 1365 sc->suspended = 1; 1366 #endif 1367 re_stop(sc); 1368 RL_UNLOCK(sc); 1369 callout_drain(&sc->rl_stat_callout); 1370 taskqueue_drain(taskqueue_fast, &sc->rl_inttask); 1371 taskqueue_drain(taskqueue_fast, &sc->rl_txtask); 1372 /* 1373 * Force off the IFF_UP flag here, in case someone 1374 * still had a BPF descriptor attached to this 1375 * interface. If they do, ether_ifdetach() will cause 1376 * the BPF code to try and clear the promisc mode 1377 * flag, which will bubble down to re_ioctl(), 1378 * which will try to call re_init() again. This will 1379 * turn the NIC back on and restart the MII ticker, 1380 * which will panic the system when the kernel tries 1381 * to invoke the re_tick() function that isn't there 1382 * anymore. 1383 */ 1384 ifp->if_flags &= ~IFF_UP; 1385 ether_ifdetach(ifp); 1386 } 1387 if (sc->rl_miibus) 1388 device_delete_child(dev, sc->rl_miibus); 1389 bus_generic_detach(dev); 1390 1391 /* 1392 * The rest is resource deallocation, so we should already be 1393 * stopped here. 1394 */ 1395 1396 if (sc->rl_intrhand) 1397 bus_teardown_intr(dev, sc->rl_irq, sc->rl_intrhand); 1398 if (ifp != NULL) 1399 if_free(ifp); 1400 if (sc->rl_irq) 1401 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->rl_irq); 1402 if (sc->rl_res) 1403 bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res); 1404 1405 /* Unload and free the RX DMA ring memory and map */ 1406 1407 if (sc->rl_ldata.rl_rx_list_tag) { 1408 bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag, 1409 sc->rl_ldata.rl_rx_list_map); 1410 bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag, 1411 sc->rl_ldata.rl_rx_list, 1412 sc->rl_ldata.rl_rx_list_map); 1413 bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag); 1414 } 1415 1416 /* Unload and free the TX DMA ring memory and map */ 1417 1418 if (sc->rl_ldata.rl_tx_list_tag) { 1419 bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag, 1420 sc->rl_ldata.rl_tx_list_map); 1421 bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag, 1422 sc->rl_ldata.rl_tx_list, 1423 sc->rl_ldata.rl_tx_list_map); 1424 bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag); 1425 } 1426 1427 /* Destroy all the RX and TX buffer maps */ 1428 1429 if (sc->rl_ldata.rl_mtag) { 1430 for (i = 0; i < RL_TX_DESC_CNT; i++) 1431 bus_dmamap_destroy(sc->rl_ldata.rl_mtag, 1432 sc->rl_ldata.rl_tx_dmamap[i]); 1433 for (i = 0; i < RL_RX_DESC_CNT; i++) 1434 bus_dmamap_destroy(sc->rl_ldata.rl_mtag, 1435 sc->rl_ldata.rl_rx_dmamap[i]); 1436 bus_dma_tag_destroy(sc->rl_ldata.rl_mtag); 1437 } 1438 1439 /* Unload and free the stats buffer and map */ 1440 1441 if (sc->rl_ldata.rl_stag) { 1442 bus_dmamap_unload(sc->rl_ldata.rl_stag, 1443 sc->rl_ldata.rl_rx_list_map); 1444 bus_dmamem_free(sc->rl_ldata.rl_stag, 1445 sc->rl_ldata.rl_stats, 1446 sc->rl_ldata.rl_smap); 1447 bus_dma_tag_destroy(sc->rl_ldata.rl_stag); 1448 } 1449 1450 if (sc->rl_parent_tag) 1451 bus_dma_tag_destroy(sc->rl_parent_tag); 1452 1453 mtx_destroy(&sc->rl_mtx); 1454 1455 return (0); 1456 } 1457 1458 static int 1459 re_newbuf(sc, idx, m) 1460 struct rl_softc *sc; 1461 int idx; 1462 struct mbuf *m; 1463 { 1464 struct rl_dmaload_arg arg; 1465 struct mbuf *n = NULL; 1466 int error; 1467 1468 if (m == NULL) { 1469 n = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1470 if (n == NULL) 1471 return (ENOBUFS); 1472 m = n; 1473 } else 1474 m->m_data = m->m_ext.ext_buf; 1475 1476 m->m_len = m->m_pkthdr.len = MCLBYTES; 1477 #ifdef RE_FIXUP_RX 1478 /* 1479 * This is part of an evil trick to deal with non-x86 platforms. 1480 * The RealTek chip requires RX buffers to be aligned on 64-bit 1481 * boundaries, but that will hose non-x86 machines. To get around 1482 * this, we leave some empty space at the start of each buffer 1483 * and for non-x86 hosts, we copy the buffer back six bytes 1484 * to achieve word alignment. This is slightly more efficient 1485 * than allocating a new buffer, copying the contents, and 1486 * discarding the old buffer. 1487 */ 1488 m_adj(m, RE_ETHER_ALIGN); 1489 #endif 1490 arg.rl_idx = idx; 1491 arg.rl_maxsegs = 1; 1492 arg.rl_flags = 0; 1493 arg.rl_ring = sc->rl_ldata.rl_rx_list; 1494 1495 error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, 1496 sc->rl_ldata.rl_rx_dmamap[idx], m, re_dma_map_desc, 1497 &arg, BUS_DMA_NOWAIT); 1498 if (error || arg.rl_maxsegs != 1) { 1499 if (n != NULL) 1500 m_freem(n); 1501 if (arg.rl_maxsegs == 0) 1502 bus_dmamap_unload(sc->rl_ldata.rl_mtag, 1503 sc->rl_ldata.rl_rx_dmamap[idx]); 1504 return (ENOMEM); 1505 } 1506 1507 sc->rl_ldata.rl_rx_list[idx].rl_cmdstat |= htole32(RL_RDESC_CMD_OWN); 1508 sc->rl_ldata.rl_rx_mbuf[idx] = m; 1509 1510 bus_dmamap_sync(sc->rl_ldata.rl_mtag, 1511 sc->rl_ldata.rl_rx_dmamap[idx], 1512 BUS_DMASYNC_PREREAD); 1513 1514 return (0); 1515 } 1516 1517 #ifdef RE_FIXUP_RX 1518 static __inline void 1519 re_fixup_rx(m) 1520 struct mbuf *m; 1521 { 1522 int i; 1523 uint16_t *src, *dst; 1524 1525 src = mtod(m, uint16_t *); 1526 dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src; 1527 1528 for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++) 1529 *dst++ = *src++; 1530 1531 m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN; 1532 1533 return; 1534 } 1535 #endif 1536 1537 static int 1538 re_tx_list_init(sc) 1539 struct rl_softc *sc; 1540 { 1541 1542 RL_LOCK_ASSERT(sc); 1543 1544 bzero ((char *)sc->rl_ldata.rl_tx_list, RL_TX_LIST_SZ); 1545 bzero ((char *)&sc->rl_ldata.rl_tx_mbuf, 1546 (RL_TX_DESC_CNT * sizeof(struct mbuf *))); 1547 1548 bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 1549 sc->rl_ldata.rl_tx_list_map, BUS_DMASYNC_PREWRITE); 1550 sc->rl_ldata.rl_tx_prodidx = 0; 1551 sc->rl_ldata.rl_tx_considx = 0; 1552 sc->rl_ldata.rl_tx_free = RL_TX_DESC_CNT; 1553 1554 return (0); 1555 } 1556 1557 static int 1558 re_rx_list_init(sc) 1559 struct rl_softc *sc; 1560 { 1561 int i; 1562 1563 bzero ((char *)sc->rl_ldata.rl_rx_list, RL_RX_LIST_SZ); 1564 bzero ((char *)&sc->rl_ldata.rl_rx_mbuf, 1565 (RL_RX_DESC_CNT * sizeof(struct mbuf *))); 1566 1567 for (i = 0; i < RL_RX_DESC_CNT; i++) { 1568 if (re_newbuf(sc, i, NULL) == ENOBUFS) 1569 return (ENOBUFS); 1570 } 1571 1572 /* Flush the RX descriptors */ 1573 1574 bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 1575 sc->rl_ldata.rl_rx_list_map, 1576 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 1577 1578 sc->rl_ldata.rl_rx_prodidx = 0; 1579 sc->rl_head = sc->rl_tail = NULL; 1580 1581 return (0); 1582 } 1583 1584 /* 1585 * RX handler for C+ and 8169. For the gigE chips, we support 1586 * the reception of jumbo frames that have been fragmented 1587 * across multiple 2K mbuf cluster buffers. 1588 */ 1589 static int 1590 re_rxeof(sc) 1591 struct rl_softc *sc; 1592 { 1593 struct mbuf *m; 1594 struct ifnet *ifp; 1595 int i, total_len; 1596 struct rl_desc *cur_rx; 1597 u_int32_t rxstat, rxvlan; 1598 int maxpkt = 16; 1599 1600 RL_LOCK_ASSERT(sc); 1601 1602 ifp = sc->rl_ifp; 1603 i = sc->rl_ldata.rl_rx_prodidx; 1604 1605 /* Invalidate the descriptor memory */ 1606 1607 bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 1608 sc->rl_ldata.rl_rx_list_map, 1609 BUS_DMASYNC_POSTREAD); 1610 1611 while (!RL_OWN(&sc->rl_ldata.rl_rx_list[i]) && maxpkt) { 1612 cur_rx = &sc->rl_ldata.rl_rx_list[i]; 1613 m = sc->rl_ldata.rl_rx_mbuf[i]; 1614 total_len = RL_RXBYTES(cur_rx); 1615 rxstat = le32toh(cur_rx->rl_cmdstat); 1616 rxvlan = le32toh(cur_rx->rl_vlanctl); 1617 1618 /* Invalidate the RX mbuf and unload its map */ 1619 1620 bus_dmamap_sync(sc->rl_ldata.rl_mtag, 1621 sc->rl_ldata.rl_rx_dmamap[i], 1622 BUS_DMASYNC_POSTWRITE); 1623 bus_dmamap_unload(sc->rl_ldata.rl_mtag, 1624 sc->rl_ldata.rl_rx_dmamap[i]); 1625 1626 if (!(rxstat & RL_RDESC_STAT_EOF)) { 1627 m->m_len = RE_RX_DESC_BUFLEN; 1628 if (sc->rl_head == NULL) 1629 sc->rl_head = sc->rl_tail = m; 1630 else { 1631 m->m_flags &= ~M_PKTHDR; 1632 sc->rl_tail->m_next = m; 1633 sc->rl_tail = m; 1634 } 1635 re_newbuf(sc, i, NULL); 1636 RL_DESC_INC(i); 1637 continue; 1638 } 1639 1640 /* 1641 * NOTE: for the 8139C+, the frame length field 1642 * is always 12 bits in size, but for the gigE chips, 1643 * it is 13 bits (since the max RX frame length is 16K). 1644 * Unfortunately, all 32 bits in the status word 1645 * were already used, so to make room for the extra 1646 * length bit, RealTek took out the 'frame alignment 1647 * error' bit and shifted the other status bits 1648 * over one slot. The OWN, EOR, FS and LS bits are 1649 * still in the same places. We have already extracted 1650 * the frame length and checked the OWN bit, so rather 1651 * than using an alternate bit mapping, we shift the 1652 * status bits one space to the right so we can evaluate 1653 * them using the 8169 status as though it was in the 1654 * same format as that of the 8139C+. 1655 */ 1656 if (sc->rl_type == RL_8169) 1657 rxstat >>= 1; 1658 1659 /* 1660 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be 1661 * set, but if CRC is clear, it will still be a valid frame. 1662 */ 1663 if (rxstat & RL_RDESC_STAT_RXERRSUM && !(total_len > 8191 && 1664 (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)) { 1665 ifp->if_ierrors++; 1666 /* 1667 * If this is part of a multi-fragment packet, 1668 * discard all the pieces. 1669 */ 1670 if (sc->rl_head != NULL) { 1671 m_freem(sc->rl_head); 1672 sc->rl_head = sc->rl_tail = NULL; 1673 } 1674 re_newbuf(sc, i, m); 1675 RL_DESC_INC(i); 1676 continue; 1677 } 1678 1679 /* 1680 * If allocating a replacement mbuf fails, 1681 * reload the current one. 1682 */ 1683 1684 if (re_newbuf(sc, i, NULL)) { 1685 ifp->if_ierrors++; 1686 if (sc->rl_head != NULL) { 1687 m_freem(sc->rl_head); 1688 sc->rl_head = sc->rl_tail = NULL; 1689 } 1690 re_newbuf(sc, i, m); 1691 RL_DESC_INC(i); 1692 continue; 1693 } 1694 1695 RL_DESC_INC(i); 1696 1697 if (sc->rl_head != NULL) { 1698 m->m_len = total_len % RE_RX_DESC_BUFLEN; 1699 if (m->m_len == 0) 1700 m->m_len = RE_RX_DESC_BUFLEN; 1701 /* 1702 * Special case: if there's 4 bytes or less 1703 * in this buffer, the mbuf can be discarded: 1704 * the last 4 bytes is the CRC, which we don't 1705 * care about anyway. 1706 */ 1707 if (m->m_len <= ETHER_CRC_LEN) { 1708 sc->rl_tail->m_len -= 1709 (ETHER_CRC_LEN - m->m_len); 1710 m_freem(m); 1711 } else { 1712 m->m_len -= ETHER_CRC_LEN; 1713 m->m_flags &= ~M_PKTHDR; 1714 sc->rl_tail->m_next = m; 1715 } 1716 m = sc->rl_head; 1717 sc->rl_head = sc->rl_tail = NULL; 1718 m->m_pkthdr.len = total_len - ETHER_CRC_LEN; 1719 } else 1720 m->m_pkthdr.len = m->m_len = 1721 (total_len - ETHER_CRC_LEN); 1722 1723 #ifdef RE_FIXUP_RX 1724 re_fixup_rx(m); 1725 #endif 1726 ifp->if_ipackets++; 1727 m->m_pkthdr.rcvif = ifp; 1728 1729 /* Do RX checksumming if enabled */ 1730 1731 if (ifp->if_capenable & IFCAP_RXCSUM) { 1732 1733 /* Check IP header checksum */ 1734 if (rxstat & RL_RDESC_STAT_PROTOID) 1735 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED; 1736 if (!(rxstat & RL_RDESC_STAT_IPSUMBAD)) 1737 m->m_pkthdr.csum_flags |= CSUM_IP_VALID; 1738 1739 /* Check TCP/UDP checksum */ 1740 if ((RL_TCPPKT(rxstat) && 1741 !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) || 1742 (RL_UDPPKT(rxstat) && 1743 !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) { 1744 m->m_pkthdr.csum_flags |= 1745 CSUM_DATA_VALID|CSUM_PSEUDO_HDR; 1746 m->m_pkthdr.csum_data = 0xffff; 1747 } 1748 } 1749 maxpkt--; 1750 if (rxvlan & RL_RDESC_VLANCTL_TAG) { 1751 m->m_pkthdr.ether_vtag = 1752 ntohs((rxvlan & RL_RDESC_VLANCTL_DATA)); 1753 m->m_flags |= M_VLANTAG; 1754 } 1755 RL_UNLOCK(sc); 1756 (*ifp->if_input)(ifp, m); 1757 RL_LOCK(sc); 1758 } 1759 1760 /* Flush the RX DMA ring */ 1761 1762 bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 1763 sc->rl_ldata.rl_rx_list_map, 1764 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 1765 1766 sc->rl_ldata.rl_rx_prodidx = i; 1767 1768 if (maxpkt) 1769 return(EAGAIN); 1770 1771 return(0); 1772 } 1773 1774 static void 1775 re_txeof(sc) 1776 struct rl_softc *sc; 1777 { 1778 struct ifnet *ifp; 1779 u_int32_t txstat; 1780 int idx; 1781 1782 ifp = sc->rl_ifp; 1783 idx = sc->rl_ldata.rl_tx_considx; 1784 1785 /* Invalidate the TX descriptor list */ 1786 bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 1787 sc->rl_ldata.rl_tx_list_map, 1788 BUS_DMASYNC_POSTREAD); 1789 1790 while (sc->rl_ldata.rl_tx_free < RL_TX_DESC_CNT) { 1791 txstat = le32toh(sc->rl_ldata.rl_tx_list[idx].rl_cmdstat); 1792 if (txstat & RL_TDESC_CMD_OWN) 1793 break; 1794 1795 sc->rl_ldata.rl_tx_list[idx].rl_bufaddr_lo = 0; 1796 1797 /* 1798 * We only stash mbufs in the last descriptor 1799 * in a fragment chain, which also happens to 1800 * be the only place where the TX status bits 1801 * are valid. 1802 */ 1803 if (txstat & RL_TDESC_CMD_EOF) { 1804 m_freem(sc->rl_ldata.rl_tx_mbuf[idx]); 1805 sc->rl_ldata.rl_tx_mbuf[idx] = NULL; 1806 bus_dmamap_unload(sc->rl_ldata.rl_mtag, 1807 sc->rl_ldata.rl_tx_dmamap[idx]); 1808 if (txstat & (RL_TDESC_STAT_EXCESSCOL| 1809 RL_TDESC_STAT_COLCNT)) 1810 ifp->if_collisions++; 1811 if (txstat & RL_TDESC_STAT_TXERRSUM) 1812 ifp->if_oerrors++; 1813 else 1814 ifp->if_opackets++; 1815 } 1816 sc->rl_ldata.rl_tx_free++; 1817 RL_DESC_INC(idx); 1818 } 1819 sc->rl_ldata.rl_tx_considx = idx; 1820 1821 /* No changes made to the TX ring, so no flush needed */ 1822 1823 if (sc->rl_ldata.rl_tx_free > RL_TX_DESC_THLD) 1824 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1825 1826 if (sc->rl_ldata.rl_tx_free < RL_TX_DESC_CNT) { 1827 /* 1828 * Some chips will ignore a second TX request issued 1829 * while an existing transmission is in progress. If 1830 * the transmitter goes idle but there are still 1831 * packets waiting to be sent, we need to restart the 1832 * channel here to flush them out. This only seems to 1833 * be required with the PCIe devices. 1834 */ 1835 CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 1836 1837 #ifdef RE_TX_MODERATION 1838 /* 1839 * If not all descriptors have been reaped yet, reload 1840 * the timer so that we will eventually get another 1841 * interrupt that will cause us to re-enter this routine. 1842 * This is done in case the transmitter has gone idle. 1843 */ 1844 CSR_WRITE_4(sc, RL_TIMERCNT, 1); 1845 #endif 1846 } else 1847 sc->rl_watchdog_timer = 0; 1848 } 1849 1850 static void 1851 re_tick(xsc) 1852 void *xsc; 1853 { 1854 struct rl_softc *sc; 1855 struct mii_data *mii; 1856 struct ifnet *ifp; 1857 1858 sc = xsc; 1859 ifp = sc->rl_ifp; 1860 1861 RL_LOCK_ASSERT(sc); 1862 1863 re_watchdog(sc); 1864 1865 mii = device_get_softc(sc->rl_miibus); 1866 mii_tick(mii); 1867 if (sc->rl_link) { 1868 if (!(mii->mii_media_status & IFM_ACTIVE)) 1869 sc->rl_link = 0; 1870 } else { 1871 if (mii->mii_media_status & IFM_ACTIVE && 1872 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 1873 sc->rl_link = 1; 1874 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1875 taskqueue_enqueue_fast(taskqueue_fast, 1876 &sc->rl_txtask); 1877 } 1878 } 1879 1880 callout_reset(&sc->rl_stat_callout, hz, re_tick, sc); 1881 } 1882 1883 #ifdef DEVICE_POLLING 1884 static void 1885 re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 1886 { 1887 struct rl_softc *sc = ifp->if_softc; 1888 1889 RL_LOCK(sc); 1890 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1891 re_poll_locked(ifp, cmd, count); 1892 RL_UNLOCK(sc); 1893 } 1894 1895 static void 1896 re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count) 1897 { 1898 struct rl_softc *sc = ifp->if_softc; 1899 1900 RL_LOCK_ASSERT(sc); 1901 1902 sc->rxcycles = count; 1903 re_rxeof(sc); 1904 re_txeof(sc); 1905 1906 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1907 taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask); 1908 1909 if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */ 1910 u_int16_t status; 1911 1912 status = CSR_READ_2(sc, RL_ISR); 1913 if (status == 0xffff) 1914 return; 1915 if (status) 1916 CSR_WRITE_2(sc, RL_ISR, status); 1917 1918 /* 1919 * XXX check behaviour on receiver stalls. 1920 */ 1921 1922 if (status & RL_ISR_SYSTEM_ERR) { 1923 re_reset(sc); 1924 re_init_locked(sc); 1925 } 1926 } 1927 } 1928 #endif /* DEVICE_POLLING */ 1929 1930 static int 1931 re_intr(arg) 1932 void *arg; 1933 { 1934 struct rl_softc *sc; 1935 uint16_t status; 1936 1937 sc = arg; 1938 1939 status = CSR_READ_2(sc, RL_ISR); 1940 if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0) 1941 return (FILTER_STRAY); 1942 CSR_WRITE_2(sc, RL_IMR, 0); 1943 1944 taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask); 1945 1946 return (FILTER_HANDLED); 1947 } 1948 1949 static void 1950 re_int_task(arg, npending) 1951 void *arg; 1952 int npending; 1953 { 1954 struct rl_softc *sc; 1955 struct ifnet *ifp; 1956 u_int16_t status; 1957 int rval = 0; 1958 1959 sc = arg; 1960 ifp = sc->rl_ifp; 1961 1962 RL_LOCK(sc); 1963 1964 status = CSR_READ_2(sc, RL_ISR); 1965 CSR_WRITE_2(sc, RL_ISR, status); 1966 1967 if (sc->suspended || !(ifp->if_flags & IFF_UP)) { 1968 RL_UNLOCK(sc); 1969 return; 1970 } 1971 1972 #ifdef DEVICE_POLLING 1973 if (ifp->if_capenable & IFCAP_POLLING) { 1974 RL_UNLOCK(sc); 1975 return; 1976 } 1977 #endif 1978 1979 if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW)) 1980 rval = re_rxeof(sc); 1981 1982 #ifdef RE_TX_MODERATION 1983 if (status & (RL_ISR_TIMEOUT_EXPIRED| 1984 #else 1985 if (status & (RL_ISR_TX_OK| 1986 #endif 1987 RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL)) 1988 re_txeof(sc); 1989 1990 if (status & RL_ISR_SYSTEM_ERR) { 1991 re_reset(sc); 1992 re_init_locked(sc); 1993 } 1994 1995 if (status & RL_ISR_LINKCHG) { 1996 callout_stop(&sc->rl_stat_callout); 1997 re_tick(sc); 1998 } 1999 2000 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2001 taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask); 2002 2003 RL_UNLOCK(sc); 2004 2005 if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) { 2006 taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask); 2007 return; 2008 } 2009 2010 CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS); 2011 2012 return; 2013 } 2014 2015 static int 2016 re_encap(sc, m_head, idx) 2017 struct rl_softc *sc; 2018 struct mbuf **m_head; 2019 int *idx; 2020 { 2021 struct mbuf *m_new = NULL; 2022 struct rl_dmaload_arg arg; 2023 bus_dmamap_t map; 2024 int error; 2025 2026 RL_LOCK_ASSERT(sc); 2027 2028 if (sc->rl_ldata.rl_tx_free <= RL_TX_DESC_THLD) 2029 return (EFBIG); 2030 2031 /* 2032 * Set up checksum offload. Note: checksum offload bits must 2033 * appear in all descriptors of a multi-descriptor transmit 2034 * attempt. This is according to testing done with an 8169 2035 * chip. This is a requirement. 2036 */ 2037 2038 arg.rl_flags = 0; 2039 2040 if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0) 2041 arg.rl_flags = RL_TDESC_CMD_LGSEND | 2042 ((uint32_t)(*m_head)->m_pkthdr.tso_segsz << 2043 RL_TDESC_CMD_MSSVAL_SHIFT); 2044 else { 2045 if ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) 2046 arg.rl_flags |= RL_TDESC_CMD_IPCSUM; 2047 if ((*m_head)->m_pkthdr.csum_flags & CSUM_TCP) 2048 arg.rl_flags |= RL_TDESC_CMD_TCPCSUM; 2049 if ((*m_head)->m_pkthdr.csum_flags & CSUM_UDP) 2050 arg.rl_flags |= RL_TDESC_CMD_UDPCSUM; 2051 } 2052 2053 arg.rl_idx = *idx; 2054 arg.rl_maxsegs = sc->rl_ldata.rl_tx_free; 2055 if (arg.rl_maxsegs > RL_TX_DESC_THLD) 2056 arg.rl_maxsegs -= RL_TX_DESC_THLD; 2057 arg.rl_ring = sc->rl_ldata.rl_tx_list; 2058 2059 map = sc->rl_ldata.rl_tx_dmamap[*idx]; 2060 2061 /* 2062 * With some of the RealTek chips, using the checksum offload 2063 * support in conjunction with the autopadding feature results 2064 * in the transmission of corrupt frames. For example, if we 2065 * need to send a really small IP fragment that's less than 60 2066 * bytes in size, and IP header checksumming is enabled, the 2067 * resulting ethernet frame that appears on the wire will 2068 * have garbled payload. To work around this, if TX checksum 2069 * offload is enabled, we always manually pad short frames out 2070 * to the minimum ethernet frame size. We do this by pretending 2071 * the mbuf chain has too many fragments so the coalescing code 2072 * below can assemble the packet into a single buffer that's 2073 * padded out to the mininum frame size. 2074 * 2075 * Note: this appears unnecessary for TCP, and doing it for TCP 2076 * with PCIe adapters seems to result in bad checksums. 2077 */ 2078 2079 if (arg.rl_flags && !(arg.rl_flags & RL_TDESC_CMD_TCPCSUM) && 2080 (*m_head)->m_pkthdr.len < RL_MIN_FRAMELEN) 2081 error = EFBIG; 2082 else 2083 error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, map, 2084 *m_head, re_dma_map_desc, &arg, BUS_DMA_NOWAIT); 2085 2086 if (error && error != EFBIG) { 2087 device_printf(sc->rl_dev, "can't map mbuf (error %d)\n", error); 2088 return (ENOBUFS); 2089 } 2090 2091 /* Too many segments to map, coalesce into a single mbuf */ 2092 2093 if (error || arg.rl_maxsegs == 0) { 2094 if (arg.rl_maxsegs == 0) 2095 bus_dmamap_unload(sc->rl_ldata.rl_mtag, map); 2096 m_new = m_defrag(*m_head, M_DONTWAIT); 2097 if (m_new == NULL) { 2098 m_freem(*m_head); 2099 *m_head = NULL; 2100 return (ENOBUFS); 2101 } 2102 *m_head = m_new; 2103 2104 /* 2105 * Manually pad short frames, and zero the pad space 2106 * to avoid leaking data. 2107 */ 2108 if (m_new->m_pkthdr.len < RL_MIN_FRAMELEN) { 2109 bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, 2110 RL_MIN_FRAMELEN - m_new->m_pkthdr.len); 2111 m_new->m_pkthdr.len += RL_MIN_FRAMELEN - 2112 m_new->m_pkthdr.len; 2113 m_new->m_len = m_new->m_pkthdr.len; 2114 } 2115 2116 /* Note that we'll run over RL_TX_DESC_THLD here. */ 2117 arg.rl_maxsegs = sc->rl_ldata.rl_tx_free; 2118 error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, map, 2119 *m_head, re_dma_map_desc, &arg, BUS_DMA_NOWAIT); 2120 if (error || arg.rl_maxsegs == 0) { 2121 device_printf(sc->rl_dev, 2122 "can't map defragmented mbuf (error %d)\n", error); 2123 m_freem(m_new); 2124 *m_head = NULL; 2125 if (arg.rl_maxsegs == 0) 2126 bus_dmamap_unload(sc->rl_ldata.rl_mtag, map); 2127 return (EFBIG); 2128 } 2129 } 2130 2131 /* 2132 * Insure that the map for this transmission 2133 * is placed at the array index of the last descriptor 2134 * in this chain. (Swap last and first dmamaps.) 2135 */ 2136 sc->rl_ldata.rl_tx_dmamap[*idx] = 2137 sc->rl_ldata.rl_tx_dmamap[arg.rl_idx]; 2138 sc->rl_ldata.rl_tx_dmamap[arg.rl_idx] = map; 2139 2140 sc->rl_ldata.rl_tx_mbuf[arg.rl_idx] = *m_head; 2141 sc->rl_ldata.rl_tx_free -= arg.rl_maxsegs; 2142 2143 /* 2144 * Set up hardware VLAN tagging. Note: vlan tag info must 2145 * appear in the first descriptor of a multi-descriptor 2146 * transmission attempt. 2147 */ 2148 if ((*m_head)->m_flags & M_VLANTAG) 2149 sc->rl_ldata.rl_tx_list[*idx].rl_vlanctl = 2150 htole32(htons((*m_head)->m_pkthdr.ether_vtag) | 2151 RL_TDESC_VLANCTL_TAG); 2152 2153 /* Transfer ownership of packet to the chip. */ 2154 2155 sc->rl_ldata.rl_tx_list[arg.rl_idx].rl_cmdstat |= 2156 htole32(RL_TDESC_CMD_OWN); 2157 if (*idx != arg.rl_idx) 2158 sc->rl_ldata.rl_tx_list[*idx].rl_cmdstat |= 2159 htole32(RL_TDESC_CMD_OWN); 2160 2161 RL_DESC_INC(arg.rl_idx); 2162 *idx = arg.rl_idx; 2163 2164 return (0); 2165 } 2166 2167 static void 2168 re_tx_task(arg, npending) 2169 void *arg; 2170 int npending; 2171 { 2172 struct ifnet *ifp; 2173 2174 ifp = arg; 2175 re_start(ifp); 2176 2177 return; 2178 } 2179 2180 /* 2181 * Main transmit routine for C+ and gigE NICs. 2182 */ 2183 static void 2184 re_start(ifp) 2185 struct ifnet *ifp; 2186 { 2187 struct rl_softc *sc; 2188 struct mbuf *m_head = NULL; 2189 int idx, queued = 0; 2190 2191 sc = ifp->if_softc; 2192 2193 RL_LOCK(sc); 2194 2195 if (!sc->rl_link || ifp->if_drv_flags & IFF_DRV_OACTIVE) { 2196 RL_UNLOCK(sc); 2197 return; 2198 } 2199 2200 idx = sc->rl_ldata.rl_tx_prodidx; 2201 2202 while (sc->rl_ldata.rl_tx_mbuf[idx] == NULL) { 2203 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head); 2204 if (m_head == NULL) 2205 break; 2206 2207 if (re_encap(sc, &m_head, &idx)) { 2208 if (m_head == NULL) 2209 break; 2210 IFQ_DRV_PREPEND(&ifp->if_snd, m_head); 2211 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2212 break; 2213 } 2214 2215 /* 2216 * If there's a BPF listener, bounce a copy of this frame 2217 * to him. 2218 */ 2219 ETHER_BPF_MTAP(ifp, m_head); 2220 2221 queued++; 2222 } 2223 2224 if (queued == 0) { 2225 #ifdef RE_TX_MODERATION 2226 if (sc->rl_ldata.rl_tx_free != RL_TX_DESC_CNT) 2227 CSR_WRITE_4(sc, RL_TIMERCNT, 1); 2228 #endif 2229 RL_UNLOCK(sc); 2230 return; 2231 } 2232 2233 /* Flush the TX descriptors */ 2234 2235 bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 2236 sc->rl_ldata.rl_tx_list_map, 2237 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 2238 2239 sc->rl_ldata.rl_tx_prodidx = idx; 2240 2241 CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 2242 2243 #ifdef RE_TX_MODERATION 2244 /* 2245 * Use the countdown timer for interrupt moderation. 2246 * 'TX done' interrupts are disabled. Instead, we reset the 2247 * countdown timer, which will begin counting until it hits 2248 * the value in the TIMERINT register, and then trigger an 2249 * interrupt. Each time we write to the TIMERCNT register, 2250 * the timer count is reset to 0. 2251 */ 2252 CSR_WRITE_4(sc, RL_TIMERCNT, 1); 2253 #endif 2254 2255 /* 2256 * Set a timeout in case the chip goes out to lunch. 2257 */ 2258 sc->rl_watchdog_timer = 5; 2259 2260 RL_UNLOCK(sc); 2261 2262 return; 2263 } 2264 2265 static void 2266 re_init(xsc) 2267 void *xsc; 2268 { 2269 struct rl_softc *sc = xsc; 2270 2271 RL_LOCK(sc); 2272 re_init_locked(sc); 2273 RL_UNLOCK(sc); 2274 } 2275 2276 static void 2277 re_init_locked(sc) 2278 struct rl_softc *sc; 2279 { 2280 struct ifnet *ifp = sc->rl_ifp; 2281 struct mii_data *mii; 2282 u_int32_t rxcfg = 0; 2283 union { 2284 uint32_t align_dummy; 2285 u_char eaddr[ETHER_ADDR_LEN]; 2286 } eaddr; 2287 2288 RL_LOCK_ASSERT(sc); 2289 2290 mii = device_get_softc(sc->rl_miibus); 2291 2292 /* 2293 * Cancel pending I/O and free all RX/TX buffers. 2294 */ 2295 re_stop(sc); 2296 2297 /* 2298 * Enable C+ RX and TX mode, as well as VLAN stripping and 2299 * RX checksum offload. We must configure the C+ register 2300 * before all others. 2301 */ 2302 CSR_WRITE_2(sc, RL_CPLUS_CMD, RL_CPLUSCMD_RXENB| 2303 RL_CPLUSCMD_TXENB|RL_CPLUSCMD_PCI_MRW| 2304 RL_CPLUSCMD_VLANSTRIP|RL_CPLUSCMD_RXCSUM_ENB); 2305 2306 /* 2307 * Init our MAC address. Even though the chipset 2308 * documentation doesn't mention it, we need to enter "Config 2309 * register write enable" mode to modify the ID registers. 2310 */ 2311 /* Copy MAC address on stack to align. */ 2312 bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN); 2313 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG); 2314 CSR_WRITE_4(sc, RL_IDR0, 2315 htole32(*(u_int32_t *)(&eaddr.eaddr[0]))); 2316 CSR_WRITE_4(sc, RL_IDR4, 2317 htole32(*(u_int32_t *)(&eaddr.eaddr[4]))); 2318 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 2319 2320 /* 2321 * For C+ mode, initialize the RX descriptors and mbufs. 2322 */ 2323 re_rx_list_init(sc); 2324 re_tx_list_init(sc); 2325 2326 /* 2327 * Load the addresses of the RX and TX lists into the chip. 2328 */ 2329 2330 CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI, 2331 RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr)); 2332 CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO, 2333 RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr)); 2334 2335 CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI, 2336 RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr)); 2337 CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO, 2338 RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr)); 2339 2340 /* 2341 * Enable transmit and receive. 2342 */ 2343 CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB); 2344 2345 /* 2346 * Set the initial TX and RX configuration. 2347 */ 2348 if (sc->rl_testmode) { 2349 if (sc->rl_type == RL_8169) 2350 CSR_WRITE_4(sc, RL_TXCFG, 2351 RL_TXCFG_CONFIG|RL_LOOPTEST_ON); 2352 else 2353 CSR_WRITE_4(sc, RL_TXCFG, 2354 RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS); 2355 } else 2356 CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG); 2357 2358 CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16); 2359 2360 CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG); 2361 2362 /* Set the individual bit to receive frames for this host only. */ 2363 rxcfg = CSR_READ_4(sc, RL_RXCFG); 2364 rxcfg |= RL_RXCFG_RX_INDIV; 2365 2366 /* If we want promiscuous mode, set the allframes bit. */ 2367 if (ifp->if_flags & IFF_PROMISC) 2368 rxcfg |= RL_RXCFG_RX_ALLPHYS; 2369 else 2370 rxcfg &= ~RL_RXCFG_RX_ALLPHYS; 2371 CSR_WRITE_4(sc, RL_RXCFG, rxcfg); 2372 2373 /* 2374 * Set capture broadcast bit to capture broadcast frames. 2375 */ 2376 if (ifp->if_flags & IFF_BROADCAST) 2377 rxcfg |= RL_RXCFG_RX_BROAD; 2378 else 2379 rxcfg &= ~RL_RXCFG_RX_BROAD; 2380 CSR_WRITE_4(sc, RL_RXCFG, rxcfg); 2381 2382 /* 2383 * Program the multicast filter, if necessary. 2384 */ 2385 re_setmulti(sc); 2386 2387 #ifdef DEVICE_POLLING 2388 /* 2389 * Disable interrupts if we are polling. 2390 */ 2391 if (ifp->if_capenable & IFCAP_POLLING) 2392 CSR_WRITE_2(sc, RL_IMR, 0); 2393 else /* otherwise ... */ 2394 #endif 2395 2396 /* 2397 * Enable interrupts. 2398 */ 2399 if (sc->rl_testmode) 2400 CSR_WRITE_2(sc, RL_IMR, 0); 2401 else 2402 CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS); 2403 CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS); 2404 2405 /* Set initial TX threshold */ 2406 sc->rl_txthresh = RL_TX_THRESH_INIT; 2407 2408 /* Start RX/TX process. */ 2409 CSR_WRITE_4(sc, RL_MISSEDPKT, 0); 2410 #ifdef notdef 2411 /* Enable receiver and transmitter. */ 2412 CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB); 2413 #endif 2414 2415 #ifdef RE_TX_MODERATION 2416 /* 2417 * Initialize the timer interrupt register so that 2418 * a timer interrupt will be generated once the timer 2419 * reaches a certain number of ticks. The timer is 2420 * reloaded on each transmit. This gives us TX interrupt 2421 * moderation, which dramatically improves TX frame rate. 2422 */ 2423 if (sc->rl_type == RL_8169) 2424 CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800); 2425 else 2426 CSR_WRITE_4(sc, RL_TIMERINT, 0x400); 2427 #endif 2428 2429 /* 2430 * For 8169 gigE NICs, set the max allowed RX packet 2431 * size so we can receive jumbo frames. 2432 */ 2433 if (sc->rl_type == RL_8169) 2434 CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383); 2435 2436 if (sc->rl_testmode) 2437 return; 2438 2439 mii_mediachg(mii); 2440 2441 CSR_WRITE_1(sc, RL_CFG1, CSR_READ_1(sc, RL_CFG1) | RL_CFG1_DRVLOAD); 2442 2443 ifp->if_drv_flags |= IFF_DRV_RUNNING; 2444 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2445 2446 sc->rl_link = 0; 2447 sc->rl_watchdog_timer = 0; 2448 callout_reset(&sc->rl_stat_callout, hz, re_tick, sc); 2449 } 2450 2451 /* 2452 * Set media options. 2453 */ 2454 static int 2455 re_ifmedia_upd(ifp) 2456 struct ifnet *ifp; 2457 { 2458 struct rl_softc *sc; 2459 struct mii_data *mii; 2460 2461 sc = ifp->if_softc; 2462 mii = device_get_softc(sc->rl_miibus); 2463 RL_LOCK(sc); 2464 mii_mediachg(mii); 2465 RL_UNLOCK(sc); 2466 2467 return (0); 2468 } 2469 2470 /* 2471 * Report current media status. 2472 */ 2473 static void 2474 re_ifmedia_sts(ifp, ifmr) 2475 struct ifnet *ifp; 2476 struct ifmediareq *ifmr; 2477 { 2478 struct rl_softc *sc; 2479 struct mii_data *mii; 2480 2481 sc = ifp->if_softc; 2482 mii = device_get_softc(sc->rl_miibus); 2483 2484 RL_LOCK(sc); 2485 mii_pollstat(mii); 2486 RL_UNLOCK(sc); 2487 ifmr->ifm_active = mii->mii_media_active; 2488 ifmr->ifm_status = mii->mii_media_status; 2489 } 2490 2491 static int 2492 re_ioctl(ifp, command, data) 2493 struct ifnet *ifp; 2494 u_long command; 2495 caddr_t data; 2496 { 2497 struct rl_softc *sc = ifp->if_softc; 2498 struct ifreq *ifr = (struct ifreq *) data; 2499 struct mii_data *mii; 2500 int error = 0; 2501 2502 switch (command) { 2503 case SIOCSIFMTU: 2504 RL_LOCK(sc); 2505 if (ifr->ifr_mtu > RL_JUMBO_MTU) 2506 error = EINVAL; 2507 ifp->if_mtu = ifr->ifr_mtu; 2508 RL_UNLOCK(sc); 2509 break; 2510 case SIOCSIFFLAGS: 2511 RL_LOCK(sc); 2512 if (ifp->if_flags & IFF_UP) 2513 re_init_locked(sc); 2514 else if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2515 re_stop(sc); 2516 RL_UNLOCK(sc); 2517 break; 2518 case SIOCADDMULTI: 2519 case SIOCDELMULTI: 2520 RL_LOCK(sc); 2521 re_setmulti(sc); 2522 RL_UNLOCK(sc); 2523 break; 2524 case SIOCGIFMEDIA: 2525 case SIOCSIFMEDIA: 2526 mii = device_get_softc(sc->rl_miibus); 2527 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 2528 break; 2529 case SIOCSIFCAP: 2530 { 2531 int mask, reinit; 2532 2533 mask = ifr->ifr_reqcap ^ ifp->if_capenable; 2534 reinit = 0; 2535 #ifdef DEVICE_POLLING 2536 if (mask & IFCAP_POLLING) { 2537 if (ifr->ifr_reqcap & IFCAP_POLLING) { 2538 error = ether_poll_register(re_poll, ifp); 2539 if (error) 2540 return(error); 2541 RL_LOCK(sc); 2542 /* Disable interrupts */ 2543 CSR_WRITE_2(sc, RL_IMR, 0x0000); 2544 ifp->if_capenable |= IFCAP_POLLING; 2545 RL_UNLOCK(sc); 2546 } else { 2547 error = ether_poll_deregister(ifp); 2548 /* Enable interrupts. */ 2549 RL_LOCK(sc); 2550 CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS); 2551 ifp->if_capenable &= ~IFCAP_POLLING; 2552 RL_UNLOCK(sc); 2553 } 2554 } 2555 #endif /* DEVICE_POLLING */ 2556 if (mask & IFCAP_HWCSUM) { 2557 ifp->if_capenable ^= IFCAP_HWCSUM; 2558 if (ifp->if_capenable & IFCAP_TXCSUM) 2559 ifp->if_hwassist |= RE_CSUM_FEATURES; 2560 else 2561 ifp->if_hwassist &= ~RE_CSUM_FEATURES; 2562 reinit = 1; 2563 } 2564 if (mask & IFCAP_VLAN_HWTAGGING) { 2565 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING; 2566 reinit = 1; 2567 } 2568 if (mask & IFCAP_TSO4) { 2569 ifp->if_capenable ^= IFCAP_TSO4; 2570 if ((IFCAP_TSO4 & ifp->if_capenable) && 2571 (IFCAP_TSO4 & ifp->if_capabilities)) 2572 ifp->if_hwassist |= CSUM_TSO; 2573 else 2574 ifp->if_hwassist &= ~CSUM_TSO; 2575 } 2576 if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING) 2577 re_init(sc); 2578 VLAN_CAPABILITIES(ifp); 2579 } 2580 break; 2581 default: 2582 error = ether_ioctl(ifp, command, data); 2583 break; 2584 } 2585 2586 return (error); 2587 } 2588 2589 static void 2590 re_watchdog(sc) 2591 struct rl_softc *sc; 2592 { 2593 2594 RL_LOCK_ASSERT(sc); 2595 2596 if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0) 2597 return; 2598 2599 device_printf(sc->rl_dev, "watchdog timeout\n"); 2600 sc->rl_ifp->if_oerrors++; 2601 2602 re_txeof(sc); 2603 re_rxeof(sc); 2604 re_init_locked(sc); 2605 } 2606 2607 /* 2608 * Stop the adapter and free any mbufs allocated to the 2609 * RX and TX lists. 2610 */ 2611 static void 2612 re_stop(sc) 2613 struct rl_softc *sc; 2614 { 2615 register int i; 2616 struct ifnet *ifp; 2617 2618 RL_LOCK_ASSERT(sc); 2619 2620 ifp = sc->rl_ifp; 2621 2622 sc->rl_watchdog_timer = 0; 2623 callout_stop(&sc->rl_stat_callout); 2624 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2625 2626 CSR_WRITE_1(sc, RL_COMMAND, 0x00); 2627 CSR_WRITE_2(sc, RL_IMR, 0x0000); 2628 CSR_WRITE_2(sc, RL_ISR, 0xFFFF); 2629 2630 if (sc->rl_head != NULL) { 2631 m_freem(sc->rl_head); 2632 sc->rl_head = sc->rl_tail = NULL; 2633 } 2634 2635 /* Free the TX list buffers. */ 2636 2637 for (i = 0; i < RL_TX_DESC_CNT; i++) { 2638 if (sc->rl_ldata.rl_tx_mbuf[i] != NULL) { 2639 bus_dmamap_unload(sc->rl_ldata.rl_mtag, 2640 sc->rl_ldata.rl_tx_dmamap[i]); 2641 m_freem(sc->rl_ldata.rl_tx_mbuf[i]); 2642 sc->rl_ldata.rl_tx_mbuf[i] = NULL; 2643 } 2644 } 2645 2646 /* Free the RX list buffers. */ 2647 2648 for (i = 0; i < RL_RX_DESC_CNT; i++) { 2649 if (sc->rl_ldata.rl_rx_mbuf[i] != NULL) { 2650 bus_dmamap_unload(sc->rl_ldata.rl_mtag, 2651 sc->rl_ldata.rl_rx_dmamap[i]); 2652 m_freem(sc->rl_ldata.rl_rx_mbuf[i]); 2653 sc->rl_ldata.rl_rx_mbuf[i] = NULL; 2654 } 2655 } 2656 } 2657 2658 /* 2659 * Device suspend routine. Stop the interface and save some PCI 2660 * settings in case the BIOS doesn't restore them properly on 2661 * resume. 2662 */ 2663 static int 2664 re_suspend(dev) 2665 device_t dev; 2666 { 2667 struct rl_softc *sc; 2668 2669 sc = device_get_softc(dev); 2670 2671 RL_LOCK(sc); 2672 re_stop(sc); 2673 sc->suspended = 1; 2674 RL_UNLOCK(sc); 2675 2676 return (0); 2677 } 2678 2679 /* 2680 * Device resume routine. Restore some PCI settings in case the BIOS 2681 * doesn't, re-enable busmastering, and restart the interface if 2682 * appropriate. 2683 */ 2684 static int 2685 re_resume(dev) 2686 device_t dev; 2687 { 2688 struct rl_softc *sc; 2689 struct ifnet *ifp; 2690 2691 sc = device_get_softc(dev); 2692 2693 RL_LOCK(sc); 2694 2695 ifp = sc->rl_ifp; 2696 2697 /* reinitialize interface if necessary */ 2698 if (ifp->if_flags & IFF_UP) 2699 re_init_locked(sc); 2700 2701 sc->suspended = 0; 2702 RL_UNLOCK(sc); 2703 2704 return (0); 2705 } 2706 2707 /* 2708 * Stop all chip I/O so that the kernel's probe routines don't 2709 * get confused by errant DMAs when rebooting. 2710 */ 2711 static void 2712 re_shutdown(dev) 2713 device_t dev; 2714 { 2715 struct rl_softc *sc; 2716 2717 sc = device_get_softc(dev); 2718 2719 RL_LOCK(sc); 2720 re_stop(sc); 2721 /* 2722 * Mark interface as down since otherwise we will panic if 2723 * interrupt comes in later on, which can happen in some 2724 * cases. 2725 */ 2726 sc->rl_ifp->if_flags &= ~IFF_UP; 2727 RL_UNLOCK(sc); 2728 } 2729