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