if_rl.c revision 117029
1/* 2 * Copyright (c) 1997, 1998 3 * Bill Paul <wpaul@ctr.columbia.edu>. 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/* 34 * RealTek 8129/8139 PCI NIC driver 35 * 36 * Supports several extremely cheap PCI 10/100 adapters based on 37 * the RealTek chipset. Datasheets can be obtained from 38 * www.realtek.com.tw. 39 * 40 * Written by Bill Paul <wpaul@ctr.columbia.edu> 41 * Electrical Engineering Department 42 * Columbia University, New York City 43 */ 44 45/* 46 * The RealTek 8139 PCI NIC redefines the meaning of 'low end.' This is 47 * probably the worst PCI ethernet controller ever made, with the possible 48 * exception of the FEAST chip made by SMC. The 8139 supports bus-master 49 * DMA, but it has a terrible interface that nullifies any performance 50 * gains that bus-master DMA usually offers. 51 * 52 * For transmission, the chip offers a series of four TX descriptor 53 * registers. Each transmit frame must be in a contiguous buffer, aligned 54 * on a longword (32-bit) boundary. This means we almost always have to 55 * do mbuf copies in order to transmit a frame, except in the unlikely 56 * case where a) the packet fits into a single mbuf, and b) the packet 57 * is 32-bit aligned within the mbuf's data area. The presence of only 58 * four descriptor registers means that we can never have more than four 59 * packets queued for transmission at any one time. 60 * 61 * Reception is not much better. The driver has to allocate a single large 62 * buffer area (up to 64K in size) into which the chip will DMA received 63 * frames. Because we don't know where within this region received packets 64 * will begin or end, we have no choice but to copy data from the buffer 65 * area into mbufs in order to pass the packets up to the higher protocol 66 * levels. 67 * 68 * It's impossible given this rotten design to really achieve decent 69 * performance at 100Mbps, unless you happen to have a 400Mhz PII or 70 * some equally overmuscled CPU to drive it. 71 * 72 * On the bright side, the 8139 does have a built-in PHY, although 73 * rather than using an MDIO serial interface like most other NICs, the 74 * PHY registers are directly accessible through the 8139's register 75 * space. The 8139 supports autonegotiation, as well as a 64-bit multicast 76 * filter. 77 * 78 * The 8129 chip is an older version of the 8139 that uses an external PHY 79 * chip. The 8129 has a serial MDIO interface for accessing the MII where 80 * the 8139 lets you directly access the on-board PHY registers. We need 81 * to select which interface to use depending on the chip type. 82 */ 83 84#include <sys/cdefs.h> 85__FBSDID("$FreeBSD: head/sys/pci/if_rl.c 117029 2003-06-29 17:33:33Z wpaul $"); 86 87#include <sys/param.h> 88#include <sys/endian.h> 89#include <sys/systm.h> 90#include <sys/sockio.h> 91#include <sys/mbuf.h> 92#include <sys/malloc.h> 93#include <sys/kernel.h> 94#include <sys/socket.h> 95 96#include <net/if.h> 97#include <net/if_arp.h> 98#include <net/ethernet.h> 99#include <net/if_dl.h> 100#include <net/if_media.h> 101 102#include <net/bpf.h> 103 104#include <machine/bus_pio.h> 105#include <machine/bus_memio.h> 106#include <machine/bus.h> 107#include <machine/resource.h> 108#include <sys/bus.h> 109#include <sys/rman.h> 110 111#include <dev/mii/mii.h> 112#include <dev/mii/miivar.h> 113 114#include <pci/pcireg.h> 115#include <pci/pcivar.h> 116 117MODULE_DEPEND(rl, pci, 1, 1, 1); 118MODULE_DEPEND(rl, ether, 1, 1, 1); 119MODULE_DEPEND(rl, miibus, 1, 1, 1); 120 121/* "controller miibus0" required. See GENERIC if you get errors here. */ 122#include "miibus_if.h" 123 124/* 125 * Default to using PIO access for this driver. On SMP systems, 126 * there appear to be problems with memory mapped mode: it looks like 127 * doing too many memory mapped access back to back in rapid succession 128 * can hang the bus. I'm inclined to blame this on crummy design/construction 129 * on the part of RealTek. Memory mapped mode does appear to work on 130 * uniprocessor systems though. 131 */ 132#define RL_USEIOSPACE 133 134#include <pci/if_rlreg.h> 135 136__FBSDID("$FreeBSD: head/sys/pci/if_rl.c 117029 2003-06-29 17:33:33Z wpaul $"); 137 138/* 139 * Various supported device vendors/types and their names. 140 */ 141static struct rl_type rl_devs[] = { 142 { RT_VENDORID, RT_DEVICEID_8129, 143 "RealTek 8129 10/100BaseTX" }, 144 { RT_VENDORID, RT_DEVICEID_8139, 145 "RealTek 8139 10/100BaseTX" }, 146 { RT_VENDORID, RT_DEVICEID_8138, 147 "RealTek 8139 10/100BaseTX CardBus" }, 148 { ACCTON_VENDORID, ACCTON_DEVICEID_5030, 149 "Accton MPX 5030/5038 10/100BaseTX" }, 150 { DELTA_VENDORID, DELTA_DEVICEID_8139, 151 "Delta Electronics 8139 10/100BaseTX" }, 152 { ADDTRON_VENDORID, ADDTRON_DEVICEID_8139, 153 "Addtron Technolgy 8139 10/100BaseTX" }, 154 { DLINK_VENDORID, DLINK_DEVICEID_530TXPLUS, 155 "D-Link DFE-530TX+ 10/100BaseTX" }, 156 { DLINK_VENDORID, DLINK_DEVICEID_690TXD, 157 "D-Link DFE-690TXD 10/100BaseTX" }, 158 { NORTEL_VENDORID, ACCTON_DEVICEID_5030, 159 "Nortel Networks 10/100BaseTX" }, 160 { COREGA_VENDORID, COREGA_DEVICEID_FETHERCBTXD, 161 "Corega FEther CB-TXD" }, 162 { COREGA_VENDORID, COREGA_DEVICEID_FETHERIICBTXD, 163 "Corega FEtherII CB-TXD" }, 164 { PEPPERCON_VENDORID, PEPPERCON_DEVICEID_ROLF, 165 "Peppercon AG ROL-F" }, 166 { PLANEX_VENDORID, PLANEX_DEVICEID_FNW3800TX, 167 "Planex FNW-3800-TX" }, 168 { 0, 0, NULL } 169}; 170 171static int rl_probe (device_t); 172static int rl_attach (device_t); 173static int rl_detach (device_t); 174 175static int rl_encap (struct rl_softc *, struct mbuf * ); 176 177static void rl_rxeof (struct rl_softc *); 178static void rl_txeof (struct rl_softc *); 179static void rl_intr (void *); 180static void rl_tick (void *); 181static void rl_start (struct ifnet *); 182static int rl_ioctl (struct ifnet *, u_long, caddr_t); 183static void rl_init (void *); 184static void rl_stop (struct rl_softc *); 185static void rl_watchdog (struct ifnet *); 186static int rl_suspend (device_t); 187static int rl_resume (device_t); 188static void rl_shutdown (device_t); 189static int rl_ifmedia_upd (struct ifnet *); 190static void rl_ifmedia_sts (struct ifnet *, struct ifmediareq *); 191 192static void rl_eeprom_putbyte (struct rl_softc *, int); 193static void rl_eeprom_getword (struct rl_softc *, int, u_int16_t *); 194static void rl_read_eeprom (struct rl_softc *, caddr_t, int, int, int); 195static void rl_mii_sync (struct rl_softc *); 196static void rl_mii_send (struct rl_softc *, u_int32_t, int); 197static int rl_mii_readreg (struct rl_softc *, struct rl_mii_frame *); 198static int rl_mii_writereg (struct rl_softc *, struct rl_mii_frame *); 199 200static int rl_miibus_readreg (device_t, int, int); 201static int rl_miibus_writereg (device_t, int, int, int); 202static void rl_miibus_statchg (device_t); 203 204static u_int8_t rl_calchash (caddr_t); 205static void rl_setmulti (struct rl_softc *); 206static void rl_reset (struct rl_softc *); 207static int rl_list_tx_init (struct rl_softc *); 208 209static void rl_dma_map_rxbuf (void *, bus_dma_segment_t *, int, int); 210static void rl_dma_map_txbuf (void *, bus_dma_segment_t *, int, int); 211 212#ifdef RL_USEIOSPACE 213#define RL_RES SYS_RES_IOPORT 214#define RL_RID RL_PCI_LOIO 215#else 216#define RL_RES SYS_RES_MEMORY 217#define RL_RID RL_PCI_LOMEM 218#endif 219 220static device_method_t rl_methods[] = { 221 /* Device interface */ 222 DEVMETHOD(device_probe, rl_probe), 223 DEVMETHOD(device_attach, rl_attach), 224 DEVMETHOD(device_detach, rl_detach), 225 DEVMETHOD(device_suspend, rl_suspend), 226 DEVMETHOD(device_resume, rl_resume), 227 DEVMETHOD(device_shutdown, rl_shutdown), 228 229 /* bus interface */ 230 DEVMETHOD(bus_print_child, bus_generic_print_child), 231 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 232 233 /* MII interface */ 234 DEVMETHOD(miibus_readreg, rl_miibus_readreg), 235 DEVMETHOD(miibus_writereg, rl_miibus_writereg), 236 DEVMETHOD(miibus_statchg, rl_miibus_statchg), 237 238 { 0, 0 } 239}; 240 241static driver_t rl_driver = { 242 "rl", 243 rl_methods, 244 sizeof(struct rl_softc) 245}; 246 247static devclass_t rl_devclass; 248 249DRIVER_MODULE(rl, pci, rl_driver, rl_devclass, 0, 0); 250DRIVER_MODULE(rl, cardbus, rl_driver, rl_devclass, 0, 0); 251DRIVER_MODULE(miibus, rl, miibus_driver, miibus_devclass, 0, 0); 252 253#define EE_SET(x) \ 254 CSR_WRITE_1(sc, RL_EECMD, \ 255 CSR_READ_1(sc, RL_EECMD) | x) 256 257#define EE_CLR(x) \ 258 CSR_WRITE_1(sc, RL_EECMD, \ 259 CSR_READ_1(sc, RL_EECMD) & ~x) 260 261static void 262rl_dma_map_rxbuf(arg, segs, nseg, error) 263 void *arg; 264 bus_dma_segment_t *segs; 265 int nseg, error; 266{ 267 struct rl_softc *sc; 268 269 sc = arg; 270 CSR_WRITE_4(sc, RL_RXADDR, segs->ds_addr & 0xFFFFFFFF); 271 272 return; 273} 274 275static void 276rl_dma_map_txbuf(arg, segs, nseg, error) 277 void *arg; 278 bus_dma_segment_t *segs; 279 int nseg, error; 280{ 281 struct rl_softc *sc; 282 283 sc = arg; 284 CSR_WRITE_4(sc, RL_CUR_TXADDR(sc), segs->ds_addr & 0xFFFFFFFF); 285 286 return; 287} 288 289/* 290 * Send a read command and address to the EEPROM, check for ACK. 291 */ 292static void 293rl_eeprom_putbyte(sc, addr) 294 struct rl_softc *sc; 295 int addr; 296{ 297 register int d, i; 298 299 d = addr | sc->rl_eecmd_read; 300 301 /* 302 * Feed in each bit and strobe the clock. 303 */ 304 for (i = 0x400; i; i >>= 1) { 305 if (d & i) { 306 EE_SET(RL_EE_DATAIN); 307 } else { 308 EE_CLR(RL_EE_DATAIN); 309 } 310 DELAY(100); 311 EE_SET(RL_EE_CLK); 312 DELAY(150); 313 EE_CLR(RL_EE_CLK); 314 DELAY(100); 315 } 316 317 return; 318} 319 320/* 321 * Read a word of data stored in the EEPROM at address 'addr.' 322 */ 323static void 324rl_eeprom_getword(sc, addr, dest) 325 struct rl_softc *sc; 326 int addr; 327 u_int16_t *dest; 328{ 329 register int i; 330 u_int16_t word = 0; 331 332 /* Enter EEPROM access mode. */ 333 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL); 334 335 /* 336 * Send address of word we want to read. 337 */ 338 rl_eeprom_putbyte(sc, addr); 339 340 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL); 341 342 /* 343 * Start reading bits from EEPROM. 344 */ 345 for (i = 0x8000; i; i >>= 1) { 346 EE_SET(RL_EE_CLK); 347 DELAY(100); 348 if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT) 349 word |= i; 350 EE_CLR(RL_EE_CLK); 351 DELAY(100); 352 } 353 354 /* Turn off EEPROM access mode. */ 355 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 356 357 *dest = word; 358 359 return; 360} 361 362/* 363 * Read a sequence of words from the EEPROM. 364 */ 365static void 366rl_read_eeprom(sc, dest, off, cnt, swap) 367 struct rl_softc *sc; 368 caddr_t dest; 369 int off; 370 int cnt; 371 int swap; 372{ 373 int i; 374 u_int16_t word = 0, *ptr; 375 376 for (i = 0; i < cnt; i++) { 377 rl_eeprom_getword(sc, off + i, &word); 378 ptr = (u_int16_t *)(dest + (i * 2)); 379 if (swap) 380 *ptr = ntohs(word); 381 else 382 *ptr = word; 383 } 384 385 return; 386} 387 388 389/* 390 * MII access routines are provided for the 8129, which 391 * doesn't have a built-in PHY. For the 8139, we fake things 392 * up by diverting rl_phy_readreg()/rl_phy_writereg() to the 393 * direct access PHY registers. 394 */ 395#define MII_SET(x) \ 396 CSR_WRITE_1(sc, RL_MII, \ 397 CSR_READ_1(sc, RL_MII) | (x)) 398 399#define MII_CLR(x) \ 400 CSR_WRITE_1(sc, RL_MII, \ 401 CSR_READ_1(sc, RL_MII) & ~(x)) 402 403/* 404 * Sync the PHYs by setting data bit and strobing the clock 32 times. 405 */ 406static void 407rl_mii_sync(sc) 408 struct rl_softc *sc; 409{ 410 register int i; 411 412 MII_SET(RL_MII_DIR|RL_MII_DATAOUT); 413 414 for (i = 0; i < 32; i++) { 415 MII_SET(RL_MII_CLK); 416 DELAY(1); 417 MII_CLR(RL_MII_CLK); 418 DELAY(1); 419 } 420 421 return; 422} 423 424/* 425 * Clock a series of bits through the MII. 426 */ 427static void 428rl_mii_send(sc, bits, cnt) 429 struct rl_softc *sc; 430 u_int32_t bits; 431 int cnt; 432{ 433 int i; 434 435 MII_CLR(RL_MII_CLK); 436 437 for (i = (0x1 << (cnt - 1)); i; i >>= 1) { 438 if (bits & i) { 439 MII_SET(RL_MII_DATAOUT); 440 } else { 441 MII_CLR(RL_MII_DATAOUT); 442 } 443 DELAY(1); 444 MII_CLR(RL_MII_CLK); 445 DELAY(1); 446 MII_SET(RL_MII_CLK); 447 } 448} 449 450/* 451 * Read an PHY register through the MII. 452 */ 453static int 454rl_mii_readreg(sc, frame) 455 struct rl_softc *sc; 456 struct rl_mii_frame *frame; 457 458{ 459 int i, ack; 460 461 RL_LOCK(sc); 462 463 /* 464 * Set up frame for RX. 465 */ 466 frame->mii_stdelim = RL_MII_STARTDELIM; 467 frame->mii_opcode = RL_MII_READOP; 468 frame->mii_turnaround = 0; 469 frame->mii_data = 0; 470 471 CSR_WRITE_2(sc, RL_MII, 0); 472 473 /* 474 * Turn on data xmit. 475 */ 476 MII_SET(RL_MII_DIR); 477 478 rl_mii_sync(sc); 479 480 /* 481 * Send command/address info. 482 */ 483 rl_mii_send(sc, frame->mii_stdelim, 2); 484 rl_mii_send(sc, frame->mii_opcode, 2); 485 rl_mii_send(sc, frame->mii_phyaddr, 5); 486 rl_mii_send(sc, frame->mii_regaddr, 5); 487 488 /* Idle bit */ 489 MII_CLR((RL_MII_CLK|RL_MII_DATAOUT)); 490 DELAY(1); 491 MII_SET(RL_MII_CLK); 492 DELAY(1); 493 494 /* Turn off xmit. */ 495 MII_CLR(RL_MII_DIR); 496 497 /* Check for ack */ 498 MII_CLR(RL_MII_CLK); 499 DELAY(1); 500 ack = CSR_READ_2(sc, RL_MII) & RL_MII_DATAIN; 501 MII_SET(RL_MII_CLK); 502 DELAY(1); 503 504 /* 505 * Now try reading data bits. If the ack failed, we still 506 * need to clock through 16 cycles to keep the PHY(s) in sync. 507 */ 508 if (ack) { 509 for(i = 0; i < 16; i++) { 510 MII_CLR(RL_MII_CLK); 511 DELAY(1); 512 MII_SET(RL_MII_CLK); 513 DELAY(1); 514 } 515 goto fail; 516 } 517 518 for (i = 0x8000; i; i >>= 1) { 519 MII_CLR(RL_MII_CLK); 520 DELAY(1); 521 if (!ack) { 522 if (CSR_READ_2(sc, RL_MII) & RL_MII_DATAIN) 523 frame->mii_data |= i; 524 DELAY(1); 525 } 526 MII_SET(RL_MII_CLK); 527 DELAY(1); 528 } 529 530fail: 531 532 MII_CLR(RL_MII_CLK); 533 DELAY(1); 534 MII_SET(RL_MII_CLK); 535 DELAY(1); 536 537 RL_UNLOCK(sc); 538 539 if (ack) 540 return(1); 541 return(0); 542} 543 544/* 545 * Write to a PHY register through the MII. 546 */ 547static int 548rl_mii_writereg(sc, frame) 549 struct rl_softc *sc; 550 struct rl_mii_frame *frame; 551 552{ 553 RL_LOCK(sc); 554 555 /* 556 * Set up frame for TX. 557 */ 558 559 frame->mii_stdelim = RL_MII_STARTDELIM; 560 frame->mii_opcode = RL_MII_WRITEOP; 561 frame->mii_turnaround = RL_MII_TURNAROUND; 562 563 /* 564 * Turn on data output. 565 */ 566 MII_SET(RL_MII_DIR); 567 568 rl_mii_sync(sc); 569 570 rl_mii_send(sc, frame->mii_stdelim, 2); 571 rl_mii_send(sc, frame->mii_opcode, 2); 572 rl_mii_send(sc, frame->mii_phyaddr, 5); 573 rl_mii_send(sc, frame->mii_regaddr, 5); 574 rl_mii_send(sc, frame->mii_turnaround, 2); 575 rl_mii_send(sc, frame->mii_data, 16); 576 577 /* Idle bit. */ 578 MII_SET(RL_MII_CLK); 579 DELAY(1); 580 MII_CLR(RL_MII_CLK); 581 DELAY(1); 582 583 /* 584 * Turn off xmit. 585 */ 586 MII_CLR(RL_MII_DIR); 587 588 RL_UNLOCK(sc); 589 590 return(0); 591} 592 593static int 594rl_miibus_readreg(dev, phy, reg) 595 device_t dev; 596 int phy, reg; 597{ 598 struct rl_softc *sc; 599 struct rl_mii_frame frame; 600 u_int16_t rval = 0; 601 u_int16_t rl8139_reg = 0; 602 603 sc = device_get_softc(dev); 604 RL_LOCK(sc); 605 606 if (sc->rl_type == RL_8139) { 607 /* Pretend the internal PHY is only at address 0 */ 608 if (phy) { 609 RL_UNLOCK(sc); 610 return(0); 611 } 612 switch(reg) { 613 case MII_BMCR: 614 rl8139_reg = RL_BMCR; 615 break; 616 case MII_BMSR: 617 rl8139_reg = RL_BMSR; 618 break; 619 case MII_ANAR: 620 rl8139_reg = RL_ANAR; 621 break; 622 case MII_ANER: 623 rl8139_reg = RL_ANER; 624 break; 625 case MII_ANLPAR: 626 rl8139_reg = RL_LPAR; 627 break; 628 case MII_PHYIDR1: 629 case MII_PHYIDR2: 630 RL_UNLOCK(sc); 631 return(0); 632 /* 633 * Allow the rlphy driver to read the media status 634 * register. If we have a link partner which does not 635 * support NWAY, this is the register which will tell 636 * us the results of parallel detection. 637 */ 638 case RL_MEDIASTAT: 639 rval = CSR_READ_1(sc, RL_MEDIASTAT); 640 RL_UNLOCK(sc); 641 return(rval); 642 default: 643 printf("rl%d: bad phy register\n", sc->rl_unit); 644 RL_UNLOCK(sc); 645 return(0); 646 } 647 rval = CSR_READ_2(sc, rl8139_reg); 648 RL_UNLOCK(sc); 649 return(rval); 650 } 651 652 bzero((char *)&frame, sizeof(frame)); 653 654 frame.mii_phyaddr = phy; 655 frame.mii_regaddr = reg; 656 rl_mii_readreg(sc, &frame); 657 RL_UNLOCK(sc); 658 659 return(frame.mii_data); 660} 661 662static int 663rl_miibus_writereg(dev, phy, reg, data) 664 device_t dev; 665 int phy, reg, data; 666{ 667 struct rl_softc *sc; 668 struct rl_mii_frame frame; 669 u_int16_t rl8139_reg = 0; 670 671 sc = device_get_softc(dev); 672 RL_LOCK(sc); 673 674 if (sc->rl_type == RL_8139) { 675 /* Pretend the internal PHY is only at address 0 */ 676 if (phy) { 677 RL_UNLOCK(sc); 678 return(0); 679 } 680 switch(reg) { 681 case MII_BMCR: 682 rl8139_reg = RL_BMCR; 683 break; 684 case MII_BMSR: 685 rl8139_reg = RL_BMSR; 686 break; 687 case MII_ANAR: 688 rl8139_reg = RL_ANAR; 689 break; 690 case MII_ANER: 691 rl8139_reg = RL_ANER; 692 break; 693 case MII_ANLPAR: 694 rl8139_reg = RL_LPAR; 695 break; 696 case MII_PHYIDR1: 697 case MII_PHYIDR2: 698 RL_UNLOCK(sc); 699 return(0); 700 break; 701 default: 702 printf("rl%d: bad phy register\n", sc->rl_unit); 703 RL_UNLOCK(sc); 704 return(0); 705 } 706 CSR_WRITE_2(sc, rl8139_reg, data); 707 RL_UNLOCK(sc); 708 return(0); 709 } 710 711 bzero((char *)&frame, sizeof(frame)); 712 713 frame.mii_phyaddr = phy; 714 frame.mii_regaddr = reg; 715 frame.mii_data = data; 716 717 rl_mii_writereg(sc, &frame); 718 719 RL_UNLOCK(sc); 720 return(0); 721} 722 723static void 724rl_miibus_statchg(dev) 725 device_t dev; 726{ 727 return; 728} 729 730/* 731 * Calculate CRC of a multicast group address, return the upper 6 bits. 732 */ 733static u_int8_t 734rl_calchash(addr) 735 caddr_t addr; 736{ 737 u_int32_t crc, carry; 738 int i, j; 739 u_int8_t c; 740 741 /* Compute CRC for the address value. */ 742 crc = 0xFFFFFFFF; /* initial value */ 743 744 for (i = 0; i < 6; i++) { 745 c = *(addr + i); 746 for (j = 0; j < 8; j++) { 747 carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01); 748 crc <<= 1; 749 c >>= 1; 750 if (carry) 751 crc = (crc ^ 0x04c11db6) | carry; 752 } 753 } 754 755 /* return the filter bit position */ 756 return(crc >> 26); 757} 758 759/* 760 * Program the 64-bit multicast hash filter. 761 */ 762static void 763rl_setmulti(sc) 764 struct rl_softc *sc; 765{ 766 struct ifnet *ifp; 767 int h = 0; 768 u_int32_t hashes[2] = { 0, 0 }; 769 struct ifmultiaddr *ifma; 770 u_int32_t rxfilt; 771 int mcnt = 0; 772 773 ifp = &sc->arpcom.ac_if; 774 775 rxfilt = CSR_READ_4(sc, RL_RXCFG); 776 777 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 778 rxfilt |= RL_RXCFG_RX_MULTI; 779 CSR_WRITE_4(sc, RL_RXCFG, rxfilt); 780 CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF); 781 CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF); 782 return; 783 } 784 785 /* first, zot all the existing hash bits */ 786 CSR_WRITE_4(sc, RL_MAR0, 0); 787 CSR_WRITE_4(sc, RL_MAR4, 0); 788 789 /* now program new ones */ 790 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 791 if (ifma->ifma_addr->sa_family != AF_LINK) 792 continue; 793 h = rl_calchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr)); 794 if (h < 32) 795 hashes[0] |= (1 << h); 796 else 797 hashes[1] |= (1 << (h - 32)); 798 mcnt++; 799 } 800 801 if (mcnt) 802 rxfilt |= RL_RXCFG_RX_MULTI; 803 else 804 rxfilt &= ~RL_RXCFG_RX_MULTI; 805 806 CSR_WRITE_4(sc, RL_RXCFG, rxfilt); 807 CSR_WRITE_4(sc, RL_MAR0, hashes[0]); 808 CSR_WRITE_4(sc, RL_MAR4, hashes[1]); 809 810 return; 811} 812 813static void 814rl_reset(sc) 815 struct rl_softc *sc; 816{ 817 register int i; 818 819 CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET); 820 821 for (i = 0; i < RL_TIMEOUT; i++) { 822 DELAY(10); 823 if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET)) 824 break; 825 } 826 if (i == RL_TIMEOUT) 827 printf("rl%d: reset never completed!\n", sc->rl_unit); 828 829 return; 830} 831 832/* 833 * Probe for a RealTek 8129/8139 chip. Check the PCI vendor and device 834 * IDs against our list and return a device name if we find a match. 835 */ 836static int 837rl_probe(dev) 838 device_t dev; 839{ 840 struct rl_type *t; 841 842 t = rl_devs; 843 844 while(t->rl_name != NULL) { 845 if ((pci_get_vendor(dev) == t->rl_vid) && 846 (pci_get_device(dev) == t->rl_did)) { 847 device_set_desc(dev, t->rl_name); 848 return(0); 849 } 850 t++; 851 } 852 853 return(ENXIO); 854} 855 856/* 857 * Attach the interface. Allocate softc structures, do ifmedia 858 * setup and ethernet/BPF attach. 859 */ 860static int 861rl_attach(dev) 862 device_t dev; 863{ 864 u_char eaddr[ETHER_ADDR_LEN]; 865 u_int16_t as[3]; 866 struct rl_softc *sc; 867 struct ifnet *ifp; 868 u_int16_t rl_did = 0; 869 int unit, error = 0, rid, i; 870 871 sc = device_get_softc(dev); 872 unit = device_get_unit(dev); 873 874 mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 875 MTX_DEF | MTX_RECURSE); 876 877 /* 878 * Handle power management nonsense. 879 */ 880 881 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) { 882 u_int32_t iobase, membase, irq; 883 884 /* Save important PCI config data. */ 885 iobase = pci_read_config(dev, RL_PCI_LOIO, 4); 886 membase = pci_read_config(dev, RL_PCI_LOMEM, 4); 887 irq = pci_read_config(dev, RL_PCI_INTLINE, 4); 888 889 /* Reset the power state. */ 890 printf("rl%d: chip is is in D%d power mode " 891 "-- setting to D0\n", unit, 892 pci_get_powerstate(dev)); 893 894 pci_set_powerstate(dev, PCI_POWERSTATE_D0); 895 896 /* Restore PCI config data. */ 897 pci_write_config(dev, RL_PCI_LOIO, iobase, 4); 898 pci_write_config(dev, RL_PCI_LOMEM, membase, 4); 899 pci_write_config(dev, RL_PCI_INTLINE, irq, 4); 900 } 901 902 /* 903 * Map control/status registers. 904 */ 905 pci_enable_busmaster(dev); 906 907 rid = RL_RID; 908 sc->rl_res = bus_alloc_resource(dev, RL_RES, &rid, 909 0, ~0, 1, RF_ACTIVE); 910 911 if (sc->rl_res == NULL) { 912 printf ("rl%d: couldn't map ports/memory\n", unit); 913 error = ENXIO; 914 goto fail; 915 } 916 917 /* Detect the Realtek 8139B. For some reason, this chip is very 918 * unstable when left to autoselect the media 919 * The best workaround is to set the device to the required 920 * media type or to set it to the 10 Meg speed. 921 */ 922 923 if ((rman_get_end(sc->rl_res)-rman_get_start(sc->rl_res))==0xff) { 924 printf("rl%d: Realtek 8139B detected. Warning, this may be unstable in autoselect mode\n", unit); 925 } 926 927 sc->rl_btag = rman_get_bustag(sc->rl_res); 928 sc->rl_bhandle = rman_get_bushandle(sc->rl_res); 929 930 /* Allocate interrupt */ 931 rid = 0; 932 sc->rl_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 933 RF_SHAREABLE | RF_ACTIVE); 934 935 if (sc->rl_irq == NULL) { 936 printf("rl%d: couldn't map interrupt\n", unit); 937 error = ENXIO; 938 goto fail; 939 } 940 941 /* Reset the adapter. */ 942 rl_reset(sc); 943 sc->rl_eecmd_read = RL_EECMD_READ_6BIT; 944 rl_read_eeprom(sc, (caddr_t)&rl_did, 0, 1, 0); 945 if (rl_did != 0x8129) 946 sc->rl_eecmd_read = RL_EECMD_READ_8BIT; 947 948 /* 949 * Get station address from the EEPROM. 950 */ 951 rl_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3, 0); 952 for (i = 0; i < 3; i++) { 953 eaddr[(i * 2) + 0] = as[i] & 0xff; 954 eaddr[(i * 2) + 1] = as[i] >> 8; 955 } 956 957 /* 958 * A RealTek chip was detected. Inform the world. 959 */ 960 printf("rl%d: Ethernet address: %6D\n", unit, eaddr, ":"); 961 962 sc->rl_unit = unit; 963 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); 964 965 /* 966 * Now read the exact device type from the EEPROM to find 967 * out if it's an 8129 or 8139. 968 */ 969 rl_read_eeprom(sc, (caddr_t)&rl_did, RL_EE_PCI_DID, 1, 0); 970 971 if (rl_did == RT_DEVICEID_8139 || rl_did == ACCTON_DEVICEID_5030 || 972 rl_did == DELTA_DEVICEID_8139 || rl_did == ADDTRON_DEVICEID_8139 || 973 rl_did == RT_DEVICEID_8138 || rl_did == DLINK_DEVICEID_530TXPLUS || 974 rl_did == DLINK_DEVICEID_690TXD || 975 rl_did == COREGA_DEVICEID_FETHERCBTXD || 976 rl_did == COREGA_DEVICEID_FETHERIICBTXD || 977 rl_did == PLANEX_DEVICEID_FNW3800TX) 978 sc->rl_type = RL_8139; 979 else if (rl_did == RT_DEVICEID_8129) 980 sc->rl_type = RL_8129; 981 else { 982 printf("rl%d: unknown device ID: %x\n", unit, rl_did); 983 error = ENXIO; 984 goto fail; 985 } 986 987 /* 988 * Allocate the parent bus DMA tag appropriate for PCI. 989 */ 990#define RL_NSEG_NEW 32 991 error = bus_dma_tag_create(NULL, /* parent */ 992 1, 0, /* alignment, boundary */ 993 BUS_SPACE_MAXADDR_32BIT,/* lowaddr */ 994 BUS_SPACE_MAXADDR, /* highaddr */ 995 NULL, NULL, /* filter, filterarg */ 996 MAXBSIZE, RL_NSEG_NEW, /* maxsize, nsegments */ 997 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */ 998 BUS_DMA_ALLOCNOW, /* flags */ 999 &sc->rl_parent_tag); 1000 if (error) 1001 goto fail; 1002 1003 /* 1004 * Now allocate a tag for the DMA descriptor lists. 1005 * All of our lists are allocated as a contiguous block 1006 * of memory. 1007 */ 1008 error = bus_dma_tag_create(sc->rl_parent_tag, /* parent */ 1009 1, 0, /* alignment, boundary */ 1010 BUS_SPACE_MAXADDR, /* lowaddr */ 1011 BUS_SPACE_MAXADDR, /* highaddr */ 1012 NULL, NULL, /* filter, filterarg */ 1013 RL_RXBUFLEN + 1518, 1, /* maxsize,nsegments */ 1014 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */ 1015 0, /* flags */ 1016 &sc->rl_tag); 1017 if (error) 1018 goto fail; 1019 1020 /* 1021 * Now allocate a chunk of DMA-able memory based on the 1022 * tag we just created. 1023 */ 1024 error = bus_dmamem_alloc(sc->rl_tag, 1025 (void **)&sc->rl_cdata.rl_rx_buf, BUS_DMA_NOWAIT, 1026 &sc->rl_cdata.rl_rx_dmamap); 1027 1028 if (error) { 1029 printf("rl%d: no memory for list buffers!\n", unit); 1030 bus_dma_tag_destroy(sc->rl_tag); 1031 sc->rl_tag = NULL; 1032 goto fail; 1033 } 1034 1035 /* Leave a few bytes before the start of the RX ring buffer. */ 1036 sc->rl_cdata.rl_rx_buf_ptr = sc->rl_cdata.rl_rx_buf; 1037 sc->rl_cdata.rl_rx_buf += sizeof(u_int64_t); 1038 1039 /* Do MII setup */ 1040 if (mii_phy_probe(dev, &sc->rl_miibus, 1041 rl_ifmedia_upd, rl_ifmedia_sts)) { 1042 printf("rl%d: MII without any phy!\n", sc->rl_unit); 1043 error = ENXIO; 1044 goto fail; 1045 } 1046 1047 ifp = &sc->arpcom.ac_if; 1048 ifp->if_softc = sc; 1049 ifp->if_unit = unit; 1050 ifp->if_name = "rl"; 1051 ifp->if_mtu = ETHERMTU; 1052 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1053 ifp->if_ioctl = rl_ioctl; 1054 ifp->if_output = ether_output; 1055 ifp->if_start = rl_start; 1056 ifp->if_watchdog = rl_watchdog; 1057 ifp->if_init = rl_init; 1058 ifp->if_baudrate = 10000000; 1059 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 1060 1061 callout_handle_init(&sc->rl_stat_ch); 1062 1063 /* 1064 * Call MI attach routine. 1065 */ 1066 ether_ifattach(ifp, eaddr); 1067 1068 /* Hook interrupt last to avoid having to lock softc */ 1069 error = bus_setup_intr(dev, sc->rl_irq, INTR_TYPE_NET, 1070 rl_intr, sc, &sc->rl_intrhand); 1071 1072 if (error) { 1073 printf("rl%d: couldn't set up irq\n", unit); 1074 ether_ifdetach(ifp); 1075 goto fail; 1076 } 1077 1078fail: 1079 if (error) 1080 rl_detach(dev); 1081 1082 return (error); 1083} 1084 1085/* 1086 * Shutdown hardware and free up resources. This can be called any 1087 * time after the mutex has been initialized. It is called in both 1088 * the error case in attach and the normal detach case so it needs 1089 * to be careful about only freeing resources that have actually been 1090 * allocated. 1091 */ 1092static int 1093rl_detach(dev) 1094 device_t dev; 1095{ 1096 struct rl_softc *sc; 1097 struct ifnet *ifp; 1098 1099 sc = device_get_softc(dev); 1100 KASSERT(mtx_initialized(&sc->rl_mtx), ("rl mutex not initialized")); 1101 RL_LOCK(sc); 1102 ifp = &sc->arpcom.ac_if; 1103 1104 /* These should only be active if attach succeeded */ 1105 if (device_is_attached(dev)) { 1106 rl_stop(sc); 1107 ether_ifdetach(ifp); 1108 } 1109 if (sc->rl_miibus) 1110 device_delete_child(dev, sc->rl_miibus); 1111 bus_generic_detach(dev); 1112 1113 if (sc->rl_intrhand) 1114 bus_teardown_intr(dev, sc->rl_irq, sc->rl_intrhand); 1115 if (sc->rl_irq) 1116 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->rl_irq); 1117 if (sc->rl_res) 1118 bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res); 1119 1120 if (sc->rl_tag) { 1121 bus_dmamap_unload(sc->rl_tag, sc->rl_cdata.rl_rx_dmamap); 1122 bus_dmamem_free(sc->rl_tag, sc->rl_cdata.rl_rx_buf, 1123 sc->rl_cdata.rl_rx_dmamap); 1124 bus_dma_tag_destroy(sc->rl_tag); 1125 } 1126 if (sc->rl_parent_tag) 1127 bus_dma_tag_destroy(sc->rl_parent_tag); 1128 1129 RL_UNLOCK(sc); 1130 mtx_destroy(&sc->rl_mtx); 1131 1132 return(0); 1133} 1134 1135/* 1136 * Initialize the transmit descriptors. 1137 */ 1138static int 1139rl_list_tx_init(sc) 1140 struct rl_softc *sc; 1141{ 1142 struct rl_chain_data *cd; 1143 int i; 1144 1145 cd = &sc->rl_cdata; 1146 for (i = 0; i < RL_TX_LIST_CNT; i++) { 1147 cd->rl_tx_chain[i] = NULL; 1148 CSR_WRITE_4(sc, 1149 RL_TXADDR0 + (i * sizeof(u_int32_t)), 0x0000000); 1150 } 1151 1152 sc->rl_cdata.cur_tx = 0; 1153 sc->rl_cdata.last_tx = 0; 1154 1155 return(0); 1156} 1157 1158/* 1159 * A frame has been uploaded: pass the resulting mbuf chain up to 1160 * the higher level protocols. 1161 * 1162 * You know there's something wrong with a PCI bus-master chip design 1163 * when you have to use m_devget(). 1164 * 1165 * The receive operation is badly documented in the datasheet, so I'll 1166 * attempt to document it here. The driver provides a buffer area and 1167 * places its base address in the RX buffer start address register. 1168 * The chip then begins copying frames into the RX buffer. Each frame 1169 * is preceded by a 32-bit RX status word which specifies the length 1170 * of the frame and certain other status bits. Each frame (starting with 1171 * the status word) is also 32-bit aligned. The frame length is in the 1172 * first 16 bits of the status word; the lower 15 bits correspond with 1173 * the 'rx status register' mentioned in the datasheet. 1174 * 1175 * Note: to make the Alpha happy, the frame payload needs to be aligned 1176 * on a 32-bit boundary. To achieve this, we pass RL_ETHER_ALIGN (2 bytes) 1177 * as the offset argument to m_devget(). 1178 */ 1179static void 1180rl_rxeof(sc) 1181 struct rl_softc *sc; 1182{ 1183 struct mbuf *m; 1184 struct ifnet *ifp; 1185 int total_len = 0; 1186 u_int32_t rxstat; 1187 caddr_t rxbufpos; 1188 int wrap = 0; 1189 u_int16_t cur_rx; 1190 u_int16_t limit; 1191 u_int16_t rx_bytes = 0, max_bytes; 1192 1193 ifp = &sc->arpcom.ac_if; 1194 1195 bus_dmamap_sync(sc->rl_tag, sc->rl_cdata.rl_rx_dmamap, 1196 BUS_DMASYNC_POSTREAD); 1197 1198 cur_rx = (CSR_READ_2(sc, RL_CURRXADDR) + 16) % RL_RXBUFLEN; 1199 1200 /* Do not try to read past this point. */ 1201 limit = CSR_READ_2(sc, RL_CURRXBUF) % RL_RXBUFLEN; 1202 1203 if (limit < cur_rx) 1204 max_bytes = (RL_RXBUFLEN - cur_rx) + limit; 1205 else 1206 max_bytes = limit - cur_rx; 1207 1208 while((CSR_READ_1(sc, RL_COMMAND) & RL_CMD_EMPTY_RXBUF) == 0) { 1209#ifdef DEVICE_POLLING 1210 if (ifp->if_flags & IFF_POLLING) { 1211 if (sc->rxcycles <= 0) 1212 break; 1213 sc->rxcycles--; 1214 } 1215#endif /* DEVICE_POLLING */ 1216 rxbufpos = sc->rl_cdata.rl_rx_buf + cur_rx; 1217 rxstat = le32toh(*(u_int32_t *)rxbufpos); 1218 1219 /* 1220 * Here's a totally undocumented fact for you. When the 1221 * RealTek chip is in the process of copying a packet into 1222 * RAM for you, the length will be 0xfff0. If you spot a 1223 * packet header with this value, you need to stop. The 1224 * datasheet makes absolutely no mention of this and 1225 * RealTek should be shot for this. 1226 */ 1227 if ((u_int16_t)(rxstat >> 16) == RL_RXSTAT_UNFINISHED) 1228 break; 1229 1230 if (!(rxstat & RL_RXSTAT_RXOK)) { 1231 ifp->if_ierrors++; 1232 rl_init(sc); 1233 return; 1234 } 1235 1236 /* No errors; receive the packet. */ 1237 total_len = rxstat >> 16; 1238 rx_bytes += total_len + 4; 1239 1240 /* 1241 * XXX The RealTek chip includes the CRC with every 1242 * received frame, and there's no way to turn this 1243 * behavior off (at least, I can't find anything in 1244 * the manual that explains how to do it) so we have 1245 * to trim off the CRC manually. 1246 */ 1247 total_len -= ETHER_CRC_LEN; 1248 1249 /* 1250 * Avoid trying to read more bytes than we know 1251 * the chip has prepared for us. 1252 */ 1253 if (rx_bytes > max_bytes) 1254 break; 1255 1256 rxbufpos = sc->rl_cdata.rl_rx_buf + 1257 ((cur_rx + sizeof(u_int32_t)) % RL_RXBUFLEN); 1258 1259 if (rxbufpos == (sc->rl_cdata.rl_rx_buf + RL_RXBUFLEN)) 1260 rxbufpos = sc->rl_cdata.rl_rx_buf; 1261 1262 wrap = (sc->rl_cdata.rl_rx_buf + RL_RXBUFLEN) - rxbufpos; 1263 1264 if (total_len > wrap) { 1265 m = m_devget(rxbufpos, total_len, RL_ETHER_ALIGN, ifp, 1266 NULL); 1267 if (m == NULL) { 1268 ifp->if_ierrors++; 1269 } else { 1270 m_copyback(m, wrap, total_len - wrap, 1271 sc->rl_cdata.rl_rx_buf); 1272 } 1273 cur_rx = (total_len - wrap + ETHER_CRC_LEN); 1274 } else { 1275 m = m_devget(rxbufpos, total_len, RL_ETHER_ALIGN, ifp, 1276 NULL); 1277 if (m == NULL) { 1278 ifp->if_ierrors++; 1279 } 1280 cur_rx += total_len + 4 + ETHER_CRC_LEN; 1281 } 1282 1283 /* 1284 * Round up to 32-bit boundary. 1285 */ 1286 cur_rx = (cur_rx + 3) & ~3; 1287 CSR_WRITE_2(sc, RL_CURRXADDR, cur_rx - 16); 1288 1289 if (m == NULL) 1290 continue; 1291 1292 ifp->if_ipackets++; 1293 (*ifp->if_input)(ifp, m); 1294 } 1295 1296 return; 1297} 1298 1299/* 1300 * A frame was downloaded to the chip. It's safe for us to clean up 1301 * the list buffers. 1302 */ 1303static void 1304rl_txeof(sc) 1305 struct rl_softc *sc; 1306{ 1307 struct ifnet *ifp; 1308 u_int32_t txstat; 1309 1310 ifp = &sc->arpcom.ac_if; 1311 1312 /* 1313 * Go through our tx list and free mbufs for those 1314 * frames that have been uploaded. 1315 */ 1316 do { 1317 txstat = CSR_READ_4(sc, RL_LAST_TXSTAT(sc)); 1318 if (!(txstat & (RL_TXSTAT_TX_OK| 1319 RL_TXSTAT_TX_UNDERRUN|RL_TXSTAT_TXABRT))) 1320 break; 1321 1322 ifp->if_collisions += (txstat & RL_TXSTAT_COLLCNT) >> 24; 1323 1324 if (RL_LAST_TXMBUF(sc) != NULL) { 1325 bus_dmamap_unload(sc->rl_tag, RL_LAST_DMAMAP(sc)); 1326 bus_dmamap_destroy(sc->rl_tag, RL_LAST_DMAMAP(sc)); 1327 m_freem(RL_LAST_TXMBUF(sc)); 1328 RL_LAST_TXMBUF(sc) = NULL; 1329 } 1330 if (txstat & RL_TXSTAT_TX_OK) 1331 ifp->if_opackets++; 1332 else { 1333 int oldthresh; 1334 ifp->if_oerrors++; 1335 if ((txstat & RL_TXSTAT_TXABRT) || 1336 (txstat & RL_TXSTAT_OUTOFWIN)) 1337 CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG); 1338 oldthresh = sc->rl_txthresh; 1339 /* error recovery */ 1340 rl_reset(sc); 1341 rl_init(sc); 1342 /* 1343 * If there was a transmit underrun, 1344 * bump the TX threshold. 1345 */ 1346 if (txstat & RL_TXSTAT_TX_UNDERRUN) 1347 sc->rl_txthresh = oldthresh + 32; 1348 return; 1349 } 1350 RL_INC(sc->rl_cdata.last_tx); 1351 ifp->if_flags &= ~IFF_OACTIVE; 1352 } while (sc->rl_cdata.last_tx != sc->rl_cdata.cur_tx); 1353 1354 ifp->if_timer = 1355 (sc->rl_cdata.last_tx == sc->rl_cdata.cur_tx) ? 0 : 5; 1356 1357 return; 1358} 1359 1360static void 1361rl_tick(xsc) 1362 void *xsc; 1363{ 1364 struct rl_softc *sc; 1365 struct mii_data *mii; 1366 1367 sc = xsc; 1368 RL_LOCK(sc); 1369 mii = device_get_softc(sc->rl_miibus); 1370 1371 mii_tick(mii); 1372 1373 sc->rl_stat_ch = timeout(rl_tick, sc, hz); 1374 RL_UNLOCK(sc); 1375 1376 return; 1377} 1378 1379#ifdef DEVICE_POLLING 1380static void 1381rl_poll (struct ifnet *ifp, enum poll_cmd cmd, int count) 1382{ 1383 struct rl_softc *sc = ifp->if_softc; 1384 1385 RL_LOCK(sc); 1386 if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */ 1387 CSR_WRITE_2(sc, RL_IMR, RL_INTRS); 1388 goto done; 1389 } 1390 1391 sc->rxcycles = count; 1392 rl_rxeof(sc); 1393 rl_txeof(sc); 1394 if (ifp->if_snd.ifq_head != NULL) 1395 rl_start(ifp); 1396 1397 if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */ 1398 u_int16_t status; 1399 1400 status = CSR_READ_2(sc, RL_ISR); 1401 if (status == 0xffff) 1402 goto done; 1403 if (status) 1404 CSR_WRITE_2(sc, RL_ISR, status); 1405 1406 /* 1407 * XXX check behaviour on receiver stalls. 1408 */ 1409 1410 if (status & RL_ISR_SYSTEM_ERR) { 1411 rl_reset(sc); 1412 rl_init(sc); 1413 } 1414 } 1415done: 1416 RL_UNLOCK(sc); 1417} 1418#endif /* DEVICE_POLLING */ 1419 1420static void 1421rl_intr(arg) 1422 void *arg; 1423{ 1424 struct rl_softc *sc; 1425 struct ifnet *ifp; 1426 u_int16_t status; 1427 1428 sc = arg; 1429 1430 if (sc->suspended) { 1431 return; 1432 } 1433 1434 RL_LOCK(sc); 1435 ifp = &sc->arpcom.ac_if; 1436 1437#ifdef DEVICE_POLLING 1438 if (ifp->if_flags & IFF_POLLING) 1439 goto done; 1440 if (ether_poll_register(rl_poll, ifp)) { /* ok, disable interrupts */ 1441 CSR_WRITE_2(sc, RL_IMR, 0x0000); 1442 rl_poll(ifp, 0, 1); 1443 goto done; 1444 } 1445#endif /* DEVICE_POLLING */ 1446 1447 for (;;) { 1448 1449 status = CSR_READ_2(sc, RL_ISR); 1450 /* If the card has gone away the read returns 0xffff. */ 1451 if (status == 0xffff) 1452 break; 1453 if (status) 1454 CSR_WRITE_2(sc, RL_ISR, status); 1455 1456 if ((status & RL_INTRS) == 0) 1457 break; 1458 1459 if (status & RL_ISR_RX_OK) 1460 rl_rxeof(sc); 1461 1462 if (status & RL_ISR_RX_ERR) 1463 rl_rxeof(sc); 1464 1465 if ((status & RL_ISR_TX_OK) || (status & RL_ISR_TX_ERR)) 1466 rl_txeof(sc); 1467 1468 if (status & RL_ISR_SYSTEM_ERR) { 1469 rl_reset(sc); 1470 rl_init(sc); 1471 } 1472 1473 } 1474 1475 if (ifp->if_snd.ifq_head != NULL) 1476 rl_start(ifp); 1477 1478#ifdef DEVICE_POLLING 1479done: 1480#endif 1481 RL_UNLOCK(sc); 1482 1483 return; 1484} 1485 1486/* 1487 * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data 1488 * pointers to the fragment pointers. 1489 */ 1490static int 1491rl_encap(sc, m_head) 1492 struct rl_softc *sc; 1493 struct mbuf *m_head; 1494{ 1495 struct mbuf *m_new = NULL; 1496 1497 /* 1498 * The RealTek is brain damaged and wants longword-aligned 1499 * TX buffers, plus we can only have one fragment buffer 1500 * per packet. We have to copy pretty much all the time. 1501 */ 1502 m_new = m_defrag(m_head, M_DONTWAIT); 1503 1504 if (m_new == NULL) { 1505 m_freem(m_head); 1506 return(1); 1507 } 1508 m_head = m_new; 1509 1510 /* Pad frames to at least 60 bytes. */ 1511 if (m_head->m_pkthdr.len < RL_MIN_FRAMELEN) { 1512 /* 1513 * Make security concious people happy: zero out the 1514 * bytes in the pad area, since we don't know what 1515 * this mbuf cluster buffer's previous user might 1516 * have left in it. 1517 */ 1518 bzero(mtod(m_head, char *) + m_head->m_pkthdr.len, 1519 RL_MIN_FRAMELEN - m_head->m_pkthdr.len); 1520 m_head->m_pkthdr.len += 1521 (RL_MIN_FRAMELEN - m_head->m_pkthdr.len); 1522 m_head->m_len = m_head->m_pkthdr.len; 1523 } 1524 1525 RL_CUR_TXMBUF(sc) = m_head; 1526 1527 return(0); 1528} 1529 1530/* 1531 * Main transmit routine. 1532 */ 1533 1534static void 1535rl_start(ifp) 1536 struct ifnet *ifp; 1537{ 1538 struct rl_softc *sc; 1539 struct mbuf *m_head = NULL; 1540 1541 sc = ifp->if_softc; 1542 RL_LOCK(sc); 1543 1544 while(RL_CUR_TXMBUF(sc) == NULL) { 1545 IF_DEQUEUE(&ifp->if_snd, m_head); 1546 if (m_head == NULL) 1547 break; 1548 1549 if (rl_encap(sc, m_head)) { 1550 break; 1551 } 1552 1553 /* 1554 * If there's a BPF listener, bounce a copy of this frame 1555 * to him. 1556 */ 1557 BPF_MTAP(ifp, RL_CUR_TXMBUF(sc)); 1558 1559 /* 1560 * Transmit the frame. 1561 */ 1562 bus_dmamap_create(sc->rl_tag, 0, &RL_CUR_DMAMAP(sc)); 1563 bus_dmamap_load(sc->rl_tag, RL_CUR_DMAMAP(sc), 1564 mtod(RL_CUR_TXMBUF(sc), void *), 1565 RL_CUR_TXMBUF(sc)->m_pkthdr.len, rl_dma_map_txbuf, sc, 0); 1566 bus_dmamap_sync(sc->rl_tag, RL_CUR_DMAMAP(sc), 1567 BUS_DMASYNC_PREREAD); 1568 CSR_WRITE_4(sc, RL_CUR_TXSTAT(sc), 1569 RL_TXTHRESH(sc->rl_txthresh) | 1570 RL_CUR_TXMBUF(sc)->m_pkthdr.len); 1571 1572 RL_INC(sc->rl_cdata.cur_tx); 1573 1574 /* 1575 * Set a timeout in case the chip goes out to lunch. 1576 */ 1577 ifp->if_timer = 5; 1578 } 1579 1580 /* 1581 * We broke out of the loop because all our TX slots are 1582 * full. Mark the NIC as busy until it drains some of the 1583 * packets from the queue. 1584 */ 1585 if (RL_CUR_TXMBUF(sc) != NULL) 1586 ifp->if_flags |= IFF_OACTIVE; 1587 1588 RL_UNLOCK(sc); 1589 1590 return; 1591} 1592 1593static void 1594rl_init(xsc) 1595 void *xsc; 1596{ 1597 struct rl_softc *sc = xsc; 1598 struct ifnet *ifp = &sc->arpcom.ac_if; 1599 struct mii_data *mii; 1600 u_int32_t rxcfg = 0; 1601 1602 RL_LOCK(sc); 1603 mii = device_get_softc(sc->rl_miibus); 1604 1605 /* 1606 * Cancel pending I/O and free all RX/TX buffers. 1607 */ 1608 rl_stop(sc); 1609 1610 /* 1611 * Init our MAC address. Even though the chipset 1612 * documentation doesn't mention it, we need to enter "Config 1613 * register write enable" mode to modify the ID registers. 1614 */ 1615 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG); 1616 CSR_WRITE_4(sc, RL_IDR0, *(u_int32_t *)(&sc->arpcom.ac_enaddr[0])); 1617 CSR_WRITE_4(sc, RL_IDR4, *(u_int32_t *)(&sc->arpcom.ac_enaddr[4])); 1618 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF); 1619 1620 /* Init the RX buffer pointer register. */ 1621 bus_dmamap_load(sc->rl_tag, sc->rl_cdata.rl_rx_dmamap, 1622 sc->rl_cdata.rl_rx_buf, RL_RXBUFLEN, rl_dma_map_rxbuf, sc, 0); 1623 bus_dmamap_sync(sc->rl_tag, sc->rl_cdata.rl_rx_dmamap, 1624 BUS_DMASYNC_PREWRITE); 1625 1626 /* Init TX descriptors. */ 1627 rl_list_tx_init(sc); 1628 1629 /* 1630 * Enable transmit and receive. 1631 */ 1632 CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB); 1633 1634 /* 1635 * Set the initial TX and RX configuration. 1636 */ 1637 CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG); 1638 CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG); 1639 1640 /* Set the individual bit to receive frames for this host only. */ 1641 rxcfg = CSR_READ_4(sc, RL_RXCFG); 1642 rxcfg |= RL_RXCFG_RX_INDIV; 1643 1644 /* If we want promiscuous mode, set the allframes bit. */ 1645 if (ifp->if_flags & IFF_PROMISC) { 1646 rxcfg |= RL_RXCFG_RX_ALLPHYS; 1647 CSR_WRITE_4(sc, RL_RXCFG, rxcfg); 1648 } else { 1649 rxcfg &= ~RL_RXCFG_RX_ALLPHYS; 1650 CSR_WRITE_4(sc, RL_RXCFG, rxcfg); 1651 } 1652 1653 /* 1654 * Set capture broadcast bit to capture broadcast frames. 1655 */ 1656 if (ifp->if_flags & IFF_BROADCAST) { 1657 rxcfg |= RL_RXCFG_RX_BROAD; 1658 CSR_WRITE_4(sc, RL_RXCFG, rxcfg); 1659 } else { 1660 rxcfg &= ~RL_RXCFG_RX_BROAD; 1661 CSR_WRITE_4(sc, RL_RXCFG, rxcfg); 1662 } 1663 1664 /* 1665 * Program the multicast filter, if necessary. 1666 */ 1667 rl_setmulti(sc); 1668 1669#ifdef DEVICE_POLLING 1670 /* 1671 * Disable interrupts if we are polling. 1672 */ 1673 if (ifp->if_flags & IFF_POLLING) 1674 CSR_WRITE_2(sc, RL_IMR, 0); 1675 else /* otherwise ... */ 1676#endif /* DEVICE_POLLING */ 1677 /* 1678 * Enable interrupts. 1679 */ 1680 CSR_WRITE_2(sc, RL_IMR, RL_INTRS); 1681 1682 /* Set initial TX threshold */ 1683 sc->rl_txthresh = RL_TX_THRESH_INIT; 1684 1685 /* Start RX/TX process. */ 1686 CSR_WRITE_4(sc, RL_MISSEDPKT, 0); 1687 1688 /* Enable receiver and transmitter. */ 1689 CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB); 1690 1691 mii_mediachg(mii); 1692 1693 CSR_WRITE_1(sc, RL_CFG1, RL_CFG1_DRVLOAD|RL_CFG1_FULLDUPLEX); 1694 1695 ifp->if_flags |= IFF_RUNNING; 1696 ifp->if_flags &= ~IFF_OACTIVE; 1697 1698 sc->rl_stat_ch = timeout(rl_tick, sc, hz); 1699 RL_UNLOCK(sc); 1700 1701 return; 1702} 1703 1704/* 1705 * Set media options. 1706 */ 1707static int 1708rl_ifmedia_upd(ifp) 1709 struct ifnet *ifp; 1710{ 1711 struct rl_softc *sc; 1712 struct mii_data *mii; 1713 1714 sc = ifp->if_softc; 1715 mii = device_get_softc(sc->rl_miibus); 1716 mii_mediachg(mii); 1717 1718 return(0); 1719} 1720 1721/* 1722 * Report current media status. 1723 */ 1724static void 1725rl_ifmedia_sts(ifp, ifmr) 1726 struct ifnet *ifp; 1727 struct ifmediareq *ifmr; 1728{ 1729 struct rl_softc *sc; 1730 struct mii_data *mii; 1731 1732 sc = ifp->if_softc; 1733 mii = device_get_softc(sc->rl_miibus); 1734 1735 mii_pollstat(mii); 1736 ifmr->ifm_active = mii->mii_media_active; 1737 ifmr->ifm_status = mii->mii_media_status; 1738 1739 return; 1740} 1741 1742static int 1743rl_ioctl(ifp, command, data) 1744 struct ifnet *ifp; 1745 u_long command; 1746 caddr_t data; 1747{ 1748 struct rl_softc *sc = ifp->if_softc; 1749 struct ifreq *ifr = (struct ifreq *) data; 1750 struct mii_data *mii; 1751 int error = 0; 1752 1753 RL_LOCK(sc); 1754 1755 switch(command) { 1756 case SIOCSIFFLAGS: 1757 if (ifp->if_flags & IFF_UP) { 1758 rl_init(sc); 1759 } else { 1760 if (ifp->if_flags & IFF_RUNNING) 1761 rl_stop(sc); 1762 } 1763 error = 0; 1764 break; 1765 case SIOCADDMULTI: 1766 case SIOCDELMULTI: 1767 rl_setmulti(sc); 1768 error = 0; 1769 break; 1770 case SIOCGIFMEDIA: 1771 case SIOCSIFMEDIA: 1772 mii = device_get_softc(sc->rl_miibus); 1773 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 1774 break; 1775 default: 1776 error = ether_ioctl(ifp, command, data); 1777 break; 1778 } 1779 1780 RL_UNLOCK(sc); 1781 1782 return(error); 1783} 1784 1785static void 1786rl_watchdog(ifp) 1787 struct ifnet *ifp; 1788{ 1789 struct rl_softc *sc; 1790 1791 sc = ifp->if_softc; 1792 RL_LOCK(sc); 1793 printf("rl%d: watchdog timeout\n", sc->rl_unit); 1794 ifp->if_oerrors++; 1795 1796 rl_txeof(sc); 1797 rl_rxeof(sc); 1798 rl_init(sc); 1799 RL_UNLOCK(sc); 1800 1801 return; 1802} 1803 1804/* 1805 * Stop the adapter and free any mbufs allocated to the 1806 * RX and TX lists. 1807 */ 1808static void 1809rl_stop(sc) 1810 struct rl_softc *sc; 1811{ 1812 register int i; 1813 struct ifnet *ifp; 1814 1815 RL_LOCK(sc); 1816 ifp = &sc->arpcom.ac_if; 1817 ifp->if_timer = 0; 1818 1819 untimeout(rl_tick, sc, sc->rl_stat_ch); 1820 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 1821#ifdef DEVICE_POLLING 1822 ether_poll_deregister(ifp); 1823#endif /* DEVICE_POLLING */ 1824 1825 CSR_WRITE_1(sc, RL_COMMAND, 0x00); 1826 CSR_WRITE_2(sc, RL_IMR, 0x0000); 1827 bus_dmamap_unload(sc->rl_tag, sc->rl_cdata.rl_rx_dmamap); 1828 1829 /* 1830 * Free the TX list buffers. 1831 */ 1832 for (i = 0; i < RL_TX_LIST_CNT; i++) { 1833 if (sc->rl_cdata.rl_tx_chain[i] != NULL) { 1834 bus_dmamap_unload(sc->rl_tag, 1835 sc->rl_cdata.rl_tx_dmamap[i]); 1836 bus_dmamap_destroy(sc->rl_tag, 1837 sc->rl_cdata.rl_tx_dmamap[i]); 1838 m_freem(sc->rl_cdata.rl_tx_chain[i]); 1839 sc->rl_cdata.rl_tx_chain[i] = NULL; 1840 CSR_WRITE_4(sc, RL_TXADDR0 + i, 0x0000000); 1841 } 1842 } 1843 1844 RL_UNLOCK(sc); 1845 return; 1846} 1847 1848/* 1849 * Device suspend routine. Stop the interface and save some PCI 1850 * settings in case the BIOS doesn't restore them properly on 1851 * resume. 1852 */ 1853static int 1854rl_suspend(dev) 1855 device_t dev; 1856{ 1857 register int i; 1858 struct rl_softc *sc; 1859 1860 sc = device_get_softc(dev); 1861 1862 rl_stop(sc); 1863 1864 for (i = 0; i < 5; i++) 1865 sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4); 1866 sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4); 1867 sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1); 1868 sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1); 1869 sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1); 1870 1871 sc->suspended = 1; 1872 1873 return (0); 1874} 1875 1876/* 1877 * Device resume routine. Restore some PCI settings in case the BIOS 1878 * doesn't, re-enable busmastering, and restart the interface if 1879 * appropriate. 1880 */ 1881static int 1882rl_resume(dev) 1883 device_t dev; 1884{ 1885 register int i; 1886 struct rl_softc *sc; 1887 struct ifnet *ifp; 1888 1889 sc = device_get_softc(dev); 1890 ifp = &sc->arpcom.ac_if; 1891 1892 /* better way to do this? */ 1893 for (i = 0; i < 5; i++) 1894 pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4); 1895 pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4); 1896 pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1); 1897 pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1); 1898 pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1); 1899 1900 /* reenable busmastering */ 1901 pci_enable_busmaster(dev); 1902 pci_enable_io(dev, RL_RES); 1903 1904 /* reinitialize interface if necessary */ 1905 if (ifp->if_flags & IFF_UP) 1906 rl_init(sc); 1907 1908 sc->suspended = 0; 1909 1910 return (0); 1911} 1912 1913/* 1914 * Stop all chip I/O so that the kernel's probe routines don't 1915 * get confused by errant DMAs when rebooting. 1916 */ 1917static void 1918rl_shutdown(dev) 1919 device_t dev; 1920{ 1921 struct rl_softc *sc; 1922 1923 sc = device_get_softc(dev); 1924 1925 rl_stop(sc); 1926 1927 return; 1928} 1929