1 /* 2 * Copyright (c) 1997, 1998, 1999 3 * Bill Paul <wpaul@ee.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 * $FreeBSD$ 33 */ 34 35 /* 36 * DEC "tulip" clone ethernet driver. Supports the DEC/Intel 21143 37 * series chips and several workalikes including the following: 38 * 39 * Macronix 98713/98715/98725 PMAC (www.macronix.com) 40 * Macronix/Lite-On 82c115 PNIC II (www.macronix.com) 41 * Lite-On 82c168/82c169 PNIC (www.litecom.com) 42 * ASIX Electronics AX88140A (www.asix.com.tw) 43 * ASIX Electronics AX88141 (www.asix.com.tw) 44 * ADMtek AL981 (www.admtek.com.tw) 45 * ADMtek AN985 (www.admtek.com.tw) 46 * Davicom DM9100, DM9102, DM9102A (www.davicom8.com) 47 * 48 * Datasheets for the 21143 are available at developer.intel.com. 49 * Datasheets for the clone parts can be found at their respective sites. 50 * (Except for the PNIC; see www.freebsd.org/~wpaul/PNIC/pnic.ps.gz.) 51 * The PNIC II is essentially a Macronix 98715A chip; the only difference 52 * worth noting is that its multicast hash table is only 128 bits wide 53 * instead of 512. 54 * 55 * Written by Bill Paul <wpaul@ee.columbia.edu> 56 * Electrical Engineering Department 57 * Columbia University, New York City 58 */ 59 60 /* 61 * The Intel 21143 is the successor to the DEC 21140. It is basically 62 * the same as the 21140 but with a few new features. The 21143 supports 63 * three kinds of media attachments: 64 * 65 * o MII port, for 10Mbps and 100Mbps support and NWAY 66 * autonegotiation provided by an external PHY. 67 * o SYM port, for symbol mode 100Mbps support. 68 * o 10baseT port. 69 * o AUI/BNC port. 70 * 71 * The 100Mbps SYM port and 10baseT port can be used together in 72 * combination with the internal NWAY support to create a 10/100 73 * autosensing configuration. 74 * 75 * Knowing which media is available on a given card is tough: you're 76 * supposed to go slogging through the EEPROM looking for media 77 * description structures. Unfortunately, some card vendors that use 78 * the 21143 don't obey the DEC SROM spec correctly, which means that 79 * what you find in the EEPROM may not agree with reality. Fortunately, 80 * the 21143 provides us a way to get around this issue: lurking in 81 * PCI configuration space is the Configuration Wake-Up Command Register. 82 * This register is loaded with a value from the EEPROM when wake on LAN 83 * mode is enabled; this value tells us quite clearly what kind of media 84 * is attached to the NIC. The main purpose of this register is to tell 85 * the NIC what media to scan when in wake on LAN mode, however by 86 * forcibly enabling wake on LAN mode, we can use to learn what kind of 87 * media a given NIC has available and adapt ourselves accordingly. 88 * 89 * Of course, if the media description blocks in the EEPROM are bogus. 90 * what are the odds that the CWUC aren't bogus as well, right? Well, 91 * the CWUC value is more likely to be correct since wake on LAN mode 92 * won't work correctly without it, and wake on LAN is a big selling 93 * point these days. It's also harder to screw up a single byte than 94 * a whole media descriptor block. 95 * 96 * Note that not all tulip workalikes are handled in this driver: we only 97 * deal with those which are relatively well behaved. The Winbond is 98 * handled separately due to its different register offsets and the 99 * special handling needed for its various bugs. The PNIC is handled 100 * here, but I'm not thrilled about it. 101 * 102 * All of the workalike chips use some form of MII transceiver support 103 * with the exception of the Macronix chips, which also have a SYM port. 104 * The ASIX AX88140A is also documented to have a SYM port, but all 105 * the cards I've seen use an MII transceiver, probably because the 106 * AX88140A doesn't support internal NWAY. 107 */ 108 109 #include <sys/param.h> 110 #include <sys/systm.h> 111 #include <sys/sockio.h> 112 #include <sys/mbuf.h> 113 #include <sys/malloc.h> 114 #include <sys/kernel.h> 115 #include <sys/socket.h> 116 117 #include <net/if.h> 118 #include <net/if_arp.h> 119 #include <net/ethernet.h> 120 #include <net/if_dl.h> 121 #include <net/if_media.h> 122 123 #include <net/bpf.h> 124 125 #include "opt_bdg.h" 126 #ifdef BRIDGE 127 #include <net/bridge.h> 128 #endif 129 130 #include <vm/vm.h> /* for vtophys */ 131 #include <vm/pmap.h> /* for vtophys */ 132 #include <machine/clock.h> /* for DELAY */ 133 #include <machine/bus_pio.h> 134 #include <machine/bus_memio.h> 135 #include <machine/bus.h> 136 #include <machine/resource.h> 137 #include <sys/bus.h> 138 #include <sys/rman.h> 139 140 #include <dev/mii/mii.h> 141 #include <dev/mii/miivar.h> 142 143 #include <pci/pcireg.h> 144 #include <pci/pcivar.h> 145 146 #define DC_USEIOSPACE 147 148 #include <pci/if_dcreg.h> 149 150 /* "controller miibus0" required. See GENERIC if you get errors here. */ 151 #include "miibus_if.h" 152 153 #ifndef lint 154 static const char rcsid[] = 155 "$FreeBSD$"; 156 #endif 157 158 /* 159 * Various supported device vendors/types and their names. 160 */ 161 static struct dc_type dc_devs[] = { 162 { DC_VENDORID_DEC, DC_DEVICEID_21143, 163 "Intel 21143 10/100BaseTX" }, 164 { DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100, 165 "Davicom DM9100 10/100BaseTX" }, 166 { DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102, 167 "Davicom DM9102 10/100BaseTX" }, 168 { DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102, 169 "Davicom DM9102A 10/100BaseTX" }, 170 { DC_VENDORID_ADMTEK, DC_DEVICEID_AL981, 171 "ADMtek AL981 10/100BaseTX" }, 172 { DC_VENDORID_ADMTEK, DC_DEVICEID_AN985, 173 "ADMtek AN985 10/100BaseTX" }, 174 { DC_VENDORID_ASIX, DC_DEVICEID_AX88140A, 175 "ASIX AX88140A 10/100BaseTX" }, 176 { DC_VENDORID_ASIX, DC_DEVICEID_AX88140A, 177 "ASIX AX88141 10/100BaseTX" }, 178 { DC_VENDORID_MX, DC_DEVICEID_98713, 179 "Macronix 98713 10/100BaseTX" }, 180 { DC_VENDORID_MX, DC_DEVICEID_98713, 181 "Macronix 98713A 10/100BaseTX" }, 182 { DC_VENDORID_CP, DC_DEVICEID_98713_CP, 183 "Compex RL100-TX 10/100BaseTX" }, 184 { DC_VENDORID_CP, DC_DEVICEID_98713_CP, 185 "Compex RL100-TX 10/100BaseTX" }, 186 { DC_VENDORID_MX, DC_DEVICEID_987x5, 187 "Macronix 98715/98715A 10/100BaseTX" }, 188 { DC_VENDORID_MX, DC_DEVICEID_987x5, 189 "Macronix 98725 10/100BaseTX" }, 190 { DC_VENDORID_LO, DC_DEVICEID_82C115, 191 "LC82C115 PNIC II 10/100BaseTX" }, 192 { DC_VENDORID_LO, DC_DEVICEID_82C168, 193 "82c168 PNIC 10/100BaseTX" }, 194 { DC_VENDORID_LO, DC_DEVICEID_82C168, 195 "82c169 PNIC 10/100BaseTX" }, 196 { 0, 0, NULL } 197 }; 198 199 static int dc_probe __P((device_t)); 200 static int dc_attach __P((device_t)); 201 static int dc_detach __P((device_t)); 202 static void dc_acpi __P((device_t)); 203 static struct dc_type *dc_devtype __P((device_t)); 204 static int dc_newbuf __P((struct dc_softc *, int, struct mbuf *)); 205 static int dc_encap __P((struct dc_softc *, struct mbuf *, 206 u_int32_t *)); 207 static int dc_coal __P((struct dc_softc *, struct mbuf **)); 208 static void dc_pnic_rx_bug_war __P((struct dc_softc *, int)); 209 static int dc_rx_resync __P((struct dc_softc *)); 210 static void dc_rxeof __P((struct dc_softc *)); 211 static void dc_txeof __P((struct dc_softc *)); 212 static void dc_tick __P((void *)); 213 static void dc_intr __P((void *)); 214 static void dc_start __P((struct ifnet *)); 215 static int dc_ioctl __P((struct ifnet *, u_long, caddr_t)); 216 static void dc_init __P((void *)); 217 static void dc_stop __P((struct dc_softc *)); 218 static void dc_watchdog __P((struct ifnet *)); 219 static void dc_shutdown __P((device_t)); 220 static int dc_ifmedia_upd __P((struct ifnet *)); 221 static void dc_ifmedia_sts __P((struct ifnet *, struct ifmediareq *)); 222 223 static void dc_delay __P((struct dc_softc *)); 224 static void dc_eeprom_idle __P((struct dc_softc *)); 225 static void dc_eeprom_putbyte __P((struct dc_softc *, int)); 226 static void dc_eeprom_getword __P((struct dc_softc *, int, u_int16_t *)); 227 static void dc_eeprom_getword_pnic 228 __P((struct dc_softc *, int, u_int16_t *)); 229 static void dc_read_eeprom __P((struct dc_softc *, caddr_t, int, 230 int, int)); 231 232 static void dc_mii_writebit __P((struct dc_softc *, int)); 233 static int dc_mii_readbit __P((struct dc_softc *)); 234 static void dc_mii_sync __P((struct dc_softc *)); 235 static void dc_mii_send __P((struct dc_softc *, u_int32_t, int)); 236 static int dc_mii_readreg __P((struct dc_softc *, struct dc_mii_frame *)); 237 static int dc_mii_writereg __P((struct dc_softc *, struct dc_mii_frame *)); 238 static int dc_miibus_readreg __P((device_t, int, int)); 239 static int dc_miibus_writereg __P((device_t, int, int, int)); 240 static void dc_miibus_statchg __P((device_t)); 241 static void dc_miibus_mediainit __P((device_t)); 242 243 static void dc_setcfg __P((struct dc_softc *, int)); 244 static u_int32_t dc_crc_le __P((struct dc_softc *, caddr_t)); 245 static u_int32_t dc_crc_be __P((caddr_t)); 246 static void dc_setfilt_21143 __P((struct dc_softc *)); 247 static void dc_setfilt_asix __P((struct dc_softc *)); 248 static void dc_setfilt_admtek __P((struct dc_softc *)); 249 250 static void dc_setfilt __P((struct dc_softc *)); 251 252 static void dc_reset __P((struct dc_softc *)); 253 static int dc_list_rx_init __P((struct dc_softc *)); 254 static int dc_list_tx_init __P((struct dc_softc *)); 255 256 #ifdef DC_USEIOSPACE 257 #define DC_RES SYS_RES_IOPORT 258 #define DC_RID DC_PCI_CFBIO 259 #else 260 #define DC_RES SYS_RES_MEMORY 261 #define DC_RID DC_PCI_CFBMA 262 #endif 263 264 static device_method_t dc_methods[] = { 265 /* Device interface */ 266 DEVMETHOD(device_probe, dc_probe), 267 DEVMETHOD(device_attach, dc_attach), 268 DEVMETHOD(device_detach, dc_detach), 269 DEVMETHOD(device_shutdown, dc_shutdown), 270 271 /* bus interface */ 272 DEVMETHOD(bus_print_child, bus_generic_print_child), 273 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 274 275 /* MII interface */ 276 DEVMETHOD(miibus_readreg, dc_miibus_readreg), 277 DEVMETHOD(miibus_writereg, dc_miibus_writereg), 278 DEVMETHOD(miibus_statchg, dc_miibus_statchg), 279 DEVMETHOD(miibus_mediainit, dc_miibus_mediainit), 280 281 { 0, 0 } 282 }; 283 284 static driver_t dc_driver = { 285 "dc", 286 dc_methods, 287 sizeof(struct dc_softc) 288 }; 289 290 static devclass_t dc_devclass; 291 292 DRIVER_MODULE(if_dc, pci, dc_driver, dc_devclass, 0, 0); 293 DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, 0, 0); 294 295 #define DC_SETBIT(sc, reg, x) \ 296 CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x)) 297 298 #define DC_CLRBIT(sc, reg, x) \ 299 CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x)) 300 301 #define SIO_SET(x) DC_SETBIT(sc, DC_SIO, (x)) 302 #define SIO_CLR(x) DC_CLRBIT(sc, DC_SIO, (x)) 303 304 static void dc_delay(sc) 305 struct dc_softc *sc; 306 { 307 int idx; 308 309 for (idx = (300 / 33) + 1; idx > 0; idx--) 310 CSR_READ_4(sc, DC_BUSCTL); 311 } 312 313 static void dc_eeprom_idle(sc) 314 struct dc_softc *sc; 315 { 316 register int i; 317 318 CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL); 319 dc_delay(sc); 320 DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ); 321 dc_delay(sc); 322 DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK); 323 dc_delay(sc); 324 DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS); 325 dc_delay(sc); 326 327 for (i = 0; i < 25; i++) { 328 DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK); 329 dc_delay(sc); 330 DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK); 331 dc_delay(sc); 332 } 333 334 DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK); 335 dc_delay(sc); 336 DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS); 337 dc_delay(sc); 338 CSR_WRITE_4(sc, DC_SIO, 0x00000000); 339 340 return; 341 } 342 343 /* 344 * Send a read command and address to the EEPROM, check for ACK. 345 */ 346 static void dc_eeprom_putbyte(sc, addr) 347 struct dc_softc *sc; 348 int addr; 349 { 350 register int d, i; 351 352 /* 353 * The AN985 has a 93C66 EEPROM on it instead of 354 * a 93C46. It uses a different bit sequence for 355 * specifying the "read" opcode. 356 */ 357 if (DC_IS_CENTAUR(sc)) 358 d = addr | (DC_EECMD_READ << 2); 359 else 360 d = addr | DC_EECMD_READ; 361 362 /* 363 * Feed in each bit and strobe the clock. 364 */ 365 for (i = 0x400; i; i >>= 1) { 366 if (d & i) { 367 SIO_SET(DC_SIO_EE_DATAIN); 368 } else { 369 SIO_CLR(DC_SIO_EE_DATAIN); 370 } 371 dc_delay(sc); 372 SIO_SET(DC_SIO_EE_CLK); 373 dc_delay(sc); 374 SIO_CLR(DC_SIO_EE_CLK); 375 dc_delay(sc); 376 } 377 378 return; 379 } 380 381 /* 382 * Read a word of data stored in the EEPROM at address 'addr.' 383 * The PNIC 82c168/82c169 has its own non-standard way to read 384 * the EEPROM. 385 */ 386 static void dc_eeprom_getword_pnic(sc, addr, dest) 387 struct dc_softc *sc; 388 int addr; 389 u_int16_t *dest; 390 { 391 register int i; 392 u_int32_t r; 393 394 CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ|addr); 395 396 for (i = 0; i < DC_TIMEOUT; i++) { 397 DELAY(1); 398 r = CSR_READ_4(sc, DC_SIO); 399 if (!(r & DC_PN_SIOCTL_BUSY)) { 400 *dest = (u_int16_t)(r & 0xFFFF); 401 return; 402 } 403 } 404 405 return; 406 } 407 408 /* 409 * Read a word of data stored in the EEPROM at address 'addr.' 410 */ 411 static void dc_eeprom_getword(sc, addr, dest) 412 struct dc_softc *sc; 413 int addr; 414 u_int16_t *dest; 415 { 416 register int i; 417 u_int16_t word = 0; 418 419 /* Force EEPROM to idle state. */ 420 dc_eeprom_idle(sc); 421 422 /* Enter EEPROM access mode. */ 423 CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL); 424 dc_delay(sc); 425 DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ); 426 dc_delay(sc); 427 DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK); 428 dc_delay(sc); 429 DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS); 430 dc_delay(sc); 431 432 /* 433 * Send address of word we want to read. 434 */ 435 dc_eeprom_putbyte(sc, addr); 436 437 /* 438 * Start reading bits from EEPROM. 439 */ 440 for (i = 0x8000; i; i >>= 1) { 441 SIO_SET(DC_SIO_EE_CLK); 442 dc_delay(sc); 443 if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT) 444 word |= i; 445 dc_delay(sc); 446 SIO_CLR(DC_SIO_EE_CLK); 447 dc_delay(sc); 448 } 449 450 /* Turn off EEPROM access mode. */ 451 dc_eeprom_idle(sc); 452 453 *dest = word; 454 455 return; 456 } 457 458 /* 459 * Read a sequence of words from the EEPROM. 460 */ 461 static void dc_read_eeprom(sc, dest, off, cnt, swap) 462 struct dc_softc *sc; 463 caddr_t dest; 464 int off; 465 int cnt; 466 int swap; 467 { 468 int i; 469 u_int16_t word = 0, *ptr; 470 471 for (i = 0; i < cnt; i++) { 472 if (DC_IS_PNIC(sc)) 473 dc_eeprom_getword_pnic(sc, off + i, &word); 474 else 475 dc_eeprom_getword(sc, off + i, &word); 476 ptr = (u_int16_t *)(dest + (i * 2)); 477 if (swap) 478 *ptr = ntohs(word); 479 else 480 *ptr = word; 481 } 482 483 return; 484 } 485 486 /* 487 * The following two routines are taken from the Macronix 98713 488 * Application Notes pp.19-21. 489 */ 490 /* 491 * Write a bit to the MII bus. 492 */ 493 static void dc_mii_writebit(sc, bit) 494 struct dc_softc *sc; 495 int bit; 496 { 497 if (bit) 498 CSR_WRITE_4(sc, DC_SIO, 499 DC_SIO_ROMCTL_WRITE|DC_SIO_MII_DATAOUT); 500 else 501 CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE); 502 503 DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK); 504 DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK); 505 506 return; 507 } 508 509 /* 510 * Read a bit from the MII bus. 511 */ 512 static int dc_mii_readbit(sc) 513 struct dc_softc *sc; 514 { 515 CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_READ|DC_SIO_MII_DIR); 516 CSR_READ_4(sc, DC_SIO); 517 DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK); 518 DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK); 519 if (CSR_READ_4(sc, DC_SIO) & DC_SIO_MII_DATAIN) 520 return(1); 521 522 return(0); 523 } 524 525 /* 526 * Sync the PHYs by setting data bit and strobing the clock 32 times. 527 */ 528 static void dc_mii_sync(sc) 529 struct dc_softc *sc; 530 { 531 register int i; 532 533 CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE); 534 535 for (i = 0; i < 32; i++) 536 dc_mii_writebit(sc, 1); 537 538 return; 539 } 540 541 /* 542 * Clock a series of bits through the MII. 543 */ 544 static void dc_mii_send(sc, bits, cnt) 545 struct dc_softc *sc; 546 u_int32_t bits; 547 int cnt; 548 { 549 int i; 550 551 for (i = (0x1 << (cnt - 1)); i; i >>= 1) 552 dc_mii_writebit(sc, bits & i); 553 } 554 555 /* 556 * Read an PHY register through the MII. 557 */ 558 static int dc_mii_readreg(sc, frame) 559 struct dc_softc *sc; 560 struct dc_mii_frame *frame; 561 562 { 563 int i, ack, s; 564 565 s = splimp(); 566 567 /* 568 * Set up frame for RX. 569 */ 570 frame->mii_stdelim = DC_MII_STARTDELIM; 571 frame->mii_opcode = DC_MII_READOP; 572 frame->mii_turnaround = 0; 573 frame->mii_data = 0; 574 575 /* 576 * Sync the PHYs. 577 */ 578 dc_mii_sync(sc); 579 580 /* 581 * Send command/address info. 582 */ 583 dc_mii_send(sc, frame->mii_stdelim, 2); 584 dc_mii_send(sc, frame->mii_opcode, 2); 585 dc_mii_send(sc, frame->mii_phyaddr, 5); 586 dc_mii_send(sc, frame->mii_regaddr, 5); 587 588 #ifdef notdef 589 /* Idle bit */ 590 dc_mii_writebit(sc, 1); 591 dc_mii_writebit(sc, 0); 592 #endif 593 594 /* Check for ack */ 595 ack = dc_mii_readbit(sc); 596 597 /* 598 * Now try reading data bits. If the ack failed, we still 599 * need to clock through 16 cycles to keep the PHY(s) in sync. 600 */ 601 if (ack) { 602 for(i = 0; i < 16; i++) { 603 dc_mii_readbit(sc); 604 } 605 goto fail; 606 } 607 608 for (i = 0x8000; i; i >>= 1) { 609 if (!ack) { 610 if (dc_mii_readbit(sc)) 611 frame->mii_data |= i; 612 } 613 } 614 615 fail: 616 617 dc_mii_writebit(sc, 0); 618 dc_mii_writebit(sc, 0); 619 620 splx(s); 621 622 if (ack) 623 return(1); 624 return(0); 625 } 626 627 /* 628 * Write to a PHY register through the MII. 629 */ 630 static int dc_mii_writereg(sc, frame) 631 struct dc_softc *sc; 632 struct dc_mii_frame *frame; 633 634 { 635 int s; 636 637 s = splimp(); 638 /* 639 * Set up frame for TX. 640 */ 641 642 frame->mii_stdelim = DC_MII_STARTDELIM; 643 frame->mii_opcode = DC_MII_WRITEOP; 644 frame->mii_turnaround = DC_MII_TURNAROUND; 645 646 /* 647 * Sync the PHYs. 648 */ 649 dc_mii_sync(sc); 650 651 dc_mii_send(sc, frame->mii_stdelim, 2); 652 dc_mii_send(sc, frame->mii_opcode, 2); 653 dc_mii_send(sc, frame->mii_phyaddr, 5); 654 dc_mii_send(sc, frame->mii_regaddr, 5); 655 dc_mii_send(sc, frame->mii_turnaround, 2); 656 dc_mii_send(sc, frame->mii_data, 16); 657 658 /* Idle bit. */ 659 dc_mii_writebit(sc, 0); 660 dc_mii_writebit(sc, 0); 661 662 splx(s); 663 664 return(0); 665 } 666 667 static int dc_miibus_readreg(dev, phy, reg) 668 device_t dev; 669 int phy, reg; 670 { 671 struct dc_mii_frame frame; 672 struct dc_softc *sc; 673 int i, rval, phy_reg; 674 675 sc = device_get_softc(dev); 676 bzero((char *)&frame, sizeof(frame)); 677 678 /* 679 * Note: both the AL981 and AN985 have internal PHYs, 680 * however the AL981 provides direct access to the PHY 681 * registers while the AN985 uses a serial MII interface. 682 * The AN985's MII interface is also buggy in that you 683 * can read from any MII address (0 to 31), but only address 1 684 * behaves normally. To deal with both cases, we pretend 685 * that the PHY is at MII address 1. 686 */ 687 if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR) 688 return(0); 689 690 if (sc->dc_pmode == DC_PMODE_SYM) { 691 if (phy == (MII_NPHY - 1)) { 692 switch(reg) { 693 case MII_BMSR: 694 /* 695 * Fake something to make the probe 696 * code think there's a PHY here. 697 */ 698 return(BMSR_MEDIAMASK); 699 break; 700 case MII_PHYIDR1: 701 if (DC_IS_PNIC(sc)) 702 return(DC_VENDORID_LO); 703 return(DC_VENDORID_DEC); 704 break; 705 case MII_PHYIDR2: 706 if (DC_IS_PNIC(sc)) 707 return(DC_DEVICEID_82C168); 708 return(DC_DEVICEID_21143); 709 break; 710 default: 711 return(0); 712 break; 713 } 714 } else 715 return(0); 716 } 717 718 if (DC_IS_PNIC(sc)) { 719 CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ | 720 (phy << 23) | (reg << 18)); 721 for (i = 0; i < DC_TIMEOUT; i++) { 722 DELAY(1); 723 rval = CSR_READ_4(sc, DC_PN_MII); 724 if (!(rval & DC_PN_MII_BUSY)) { 725 rval &= 0xFFFF; 726 return(rval == 0xFFFF ? 0 : rval); 727 } 728 } 729 return(0); 730 } 731 732 if (DC_IS_COMET(sc)) { 733 switch(reg) { 734 case MII_BMCR: 735 phy_reg = DC_AL_BMCR; 736 break; 737 case MII_BMSR: 738 phy_reg = DC_AL_BMSR; 739 break; 740 case MII_PHYIDR1: 741 phy_reg = DC_AL_VENID; 742 break; 743 case MII_PHYIDR2: 744 phy_reg = DC_AL_DEVID; 745 break; 746 case MII_ANAR: 747 phy_reg = DC_AL_ANAR; 748 break; 749 case MII_ANLPAR: 750 phy_reg = DC_AL_LPAR; 751 break; 752 case MII_ANER: 753 phy_reg = DC_AL_ANER; 754 break; 755 default: 756 printf("dc%d: phy_read: bad phy register %x\n", 757 sc->dc_unit, reg); 758 return(0); 759 break; 760 } 761 762 rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF; 763 764 if (rval == 0xFFFF) 765 return(0); 766 return(rval); 767 } 768 769 frame.mii_phyaddr = phy; 770 frame.mii_regaddr = reg; 771 phy_reg = CSR_READ_4(sc, DC_NETCFG); 772 CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL); 773 dc_mii_readreg(sc, &frame); 774 CSR_WRITE_4(sc, DC_NETCFG, phy_reg); 775 776 return(frame.mii_data); 777 } 778 779 static int dc_miibus_writereg(dev, phy, reg, data) 780 device_t dev; 781 int phy, reg, data; 782 { 783 struct dc_softc *sc; 784 struct dc_mii_frame frame; 785 int i, phy_reg; 786 787 sc = device_get_softc(dev); 788 bzero((char *)&frame, sizeof(frame)); 789 790 if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR) 791 return(0); 792 793 if (DC_IS_PNIC(sc)) { 794 CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE | 795 (phy << 23) | (reg << 10) | data); 796 for (i = 0; i < DC_TIMEOUT; i++) { 797 if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY)) 798 break; 799 } 800 return(0); 801 } 802 803 if (DC_IS_COMET(sc)) { 804 switch(reg) { 805 case MII_BMCR: 806 phy_reg = DC_AL_BMCR; 807 break; 808 case MII_BMSR: 809 phy_reg = DC_AL_BMSR; 810 break; 811 case MII_PHYIDR1: 812 phy_reg = DC_AL_VENID; 813 break; 814 case MII_PHYIDR2: 815 phy_reg = DC_AL_DEVID; 816 break; 817 case MII_ANAR: 818 phy_reg = DC_AL_ANAR; 819 break; 820 case MII_ANLPAR: 821 phy_reg = DC_AL_LPAR; 822 break; 823 case MII_ANER: 824 phy_reg = DC_AL_ANER; 825 break; 826 default: 827 printf("dc%d: phy_write: bad phy register %x\n", 828 sc->dc_unit, reg); 829 return(0); 830 break; 831 } 832 833 CSR_WRITE_4(sc, phy_reg, data); 834 return(0); 835 } 836 837 frame.mii_phyaddr = phy; 838 frame.mii_regaddr = reg; 839 frame.mii_data = data; 840 841 phy_reg = CSR_READ_4(sc, DC_NETCFG); 842 CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL); 843 dc_mii_writereg(sc, &frame); 844 CSR_WRITE_4(sc, DC_NETCFG, phy_reg); 845 846 return(0); 847 } 848 849 static void dc_miibus_statchg(dev) 850 device_t dev; 851 { 852 struct dc_softc *sc; 853 struct mii_data *mii; 854 struct ifmedia *ifm; 855 856 sc = device_get_softc(dev); 857 if (DC_IS_ADMTEK(sc)) 858 return; 859 mii = device_get_softc(sc->dc_miibus); 860 ifm = &mii->mii_media; 861 if (DC_IS_DAVICOM(sc) && 862 IFM_SUBTYPE(ifm->ifm_media) == IFM_homePNA) { 863 dc_setcfg(sc, ifm->ifm_media); 864 sc->dc_if_media = ifm->ifm_media; 865 } else { 866 dc_setcfg(sc, mii->mii_media_active); 867 sc->dc_if_media = mii->mii_media_active; 868 } 869 870 return; 871 } 872 873 /* 874 * Special support for DM9102A cards with HomePNA PHYs. Note: 875 * with the Davicom DM9102A/DM9801 eval board that I have, it seems 876 * to be impossible to talk to the management interface of the DM9801 877 * PHY (its MDIO pin is not connected to anything). Consequently, 878 * the driver has to just 'know' about the additional mode and deal 879 * with it itself. *sigh* 880 */ 881 static void dc_miibus_mediainit(dev) 882 device_t dev; 883 { 884 struct dc_softc *sc; 885 struct mii_data *mii; 886 struct ifmedia *ifm; 887 int rev; 888 889 rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF; 890 891 sc = device_get_softc(dev); 892 mii = device_get_softc(sc->dc_miibus); 893 ifm = &mii->mii_media; 894 895 if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A) 896 ifmedia_add(ifm, IFM_ETHER|IFM_homePNA, 0, NULL); 897 898 return; 899 } 900 901 #define DC_POLY 0xEDB88320 902 #define DC_BITS 9 903 #define DC_BITS_PNIC_II 7 904 905 static u_int32_t dc_crc_le(sc, addr) 906 struct dc_softc *sc; 907 caddr_t addr; 908 { 909 u_int32_t idx, bit, data, crc; 910 911 /* Compute CRC for the address value. */ 912 crc = 0xFFFFFFFF; /* initial value */ 913 914 for (idx = 0; idx < 6; idx++) { 915 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1) 916 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? DC_POLY : 0); 917 } 918 919 /* The hash table on the PNIC II is only 128 bits wide. */ 920 if (DC_IS_PNICII(sc)) 921 return (crc & ((1 << DC_BITS_PNIC_II) - 1)); 922 923 return (crc & ((1 << DC_BITS) - 1)); 924 } 925 926 /* 927 * Calculate CRC of a multicast group address, return the lower 6 bits. 928 */ 929 static u_int32_t dc_crc_be(addr) 930 caddr_t addr; 931 { 932 u_int32_t crc, carry; 933 int i, j; 934 u_int8_t c; 935 936 /* Compute CRC for the address value. */ 937 crc = 0xFFFFFFFF; /* initial value */ 938 939 for (i = 0; i < 6; i++) { 940 c = *(addr + i); 941 for (j = 0; j < 8; j++) { 942 carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01); 943 crc <<= 1; 944 c >>= 1; 945 if (carry) 946 crc = (crc ^ 0x04c11db6) | carry; 947 } 948 } 949 950 /* return the filter bit position */ 951 return((crc >> 26) & 0x0000003F); 952 } 953 954 /* 955 * 21143-style RX filter setup routine. Filter programming is done by 956 * downloading a special setup frame into the TX engine. 21143, Macronix, 957 * PNIC, PNIC II and Davicom chips are programmed this way. 958 * 959 * We always program the chip using 'hash perfect' mode, i.e. one perfect 960 * address (our node address) and a 512-bit hash filter for multicast 961 * frames. We also sneak the broadcast address into the hash filter since 962 * we need that too. 963 */ 964 void dc_setfilt_21143(sc) 965 struct dc_softc *sc; 966 { 967 struct dc_desc *sframe; 968 u_int32_t h, *sp; 969 struct ifmultiaddr *ifma; 970 struct ifnet *ifp; 971 int i; 972 973 ifp = &sc->arpcom.ac_if; 974 975 i = sc->dc_cdata.dc_tx_prod; 976 DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT); 977 sc->dc_cdata.dc_tx_cnt++; 978 sframe = &sc->dc_ldata->dc_tx_list[i]; 979 sp = (u_int32_t *)&sc->dc_cdata.dc_sbuf; 980 bzero((char *)sp, DC_SFRAME_LEN); 981 982 sframe->dc_data = vtophys(&sc->dc_cdata.dc_sbuf); 983 sframe->dc_ctl = DC_SFRAME_LEN | DC_TXCTL_SETUP | DC_TXCTL_TLINK | 984 DC_FILTER_HASHPERF | DC_TXCTL_FINT; 985 986 sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)&sc->dc_cdata.dc_sbuf; 987 988 /* If we want promiscuous mode, set the allframes bit. */ 989 if (ifp->if_flags & IFF_PROMISC) 990 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC); 991 else 992 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC); 993 994 if (ifp->if_flags & IFF_ALLMULTI) 995 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI); 996 else 997 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI); 998 999 for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL; 1000 ifma = ifma->ifma_link.le_next) { 1001 if (ifma->ifma_addr->sa_family != AF_LINK) 1002 continue; 1003 h = dc_crc_le(sc, 1004 LLADDR((struct sockaddr_dl *)ifma->ifma_addr)); 1005 sp[h >> 4] |= 1 << (h & 0xF); 1006 } 1007 1008 if (ifp->if_flags & IFF_BROADCAST) { 1009 h = dc_crc_le(sc, (caddr_t)ðerbroadcastaddr); 1010 sp[h >> 4] |= 1 << (h & 0xF); 1011 } 1012 1013 /* Set our MAC address */ 1014 sp[39] = ((u_int16_t *)sc->arpcom.ac_enaddr)[0]; 1015 sp[40] = ((u_int16_t *)sc->arpcom.ac_enaddr)[1]; 1016 sp[41] = ((u_int16_t *)sc->arpcom.ac_enaddr)[2]; 1017 1018 sframe->dc_status = DC_TXSTAT_OWN; 1019 CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF); 1020 1021 /* 1022 * The PNIC takes an exceedingly long time to process its 1023 * setup frame; wait 10ms after posting the setup frame 1024 * before proceeding, just so it has time to swallow its 1025 * medicine. 1026 */ 1027 DELAY(10000); 1028 1029 ifp->if_timer = 5; 1030 1031 return; 1032 } 1033 1034 void dc_setfilt_admtek(sc) 1035 struct dc_softc *sc; 1036 { 1037 struct ifnet *ifp; 1038 int h = 0; 1039 u_int32_t hashes[2] = { 0, 0 }; 1040 struct ifmultiaddr *ifma; 1041 1042 ifp = &sc->arpcom.ac_if; 1043 1044 /* Init our MAC address */ 1045 CSR_WRITE_4(sc, DC_AL_PAR0, *(u_int32_t *)(&sc->arpcom.ac_enaddr[0])); 1046 CSR_WRITE_4(sc, DC_AL_PAR1, *(u_int32_t *)(&sc->arpcom.ac_enaddr[4])); 1047 1048 /* If we want promiscuous mode, set the allframes bit. */ 1049 if (ifp->if_flags & IFF_PROMISC) 1050 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC); 1051 else 1052 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC); 1053 1054 if (ifp->if_flags & IFF_ALLMULTI) 1055 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI); 1056 else 1057 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI); 1058 1059 /* first, zot all the existing hash bits */ 1060 CSR_WRITE_4(sc, DC_AL_MAR0, 0); 1061 CSR_WRITE_4(sc, DC_AL_MAR1, 0); 1062 1063 /* 1064 * If we're already in promisc or allmulti mode, we 1065 * don't have to bother programming the multicast filter. 1066 */ 1067 if (ifp->if_flags & (IFF_PROMISC|IFF_ALLMULTI)) 1068 return; 1069 1070 /* now program new ones */ 1071 for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL; 1072 ifma = ifma->ifma_link.le_next) { 1073 if (ifma->ifma_addr->sa_family != AF_LINK) 1074 continue; 1075 h = dc_crc_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr)); 1076 if (h < 32) 1077 hashes[0] |= (1 << h); 1078 else 1079 hashes[1] |= (1 << (h - 32)); 1080 } 1081 1082 CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]); 1083 CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]); 1084 1085 return; 1086 } 1087 1088 void dc_setfilt_asix(sc) 1089 struct dc_softc *sc; 1090 { 1091 struct ifnet *ifp; 1092 int h = 0; 1093 u_int32_t hashes[2] = { 0, 0 }; 1094 struct ifmultiaddr *ifma; 1095 1096 ifp = &sc->arpcom.ac_if; 1097 1098 /* Init our MAC address */ 1099 CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0); 1100 CSR_WRITE_4(sc, DC_AX_FILTDATA, 1101 *(u_int32_t *)(&sc->arpcom.ac_enaddr[0])); 1102 CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1); 1103 CSR_WRITE_4(sc, DC_AX_FILTDATA, 1104 *(u_int32_t *)(&sc->arpcom.ac_enaddr[4])); 1105 1106 /* If we want promiscuous mode, set the allframes bit. */ 1107 if (ifp->if_flags & IFF_PROMISC) 1108 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC); 1109 else 1110 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC); 1111 1112 if (ifp->if_flags & IFF_ALLMULTI) 1113 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI); 1114 else 1115 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI); 1116 1117 /* 1118 * The ASIX chip has a special bit to enable reception 1119 * of broadcast frames. 1120 */ 1121 if (ifp->if_flags & IFF_BROADCAST) 1122 DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD); 1123 else 1124 DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD); 1125 1126 /* first, zot all the existing hash bits */ 1127 CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0); 1128 CSR_WRITE_4(sc, DC_AX_FILTDATA, 0); 1129 CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1); 1130 CSR_WRITE_4(sc, DC_AX_FILTDATA, 0); 1131 1132 /* 1133 * If we're already in promisc or allmulti mode, we 1134 * don't have to bother programming the multicast filter. 1135 */ 1136 if (ifp->if_flags & (IFF_PROMISC|IFF_ALLMULTI)) 1137 return; 1138 1139 /* now program new ones */ 1140 for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL; 1141 ifma = ifma->ifma_link.le_next) { 1142 if (ifma->ifma_addr->sa_family != AF_LINK) 1143 continue; 1144 h = dc_crc_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr)); 1145 if (h < 32) 1146 hashes[0] |= (1 << h); 1147 else 1148 hashes[1] |= (1 << (h - 32)); 1149 } 1150 1151 CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0); 1152 CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]); 1153 CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1); 1154 CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]); 1155 1156 return; 1157 } 1158 1159 static void dc_setfilt(sc) 1160 struct dc_softc *sc; 1161 { 1162 if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) || 1163 DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc)) 1164 dc_setfilt_21143(sc); 1165 1166 if (DC_IS_ASIX(sc)) 1167 dc_setfilt_asix(sc); 1168 1169 if (DC_IS_ADMTEK(sc)) 1170 dc_setfilt_admtek(sc); 1171 1172 return; 1173 } 1174 1175 /* 1176 * In order to fiddle with the 1177 * 'full-duplex' and '100Mbps' bits in the netconfig register, we 1178 * first have to put the transmit and/or receive logic in the idle state. 1179 */ 1180 static void dc_setcfg(sc, media) 1181 struct dc_softc *sc; 1182 int media; 1183 { 1184 int i, restart = 0; 1185 u_int32_t isr; 1186 1187 if (IFM_SUBTYPE(media) == IFM_NONE) 1188 return; 1189 1190 if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON|DC_NETCFG_RX_ON)) { 1191 restart = 1; 1192 DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON|DC_NETCFG_RX_ON)); 1193 1194 for (i = 0; i < DC_TIMEOUT; i++) { 1195 DELAY(10); 1196 isr = CSR_READ_4(sc, DC_ISR); 1197 if (isr & DC_ISR_TX_IDLE || 1198 (isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED) 1199 break; 1200 } 1201 1202 if (i == DC_TIMEOUT) 1203 printf("dc%d: failed to force tx and " 1204 "rx to idle state\n", sc->dc_unit); 1205 1206 } 1207 1208 if (IFM_SUBTYPE(media) == IFM_100_TX) { 1209 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL); 1210 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT); 1211 if (sc->dc_pmode == DC_PMODE_MII) { 1212 DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS); 1213 DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS| 1214 DC_NETCFG_PORTSEL|DC_NETCFG_SCRAMBLER)); 1215 if (sc->dc_type == DC_TYPE_98713) 1216 DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS| 1217 DC_NETCFG_SCRAMBLER)); 1218 if (!DC_IS_DAVICOM(sc)) 1219 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL); 1220 DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF); 1221 } else { 1222 if (DC_IS_PNIC(sc)) { 1223 DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL); 1224 DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP); 1225 DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL); 1226 } 1227 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL| 1228 DC_NETCFG_PCS|DC_NETCFG_SCRAMBLER); 1229 } 1230 } 1231 1232 if (IFM_SUBTYPE(media) == IFM_10_T) { 1233 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL); 1234 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT); 1235 if (sc->dc_pmode == DC_PMODE_MII) { 1236 DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS); 1237 DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS| 1238 DC_NETCFG_PORTSEL|DC_NETCFG_SCRAMBLER)); 1239 if (sc->dc_type == DC_TYPE_98713) 1240 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS); 1241 if (!DC_IS_DAVICOM(sc)) 1242 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL); 1243 DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF); 1244 } else { 1245 if (DC_IS_PNIC(sc)) { 1246 DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL); 1247 DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP); 1248 DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL); 1249 } 1250 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL); 1251 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER); 1252 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS); 1253 } 1254 } 1255 1256 /* 1257 * If this is a Davicom DM9102A card with a DM9801 HomePNA 1258 * PHY and we want HomePNA mode, set the portsel bit to turn 1259 * on the external MII port. 1260 */ 1261 if (DC_IS_DAVICOM(sc)) { 1262 if (IFM_SUBTYPE(media) == IFM_homePNA) { 1263 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL); 1264 sc->dc_link = 1; 1265 } else { 1266 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL); 1267 } 1268 } 1269 1270 if ((media & IFM_GMASK) == IFM_FDX) { 1271 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX); 1272 if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc)) 1273 DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX); 1274 } else { 1275 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX); 1276 if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc)) 1277 DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX); 1278 } 1279 1280 if (restart) 1281 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON|DC_NETCFG_RX_ON); 1282 1283 return; 1284 } 1285 1286 static void dc_reset(sc) 1287 struct dc_softc *sc; 1288 { 1289 register int i; 1290 1291 DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET); 1292 1293 for (i = 0; i < DC_TIMEOUT; i++) { 1294 DELAY(10); 1295 if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET)) 1296 break; 1297 } 1298 1299 if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc)) { 1300 DELAY(10000); 1301 DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET); 1302 i = 0; 1303 } 1304 1305 if (i == DC_TIMEOUT) 1306 printf("dc%d: reset never completed!\n", sc->dc_unit); 1307 1308 /* Wait a little while for the chip to get its brains in order. */ 1309 DELAY(1000); 1310 1311 CSR_WRITE_4(sc, DC_IMR, 0x00000000); 1312 CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000); 1313 CSR_WRITE_4(sc, DC_NETCFG, 0x00000000); 1314 1315 /* 1316 * Bring the SIA out of reset. In some cases, it looks 1317 * like failing to unreset the SIA soon enough gets it 1318 * into a state where it will never come out of reset 1319 * until we reset the whole chip again. 1320 */ 1321 if (DC_IS_INTEL(sc)) 1322 DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET); 1323 1324 return; 1325 } 1326 1327 static struct dc_type *dc_devtype(dev) 1328 device_t dev; 1329 { 1330 struct dc_type *t; 1331 u_int32_t rev; 1332 1333 t = dc_devs; 1334 1335 while(t->dc_name != NULL) { 1336 if ((pci_get_vendor(dev) == t->dc_vid) && 1337 (pci_get_device(dev) == t->dc_did)) { 1338 /* Check the PCI revision */ 1339 rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF; 1340 if (t->dc_did == DC_DEVICEID_98713 && 1341 rev >= DC_REVISION_98713A) 1342 t++; 1343 if (t->dc_did == DC_DEVICEID_98713_CP && 1344 rev >= DC_REVISION_98713A) 1345 t++; 1346 if (t->dc_did == DC_DEVICEID_987x5 && 1347 rev >= DC_REVISION_98725) 1348 t++; 1349 if (t->dc_did == DC_DEVICEID_AX88140A && 1350 rev >= DC_REVISION_88141) 1351 t++; 1352 if (t->dc_did == DC_DEVICEID_82C168 && 1353 rev >= DC_REVISION_82C169) 1354 t++; 1355 if (t->dc_did == DC_DEVICEID_DM9102 && 1356 rev >= DC_REVISION_DM9102A) 1357 t++; 1358 return(t); 1359 } 1360 t++; 1361 } 1362 1363 return(NULL); 1364 } 1365 1366 /* 1367 * Probe for a 21143 or clone chip. Check the PCI vendor and device 1368 * IDs against our list and return a device name if we find a match. 1369 * We do a little bit of extra work to identify the exact type of 1370 * chip. The MX98713 and MX98713A have the same PCI vendor/device ID, 1371 * but different revision IDs. The same is true for 98715/98715A 1372 * chips and the 98725, as well as the ASIX and ADMtek chips. In some 1373 * cases, the exact chip revision affects driver behavior. 1374 */ 1375 static int dc_probe(dev) 1376 device_t dev; 1377 { 1378 struct dc_type *t; 1379 1380 t = dc_devtype(dev); 1381 1382 if (t != NULL) { 1383 device_set_desc(dev, t->dc_name); 1384 return(0); 1385 } 1386 1387 return(ENXIO); 1388 } 1389 1390 static void dc_acpi(dev) 1391 device_t dev; 1392 { 1393 u_int32_t r, cptr; 1394 int unit; 1395 1396 unit = device_get_unit(dev); 1397 1398 /* Find the location of the capabilities block */ 1399 cptr = pci_read_config(dev, DC_PCI_CCAP, 4) & 0xFF; 1400 1401 r = pci_read_config(dev, cptr, 4) & 0xFF; 1402 if (r == 0x01) { 1403 1404 r = pci_read_config(dev, cptr + 4, 4); 1405 if (r & DC_PSTATE_D3) { 1406 u_int32_t iobase, membase, irq; 1407 1408 /* Save important PCI config data. */ 1409 iobase = pci_read_config(dev, DC_PCI_CFBIO, 4); 1410 membase = pci_read_config(dev, DC_PCI_CFBMA, 4); 1411 irq = pci_read_config(dev, DC_PCI_CFIT, 4); 1412 1413 /* Reset the power state. */ 1414 printf("dc%d: chip is in D%d power mode " 1415 "-- setting to D0\n", unit, r & DC_PSTATE_D3); 1416 r &= 0xFFFFFFFC; 1417 pci_write_config(dev, cptr + 4, r, 4); 1418 1419 /* Restore PCI config data. */ 1420 pci_write_config(dev, DC_PCI_CFBIO, iobase, 4); 1421 pci_write_config(dev, DC_PCI_CFBMA, membase, 4); 1422 pci_write_config(dev, DC_PCI_CFIT, irq, 4); 1423 } 1424 } 1425 return; 1426 } 1427 1428 /* 1429 * Attach the interface. Allocate softc structures, do ifmedia 1430 * setup and ethernet/BPF attach. 1431 */ 1432 static int dc_attach(dev) 1433 device_t dev; 1434 { 1435 int s; 1436 u_char eaddr[ETHER_ADDR_LEN]; 1437 u_int32_t command; 1438 struct dc_softc *sc; 1439 struct ifnet *ifp; 1440 u_int32_t revision; 1441 int unit, error = 0, rid, mac_offset; 1442 1443 s = splimp(); 1444 1445 sc = device_get_softc(dev); 1446 unit = device_get_unit(dev); 1447 bzero(sc, sizeof(struct dc_softc)); 1448 1449 /* 1450 * Handle power management nonsense. 1451 */ 1452 dc_acpi(dev); 1453 1454 /* 1455 * Map control/status registers. 1456 */ 1457 command = pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4); 1458 command |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN); 1459 pci_write_config(dev, PCI_COMMAND_STATUS_REG, command, 4); 1460 command = pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4); 1461 1462 #ifdef DC_USEIOSPACE 1463 if (!(command & PCIM_CMD_PORTEN)) { 1464 printf("dc%d: failed to enable I/O ports!\n", unit); 1465 error = ENXIO; 1466 goto fail; 1467 } 1468 #else 1469 if (!(command & PCIM_CMD_MEMEN)) { 1470 printf("dc%d: failed to enable memory mapping!\n", unit); 1471 error = ENXIO; 1472 goto fail; 1473 } 1474 #endif 1475 1476 rid = DC_RID; 1477 sc->dc_res = bus_alloc_resource(dev, DC_RES, &rid, 1478 0, ~0, 1, RF_ACTIVE); 1479 1480 if (sc->dc_res == NULL) { 1481 printf("dc%d: couldn't map ports/memory\n", unit); 1482 error = ENXIO; 1483 goto fail; 1484 } 1485 1486 sc->dc_btag = rman_get_bustag(sc->dc_res); 1487 sc->dc_bhandle = rman_get_bushandle(sc->dc_res); 1488 1489 /* Allocate interrupt */ 1490 rid = 0; 1491 sc->dc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, 1492 RF_SHAREABLE | RF_ACTIVE); 1493 1494 if (sc->dc_irq == NULL) { 1495 printf("dc%d: couldn't map interrupt\n", unit); 1496 bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res); 1497 error = ENXIO; 1498 goto fail; 1499 } 1500 1501 error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET, 1502 dc_intr, sc, &sc->dc_intrhand); 1503 1504 if (error) { 1505 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq); 1506 bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res); 1507 printf("dc%d: couldn't set up irq\n", unit); 1508 goto fail; 1509 } 1510 1511 /* Need this info to decide on a chip type. */ 1512 sc->dc_info = dc_devtype(dev); 1513 revision = pci_read_config(dev, DC_PCI_CFRV, 4) & 0x000000FF; 1514 1515 switch(sc->dc_info->dc_did) { 1516 case DC_DEVICEID_21143: 1517 sc->dc_type = DC_TYPE_21143; 1518 sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR; 1519 sc->dc_flags |= DC_REDUCED_MII_POLL; 1520 break; 1521 case DC_DEVICEID_DM9100: 1522 case DC_DEVICEID_DM9102: 1523 sc->dc_type = DC_TYPE_DM9102; 1524 sc->dc_flags |= DC_TX_COALESCE|DC_TX_USE_TX_INTR; 1525 sc->dc_flags |= DC_REDUCED_MII_POLL; 1526 sc->dc_pmode = DC_PMODE_MII; 1527 break; 1528 case DC_DEVICEID_AL981: 1529 sc->dc_type = DC_TYPE_AL981; 1530 sc->dc_flags |= DC_TX_USE_TX_INTR; 1531 sc->dc_flags |= DC_TX_ADMTEK_WAR; 1532 sc->dc_pmode = DC_PMODE_MII; 1533 break; 1534 case DC_DEVICEID_AN985: 1535 sc->dc_type = DC_TYPE_AN985; 1536 sc->dc_flags |= DC_TX_USE_TX_INTR; 1537 sc->dc_flags |= DC_TX_ADMTEK_WAR; 1538 sc->dc_pmode = DC_PMODE_MII; 1539 break; 1540 case DC_DEVICEID_98713: 1541 case DC_DEVICEID_98713_CP: 1542 if (revision < DC_REVISION_98713A) { 1543 sc->dc_type = DC_TYPE_98713; 1544 sc->dc_flags |= DC_REDUCED_MII_POLL; 1545 } 1546 if (revision >= DC_REVISION_98713A) 1547 sc->dc_type = DC_TYPE_98713A; 1548 sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR; 1549 break; 1550 case DC_DEVICEID_987x5: 1551 sc->dc_type = DC_TYPE_987x5; 1552 sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR; 1553 break; 1554 case DC_DEVICEID_82C115: 1555 sc->dc_type = DC_TYPE_PNICII; 1556 sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR; 1557 break; 1558 case DC_DEVICEID_82C168: 1559 sc->dc_type = DC_TYPE_PNIC; 1560 sc->dc_flags |= DC_TX_STORENFWD|DC_TX_INTR_ALWAYS; 1561 sc->dc_flags |= DC_PNIC_RX_BUG_WAR; 1562 sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT); 1563 if (revision < DC_REVISION_82C169) 1564 sc->dc_pmode = DC_PMODE_SYM; 1565 break; 1566 case DC_DEVICEID_AX88140A: 1567 sc->dc_type = DC_TYPE_ASIX; 1568 sc->dc_flags |= DC_TX_USE_TX_INTR|DC_TX_INTR_FIRSTFRAG; 1569 sc->dc_flags |= DC_REDUCED_MII_POLL; 1570 sc->dc_pmode = DC_PMODE_MII; 1571 break; 1572 default: 1573 printf("dc%d: unknown device: %x\n", sc->dc_unit, 1574 sc->dc_info->dc_did); 1575 break; 1576 } 1577 1578 /* Save the cache line size. */ 1579 if (DC_IS_DAVICOM(sc)) 1580 sc->dc_cachesize = 0; 1581 else 1582 sc->dc_cachesize = pci_read_config(dev, 1583 DC_PCI_CFLT, 4) & 0xFF; 1584 1585 /* Reset the adapter. */ 1586 dc_reset(sc); 1587 1588 /* Take 21143 out of snooze mode */ 1589 if (DC_IS_INTEL(sc)) { 1590 command = pci_read_config(dev, DC_PCI_CFDD, 4); 1591 command &= ~(DC_CFDD_SNOOZE_MODE|DC_CFDD_SLEEP_MODE); 1592 pci_write_config(dev, DC_PCI_CFDD, command, 4); 1593 } 1594 1595 /* 1596 * Try to learn something about the supported media. 1597 * We know that ASIX and ADMtek and Davicom devices 1598 * will *always* be using MII media, so that's a no-brainer. 1599 * The tricky ones are the Macronix/PNIC II and the 1600 * Intel 21143. 1601 */ 1602 if (DC_IS_INTEL(sc)) { 1603 u_int32_t media, cwuc; 1604 cwuc = pci_read_config(dev, DC_PCI_CWUC, 4); 1605 cwuc |= DC_CWUC_FORCE_WUL; 1606 pci_write_config(dev, DC_PCI_CWUC, cwuc, 4); 1607 DELAY(10000); 1608 media = pci_read_config(dev, DC_PCI_CWUC, 4); 1609 cwuc &= ~DC_CWUC_FORCE_WUL; 1610 pci_write_config(dev, DC_PCI_CWUC, cwuc, 4); 1611 DELAY(10000); 1612 if (media & DC_CWUC_MII_ABILITY) 1613 sc->dc_pmode = DC_PMODE_MII; 1614 if (media & DC_CWUC_SYM_ABILITY) 1615 sc->dc_pmode = DC_PMODE_SYM; 1616 /* 1617 * If none of the bits are set, then this NIC 1618 * isn't meant to support 'wake up LAN' mode. 1619 * This is usually only the case on multiport 1620 * cards, and these cards almost always have 1621 * MII transceivers. 1622 */ 1623 if (media == 0) 1624 sc->dc_pmode = DC_PMODE_MII; 1625 } else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) { 1626 if (sc->dc_type == DC_TYPE_98713) 1627 sc->dc_pmode = DC_PMODE_MII; 1628 else 1629 sc->dc_pmode = DC_PMODE_SYM; 1630 } else if (!sc->dc_pmode) 1631 sc->dc_pmode = DC_PMODE_MII; 1632 1633 /* 1634 * Get station address from the EEPROM. 1635 */ 1636 switch(sc->dc_type) { 1637 case DC_TYPE_98713: 1638 case DC_TYPE_98713A: 1639 case DC_TYPE_987x5: 1640 case DC_TYPE_PNICII: 1641 dc_read_eeprom(sc, (caddr_t)&mac_offset, 1642 (DC_EE_NODEADDR_OFFSET / 2), 1, 0); 1643 dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0); 1644 break; 1645 case DC_TYPE_PNIC: 1646 dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1); 1647 break; 1648 case DC_TYPE_DM9102: 1649 case DC_TYPE_21143: 1650 case DC_TYPE_ASIX: 1651 dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0); 1652 break; 1653 case DC_TYPE_AL981: 1654 case DC_TYPE_AN985: 1655 dc_read_eeprom(sc, (caddr_t)&eaddr, DC_AL_EE_NODEADDR, 3, 0); 1656 break; 1657 default: 1658 dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0); 1659 break; 1660 } 1661 1662 /* 1663 * A 21143 or clone chip was detected. Inform the world. 1664 */ 1665 printf("dc%d: Ethernet address: %6D\n", unit, eaddr, ":"); 1666 1667 sc->dc_unit = unit; 1668 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); 1669 1670 sc->dc_ldata = contigmalloc(sizeof(struct dc_list_data), M_DEVBUF, 1671 M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0); 1672 1673 if (sc->dc_ldata == NULL) { 1674 printf("dc%d: no memory for list buffers!\n", unit); 1675 bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand); 1676 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq); 1677 bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res); 1678 error = ENXIO; 1679 goto fail; 1680 } 1681 1682 bzero(sc->dc_ldata, sizeof(struct dc_list_data)); 1683 1684 ifp = &sc->arpcom.ac_if; 1685 ifp->if_softc = sc; 1686 ifp->if_unit = unit; 1687 ifp->if_name = "dc"; 1688 ifp->if_mtu = ETHERMTU; 1689 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1690 ifp->if_ioctl = dc_ioctl; 1691 ifp->if_output = ether_output; 1692 ifp->if_start = dc_start; 1693 ifp->if_watchdog = dc_watchdog; 1694 ifp->if_init = dc_init; 1695 ifp->if_baudrate = 10000000; 1696 ifp->if_snd.ifq_maxlen = DC_TX_LIST_CNT - 1; 1697 1698 /* 1699 * Do MII setup. 1700 */ 1701 error = mii_phy_probe(dev, &sc->dc_miibus, 1702 dc_ifmedia_upd, dc_ifmedia_sts); 1703 1704 if (error && DC_IS_INTEL(sc)) { 1705 sc->dc_pmode = DC_PMODE_SYM; 1706 mii_phy_probe(dev, &sc->dc_miibus, 1707 dc_ifmedia_upd, dc_ifmedia_sts); 1708 error = 0; 1709 } 1710 1711 if (error) { 1712 printf("dc%d: MII without any PHY!\n", sc->dc_unit); 1713 bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand); 1714 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq); 1715 bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res); 1716 error = ENXIO; 1717 goto fail; 1718 } 1719 1720 /* 1721 * Call MI attach routines. 1722 */ 1723 if_attach(ifp); 1724 ether_ifattach(ifp); 1725 callout_handle_init(&sc->dc_stat_ch); 1726 1727 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header)); 1728 1729 #ifdef __alpha__ 1730 sc->dc_srm_media = 0; 1731 1732 /* Remember the SRM console media setting */ 1733 if (DC_IS_INTEL(sc)) { 1734 command = pci_read_config(dev, DC_PCI_CFDD, 4); 1735 command &= ~(DC_CFDD_SNOOZE_MODE|DC_CFDD_SLEEP_MODE); 1736 switch ((command >> 8) & 0xff) { 1737 case 3: 1738 sc->dc_srm_media = IFM_10_T; 1739 break; 1740 case 4: 1741 sc->dc_srm_media = IFM_10_T | IFM_FDX; 1742 break; 1743 case 5: 1744 sc->dc_srm_media = IFM_100_TX; 1745 break; 1746 case 6: 1747 sc->dc_srm_media = IFM_100_TX | IFM_FDX; 1748 break; 1749 } 1750 if (sc->dc_srm_media) 1751 sc->dc_srm_media |= IFM_ACTIVE | IFM_ETHER; 1752 } 1753 #endif 1754 1755 1756 fail: 1757 splx(s); 1758 1759 return(error); 1760 } 1761 1762 static int dc_detach(dev) 1763 device_t dev; 1764 { 1765 struct dc_softc *sc; 1766 struct ifnet *ifp; 1767 int s; 1768 1769 s = splimp(); 1770 1771 sc = device_get_softc(dev); 1772 ifp = &sc->arpcom.ac_if; 1773 1774 dc_stop(sc); 1775 if_detach(ifp); 1776 1777 bus_generic_detach(dev); 1778 device_delete_child(dev, sc->dc_miibus); 1779 1780 bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand); 1781 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq); 1782 bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res); 1783 1784 contigfree(sc->dc_ldata, sizeof(struct dc_list_data), M_DEVBUF); 1785 if (sc->dc_pnic_rx_buf != NULL) 1786 free(sc->dc_pnic_rx_buf, M_DEVBUF); 1787 1788 splx(s); 1789 1790 return(0); 1791 } 1792 1793 /* 1794 * Initialize the transmit descriptors. 1795 */ 1796 static int dc_list_tx_init(sc) 1797 struct dc_softc *sc; 1798 { 1799 struct dc_chain_data *cd; 1800 struct dc_list_data *ld; 1801 int i; 1802 1803 cd = &sc->dc_cdata; 1804 ld = sc->dc_ldata; 1805 for (i = 0; i < DC_TX_LIST_CNT; i++) { 1806 if (i == (DC_TX_LIST_CNT - 1)) { 1807 ld->dc_tx_list[i].dc_next = 1808 vtophys(&ld->dc_tx_list[0]); 1809 } else { 1810 ld->dc_tx_list[i].dc_next = 1811 vtophys(&ld->dc_tx_list[i + 1]); 1812 } 1813 cd->dc_tx_chain[i] = NULL; 1814 ld->dc_tx_list[i].dc_data = 0; 1815 ld->dc_tx_list[i].dc_ctl = 0; 1816 } 1817 1818 cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0; 1819 1820 return(0); 1821 } 1822 1823 1824 /* 1825 * Initialize the RX descriptors and allocate mbufs for them. Note that 1826 * we arrange the descriptors in a closed ring, so that the last descriptor 1827 * points back to the first. 1828 */ 1829 static int dc_list_rx_init(sc) 1830 struct dc_softc *sc; 1831 { 1832 struct dc_chain_data *cd; 1833 struct dc_list_data *ld; 1834 int i; 1835 1836 cd = &sc->dc_cdata; 1837 ld = sc->dc_ldata; 1838 1839 for (i = 0; i < DC_RX_LIST_CNT; i++) { 1840 if (dc_newbuf(sc, i, NULL) == ENOBUFS) 1841 return(ENOBUFS); 1842 if (i == (DC_RX_LIST_CNT - 1)) { 1843 ld->dc_rx_list[i].dc_next = 1844 vtophys(&ld->dc_rx_list[0]); 1845 } else { 1846 ld->dc_rx_list[i].dc_next = 1847 vtophys(&ld->dc_rx_list[i + 1]); 1848 } 1849 } 1850 1851 cd->dc_rx_prod = 0; 1852 1853 return(0); 1854 } 1855 1856 /* 1857 * Initialize an RX descriptor and attach an MBUF cluster. 1858 */ 1859 static int dc_newbuf(sc, i, m) 1860 struct dc_softc *sc; 1861 int i; 1862 struct mbuf *m; 1863 { 1864 struct mbuf *m_new = NULL; 1865 struct dc_desc *c; 1866 1867 c = &sc->dc_ldata->dc_rx_list[i]; 1868 1869 if (m == NULL) { 1870 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 1871 if (m_new == NULL) { 1872 printf("dc%d: no memory for rx list " 1873 "-- packet dropped!\n", sc->dc_unit); 1874 return(ENOBUFS); 1875 } 1876 1877 MCLGET(m_new, M_DONTWAIT); 1878 if (!(m_new->m_flags & M_EXT)) { 1879 printf("dc%d: no memory for rx list " 1880 "-- packet dropped!\n", sc->dc_unit); 1881 m_freem(m_new); 1882 return(ENOBUFS); 1883 } 1884 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 1885 } else { 1886 m_new = m; 1887 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 1888 m_new->m_data = m_new->m_ext.ext_buf; 1889 } 1890 1891 m_adj(m_new, sizeof(u_int64_t)); 1892 1893 /* 1894 * If this is a PNIC chip, zero the buffer. This is part 1895 * of the workaround for the receive bug in the 82c168 and 1896 * 82c169 chips. 1897 */ 1898 if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) 1899 bzero((char *)mtod(m_new, char *), m_new->m_len); 1900 1901 sc->dc_cdata.dc_rx_chain[i] = m_new; 1902 c->dc_data = vtophys(mtod(m_new, caddr_t)); 1903 c->dc_ctl = DC_RXCTL_RLINK | DC_RXLEN; 1904 c->dc_status = DC_RXSTAT_OWN; 1905 1906 return(0); 1907 } 1908 1909 /* 1910 * Grrrrr. 1911 * The PNIC chip has a terrible bug in it that manifests itself during 1912 * periods of heavy activity. The exact mode of failure if difficult to 1913 * pinpoint: sometimes it only happens in promiscuous mode, sometimes it 1914 * will happen on slow machines. The bug is that sometimes instead of 1915 * uploading one complete frame during reception, it uploads what looks 1916 * like the entire contents of its FIFO memory. The frame we want is at 1917 * the end of the whole mess, but we never know exactly how much data has 1918 * been uploaded, so salvaging the frame is hard. 1919 * 1920 * There is only one way to do it reliably, and it's disgusting. 1921 * Here's what we know: 1922 * 1923 * - We know there will always be somewhere between one and three extra 1924 * descriptors uploaded. 1925 * 1926 * - We know the desired received frame will always be at the end of the 1927 * total data upload. 1928 * 1929 * - We know the size of the desired received frame because it will be 1930 * provided in the length field of the status word in the last descriptor. 1931 * 1932 * Here's what we do: 1933 * 1934 * - When we allocate buffers for the receive ring, we bzero() them. 1935 * This means that we know that the buffer contents should be all 1936 * zeros, except for data uploaded by the chip. 1937 * 1938 * - We also force the PNIC chip to upload frames that include the 1939 * ethernet CRC at the end. 1940 * 1941 * - We gather all of the bogus frame data into a single buffer. 1942 * 1943 * - We then position a pointer at the end of this buffer and scan 1944 * backwards until we encounter the first non-zero byte of data. 1945 * This is the end of the received frame. We know we will encounter 1946 * some data at the end of the frame because the CRC will always be 1947 * there, so even if the sender transmits a packet of all zeros, 1948 * we won't be fooled. 1949 * 1950 * - We know the size of the actual received frame, so we subtract 1951 * that value from the current pointer location. This brings us 1952 * to the start of the actual received packet. 1953 * 1954 * - We copy this into an mbuf and pass it on, along with the actual 1955 * frame length. 1956 * 1957 * The performance hit is tremendous, but it beats dropping frames all 1958 * the time. 1959 */ 1960 1961 #define DC_WHOLEFRAME (DC_RXSTAT_FIRSTFRAG|DC_RXSTAT_LASTFRAG) 1962 static void dc_pnic_rx_bug_war(sc, idx) 1963 struct dc_softc *sc; 1964 int idx; 1965 { 1966 struct dc_desc *cur_rx; 1967 struct dc_desc *c = NULL; 1968 struct mbuf *m = NULL; 1969 unsigned char *ptr; 1970 int i, total_len; 1971 u_int32_t rxstat = 0; 1972 1973 i = sc->dc_pnic_rx_bug_save; 1974 cur_rx = &sc->dc_ldata->dc_rx_list[idx]; 1975 ptr = sc->dc_pnic_rx_buf; 1976 bzero(ptr, sizeof(DC_RXLEN * 5)); 1977 1978 /* Copy all the bytes from the bogus buffers. */ 1979 while (1) { 1980 c = &sc->dc_ldata->dc_rx_list[i]; 1981 rxstat = c->dc_status; 1982 m = sc->dc_cdata.dc_rx_chain[i]; 1983 bcopy(mtod(m, char *), ptr, DC_RXLEN); 1984 ptr += DC_RXLEN; 1985 /* If this is the last buffer, break out. */ 1986 if (i == idx || rxstat & DC_RXSTAT_LASTFRAG) 1987 break; 1988 dc_newbuf(sc, i, m); 1989 DC_INC(i, DC_RX_LIST_CNT); 1990 } 1991 1992 /* Find the length of the actual receive frame. */ 1993 total_len = DC_RXBYTES(rxstat); 1994 1995 /* Scan backwards until we hit a non-zero byte. */ 1996 while(*ptr == 0x00) 1997 ptr--; 1998 1999 /* Round off. */ 2000 if ((uintptr_t)(ptr) & 0x3) 2001 ptr -= 1; 2002 2003 /* Now find the start of the frame. */ 2004 ptr -= total_len; 2005 if (ptr < sc->dc_pnic_rx_buf) 2006 ptr = sc->dc_pnic_rx_buf; 2007 2008 /* 2009 * Now copy the salvaged frame to the last mbuf and fake up 2010 * the status word to make it look like a successful 2011 * frame reception. 2012 */ 2013 dc_newbuf(sc, i, m); 2014 bcopy(ptr, mtod(m, char *), total_len); 2015 cur_rx->dc_status = rxstat | DC_RXSTAT_FIRSTFRAG; 2016 2017 return; 2018 } 2019 2020 /* 2021 * This routine searches the RX ring for dirty descriptors in the 2022 * event that the rxeof routine falls out of sync with the chip's 2023 * current descriptor pointer. This may happen sometimes as a result 2024 * of a "no RX buffer available" condition that happens when the chip 2025 * consumes all of the RX buffers before the driver has a chance to 2026 * process the RX ring. This routine may need to be called more than 2027 * once to bring the driver back in sync with the chip, however we 2028 * should still be getting RX DONE interrupts to drive the search 2029 * for new packets in the RX ring, so we should catch up eventually. 2030 */ 2031 static int dc_rx_resync(sc) 2032 struct dc_softc *sc; 2033 { 2034 int i, pos; 2035 struct dc_desc *cur_rx; 2036 2037 pos = sc->dc_cdata.dc_rx_prod; 2038 2039 for (i = 0; i < DC_RX_LIST_CNT; i++) { 2040 cur_rx = &sc->dc_ldata->dc_rx_list[pos]; 2041 if (!(cur_rx->dc_status & DC_RXSTAT_OWN)) 2042 break; 2043 DC_INC(pos, DC_RX_LIST_CNT); 2044 } 2045 2046 /* If the ring really is empty, then just return. */ 2047 if (i == DC_RX_LIST_CNT) 2048 return(0); 2049 2050 /* We've fallen behing the chip: catch it. */ 2051 sc->dc_cdata.dc_rx_prod = pos; 2052 2053 return(EAGAIN); 2054 } 2055 2056 /* 2057 * A frame has been uploaded: pass the resulting mbuf chain up to 2058 * the higher level protocols. 2059 */ 2060 static void dc_rxeof(sc) 2061 struct dc_softc *sc; 2062 { 2063 struct ether_header *eh; 2064 struct mbuf *m; 2065 struct ifnet *ifp; 2066 struct dc_desc *cur_rx; 2067 int i, total_len = 0; 2068 u_int32_t rxstat; 2069 2070 ifp = &sc->arpcom.ac_if; 2071 i = sc->dc_cdata.dc_rx_prod; 2072 2073 while(!(sc->dc_ldata->dc_rx_list[i].dc_status & DC_RXSTAT_OWN)) { 2074 struct mbuf *m0 = NULL; 2075 2076 cur_rx = &sc->dc_ldata->dc_rx_list[i]; 2077 rxstat = cur_rx->dc_status; 2078 m = sc->dc_cdata.dc_rx_chain[i]; 2079 total_len = DC_RXBYTES(rxstat); 2080 2081 if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) { 2082 if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) { 2083 if (rxstat & DC_RXSTAT_FIRSTFRAG) 2084 sc->dc_pnic_rx_bug_save = i; 2085 if ((rxstat & DC_RXSTAT_LASTFRAG) == 0) { 2086 DC_INC(i, DC_RX_LIST_CNT); 2087 continue; 2088 } 2089 dc_pnic_rx_bug_war(sc, i); 2090 rxstat = cur_rx->dc_status; 2091 total_len = DC_RXBYTES(rxstat); 2092 } 2093 } 2094 2095 sc->dc_cdata.dc_rx_chain[i] = NULL; 2096 2097 /* 2098 * If an error occurs, update stats, clear the 2099 * status word and leave the mbuf cluster in place: 2100 * it should simply get re-used next time this descriptor 2101 * comes up in the ring. 2102 */ 2103 if (rxstat & DC_RXSTAT_RXERR) { 2104 ifp->if_ierrors++; 2105 if (rxstat & DC_RXSTAT_COLLSEEN) 2106 ifp->if_collisions++; 2107 dc_newbuf(sc, i, m); 2108 if (rxstat & DC_RXSTAT_CRCERR) { 2109 DC_INC(i, DC_RX_LIST_CNT); 2110 continue; 2111 } else { 2112 dc_init(sc); 2113 return; 2114 } 2115 } 2116 2117 /* No errors; receive the packet. */ 2118 total_len -= ETHER_CRC_LEN; 2119 2120 m0 = m_devget(mtod(m, char *) - ETHER_ALIGN, 2121 total_len + ETHER_ALIGN, 0, ifp, NULL); 2122 dc_newbuf(sc, i, m); 2123 DC_INC(i, DC_RX_LIST_CNT); 2124 if (m0 == NULL) { 2125 ifp->if_ierrors++; 2126 continue; 2127 } 2128 m_adj(m0, ETHER_ALIGN); 2129 m = m0; 2130 2131 ifp->if_ipackets++; 2132 eh = mtod(m, struct ether_header *); 2133 2134 /* Handle BPF listeners. Let the BPF user see the packet */ 2135 if (ifp->if_bpf) 2136 bpf_mtap(ifp, m); 2137 2138 #ifdef BRIDGE 2139 if (do_bridge) { 2140 struct ifnet *bdg_ifp ; 2141 bdg_ifp = bridge_in(m); 2142 if (bdg_ifp != BDG_LOCAL && bdg_ifp != BDG_DROP) 2143 bdg_forward(&m, bdg_ifp); 2144 if (((bdg_ifp != BDG_LOCAL) && (bdg_ifp != BDG_BCAST) && (bdg_ifp != BDG_MCAST)) || bdg_ifp == BDG_DROP) { 2145 m_freem(m); 2146 continue; 2147 } 2148 } 2149 2150 eh = mtod(m, struct ether_header *); 2151 #endif 2152 2153 /* Don't pass it up to the ether_input() layer unless it's 2154 * a broadcast packet, multicast packet, matches our ethernet 2155 * address or the interface is in promiscuous mode. 2156 */ 2157 if (ifp->if_bpf) { 2158 if (ifp->if_flags & IFF_PROMISC && 2159 (bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr, 2160 ETHER_ADDR_LEN) && 2161 (eh->ether_dhost[0] & 1) == 0)) { 2162 m_freem(m); 2163 continue; 2164 } 2165 } 2166 2167 /* Remove header from mbuf and pass it on. */ 2168 m_adj(m, sizeof(struct ether_header)); 2169 ether_input(ifp, eh, m); 2170 } 2171 2172 sc->dc_cdata.dc_rx_prod = i; 2173 2174 return; 2175 } 2176 2177 /* 2178 * A frame was downloaded to the chip. It's safe for us to clean up 2179 * the list buffers. 2180 */ 2181 2182 static void dc_txeof(sc) 2183 struct dc_softc *sc; 2184 { 2185 struct dc_desc *cur_tx = NULL; 2186 struct ifnet *ifp; 2187 int idx; 2188 2189 ifp = &sc->arpcom.ac_if; 2190 2191 /* Clear the timeout timer. */ 2192 ifp->if_timer = 0; 2193 2194 /* 2195 * Go through our tx list and free mbufs for those 2196 * frames that have been transmitted. 2197 */ 2198 idx = sc->dc_cdata.dc_tx_cons; 2199 while(idx != sc->dc_cdata.dc_tx_prod) { 2200 u_int32_t txstat; 2201 2202 cur_tx = &sc->dc_ldata->dc_tx_list[idx]; 2203 txstat = cur_tx->dc_status; 2204 2205 if (txstat & DC_TXSTAT_OWN) 2206 break; 2207 2208 if (!(cur_tx->dc_ctl & DC_TXCTL_LASTFRAG) || 2209 cur_tx->dc_ctl & DC_TXCTL_SETUP) { 2210 sc->dc_cdata.dc_tx_cnt--; 2211 if (cur_tx->dc_ctl & DC_TXCTL_SETUP) { 2212 /* 2213 * Yes, the PNIC is so brain damaged 2214 * that it will sometimes generate a TX 2215 * underrun error while DMAing the RX 2216 * filter setup frame. If we detect this, 2217 * we have to send the setup frame again, 2218 * or else the filter won't be programmed 2219 * correctly. 2220 */ 2221 if (DC_IS_PNIC(sc)) { 2222 if (txstat & DC_TXSTAT_ERRSUM) 2223 dc_setfilt(sc); 2224 } 2225 sc->dc_cdata.dc_tx_chain[idx] = NULL; 2226 } 2227 DC_INC(idx, DC_TX_LIST_CNT); 2228 continue; 2229 } 2230 2231 if (/*sc->dc_type == DC_TYPE_21143 &&*/ 2232 sc->dc_pmode == DC_PMODE_MII && 2233 ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM| 2234 DC_TXSTAT_NOCARRIER|DC_TXSTAT_CARRLOST))) 2235 txstat &= ~DC_TXSTAT_ERRSUM; 2236 2237 if (txstat & DC_TXSTAT_ERRSUM) { 2238 ifp->if_oerrors++; 2239 if (txstat & DC_TXSTAT_EXCESSCOLL) 2240 ifp->if_collisions++; 2241 if (txstat & DC_TXSTAT_LATECOLL) 2242 ifp->if_collisions++; 2243 if (!(txstat & DC_TXSTAT_UNDERRUN)) { 2244 dc_init(sc); 2245 return; 2246 } 2247 } 2248 2249 ifp->if_collisions += (txstat & DC_TXSTAT_COLLCNT) >> 3; 2250 2251 ifp->if_opackets++; 2252 if (sc->dc_cdata.dc_tx_chain[idx] != NULL) { 2253 m_freem(sc->dc_cdata.dc_tx_chain[idx]); 2254 sc->dc_cdata.dc_tx_chain[idx] = NULL; 2255 } 2256 2257 sc->dc_cdata.dc_tx_cnt--; 2258 DC_INC(idx, DC_TX_LIST_CNT); 2259 } 2260 2261 sc->dc_cdata.dc_tx_cons = idx; 2262 if (cur_tx != NULL) 2263 ifp->if_flags &= ~IFF_OACTIVE; 2264 2265 return; 2266 } 2267 2268 static void dc_tick(xsc) 2269 void *xsc; 2270 { 2271 struct dc_softc *sc; 2272 struct mii_data *mii; 2273 struct ifnet *ifp; 2274 int s; 2275 u_int32_t r; 2276 2277 s = splimp(); 2278 2279 sc = xsc; 2280 ifp = &sc->arpcom.ac_if; 2281 mii = device_get_softc(sc->dc_miibus); 2282 2283 if (sc->dc_flags & DC_REDUCED_MII_POLL) { 2284 r = CSR_READ_4(sc, DC_ISR); 2285 if (DC_IS_INTEL(sc)) { 2286 if (r & DC_ISR_LINKFAIL) 2287 sc->dc_link = 0; 2288 if (sc->dc_link == 0) 2289 mii_tick(mii); 2290 } else { 2291 if ((r & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT && 2292 sc->dc_cdata.dc_tx_prod == 0) 2293 mii_tick(mii); 2294 } 2295 } else 2296 mii_tick(mii); 2297 2298 /* 2299 * When the init routine completes, we expect to be able to send 2300 * packets right away, and in fact the network code will send a 2301 * gratuitous ARP the moment the init routine marks the interface 2302 * as running. However, even though the MAC may have been initialized, 2303 * there may be a delay of a few seconds before the PHY completes 2304 * autonegotiation and the link is brought up. Any transmissions 2305 * made during that delay will be lost. Dealing with this is tricky: 2306 * we can't just pause in the init routine while waiting for the 2307 * PHY to come ready since that would bring the whole system to 2308 * a screeching halt for several seconds. 2309 * 2310 * What we do here is prevent the TX start routine from sending 2311 * any packets until a link has been established. After the 2312 * interface has been initialized, the tick routine will poll 2313 * the state of the PHY until the IFM_ACTIVE flag is set. Until 2314 * that time, packets will stay in the send queue, and once the 2315 * link comes up, they will be flushed out to the wire. 2316 */ 2317 if (!sc->dc_link) { 2318 mii_pollstat(mii); 2319 if (mii->mii_media_status & IFM_ACTIVE && 2320 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 2321 sc->dc_link++; 2322 if (ifp->if_snd.ifq_head != NULL) 2323 dc_start(ifp); 2324 } 2325 } 2326 2327 sc->dc_stat_ch = timeout(dc_tick, sc, hz); 2328 2329 splx(s); 2330 2331 return; 2332 } 2333 2334 static void dc_intr(arg) 2335 void *arg; 2336 { 2337 struct dc_softc *sc; 2338 struct ifnet *ifp; 2339 u_int32_t status; 2340 2341 sc = arg; 2342 ifp = &sc->arpcom.ac_if; 2343 2344 /* Supress unwanted interrupts */ 2345 if (!(ifp->if_flags & IFF_UP)) { 2346 if (CSR_READ_4(sc, DC_ISR) & DC_INTRS) 2347 dc_stop(sc); 2348 return; 2349 } 2350 2351 /* Disable interrupts. */ 2352 CSR_WRITE_4(sc, DC_IMR, 0x00000000); 2353 2354 while((status = CSR_READ_4(sc, DC_ISR)) & DC_INTRS) { 2355 2356 CSR_WRITE_4(sc, DC_ISR, status); 2357 2358 if (status & DC_ISR_RX_OK) { 2359 int curpkts; 2360 curpkts = ifp->if_ipackets; 2361 dc_rxeof(sc); 2362 if (curpkts == ifp->if_ipackets) { 2363 while(dc_rx_resync(sc)) 2364 dc_rxeof(sc); 2365 } 2366 } 2367 2368 if (status & (DC_ISR_TX_OK|DC_ISR_TX_NOBUF)) 2369 dc_txeof(sc); 2370 2371 if (status & DC_ISR_TX_IDLE) { 2372 dc_txeof(sc); 2373 if (sc->dc_cdata.dc_tx_cnt) { 2374 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON); 2375 CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF); 2376 } 2377 } 2378 2379 if (status & DC_ISR_TX_UNDERRUN) { 2380 u_int32_t cfg; 2381 2382 printf("dc%d: TX underrun -- ", sc->dc_unit); 2383 if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) 2384 dc_init(sc); 2385 cfg = CSR_READ_4(sc, DC_NETCFG); 2386 cfg &= ~DC_NETCFG_TX_THRESH; 2387 if (sc->dc_txthresh == DC_TXTHRESH_160BYTES) { 2388 printf("using store and forward mode\n"); 2389 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD); 2390 } else if (sc->dc_flags & DC_TX_STORENFWD) { 2391 printf("resetting\n"); 2392 } else { 2393 sc->dc_txthresh += 0x4000; 2394 printf("increasing TX threshold\n"); 2395 CSR_WRITE_4(sc, DC_NETCFG, cfg); 2396 DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh); 2397 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD); 2398 } 2399 } 2400 2401 if ((status & DC_ISR_RX_WATDOGTIMEO) 2402 || (status & DC_ISR_RX_NOBUF)) { 2403 int curpkts; 2404 curpkts = ifp->if_ipackets; 2405 dc_rxeof(sc); 2406 if (curpkts == ifp->if_ipackets) { 2407 while(dc_rx_resync(sc)) 2408 dc_rxeof(sc); 2409 } 2410 } 2411 2412 if (status & DC_ISR_BUS_ERR) { 2413 dc_reset(sc); 2414 dc_init(sc); 2415 } 2416 } 2417 2418 /* Re-enable interrupts. */ 2419 CSR_WRITE_4(sc, DC_IMR, DC_INTRS); 2420 2421 if (ifp->if_snd.ifq_head != NULL) 2422 dc_start(ifp); 2423 2424 return; 2425 } 2426 2427 /* 2428 * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data 2429 * pointers to the fragment pointers. 2430 */ 2431 static int dc_encap(sc, m_head, txidx) 2432 struct dc_softc *sc; 2433 struct mbuf *m_head; 2434 u_int32_t *txidx; 2435 { 2436 struct dc_desc *f = NULL; 2437 struct mbuf *m; 2438 int frag, cur, cnt = 0; 2439 2440 /* 2441 * Start packing the mbufs in this chain into 2442 * the fragment pointers. Stop when we run out 2443 * of fragments or hit the end of the mbuf chain. 2444 */ 2445 m = m_head; 2446 cur = frag = *txidx; 2447 2448 for (m = m_head; m != NULL; m = m->m_next) { 2449 if (m->m_len != 0) { 2450 if (sc->dc_flags & DC_TX_ADMTEK_WAR) { 2451 if (*txidx != sc->dc_cdata.dc_tx_prod && 2452 frag == (DC_TX_LIST_CNT - 1)) 2453 return(ENOBUFS); 2454 } 2455 if ((DC_TX_LIST_CNT - 2456 (sc->dc_cdata.dc_tx_cnt + cnt)) < 5) 2457 return(ENOBUFS); 2458 2459 f = &sc->dc_ldata->dc_tx_list[frag]; 2460 f->dc_ctl = DC_TXCTL_TLINK | m->m_len; 2461 if (cnt == 0) { 2462 f->dc_status = 0; 2463 f->dc_ctl |= DC_TXCTL_FIRSTFRAG; 2464 } else 2465 f->dc_status = DC_TXSTAT_OWN; 2466 f->dc_data = vtophys(mtod(m, vm_offset_t)); 2467 cur = frag; 2468 DC_INC(frag, DC_TX_LIST_CNT); 2469 cnt++; 2470 } 2471 } 2472 2473 if (m != NULL) 2474 return(ENOBUFS); 2475 2476 sc->dc_cdata.dc_tx_cnt += cnt; 2477 sc->dc_cdata.dc_tx_chain[cur] = m_head; 2478 sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_LASTFRAG; 2479 if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG) 2480 sc->dc_ldata->dc_tx_list[*txidx].dc_ctl |= DC_TXCTL_FINT; 2481 if (sc->dc_flags & DC_TX_INTR_ALWAYS) 2482 sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_FINT; 2483 if (sc->dc_flags & DC_TX_USE_TX_INTR && sc->dc_cdata.dc_tx_cnt > 64) 2484 sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_FINT; 2485 sc->dc_ldata->dc_tx_list[*txidx].dc_status = DC_TXSTAT_OWN; 2486 *txidx = frag; 2487 2488 return(0); 2489 } 2490 2491 /* 2492 * Coalesce an mbuf chain into a single mbuf cluster buffer. 2493 * Needed for some really badly behaved chips that just can't 2494 * do scatter/gather correctly. 2495 */ 2496 static int dc_coal(sc, m_head) 2497 struct dc_softc *sc; 2498 struct mbuf **m_head; 2499 { 2500 struct mbuf *m_new, *m; 2501 2502 m = *m_head; 2503 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 2504 if (m_new == NULL) { 2505 printf("dc%d: no memory for tx list", sc->dc_unit); 2506 return(ENOBUFS); 2507 } 2508 if (m->m_pkthdr.len > MHLEN) { 2509 MCLGET(m_new, M_DONTWAIT); 2510 if (!(m_new->m_flags & M_EXT)) { 2511 m_freem(m_new); 2512 printf("dc%d: no memory for tx list", sc->dc_unit); 2513 return(ENOBUFS); 2514 } 2515 } 2516 m_copydata(m, 0, m->m_pkthdr.len, mtod(m_new, caddr_t)); 2517 m_new->m_pkthdr.len = m_new->m_len = m->m_pkthdr.len; 2518 m_freem(m); 2519 *m_head = m_new; 2520 2521 return(0); 2522 } 2523 2524 /* 2525 * Main transmit routine. To avoid having to do mbuf copies, we put pointers 2526 * to the mbuf data regions directly in the transmit lists. We also save a 2527 * copy of the pointers since the transmit list fragment pointers are 2528 * physical addresses. 2529 */ 2530 2531 static void dc_start(ifp) 2532 struct ifnet *ifp; 2533 { 2534 struct dc_softc *sc; 2535 struct mbuf *m_head = NULL; 2536 int idx; 2537 2538 sc = ifp->if_softc; 2539 2540 if (!sc->dc_link) 2541 return; 2542 2543 if (ifp->if_flags & IFF_OACTIVE) 2544 return; 2545 2546 idx = sc->dc_cdata.dc_tx_prod; 2547 2548 while(sc->dc_cdata.dc_tx_chain[idx] == NULL) { 2549 IF_DEQUEUE(&ifp->if_snd, m_head); 2550 if (m_head == NULL) 2551 break; 2552 2553 if (sc->dc_flags & DC_TX_COALESCE) { 2554 if (dc_coal(sc, &m_head)) { 2555 IF_PREPEND(&ifp->if_snd, m_head); 2556 ifp->if_flags |= IFF_OACTIVE; 2557 break; 2558 } 2559 } 2560 2561 if (dc_encap(sc, m_head, &idx)) { 2562 IF_PREPEND(&ifp->if_snd, m_head); 2563 ifp->if_flags |= IFF_OACTIVE; 2564 break; 2565 } 2566 2567 /* 2568 * If there's a BPF listener, bounce a copy of this frame 2569 * to him. 2570 */ 2571 if (ifp->if_bpf) 2572 bpf_mtap(ifp, m_head); 2573 } 2574 2575 /* Transmit */ 2576 sc->dc_cdata.dc_tx_prod = idx; 2577 if (!(sc->dc_flags & DC_TX_POLL)) 2578 CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF); 2579 2580 /* 2581 * Set a timeout in case the chip goes out to lunch. 2582 */ 2583 ifp->if_timer = 5; 2584 2585 return; 2586 } 2587 2588 static void dc_init(xsc) 2589 void *xsc; 2590 { 2591 struct dc_softc *sc = xsc; 2592 struct ifnet *ifp = &sc->arpcom.ac_if; 2593 struct mii_data *mii; 2594 int s; 2595 2596 s = splimp(); 2597 2598 mii = device_get_softc(sc->dc_miibus); 2599 2600 /* 2601 * Cancel pending I/O and free all RX/TX buffers. 2602 */ 2603 dc_stop(sc); 2604 dc_reset(sc); 2605 2606 /* 2607 * Set cache alignment and burst length. 2608 */ 2609 if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc)) 2610 CSR_WRITE_4(sc, DC_BUSCTL, 0); 2611 else 2612 CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME|DC_BUSCTL_MRLE); 2613 if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) { 2614 DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA); 2615 } else { 2616 DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG); 2617 } 2618 if (sc->dc_flags & DC_TX_POLL) 2619 DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1); 2620 switch(sc->dc_cachesize) { 2621 case 32: 2622 DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG); 2623 break; 2624 case 16: 2625 DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG); 2626 break; 2627 case 8: 2628 DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG); 2629 break; 2630 case 0: 2631 default: 2632 DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE); 2633 break; 2634 } 2635 2636 if (sc->dc_flags & DC_TX_STORENFWD) 2637 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD); 2638 else { 2639 if (sc->dc_txthresh == DC_TXTHRESH_160BYTES) { 2640 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD); 2641 } else { 2642 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD); 2643 DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh); 2644 } 2645 } 2646 2647 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC); 2648 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF); 2649 2650 if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) { 2651 /* 2652 * The app notes for the 98713 and 98715A say that 2653 * in order to have the chips operate properly, a magic 2654 * number must be written to CSR16. Macronix does not 2655 * document the meaning of these bits so there's no way 2656 * to know exactly what they do. The 98713 has a magic 2657 * number all its own; the rest all use a different one. 2658 */ 2659 DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000); 2660 if (sc->dc_type == DC_TYPE_98713) 2661 DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713); 2662 else 2663 DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715); 2664 } 2665 2666 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH); 2667 DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_72BYTES); 2668 2669 /* Init circular RX list. */ 2670 if (dc_list_rx_init(sc) == ENOBUFS) { 2671 printf("dc%d: initialization failed: no " 2672 "memory for rx buffers\n", sc->dc_unit); 2673 dc_stop(sc); 2674 (void)splx(s); 2675 return; 2676 } 2677 2678 /* 2679 * Init tx descriptors. 2680 */ 2681 dc_list_tx_init(sc); 2682 2683 /* 2684 * Load the address of the RX list. 2685 */ 2686 CSR_WRITE_4(sc, DC_RXADDR, vtophys(&sc->dc_ldata->dc_rx_list[0])); 2687 CSR_WRITE_4(sc, DC_TXADDR, vtophys(&sc->dc_ldata->dc_tx_list[0])); 2688 2689 /* 2690 * Enable interrupts. 2691 */ 2692 CSR_WRITE_4(sc, DC_IMR, DC_INTRS); 2693 CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF); 2694 2695 /* Enable transmitter. */ 2696 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON); 2697 2698 /* 2699 * Load the RX/multicast filter. We do this sort of late 2700 * because the filter programming scheme on the 21143 and 2701 * some clones requires DMAing a setup frame via the TX 2702 * engine, and we need the transmitter enabled for that. 2703 */ 2704 dc_setfilt(sc); 2705 2706 /* Enable receiver. */ 2707 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON); 2708 CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF); 2709 2710 mii_mediachg(mii); 2711 dc_setcfg(sc, sc->dc_if_media); 2712 2713 ifp->if_flags |= IFF_RUNNING; 2714 ifp->if_flags &= ~IFF_OACTIVE; 2715 2716 (void)splx(s); 2717 2718 sc->dc_stat_ch = timeout(dc_tick, sc, hz); 2719 2720 #ifdef __alpha__ 2721 if(sc->dc_srm_media) { 2722 struct ifreq ifr; 2723 2724 ifr.ifr_media = sc->dc_srm_media; 2725 ifmedia_ioctl(ifp, &ifr, &mii->mii_media, SIOCSIFMEDIA); 2726 sc->dc_srm_media = 0; 2727 } 2728 #endif 2729 return; 2730 } 2731 2732 /* 2733 * Set media options. 2734 */ 2735 static int dc_ifmedia_upd(ifp) 2736 struct ifnet *ifp; 2737 { 2738 struct dc_softc *sc; 2739 struct mii_data *mii; 2740 struct ifmedia *ifm; 2741 2742 sc = ifp->if_softc; 2743 mii = device_get_softc(sc->dc_miibus); 2744 mii_mediachg(mii); 2745 ifm = &mii->mii_media; 2746 2747 if (DC_IS_DAVICOM(sc) && 2748 IFM_SUBTYPE(ifm->ifm_media) == IFM_homePNA) 2749 dc_setcfg(sc, ifm->ifm_media); 2750 else 2751 sc->dc_link = 0; 2752 2753 return(0); 2754 } 2755 2756 /* 2757 * Report current media status. 2758 */ 2759 static void dc_ifmedia_sts(ifp, ifmr) 2760 struct ifnet *ifp; 2761 struct ifmediareq *ifmr; 2762 { 2763 struct dc_softc *sc; 2764 struct mii_data *mii; 2765 struct ifmedia *ifm; 2766 2767 sc = ifp->if_softc; 2768 mii = device_get_softc(sc->dc_miibus); 2769 mii_pollstat(mii); 2770 ifm = &mii->mii_media; 2771 if (DC_IS_DAVICOM(sc)) { 2772 if (IFM_SUBTYPE(ifm->ifm_media) == IFM_homePNA) { 2773 ifmr->ifm_active = ifm->ifm_media; 2774 ifmr->ifm_status = 0; 2775 return; 2776 } 2777 } 2778 ifmr->ifm_active = mii->mii_media_active; 2779 ifmr->ifm_status = mii->mii_media_status; 2780 2781 return; 2782 } 2783 2784 static int dc_ioctl(ifp, command, data) 2785 struct ifnet *ifp; 2786 u_long command; 2787 caddr_t data; 2788 { 2789 struct dc_softc *sc = ifp->if_softc; 2790 struct ifreq *ifr = (struct ifreq *) data; 2791 struct mii_data *mii; 2792 int s, error = 0; 2793 2794 s = splimp(); 2795 2796 switch(command) { 2797 case SIOCSIFADDR: 2798 case SIOCGIFADDR: 2799 case SIOCSIFMTU: 2800 error = ether_ioctl(ifp, command, data); 2801 break; 2802 case SIOCSIFFLAGS: 2803 if (ifp->if_flags & IFF_UP) { 2804 if (ifp->if_flags & IFF_RUNNING && 2805 ifp->if_flags & IFF_PROMISC && 2806 !(sc->dc_if_flags & IFF_PROMISC)) { 2807 dc_setfilt(sc); 2808 } else if (ifp->if_flags & IFF_RUNNING && 2809 !(ifp->if_flags & IFF_PROMISC) && 2810 sc->dc_if_flags & IFF_PROMISC) { 2811 dc_setfilt(sc); 2812 } else if (!(ifp->if_flags & IFF_RUNNING)) { 2813 sc->dc_txthresh = 0; 2814 dc_init(sc); 2815 } 2816 } else { 2817 if (ifp->if_flags & IFF_RUNNING) 2818 dc_stop(sc); 2819 } 2820 sc->dc_if_flags = ifp->if_flags; 2821 error = 0; 2822 break; 2823 case SIOCADDMULTI: 2824 case SIOCDELMULTI: 2825 dc_setfilt(sc); 2826 error = 0; 2827 break; 2828 case SIOCGIFMEDIA: 2829 case SIOCSIFMEDIA: 2830 mii = device_get_softc(sc->dc_miibus); 2831 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 2832 #ifdef __alpha__ 2833 if (sc->dc_srm_media) 2834 sc->dc_srm_media = 0; 2835 #endif 2836 break; 2837 default: 2838 error = EINVAL; 2839 break; 2840 } 2841 2842 (void)splx(s); 2843 2844 return(error); 2845 } 2846 2847 static void dc_watchdog(ifp) 2848 struct ifnet *ifp; 2849 { 2850 struct dc_softc *sc; 2851 2852 sc = ifp->if_softc; 2853 2854 ifp->if_oerrors++; 2855 printf("dc%d: watchdog timeout\n", sc->dc_unit); 2856 2857 dc_stop(sc); 2858 dc_reset(sc); 2859 dc_init(sc); 2860 2861 if (ifp->if_snd.ifq_head != NULL) 2862 dc_start(ifp); 2863 2864 return; 2865 } 2866 2867 /* 2868 * Stop the adapter and free any mbufs allocated to the 2869 * RX and TX lists. 2870 */ 2871 static void dc_stop(sc) 2872 struct dc_softc *sc; 2873 { 2874 register int i; 2875 struct ifnet *ifp; 2876 2877 ifp = &sc->arpcom.ac_if; 2878 ifp->if_timer = 0; 2879 2880 untimeout(dc_tick, sc, sc->dc_stat_ch); 2881 2882 DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_RX_ON|DC_NETCFG_TX_ON)); 2883 CSR_WRITE_4(sc, DC_IMR, 0x00000000); 2884 CSR_WRITE_4(sc, DC_TXADDR, 0x00000000); 2885 CSR_WRITE_4(sc, DC_RXADDR, 0x00000000); 2886 sc->dc_link = 0; 2887 2888 /* 2889 * Free data in the RX lists. 2890 */ 2891 for (i = 0; i < DC_RX_LIST_CNT; i++) { 2892 if (sc->dc_cdata.dc_rx_chain[i] != NULL) { 2893 m_freem(sc->dc_cdata.dc_rx_chain[i]); 2894 sc->dc_cdata.dc_rx_chain[i] = NULL; 2895 } 2896 } 2897 bzero((char *)&sc->dc_ldata->dc_rx_list, 2898 sizeof(sc->dc_ldata->dc_rx_list)); 2899 2900 /* 2901 * Free the TX list buffers. 2902 */ 2903 for (i = 0; i < DC_TX_LIST_CNT; i++) { 2904 if (sc->dc_cdata.dc_tx_chain[i] != NULL) { 2905 if (sc->dc_ldata->dc_tx_list[i].dc_ctl & 2906 DC_TXCTL_SETUP) { 2907 sc->dc_cdata.dc_tx_chain[i] = NULL; 2908 continue; 2909 } 2910 m_freem(sc->dc_cdata.dc_tx_chain[i]); 2911 sc->dc_cdata.dc_tx_chain[i] = NULL; 2912 } 2913 } 2914 2915 bzero((char *)&sc->dc_ldata->dc_tx_list, 2916 sizeof(sc->dc_ldata->dc_tx_list)); 2917 2918 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 2919 2920 return; 2921 } 2922 2923 /* 2924 * Stop all chip I/O so that the kernel's probe routines don't 2925 * get confused by errant DMAs when rebooting. 2926 */ 2927 static void dc_shutdown(dev) 2928 device_t dev; 2929 { 2930 struct dc_softc *sc; 2931 2932 sc = device_get_softc(dev); 2933 2934 dc_stop(sc); 2935 2936 return; 2937 } 2938