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