1 /* $NetBSD: tulip.c,v 1.179 2011/08/13 19:23:34 jakllsch Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999, 2000, 2002 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center; and by Charles M. Hannum. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 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 THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Device driver for the Digital Semiconductor ``Tulip'' (21x4x) 35 * Ethernet controller family, and a variety of clone chips. 36 */ 37 38 #include <sys/cdefs.h> 39 __KERNEL_RCSID(0, "$NetBSD: tulip.c,v 1.179 2011/08/13 19:23:34 jakllsch Exp $"); 40 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/callout.h> 45 #include <sys/mbuf.h> 46 #include <sys/malloc.h> 47 #include <sys/kernel.h> 48 #include <sys/socket.h> 49 #include <sys/ioctl.h> 50 #include <sys/errno.h> 51 #include <sys/device.h> 52 53 #include <machine/endian.h> 54 55 #include <net/if.h> 56 #include <net/if_dl.h> 57 #include <net/if_media.h> 58 #include <net/if_ether.h> 59 60 #include <net/bpf.h> 61 62 #include <sys/bus.h> 63 #include <sys/intr.h> 64 65 #include <dev/mii/mii.h> 66 #include <dev/mii/miivar.h> 67 #include <dev/mii/mii_bitbang.h> 68 69 #include <dev/ic/tulipreg.h> 70 #include <dev/ic/tulipvar.h> 71 72 static const char * const tlp_chip_names[] = TULIP_CHIP_NAMES; 73 74 static const struct tulip_txthresh_tab tlp_10_txthresh_tab[] = 75 TLP_TXTHRESH_TAB_10; 76 77 static const struct tulip_txthresh_tab tlp_10_100_txthresh_tab[] = 78 TLP_TXTHRESH_TAB_10_100; 79 80 static const struct tulip_txthresh_tab tlp_winb_txthresh_tab[] = 81 TLP_TXTHRESH_TAB_WINB; 82 83 static const struct tulip_txthresh_tab tlp_dm9102_txthresh_tab[] = 84 TLP_TXTHRESH_TAB_DM9102; 85 86 static void tlp_start(struct ifnet *); 87 static void tlp_watchdog(struct ifnet *); 88 static int tlp_ioctl(struct ifnet *, u_long, void *); 89 static int tlp_init(struct ifnet *); 90 static void tlp_stop(struct ifnet *, int); 91 static int tlp_ifflags_cb(struct ethercom *); 92 93 static void tlp_rxdrain(struct tulip_softc *); 94 static int tlp_add_rxbuf(struct tulip_softc *, int); 95 static void tlp_srom_idle(struct tulip_softc *); 96 static int tlp_srom_size(struct tulip_softc *); 97 98 static int tlp_enable(struct tulip_softc *); 99 static void tlp_disable(struct tulip_softc *); 100 101 static void tlp_filter_setup(struct tulip_softc *); 102 static void tlp_winb_filter_setup(struct tulip_softc *); 103 static void tlp_al981_filter_setup(struct tulip_softc *); 104 static void tlp_asix_filter_setup(struct tulip_softc *); 105 106 static void tlp_rxintr(struct tulip_softc *); 107 static void tlp_txintr(struct tulip_softc *); 108 109 static void tlp_mii_tick(void *); 110 static void tlp_mii_statchg(device_t); 111 static void tlp_winb_mii_statchg(device_t); 112 static void tlp_dm9102_mii_statchg(device_t); 113 114 static void tlp_mii_getmedia(struct tulip_softc *, struct ifmediareq *); 115 static int tlp_mii_setmedia(struct tulip_softc *); 116 117 static int tlp_bitbang_mii_readreg(device_t, int, int); 118 static void tlp_bitbang_mii_writereg(device_t, int, int, int); 119 120 static int tlp_pnic_mii_readreg(device_t, int, int); 121 static void tlp_pnic_mii_writereg(device_t, int, int, int); 122 123 static int tlp_al981_mii_readreg(device_t, int, int); 124 static void tlp_al981_mii_writereg(device_t, int, int, int); 125 126 static void tlp_2114x_preinit(struct tulip_softc *); 127 static void tlp_2114x_mii_preinit(struct tulip_softc *); 128 static void tlp_pnic_preinit(struct tulip_softc *); 129 static void tlp_dm9102_preinit(struct tulip_softc *); 130 static void tlp_asix_preinit(struct tulip_softc *); 131 132 static void tlp_21140_reset(struct tulip_softc *); 133 static void tlp_21142_reset(struct tulip_softc *); 134 static void tlp_pmac_reset(struct tulip_softc *); 135 #if 0 136 static void tlp_dm9102_reset(struct tulip_softc *); 137 #endif 138 139 static void tlp_2114x_nway_tick(void *); 140 141 #define tlp_mchash(addr, sz) \ 142 (ether_crc32_le((addr), ETHER_ADDR_LEN) & ((sz) - 1)) 143 144 /* 145 * MII bit-bang glue. 146 */ 147 static uint32_t tlp_sio_mii_bitbang_read(device_t); 148 static void tlp_sio_mii_bitbang_write(device_t, uint32_t); 149 150 static const struct mii_bitbang_ops tlp_sio_mii_bitbang_ops = { 151 tlp_sio_mii_bitbang_read, 152 tlp_sio_mii_bitbang_write, 153 { 154 MIIROM_MDO, /* MII_BIT_MDO */ 155 MIIROM_MDI, /* MII_BIT_MDI */ 156 MIIROM_MDC, /* MII_BIT_MDC */ 157 0, /* MII_BIT_DIR_HOST_PHY */ 158 MIIROM_MIIDIR, /* MII_BIT_DIR_PHY_HOST */ 159 } 160 }; 161 162 #ifdef TLP_DEBUG 163 #define DPRINTF(sc, x) if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \ 164 printf x 165 #else 166 #define DPRINTF(sc, x) /* nothing */ 167 #endif 168 169 #ifdef TLP_STATS 170 static void tlp_print_stats(struct tulip_softc *); 171 #endif 172 173 /* 174 * Can be used to debug the SROM-related things, including contents. 175 * Initialized so that it's patchable. 176 */ 177 int tlp_srom_debug = 0; 178 179 /* 180 * tlp_attach: 181 * 182 * Attach a Tulip interface to the system. 183 */ 184 int 185 tlp_attach(struct tulip_softc *sc, const uint8_t *enaddr) 186 { 187 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 188 device_t self = sc->sc_dev; 189 int i, error; 190 191 callout_init(&sc->sc_nway_callout, 0); 192 callout_init(&sc->sc_tick_callout, 0); 193 194 /* 195 * NOTE: WE EXPECT THE FRONT-END TO INITIALIZE sc_regshift! 196 */ 197 198 /* 199 * Setup the transmit threshold table. 200 */ 201 switch (sc->sc_chip) { 202 case TULIP_CHIP_DE425: 203 case TULIP_CHIP_21040: 204 case TULIP_CHIP_21041: 205 sc->sc_txth = tlp_10_txthresh_tab; 206 break; 207 208 case TULIP_CHIP_DM9102: 209 case TULIP_CHIP_DM9102A: 210 sc->sc_txth = tlp_dm9102_txthresh_tab; 211 break; 212 213 default: 214 sc->sc_txth = tlp_10_100_txthresh_tab; 215 break; 216 } 217 218 /* 219 * Setup the filter setup function. 220 */ 221 switch (sc->sc_chip) { 222 case TULIP_CHIP_WB89C840F: 223 sc->sc_filter_setup = tlp_winb_filter_setup; 224 break; 225 226 case TULIP_CHIP_AL981: 227 case TULIP_CHIP_AN983: 228 case TULIP_CHIP_AN985: 229 sc->sc_filter_setup = tlp_al981_filter_setup; 230 break; 231 232 case TULIP_CHIP_AX88140: 233 case TULIP_CHIP_AX88141: 234 sc->sc_filter_setup = tlp_asix_filter_setup; 235 break; 236 237 default: 238 sc->sc_filter_setup = tlp_filter_setup; 239 break; 240 } 241 242 /* 243 * Set up the media status change function. 244 */ 245 switch (sc->sc_chip) { 246 case TULIP_CHIP_WB89C840F: 247 sc->sc_statchg = tlp_winb_mii_statchg; 248 break; 249 250 case TULIP_CHIP_DM9102: 251 case TULIP_CHIP_DM9102A: 252 sc->sc_statchg = tlp_dm9102_mii_statchg; 253 break; 254 255 default: 256 /* 257 * We may override this if we have special media 258 * handling requirements (e.g. flipping GPIO pins). 259 * 260 * The pure-MII statchg function covers the basics. 261 */ 262 sc->sc_statchg = tlp_mii_statchg; 263 break; 264 } 265 266 /* 267 * Default to no FS|LS in setup packet descriptors. They're 268 * supposed to be zero according to the 21040 and 21143 269 * manuals, and some chips fall over badly if they're 270 * included. Yet, other chips seem to require them. Sigh. 271 */ 272 switch (sc->sc_chip) { 273 case TULIP_CHIP_X3201_3: 274 sc->sc_setup_fsls = TDCTL_Tx_FS|TDCTL_Tx_LS; 275 break; 276 277 default: 278 sc->sc_setup_fsls = 0; 279 } 280 281 /* 282 * Set up various chip-specific quirks. 283 * 284 * Note that wherever we can, we use the "ring" option for 285 * transmit and receive descriptors. This is because some 286 * clone chips apparently have problems when using chaining, 287 * although some *only* support chaining. 288 * 289 * What we do is always program the "next" pointer, and then 290 * conditionally set the TDCTL_CH and TDCTL_ER bits in the 291 * appropriate places. 292 */ 293 switch (sc->sc_chip) { 294 case TULIP_CHIP_21140: 295 case TULIP_CHIP_21140A: 296 case TULIP_CHIP_21142: 297 case TULIP_CHIP_21143: 298 case TULIP_CHIP_82C115: /* 21143-like */ 299 case TULIP_CHIP_MX98713: /* 21140-like */ 300 case TULIP_CHIP_MX98713A: /* 21143-like */ 301 case TULIP_CHIP_MX98715: /* 21143-like */ 302 case TULIP_CHIP_MX98715A: /* 21143-like */ 303 case TULIP_CHIP_MX98715AEC_X: /* 21143-like */ 304 case TULIP_CHIP_MX98725: /* 21143-like */ 305 case TULIP_CHIP_RS7112: /* 21143-like */ 306 /* 307 * Run these chips in ring mode. 308 */ 309 sc->sc_tdctl_ch = 0; 310 sc->sc_tdctl_er = TDCTL_ER; 311 sc->sc_preinit = tlp_2114x_preinit; 312 break; 313 314 case TULIP_CHIP_82C168: 315 case TULIP_CHIP_82C169: 316 /* 317 * Run these chips in ring mode. 318 */ 319 sc->sc_tdctl_ch = 0; 320 sc->sc_tdctl_er = TDCTL_ER; 321 sc->sc_preinit = tlp_pnic_preinit; 322 323 /* 324 * These chips seem to have busted DMA engines; just put them 325 * in Store-and-Forward mode from the get-go. 326 */ 327 sc->sc_txthresh = TXTH_SF; 328 break; 329 330 case TULIP_CHIP_WB89C840F: 331 /* 332 * Run this chip in chained mode. 333 */ 334 sc->sc_tdctl_ch = TDCTL_CH; 335 sc->sc_tdctl_er = 0; 336 sc->sc_flags |= TULIPF_IC_FS; 337 break; 338 339 case TULIP_CHIP_DM9102: 340 case TULIP_CHIP_DM9102A: 341 /* 342 * Run these chips in chained mode. 343 */ 344 sc->sc_tdctl_ch = TDCTL_CH; 345 sc->sc_tdctl_er = 0; 346 sc->sc_preinit = tlp_dm9102_preinit; 347 348 /* 349 * These chips have a broken bus interface, so we 350 * can't use any optimized bus commands. For this 351 * reason, we tend to underrun pretty quickly, so 352 * just to Store-and-Forward mode from the get-go. 353 */ 354 sc->sc_txthresh = TXTH_DM9102_SF; 355 break; 356 357 case TULIP_CHIP_AX88140: 358 case TULIP_CHIP_AX88141: 359 /* 360 * Run these chips in ring mode. 361 */ 362 sc->sc_tdctl_ch = 0; 363 sc->sc_tdctl_er = TDCTL_ER; 364 sc->sc_preinit = tlp_asix_preinit; 365 break; 366 367 default: 368 /* 369 * Default to running in ring mode. 370 */ 371 sc->sc_tdctl_ch = 0; 372 sc->sc_tdctl_er = TDCTL_ER; 373 } 374 375 /* 376 * Set up the MII bit-bang operations. 377 */ 378 switch (sc->sc_chip) { 379 case TULIP_CHIP_WB89C840F: /* XXX direction bit different? */ 380 sc->sc_bitbang_ops = &tlp_sio_mii_bitbang_ops; 381 break; 382 383 default: 384 sc->sc_bitbang_ops = &tlp_sio_mii_bitbang_ops; 385 } 386 387 SIMPLEQ_INIT(&sc->sc_txfreeq); 388 SIMPLEQ_INIT(&sc->sc_txdirtyq); 389 390 /* 391 * Allocate the control data structures, and create and load the 392 * DMA map for it. 393 */ 394 if ((error = bus_dmamem_alloc(sc->sc_dmat, 395 sizeof(struct tulip_control_data), PAGE_SIZE, 0, &sc->sc_cdseg, 396 1, &sc->sc_cdnseg, 0)) != 0) { 397 aprint_error_dev(self, "unable to allocate control data, error = %d\n", 398 error); 399 goto fail_0; 400 } 401 402 if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg, 403 sizeof(struct tulip_control_data), (void **)&sc->sc_control_data, 404 BUS_DMA_COHERENT)) != 0) { 405 aprint_error_dev(self, "unable to map control data, error = %d\n", 406 error); 407 goto fail_1; 408 } 409 410 if ((error = bus_dmamap_create(sc->sc_dmat, 411 sizeof(struct tulip_control_data), 1, 412 sizeof(struct tulip_control_data), 0, 0, &sc->sc_cddmamap)) != 0) { 413 sc->sc_cddmamap = NULL; 414 aprint_error_dev(self, "unable to create control data DMA map, " 415 "error = %d\n", error); 416 goto fail_2; 417 } 418 419 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap, 420 sc->sc_control_data, sizeof(struct tulip_control_data), NULL, 421 0)) != 0) { 422 aprint_error_dev(self, "unable to load control data DMA map, error = %d\n", 423 error); 424 goto fail_3; 425 } 426 427 /* 428 * Create the transmit buffer DMA maps. 429 * 430 * Note that on the Xircom clone, transmit buffers must be 431 * 4-byte aligned. We're almost guaranteed to have to copy 432 * the packet in that case, so we just limit ourselves to 433 * one segment. 434 * 435 * On the DM9102, the transmit logic can only handle one 436 * DMA segment. 437 */ 438 switch (sc->sc_chip) { 439 case TULIP_CHIP_X3201_3: 440 case TULIP_CHIP_DM9102: 441 case TULIP_CHIP_DM9102A: 442 case TULIP_CHIP_AX88140: 443 case TULIP_CHIP_AX88141: 444 sc->sc_ntxsegs = 1; 445 break; 446 447 default: 448 sc->sc_ntxsegs = TULIP_NTXSEGS; 449 } 450 for (i = 0; i < TULIP_TXQUEUELEN; i++) { 451 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 452 sc->sc_ntxsegs, MCLBYTES, 0, 0, 453 &sc->sc_txsoft[i].txs_dmamap)) != 0) { 454 sc->sc_txsoft[i].txs_dmamap = NULL; 455 aprint_error_dev(self, "unable to create tx DMA map %d, " 456 "error = %d\n", i, error); 457 goto fail_4; 458 } 459 } 460 461 /* 462 * Create the receive buffer DMA maps. 463 */ 464 for (i = 0; i < TULIP_NRXDESC; i++) { 465 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, 466 MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) { 467 sc->sc_rxsoft[i].rxs_dmamap = NULL; 468 aprint_error_dev(self, "unable to create rx DMA map %d, " 469 "error = %d\n", i, error); 470 goto fail_5; 471 } 472 sc->sc_rxsoft[i].rxs_mbuf = NULL; 473 } 474 475 /* 476 * From this point forward, the attachment cannot fail. A failure 477 * before this point releases all resources that may have been 478 * allocated. 479 */ 480 sc->sc_flags |= TULIPF_ATTACHED; 481 482 /* 483 * Reset the chip to a known state. 484 */ 485 tlp_reset(sc); 486 487 /* Announce ourselves. */ 488 aprint_normal_dev(self, "%s%sEthernet address %s\n", 489 sc->sc_name[0] != '\0' ? sc->sc_name : "", 490 sc->sc_name[0] != '\0' ? ", " : "", 491 ether_sprintf(enaddr)); 492 493 /* 494 * Check to see if we're the simulated Ethernet on Connectix 495 * Virtual PC. 496 */ 497 if (enaddr[0] == 0x00 && enaddr[1] == 0x03 && enaddr[2] == 0xff) 498 sc->sc_flags |= TULIPF_VPC; 499 500 /* 501 * Initialize our media structures. This may probe the MII, if 502 * present. 503 */ 504 (*sc->sc_mediasw->tmsw_init)(sc); 505 506 strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ); 507 ifp->if_softc = sc; 508 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 509 sc->sc_if_flags = ifp->if_flags; 510 ifp->if_ioctl = tlp_ioctl; 511 ifp->if_start = tlp_start; 512 ifp->if_watchdog = tlp_watchdog; 513 ifp->if_init = tlp_init; 514 ifp->if_stop = tlp_stop; 515 IFQ_SET_READY(&ifp->if_snd); 516 517 /* 518 * We can support 802.1Q VLAN-sized frames. 519 */ 520 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU; 521 522 /* 523 * Attach the interface. 524 */ 525 if_attach(ifp); 526 ether_ifattach(ifp, enaddr); 527 ether_set_ifflags_cb(&sc->sc_ethercom, tlp_ifflags_cb); 528 #if NRND > 0 529 rnd_attach_source(&sc->sc_rnd_source, device_xname(self), 530 RND_TYPE_NET, 0); 531 #endif 532 533 if (pmf_device_register(self, NULL, NULL)) 534 pmf_class_network_register(self, ifp); 535 else 536 aprint_error_dev(self, "couldn't establish power handler\n"); 537 538 return 0; 539 540 /* 541 * Free any resources we've allocated during the failed attach 542 * attempt. Do this in reverse order and fall through. 543 */ 544 fail_5: 545 for (i = 0; i < TULIP_NRXDESC; i++) { 546 if (sc->sc_rxsoft[i].rxs_dmamap != NULL) 547 bus_dmamap_destroy(sc->sc_dmat, 548 sc->sc_rxsoft[i].rxs_dmamap); 549 } 550 fail_4: 551 for (i = 0; i < TULIP_TXQUEUELEN; i++) { 552 if (sc->sc_txsoft[i].txs_dmamap != NULL) 553 bus_dmamap_destroy(sc->sc_dmat, 554 sc->sc_txsoft[i].txs_dmamap); 555 } 556 bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap); 557 fail_3: 558 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap); 559 fail_2: 560 bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data, 561 sizeof(struct tulip_control_data)); 562 fail_1: 563 bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg); 564 fail_0: 565 return error; 566 } 567 568 /* 569 * tlp_activate: 570 * 571 * Handle device activation/deactivation requests. 572 */ 573 int 574 tlp_activate(device_t self, enum devact act) 575 { 576 struct tulip_softc *sc = device_private(self); 577 578 switch (act) { 579 case DVACT_DEACTIVATE: 580 if_deactivate(&sc->sc_ethercom.ec_if); 581 return 0; 582 default: 583 return EOPNOTSUPP; 584 } 585 } 586 587 /* 588 * tlp_detach: 589 * 590 * Detach a Tulip interface. 591 */ 592 int 593 tlp_detach(struct tulip_softc *sc) 594 { 595 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 596 struct tulip_rxsoft *rxs; 597 struct tulip_txsoft *txs; 598 device_t self = sc->sc_dev; 599 int i; 600 601 /* 602 * Succeed now if there isn't any work to do. 603 */ 604 if ((sc->sc_flags & TULIPF_ATTACHED) == 0) 605 return (0); 606 607 /* Unhook our tick handler. */ 608 if (sc->sc_tick) 609 callout_stop(&sc->sc_tick_callout); 610 611 if (sc->sc_flags & TULIPF_HAS_MII) { 612 /* Detach all PHYs */ 613 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY); 614 } 615 616 /* Delete all remaining media. */ 617 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY); 618 619 #if NRND > 0 620 rnd_detach_source(&sc->sc_rnd_source); 621 #endif 622 ether_ifdetach(ifp); 623 if_detach(ifp); 624 625 for (i = 0; i < TULIP_NRXDESC; i++) { 626 rxs = &sc->sc_rxsoft[i]; 627 if (rxs->rxs_mbuf != NULL) { 628 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap); 629 m_freem(rxs->rxs_mbuf); 630 rxs->rxs_mbuf = NULL; 631 } 632 bus_dmamap_destroy(sc->sc_dmat, rxs->rxs_dmamap); 633 } 634 for (i = 0; i < TULIP_TXQUEUELEN; i++) { 635 txs = &sc->sc_txsoft[i]; 636 if (txs->txs_mbuf != NULL) { 637 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap); 638 m_freem(txs->txs_mbuf); 639 txs->txs_mbuf = NULL; 640 } 641 bus_dmamap_destroy(sc->sc_dmat, txs->txs_dmamap); 642 } 643 bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap); 644 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap); 645 bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data, 646 sizeof(struct tulip_control_data)); 647 bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg); 648 649 pmf_device_deregister(self); 650 651 if (sc->sc_srom) 652 free(sc->sc_srom, M_DEVBUF); 653 654 return (0); 655 } 656 657 /* 658 * tlp_start: [ifnet interface function] 659 * 660 * Start packet transmission on the interface. 661 */ 662 static void 663 tlp_start(struct ifnet *ifp) 664 { 665 struct tulip_softc *sc = ifp->if_softc; 666 struct mbuf *m0, *m; 667 struct tulip_txsoft *txs, *last_txs = NULL; 668 bus_dmamap_t dmamap; 669 int error, firsttx, nexttx, lasttx = 1, ofree, seg; 670 struct tulip_desc *txd; 671 672 DPRINTF(sc, ("%s: tlp_start: sc_flags 0x%08x, if_flags 0x%08x\n", 673 device_xname(sc->sc_dev), sc->sc_flags, ifp->if_flags)); 674 675 /* 676 * If we want a filter setup, it means no more descriptors were 677 * available for the setup routine. Let it get a chance to wedge 678 * itself into the ring. 679 */ 680 if (sc->sc_flags & TULIPF_WANT_SETUP) 681 ifp->if_flags |= IFF_OACTIVE; 682 683 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING) 684 return; 685 686 if (sc->sc_tick == tlp_2114x_nway_tick && 687 (sc->sc_flags & TULIPF_LINK_UP) == 0 && ifp->if_snd.ifq_len < 10) 688 return; 689 690 /* 691 * Remember the previous number of free descriptors and 692 * the first descriptor we'll use. 693 */ 694 ofree = sc->sc_txfree; 695 firsttx = sc->sc_txnext; 696 697 DPRINTF(sc, ("%s: tlp_start: txfree %d, txnext %d\n", 698 device_xname(sc->sc_dev), ofree, firsttx)); 699 700 /* 701 * Loop through the send queue, setting up transmit descriptors 702 * until we drain the queue, or use up all available transmit 703 * descriptors. 704 */ 705 while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL && 706 sc->sc_txfree != 0) { 707 /* 708 * Grab a packet off the queue. 709 */ 710 IFQ_POLL(&ifp->if_snd, m0); 711 if (m0 == NULL) 712 break; 713 m = NULL; 714 715 dmamap = txs->txs_dmamap; 716 717 /* 718 * Load the DMA map. If this fails, the packet either 719 * didn't fit in the alloted number of segments, or we were 720 * short on resources. In this case, we'll copy and try 721 * again. 722 * 723 * Note that if we're only allowed 1 Tx segment, we 724 * have an alignment restriction. Do this test before 725 * attempting to load the DMA map, because it's more 726 * likely we'll trip the alignment test than the 727 * more-than-one-segment test. 728 */ 729 if ((sc->sc_ntxsegs == 1 && (mtod(m0, uintptr_t) & 3) != 0) || 730 bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0, 731 BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) { 732 MGETHDR(m, M_DONTWAIT, MT_DATA); 733 if (m == NULL) { 734 aprint_error_dev(sc->sc_dev, "unable to allocate Tx mbuf\n"); 735 break; 736 } 737 MCLAIM(m, &sc->sc_ethercom.ec_tx_mowner); 738 if (m0->m_pkthdr.len > MHLEN) { 739 MCLGET(m, M_DONTWAIT); 740 if ((m->m_flags & M_EXT) == 0) { 741 aprint_error_dev(sc->sc_dev, 742 "unable to allocate Tx cluster\n"); 743 m_freem(m); 744 break; 745 } 746 } 747 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *)); 748 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len; 749 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, 750 m, BUS_DMA_WRITE|BUS_DMA_NOWAIT); 751 if (error) { 752 aprint_error_dev(sc->sc_dev, 753 "unable to load Tx buffer, error = %d", 754 error); 755 break; 756 } 757 } 758 759 /* 760 * Ensure we have enough descriptors free to describe 761 * the packet. 762 */ 763 if (dmamap->dm_nsegs > sc->sc_txfree) { 764 /* 765 * Not enough free descriptors to transmit this 766 * packet. We haven't committed to anything yet, 767 * so just unload the DMA map, put the packet 768 * back on the queue, and punt. Notify the upper 769 * layer that there are no more slots left. 770 * 771 * XXX We could allocate an mbuf and copy, but 772 * XXX it is worth it? 773 */ 774 ifp->if_flags |= IFF_OACTIVE; 775 bus_dmamap_unload(sc->sc_dmat, dmamap); 776 if (m != NULL) 777 m_freem(m); 778 break; 779 } 780 781 IFQ_DEQUEUE(&ifp->if_snd, m0); 782 if (m != NULL) { 783 m_freem(m0); 784 m0 = m; 785 } 786 787 /* 788 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET. 789 */ 790 791 /* Sync the DMA map. */ 792 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize, 793 BUS_DMASYNC_PREWRITE); 794 795 /* 796 * Initialize the transmit descriptors. 797 */ 798 for (nexttx = sc->sc_txnext, seg = 0; 799 seg < dmamap->dm_nsegs; 800 seg++, nexttx = TULIP_NEXTTX(nexttx)) { 801 /* 802 * If this is the first descriptor we're 803 * enqueueing, don't set the OWN bit just 804 * yet. That could cause a race condition. 805 * We'll do it below. 806 */ 807 txd = &sc->sc_txdescs[nexttx]; 808 txd->td_status = 809 (nexttx == firsttx) ? 0 : htole32(TDSTAT_OWN); 810 txd->td_bufaddr1 = 811 htole32(dmamap->dm_segs[seg].ds_addr); 812 txd->td_ctl = 813 htole32((dmamap->dm_segs[seg].ds_len << 814 TDCTL_SIZE1_SHIFT) | sc->sc_tdctl_ch | 815 (nexttx == (TULIP_NTXDESC - 1) ? 816 sc->sc_tdctl_er : 0)); 817 lasttx = nexttx; 818 } 819 820 KASSERT(lasttx != -1); 821 822 /* Set `first segment' and `last segment' appropriately. */ 823 sc->sc_txdescs[sc->sc_txnext].td_ctl |= htole32(TDCTL_Tx_FS); 824 sc->sc_txdescs[lasttx].td_ctl |= htole32(TDCTL_Tx_LS); 825 826 #ifdef TLP_DEBUG 827 if (ifp->if_flags & IFF_DEBUG) { 828 printf(" txsoft %p transmit chain:\n", txs); 829 for (seg = sc->sc_txnext;; seg = TULIP_NEXTTX(seg)) { 830 txd = &sc->sc_txdescs[seg]; 831 printf(" descriptor %d:\n", seg); 832 printf(" td_status: 0x%08x\n", 833 le32toh(txd->td_status)); 834 printf(" td_ctl: 0x%08x\n", 835 le32toh(txd->td_ctl)); 836 printf(" td_bufaddr1: 0x%08x\n", 837 le32toh(txd->td_bufaddr1)); 838 printf(" td_bufaddr2: 0x%08x\n", 839 le32toh(txd->td_bufaddr2)); 840 if (seg == lasttx) 841 break; 842 } 843 } 844 #endif 845 846 /* Sync the descriptors we're using. */ 847 TULIP_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs, 848 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 849 850 /* 851 * Store a pointer to the packet so we can free it later, 852 * and remember what txdirty will be once the packet is 853 * done. 854 */ 855 txs->txs_mbuf = m0; 856 txs->txs_firstdesc = sc->sc_txnext; 857 txs->txs_lastdesc = lasttx; 858 txs->txs_ndescs = dmamap->dm_nsegs; 859 860 /* Advance the tx pointer. */ 861 sc->sc_txfree -= dmamap->dm_nsegs; 862 sc->sc_txnext = nexttx; 863 864 SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q); 865 SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q); 866 867 last_txs = txs; 868 869 /* 870 * Pass the packet to any BPF listeners. 871 */ 872 bpf_mtap(ifp, m0); 873 } 874 875 if (txs == NULL || sc->sc_txfree == 0) { 876 /* No more slots left; notify upper layer. */ 877 ifp->if_flags |= IFF_OACTIVE; 878 } 879 880 if (sc->sc_txfree != ofree) { 881 DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n", 882 device_xname(sc->sc_dev), lasttx, firsttx)); 883 /* 884 * Cause a transmit interrupt to happen on the 885 * last packet we enqueued. 886 */ 887 sc->sc_txdescs[lasttx].td_ctl |= htole32(TDCTL_Tx_IC); 888 TULIP_CDTXSYNC(sc, lasttx, 1, 889 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 890 891 /* 892 * Some clone chips want IC on the *first* segment in 893 * the packet. Appease them. 894 */ 895 KASSERT(last_txs != NULL); 896 if ((sc->sc_flags & TULIPF_IC_FS) != 0 && 897 last_txs->txs_firstdesc != lasttx) { 898 sc->sc_txdescs[last_txs->txs_firstdesc].td_ctl |= 899 htole32(TDCTL_Tx_IC); 900 TULIP_CDTXSYNC(sc, last_txs->txs_firstdesc, 1, 901 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 902 } 903 904 /* 905 * The entire packet chain is set up. Give the 906 * first descriptor to the chip now. 907 */ 908 sc->sc_txdescs[firsttx].td_status |= htole32(TDSTAT_OWN); 909 TULIP_CDTXSYNC(sc, firsttx, 1, 910 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 911 912 /* Wake up the transmitter. */ 913 /* XXX USE AUTOPOLLING? */ 914 TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD); 915 916 /* Set a watchdog timer in case the chip flakes out. */ 917 ifp->if_timer = 5; 918 } 919 } 920 921 /* 922 * tlp_watchdog: [ifnet interface function] 923 * 924 * Watchdog timer handler. 925 */ 926 static void 927 tlp_watchdog(struct ifnet *ifp) 928 { 929 struct tulip_softc *sc = ifp->if_softc; 930 int doing_setup, doing_transmit; 931 932 doing_setup = (sc->sc_flags & TULIPF_DOING_SETUP); 933 doing_transmit = (! SIMPLEQ_EMPTY(&sc->sc_txdirtyq)); 934 935 if (doing_setup && doing_transmit) { 936 printf("%s: filter setup and transmit timeout\n", device_xname(sc->sc_dev)); 937 ifp->if_oerrors++; 938 } else if (doing_transmit) { 939 printf("%s: transmit timeout\n", device_xname(sc->sc_dev)); 940 ifp->if_oerrors++; 941 } else if (doing_setup) 942 printf("%s: filter setup timeout\n", device_xname(sc->sc_dev)); 943 else 944 printf("%s: spurious watchdog timeout\n", device_xname(sc->sc_dev)); 945 946 (void) tlp_init(ifp); 947 948 /* Try to get more packets going. */ 949 tlp_start(ifp); 950 } 951 952 /* If the interface is up and running, only modify the receive 953 * filter when setting promiscuous or debug mode. Otherwise fall 954 * through to ether_ioctl, which will reset the chip. 955 */ 956 static int 957 tlp_ifflags_cb(struct ethercom *ec) 958 { 959 struct ifnet *ifp = &ec->ec_if; 960 struct tulip_softc *sc = ifp->if_softc; 961 int change = ifp->if_flags ^ sc->sc_if_flags; 962 963 if ((change & ~(IFF_CANTCHANGE|IFF_DEBUG)) != 0) 964 return ENETRESET; 965 if ((change & IFF_PROMISC) != 0) 966 (*sc->sc_filter_setup)(sc); 967 return 0; 968 } 969 970 /* 971 * tlp_ioctl: [ifnet interface function] 972 * 973 * Handle control requests from the operator. 974 */ 975 static int 976 tlp_ioctl(struct ifnet *ifp, u_long cmd, void *data) 977 { 978 struct tulip_softc *sc = ifp->if_softc; 979 struct ifreq *ifr = (struct ifreq *)data; 980 int s, error; 981 982 s = splnet(); 983 984 switch (cmd) { 985 case SIOCSIFMEDIA: 986 case SIOCGIFMEDIA: 987 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd); 988 break; 989 default: 990 error = ether_ioctl(ifp, cmd, data); 991 if (error == ENETRESET) { 992 if (ifp->if_flags & IFF_RUNNING) { 993 /* 994 * Multicast list has changed. Set the 995 * hardware filter accordingly. 996 */ 997 (*sc->sc_filter_setup)(sc); 998 } 999 error = 0; 1000 } 1001 break; 1002 } 1003 1004 /* Try to get more packets going. */ 1005 if (TULIP_IS_ENABLED(sc)) 1006 tlp_start(ifp); 1007 1008 sc->sc_if_flags = ifp->if_flags; 1009 splx(s); 1010 return (error); 1011 } 1012 1013 /* 1014 * tlp_intr: 1015 * 1016 * Interrupt service routine. 1017 */ 1018 int 1019 tlp_intr(void *arg) 1020 { 1021 struct tulip_softc *sc = arg; 1022 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 1023 uint32_t status, rxstatus, txstatus; 1024 int handled = 0, txthresh; 1025 1026 DPRINTF(sc, ("%s: tlp_intr\n", device_xname(sc->sc_dev))); 1027 1028 #ifdef DEBUG 1029 if (TULIP_IS_ENABLED(sc) == 0) 1030 panic("%s: tlp_intr: not enabled", device_xname(sc->sc_dev)); 1031 #endif 1032 1033 /* 1034 * If the interface isn't running, the interrupt couldn't 1035 * possibly have come from us. 1036 */ 1037 if ((ifp->if_flags & IFF_RUNNING) == 0 || 1038 !device_is_active(sc->sc_dev)) 1039 return (0); 1040 1041 /* Disable interrupts on the DM9102 (interrupt edge bug). */ 1042 switch (sc->sc_chip) { 1043 case TULIP_CHIP_DM9102: 1044 case TULIP_CHIP_DM9102A: 1045 TULIP_WRITE(sc, CSR_INTEN, 0); 1046 break; 1047 1048 default: 1049 /* Nothing. */ 1050 break; 1051 } 1052 1053 for (;;) { 1054 status = TULIP_READ(sc, CSR_STATUS); 1055 if (status) 1056 TULIP_WRITE(sc, CSR_STATUS, status); 1057 1058 if ((status & sc->sc_inten) == 0) 1059 break; 1060 1061 handled = 1; 1062 1063 rxstatus = status & sc->sc_rxint_mask; 1064 txstatus = status & sc->sc_txint_mask; 1065 1066 if (rxstatus) { 1067 /* Grab new any new packets. */ 1068 tlp_rxintr(sc); 1069 1070 if (rxstatus & STATUS_RWT) 1071 printf("%s: receive watchdog timeout\n", 1072 device_xname(sc->sc_dev)); 1073 1074 if (rxstatus & STATUS_RU) { 1075 printf("%s: receive ring overrun\n", 1076 device_xname(sc->sc_dev)); 1077 /* Get the receive process going again. */ 1078 if (sc->sc_tdctl_er != TDCTL_ER) { 1079 tlp_idle(sc, OPMODE_SR); 1080 TULIP_WRITE(sc, CSR_RXLIST, 1081 TULIP_CDRXADDR(sc, sc->sc_rxptr)); 1082 TULIP_WRITE(sc, CSR_OPMODE, 1083 sc->sc_opmode); 1084 } 1085 TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD); 1086 break; 1087 } 1088 } 1089 1090 if (txstatus) { 1091 /* Sweep up transmit descriptors. */ 1092 tlp_txintr(sc); 1093 1094 if (txstatus & STATUS_TJT) 1095 printf("%s: transmit jabber timeout\n", 1096 device_xname(sc->sc_dev)); 1097 1098 if (txstatus & STATUS_UNF) { 1099 /* 1100 * Increase our transmit threshold if 1101 * another is available. 1102 */ 1103 txthresh = sc->sc_txthresh + 1; 1104 if (sc->sc_txth[txthresh].txth_name != NULL) { 1105 /* Idle the transmit process. */ 1106 tlp_idle(sc, OPMODE_ST); 1107 1108 sc->sc_txthresh = txthresh; 1109 sc->sc_opmode &= ~(OPMODE_TR|OPMODE_SF); 1110 sc->sc_opmode |= 1111 sc->sc_txth[txthresh].txth_opmode; 1112 printf("%s: transmit underrun; new " 1113 "threshold: %s\n", 1114 device_xname(sc->sc_dev), 1115 sc->sc_txth[txthresh].txth_name); 1116 1117 /* 1118 * Set the new threshold and restart 1119 * the transmit process. 1120 */ 1121 TULIP_WRITE(sc, CSR_OPMODE, 1122 sc->sc_opmode); 1123 } 1124 /* 1125 * XXX Log every Nth underrun from 1126 * XXX now on? 1127 */ 1128 } 1129 } 1130 1131 if (status & (STATUS_TPS|STATUS_RPS)) { 1132 if (status & STATUS_TPS) 1133 printf("%s: transmit process stopped\n", 1134 device_xname(sc->sc_dev)); 1135 if (status & STATUS_RPS) 1136 printf("%s: receive process stopped\n", 1137 device_xname(sc->sc_dev)); 1138 (void) tlp_init(ifp); 1139 break; 1140 } 1141 1142 if (status & STATUS_SE) { 1143 const char *str; 1144 switch (status & STATUS_EB) { 1145 case STATUS_EB_PARITY: 1146 str = "parity error"; 1147 break; 1148 1149 case STATUS_EB_MABT: 1150 str = "master abort"; 1151 break; 1152 1153 case STATUS_EB_TABT: 1154 str = "target abort"; 1155 break; 1156 1157 default: 1158 str = "unknown error"; 1159 break; 1160 } 1161 aprint_error_dev(sc->sc_dev, "fatal system error: %s\n", 1162 str); 1163 (void) tlp_init(ifp); 1164 break; 1165 } 1166 1167 /* 1168 * Not handled: 1169 * 1170 * Transmit buffer unavailable -- normal 1171 * condition, nothing to do, really. 1172 * 1173 * General purpose timer experied -- we don't 1174 * use the general purpose timer. 1175 * 1176 * Early receive interrupt -- not available on 1177 * all chips, we just use RI. We also only 1178 * use single-segment receive DMA, so this 1179 * is mostly useless. 1180 */ 1181 } 1182 1183 /* Bring interrupts back up on the DM9102. */ 1184 switch (sc->sc_chip) { 1185 case TULIP_CHIP_DM9102: 1186 case TULIP_CHIP_DM9102A: 1187 TULIP_WRITE(sc, CSR_INTEN, sc->sc_inten); 1188 break; 1189 1190 default: 1191 /* Nothing. */ 1192 break; 1193 } 1194 1195 /* Try to get more packets going. */ 1196 tlp_start(ifp); 1197 1198 #if NRND > 0 1199 if (handled) 1200 rnd_add_uint32(&sc->sc_rnd_source, status); 1201 #endif 1202 return (handled); 1203 } 1204 1205 /* 1206 * tlp_rxintr: 1207 * 1208 * Helper; handle receive interrupts. 1209 */ 1210 static void 1211 tlp_rxintr(struct tulip_softc *sc) 1212 { 1213 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 1214 struct ether_header *eh; 1215 struct tulip_rxsoft *rxs; 1216 struct mbuf *m; 1217 uint32_t rxstat, errors; 1218 int i, len; 1219 1220 for (i = sc->sc_rxptr;; i = TULIP_NEXTRX(i)) { 1221 rxs = &sc->sc_rxsoft[i]; 1222 1223 TULIP_CDRXSYNC(sc, i, 1224 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 1225 1226 rxstat = le32toh(sc->sc_rxdescs[i].td_status); 1227 1228 if (rxstat & TDSTAT_OWN) { 1229 /* 1230 * We have processed all of the receive buffers. 1231 */ 1232 break; 1233 } 1234 1235 /* 1236 * Make sure the packet fit in one buffer. This should 1237 * always be the case. But the Lite-On PNIC, rev 33 1238 * has an awful receive engine bug, which may require 1239 * a very icky work-around. 1240 */ 1241 if ((rxstat & (TDSTAT_Rx_FS|TDSTAT_Rx_LS)) != 1242 (TDSTAT_Rx_FS|TDSTAT_Rx_LS)) { 1243 printf("%s: incoming packet spilled, resetting\n", 1244 device_xname(sc->sc_dev)); 1245 (void) tlp_init(ifp); 1246 return; 1247 } 1248 1249 /* 1250 * If any collisions were seen on the wire, count one. 1251 */ 1252 if (rxstat & TDSTAT_Rx_CS) 1253 ifp->if_collisions++; 1254 1255 /* 1256 * If an error occurred, update stats, clear the status 1257 * word, and leave the packet buffer in place. It will 1258 * simply be reused the next time the ring comes around. 1259 */ 1260 errors = TDSTAT_Rx_DE | TDSTAT_Rx_RF | TDSTAT_Rx_TL | 1261 TDSTAT_Rx_CS | TDSTAT_Rx_RE | TDSTAT_Rx_DB | TDSTAT_Rx_CE; 1262 /* 1263 * If 802.1Q VLAN MTU is enabled, ignore the Frame Too Long 1264 * error. 1265 */ 1266 if ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) != 0) 1267 errors &= ~TDSTAT_Rx_TL; 1268 /* 1269 * If chip doesn't have MII, ignore the MII error bit. 1270 */ 1271 if ((sc->sc_flags & TULIPF_HAS_MII) == 0) 1272 errors &= ~TDSTAT_Rx_RE; 1273 1274 if ((rxstat & TDSTAT_ES) != 0 && 1275 (rxstat & errors) != 0) { 1276 rxstat &= errors; 1277 #define PRINTERR(bit, str) \ 1278 if (rxstat & (bit)) \ 1279 aprint_error_dev(sc->sc_dev, "receive error: %s\n", \ 1280 str) 1281 ifp->if_ierrors++; 1282 PRINTERR(TDSTAT_Rx_DE, "descriptor error"); 1283 PRINTERR(TDSTAT_Rx_RF, "runt frame"); 1284 PRINTERR(TDSTAT_Rx_TL, "frame too long"); 1285 PRINTERR(TDSTAT_Rx_RE, "MII error"); 1286 PRINTERR(TDSTAT_Rx_DB, "dribbling bit"); 1287 PRINTERR(TDSTAT_Rx_CE, "CRC error"); 1288 #undef PRINTERR 1289 TULIP_INIT_RXDESC(sc, i); 1290 continue; 1291 } 1292 1293 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0, 1294 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD); 1295 1296 /* 1297 * No errors; receive the packet. Note the Tulip 1298 * includes the CRC with every packet. 1299 */ 1300 len = TDSTAT_Rx_LENGTH(rxstat) - ETHER_CRC_LEN; 1301 1302 #ifdef __NO_STRICT_ALIGNMENT 1303 /* 1304 * Allocate a new mbuf cluster. If that fails, we are 1305 * out of memory, and must drop the packet and recycle 1306 * the buffer that's already attached to this descriptor. 1307 */ 1308 m = rxs->rxs_mbuf; 1309 if (tlp_add_rxbuf(sc, i) != 0) { 1310 ifp->if_ierrors++; 1311 TULIP_INIT_RXDESC(sc, i); 1312 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0, 1313 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD); 1314 continue; 1315 } 1316 #else 1317 /* 1318 * The Tulip's receive buffers must be 4-byte aligned. 1319 * But this means that the data after the Ethernet header 1320 * is misaligned. We must allocate a new buffer and 1321 * copy the data, shifted forward 2 bytes. 1322 */ 1323 MGETHDR(m, M_DONTWAIT, MT_DATA); 1324 if (m == NULL) { 1325 dropit: 1326 ifp->if_ierrors++; 1327 TULIP_INIT_RXDESC(sc, i); 1328 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0, 1329 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD); 1330 continue; 1331 } 1332 MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner); 1333 if (len > (MHLEN - 2)) { 1334 MCLGET(m, M_DONTWAIT); 1335 if ((m->m_flags & M_EXT) == 0) { 1336 m_freem(m); 1337 goto dropit; 1338 } 1339 } 1340 m->m_data += 2; 1341 1342 /* 1343 * Note that we use clusters for incoming frames, so the 1344 * buffer is virtually contiguous. 1345 */ 1346 memcpy(mtod(m, void *), mtod(rxs->rxs_mbuf, void *), len); 1347 1348 /* Allow the receive descriptor to continue using its mbuf. */ 1349 TULIP_INIT_RXDESC(sc, i); 1350 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0, 1351 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD); 1352 #endif /* __NO_STRICT_ALIGNMENT */ 1353 1354 ifp->if_ipackets++; 1355 eh = mtod(m, struct ether_header *); 1356 m->m_pkthdr.rcvif = ifp; 1357 m->m_pkthdr.len = m->m_len = len; 1358 1359 /* 1360 * XXX Work-around for a weird problem with the emulated 1361 * 21041 on Connectix Virtual PC: 1362 * 1363 * When we receive a full-size TCP segment, we seem to get 1364 * a packet there the Rx status says 1522 bytes, yet we do 1365 * not get a frame-too-long error from the chip. The extra 1366 * bytes seem to always be zeros. Perhaps Virtual PC is 1367 * inserting 4 bytes of zeros after every packet. In any 1368 * case, let's try and detect this condition and truncate 1369 * the length so that it will pass up the stack. 1370 */ 1371 if (__predict_false((sc->sc_flags & TULIPF_VPC) != 0)) { 1372 uint16_t etype = ntohs(eh->ether_type); 1373 1374 if (len > ETHER_MAX_FRAME(ifp, etype, 0)) 1375 m->m_pkthdr.len = m->m_len = len = 1376 ETHER_MAX_FRAME(ifp, etype, 0); 1377 } 1378 1379 /* 1380 * Pass this up to any BPF listeners, but only 1381 * pass it up the stack if it's for us. 1382 */ 1383 bpf_mtap(ifp, m); 1384 1385 /* 1386 * We sometimes have to run the 21140 in Hash-Only 1387 * mode. If we're in that mode, and not in promiscuous 1388 * mode, and we have a unicast packet that isn't for 1389 * us, then drop it. 1390 */ 1391 if (sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY && 1392 (ifp->if_flags & IFF_PROMISC) == 0 && 1393 ETHER_IS_MULTICAST(eh->ether_dhost) == 0 && 1394 memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost, 1395 ETHER_ADDR_LEN) != 0) { 1396 m_freem(m); 1397 continue; 1398 } 1399 1400 /* Pass it on. */ 1401 (*ifp->if_input)(ifp, m); 1402 } 1403 1404 /* Update the receive pointer. */ 1405 sc->sc_rxptr = i; 1406 } 1407 1408 /* 1409 * tlp_txintr: 1410 * 1411 * Helper; handle transmit interrupts. 1412 */ 1413 static void 1414 tlp_txintr(struct tulip_softc *sc) 1415 { 1416 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 1417 struct tulip_txsoft *txs; 1418 uint32_t txstat; 1419 1420 DPRINTF(sc, ("%s: tlp_txintr: sc_flags 0x%08x\n", 1421 device_xname(sc->sc_dev), sc->sc_flags)); 1422 1423 ifp->if_flags &= ~IFF_OACTIVE; 1424 1425 /* 1426 * Go through our Tx list and free mbufs for those 1427 * frames that have been transmitted. 1428 */ 1429 while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) { 1430 TULIP_CDTXSYNC(sc, txs->txs_lastdesc, 1431 txs->txs_ndescs, 1432 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 1433 1434 #ifdef TLP_DEBUG 1435 if (ifp->if_flags & IFF_DEBUG) { 1436 int i; 1437 struct tulip_desc *txd; 1438 printf(" txsoft %p transmit chain:\n", txs); 1439 for (i = txs->txs_firstdesc;; i = TULIP_NEXTTX(i)) { 1440 txd = &sc->sc_txdescs[i]; 1441 printf(" descriptor %d:\n", i); 1442 printf(" td_status: 0x%08x\n", 1443 le32toh(txd->td_status)); 1444 printf(" td_ctl: 0x%08x\n", 1445 le32toh(txd->td_ctl)); 1446 printf(" td_bufaddr1: 0x%08x\n", 1447 le32toh(txd->td_bufaddr1)); 1448 printf(" td_bufaddr2: 0x%08x\n", 1449 le32toh(sc->sc_txdescs[i].td_bufaddr2)); 1450 if (i == txs->txs_lastdesc) 1451 break; 1452 } 1453 } 1454 #endif 1455 1456 txstat = le32toh(sc->sc_txdescs[txs->txs_lastdesc].td_status); 1457 if (txstat & TDSTAT_OWN) 1458 break; 1459 1460 SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q); 1461 1462 sc->sc_txfree += txs->txs_ndescs; 1463 1464 if (txs->txs_mbuf == NULL) { 1465 /* 1466 * If we didn't have an mbuf, it was the setup 1467 * packet. 1468 */ 1469 #ifdef DIAGNOSTIC 1470 if ((sc->sc_flags & TULIPF_DOING_SETUP) == 0) 1471 panic("tlp_txintr: null mbuf, not doing setup"); 1472 #endif 1473 TULIP_CDSPSYNC(sc, BUS_DMASYNC_POSTWRITE); 1474 sc->sc_flags &= ~TULIPF_DOING_SETUP; 1475 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 1476 continue; 1477 } 1478 1479 bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap, 1480 0, txs->txs_dmamap->dm_mapsize, 1481 BUS_DMASYNC_POSTWRITE); 1482 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap); 1483 m_freem(txs->txs_mbuf); 1484 txs->txs_mbuf = NULL; 1485 1486 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 1487 1488 /* 1489 * Check for errors and collisions. 1490 */ 1491 #ifdef TLP_STATS 1492 if (txstat & TDSTAT_Tx_UF) 1493 sc->sc_stats.ts_tx_uf++; 1494 if (txstat & TDSTAT_Tx_TO) 1495 sc->sc_stats.ts_tx_to++; 1496 if (txstat & TDSTAT_Tx_EC) 1497 sc->sc_stats.ts_tx_ec++; 1498 if (txstat & TDSTAT_Tx_LC) 1499 sc->sc_stats.ts_tx_lc++; 1500 #endif 1501 1502 if (txstat & (TDSTAT_Tx_UF|TDSTAT_Tx_TO)) 1503 ifp->if_oerrors++; 1504 1505 if (txstat & TDSTAT_Tx_EC) 1506 ifp->if_collisions += 16; 1507 else 1508 ifp->if_collisions += TDSTAT_Tx_COLLISIONS(txstat); 1509 if (txstat & TDSTAT_Tx_LC) 1510 ifp->if_collisions++; 1511 1512 ifp->if_opackets++; 1513 } 1514 1515 /* 1516 * If there are no more pending transmissions, cancel the watchdog 1517 * timer. 1518 */ 1519 if (txs == NULL && (sc->sc_flags & TULIPF_DOING_SETUP) == 0) 1520 ifp->if_timer = 0; 1521 1522 /* 1523 * If we have a receive filter setup pending, do it now. 1524 */ 1525 if (sc->sc_flags & TULIPF_WANT_SETUP) 1526 (*sc->sc_filter_setup)(sc); 1527 } 1528 1529 #ifdef TLP_STATS 1530 void 1531 tlp_print_stats(struct tulip_softc *sc) 1532 { 1533 1534 printf("%s: tx_uf %lu, tx_to %lu, tx_ec %lu, tx_lc %lu\n", 1535 device_xname(sc->sc_dev), 1536 sc->sc_stats.ts_tx_uf, sc->sc_stats.ts_tx_to, 1537 sc->sc_stats.ts_tx_ec, sc->sc_stats.ts_tx_lc); 1538 } 1539 #endif 1540 1541 /* 1542 * tlp_reset: 1543 * 1544 * Perform a soft reset on the Tulip. 1545 */ 1546 void 1547 tlp_reset(struct tulip_softc *sc) 1548 { 1549 int i; 1550 1551 TULIP_WRITE(sc, CSR_BUSMODE, BUSMODE_SWR); 1552 1553 /* 1554 * Xircom, ASIX and Conexant clones don't bring themselves 1555 * out of reset automatically. 1556 * Instead, we have to wait at least 50 PCI cycles, and then 1557 * clear SWR. 1558 */ 1559 switch (sc->sc_chip) { 1560 case TULIP_CHIP_X3201_3: 1561 case TULIP_CHIP_AX88140: 1562 case TULIP_CHIP_AX88141: 1563 case TULIP_CHIP_RS7112: 1564 delay(10); 1565 TULIP_WRITE(sc, CSR_BUSMODE, 0); 1566 break; 1567 default: 1568 break; 1569 } 1570 1571 for (i = 0; i < 1000; i++) { 1572 /* 1573 * Wait at least 50 PCI cycles for the reset to 1574 * complete before peeking at the Tulip again. 1575 * 10 uSec is a bit longer than 50 PCI cycles 1576 * (at 33MHz), but it doesn't hurt have the extra 1577 * wait. 1578 */ 1579 delay(10); 1580 if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR) == 0) 1581 break; 1582 } 1583 1584 if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR)) 1585 aprint_error_dev(sc->sc_dev, "reset failed to complete\n"); 1586 1587 delay(1000); 1588 1589 /* 1590 * If the board has any GPIO reset sequences to issue, do them now. 1591 */ 1592 if (sc->sc_reset != NULL) 1593 (*sc->sc_reset)(sc); 1594 } 1595 1596 /* 1597 * tlp_init: [ ifnet interface function ] 1598 * 1599 * Initialize the interface. Must be called at splnet(). 1600 */ 1601 static int 1602 tlp_init(struct ifnet *ifp) 1603 { 1604 struct tulip_softc *sc = ifp->if_softc; 1605 struct tulip_txsoft *txs; 1606 struct tulip_rxsoft *rxs; 1607 int i, error = 0; 1608 1609 if ((error = tlp_enable(sc)) != 0) 1610 goto out; 1611 1612 /* 1613 * Cancel any pending I/O. 1614 */ 1615 tlp_stop(ifp, 0); 1616 1617 /* 1618 * Initialize `opmode' to 0, and call the pre-init routine, if 1619 * any. This is required because the 2114x and some of the 1620 * clones require that the media-related bits in `opmode' be 1621 * set before performing a soft-reset in order to get internal 1622 * chip pathways are correct. Yay! 1623 */ 1624 sc->sc_opmode = 0; 1625 if (sc->sc_preinit != NULL) 1626 (*sc->sc_preinit)(sc); 1627 1628 /* 1629 * Reset the Tulip to a known state. 1630 */ 1631 tlp_reset(sc); 1632 1633 /* 1634 * Initialize the BUSMODE register. 1635 */ 1636 sc->sc_busmode = BUSMODE_BAR; 1637 switch (sc->sc_chip) { 1638 case TULIP_CHIP_21140: 1639 case TULIP_CHIP_21140A: 1640 case TULIP_CHIP_21142: 1641 case TULIP_CHIP_21143: 1642 case TULIP_CHIP_82C115: 1643 case TULIP_CHIP_MX98725: 1644 /* 1645 * If we're allowed to do so, use Memory Read Line 1646 * and Memory Read Multiple. 1647 * 1648 * XXX Should we use Memory Write and Invalidate? 1649 */ 1650 if (sc->sc_flags & TULIPF_MRL) 1651 sc->sc_busmode |= BUSMODE_RLE; 1652 if (sc->sc_flags & TULIPF_MRM) 1653 sc->sc_busmode |= BUSMODE_RME; 1654 #if 0 1655 if (sc->sc_flags & TULIPF_MWI) 1656 sc->sc_busmode |= BUSMODE_WLE; 1657 #endif 1658 break; 1659 1660 case TULIP_CHIP_82C168: 1661 case TULIP_CHIP_82C169: 1662 sc->sc_busmode |= BUSMODE_PNIC_MBO; 1663 if (sc->sc_maxburst == 0) 1664 sc->sc_maxburst = 16; 1665 break; 1666 1667 case TULIP_CHIP_AX88140: 1668 case TULIP_CHIP_AX88141: 1669 if (sc->sc_maxburst == 0) 1670 sc->sc_maxburst = 16; 1671 break; 1672 1673 default: 1674 /* Nothing. */ 1675 break; 1676 } 1677 switch (sc->sc_cacheline) { 1678 default: 1679 /* 1680 * Note: We must *always* set these bits; a cache 1681 * alignment of 0 is RESERVED. 1682 */ 1683 case 8: 1684 sc->sc_busmode |= BUSMODE_CAL_8LW; 1685 break; 1686 case 16: 1687 sc->sc_busmode |= BUSMODE_CAL_16LW; 1688 break; 1689 case 32: 1690 sc->sc_busmode |= BUSMODE_CAL_32LW; 1691 break; 1692 } 1693 switch (sc->sc_maxburst) { 1694 case 1: 1695 sc->sc_busmode |= BUSMODE_PBL_1LW; 1696 break; 1697 case 2: 1698 sc->sc_busmode |= BUSMODE_PBL_2LW; 1699 break; 1700 case 4: 1701 sc->sc_busmode |= BUSMODE_PBL_4LW; 1702 break; 1703 case 8: 1704 sc->sc_busmode |= BUSMODE_PBL_8LW; 1705 break; 1706 case 16: 1707 sc->sc_busmode |= BUSMODE_PBL_16LW; 1708 break; 1709 case 32: 1710 sc->sc_busmode |= BUSMODE_PBL_32LW; 1711 break; 1712 default: 1713 sc->sc_busmode |= BUSMODE_PBL_DEFAULT; 1714 break; 1715 } 1716 #if BYTE_ORDER == BIG_ENDIAN 1717 /* 1718 * Can't use BUSMODE_BLE or BUSMODE_DBO; not all chips 1719 * support them, and even on ones that do, it doesn't 1720 * always work. So we always access descriptors with 1721 * little endian via htole32/le32toh. 1722 */ 1723 #endif 1724 /* 1725 * Big-endian bus requires BUSMODE_BLE anyway. 1726 * Also, BUSMODE_DBO is needed because we assume 1727 * descriptors are little endian. 1728 */ 1729 if (sc->sc_flags & TULIPF_BLE) 1730 sc->sc_busmode |= BUSMODE_BLE; 1731 if (sc->sc_flags & TULIPF_DBO) 1732 sc->sc_busmode |= BUSMODE_DBO; 1733 1734 /* 1735 * Some chips have a broken bus interface. 1736 */ 1737 switch (sc->sc_chip) { 1738 case TULIP_CHIP_DM9102: 1739 case TULIP_CHIP_DM9102A: 1740 sc->sc_busmode = 0; 1741 break; 1742 1743 default: 1744 /* Nothing. */ 1745 break; 1746 } 1747 1748 TULIP_WRITE(sc, CSR_BUSMODE, sc->sc_busmode); 1749 1750 /* 1751 * Initialize the OPMODE register. We don't write it until 1752 * we're ready to begin the transmit and receive processes. 1753 * 1754 * Media-related OPMODE bits are set in the media callbacks 1755 * for each specific chip/board. 1756 */ 1757 sc->sc_opmode |= OPMODE_SR | OPMODE_ST | 1758 sc->sc_txth[sc->sc_txthresh].txth_opmode; 1759 1760 /* 1761 * Magical mystery initialization on the Macronix chips. 1762 * The MX98713 uses its own magic value, the rest share 1763 * a common one. 1764 */ 1765 switch (sc->sc_chip) { 1766 case TULIP_CHIP_MX98713: 1767 TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98713); 1768 break; 1769 1770 case TULIP_CHIP_MX98713A: 1771 case TULIP_CHIP_MX98715: 1772 case TULIP_CHIP_MX98715A: 1773 case TULIP_CHIP_MX98715AEC_X: 1774 case TULIP_CHIP_MX98725: 1775 TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98715); 1776 break; 1777 1778 default: 1779 /* Nothing. */ 1780 break; 1781 } 1782 1783 /* 1784 * Initialize the transmit descriptor ring. 1785 */ 1786 memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs)); 1787 for (i = 0; i < TULIP_NTXDESC; i++) { 1788 struct tulip_desc *txd = &sc->sc_txdescs[i]; 1789 txd->td_ctl = htole32(sc->sc_tdctl_ch); 1790 txd->td_bufaddr2 = htole32(TULIP_CDTXADDR(sc, TULIP_NEXTTX(i))); 1791 } 1792 sc->sc_txdescs[TULIP_NTXDESC - 1].td_ctl |= htole32(sc->sc_tdctl_er); 1793 TULIP_CDTXSYNC(sc, 0, TULIP_NTXDESC, 1794 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 1795 sc->sc_txfree = TULIP_NTXDESC; 1796 sc->sc_txnext = 0; 1797 1798 /* 1799 * Initialize the transmit job descriptors. 1800 */ 1801 SIMPLEQ_INIT(&sc->sc_txfreeq); 1802 SIMPLEQ_INIT(&sc->sc_txdirtyq); 1803 for (i = 0; i < TULIP_TXQUEUELEN; i++) { 1804 txs = &sc->sc_txsoft[i]; 1805 txs->txs_mbuf = NULL; 1806 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 1807 } 1808 1809 /* 1810 * Initialize the receive descriptor and receive job 1811 * descriptor rings. 1812 */ 1813 for (i = 0; i < TULIP_NRXDESC; i++) { 1814 rxs = &sc->sc_rxsoft[i]; 1815 if (rxs->rxs_mbuf == NULL) { 1816 if ((error = tlp_add_rxbuf(sc, i)) != 0) { 1817 aprint_error_dev(sc->sc_dev, "unable to allocate or map rx " 1818 "buffer %d, error = %d\n", 1819 i, error); 1820 /* 1821 * XXX Should attempt to run with fewer receive 1822 * XXX buffers instead of just failing. 1823 */ 1824 tlp_rxdrain(sc); 1825 goto out; 1826 } 1827 } else 1828 TULIP_INIT_RXDESC(sc, i); 1829 } 1830 sc->sc_rxptr = 0; 1831 1832 /* 1833 * Initialize the interrupt mask and enable interrupts. 1834 */ 1835 /* normal interrupts */ 1836 sc->sc_inten = STATUS_TI | STATUS_TU | STATUS_RI | STATUS_NIS; 1837 1838 /* abnormal interrupts */ 1839 sc->sc_inten |= STATUS_TPS | STATUS_TJT | STATUS_UNF | 1840 STATUS_RU | STATUS_RPS | STATUS_RWT | STATUS_SE | STATUS_AIS; 1841 1842 sc->sc_rxint_mask = STATUS_RI|STATUS_RU|STATUS_RWT; 1843 sc->sc_txint_mask = STATUS_TI|STATUS_UNF|STATUS_TJT; 1844 1845 switch (sc->sc_chip) { 1846 case TULIP_CHIP_WB89C840F: 1847 /* 1848 * Clear bits that we don't want that happen to 1849 * overlap or don't exist. 1850 */ 1851 sc->sc_inten &= ~(STATUS_WINB_REI|STATUS_RWT); 1852 break; 1853 1854 default: 1855 /* Nothing. */ 1856 break; 1857 } 1858 1859 sc->sc_rxint_mask &= sc->sc_inten; 1860 sc->sc_txint_mask &= sc->sc_inten; 1861 1862 TULIP_WRITE(sc, CSR_INTEN, sc->sc_inten); 1863 TULIP_WRITE(sc, CSR_STATUS, 0xffffffff); 1864 1865 /* 1866 * Give the transmit and receive rings to the Tulip. 1867 */ 1868 TULIP_WRITE(sc, CSR_TXLIST, TULIP_CDTXADDR(sc, sc->sc_txnext)); 1869 TULIP_WRITE(sc, CSR_RXLIST, TULIP_CDRXADDR(sc, sc->sc_rxptr)); 1870 1871 /* 1872 * On chips that do this differently, set the station address. 1873 */ 1874 switch (sc->sc_chip) { 1875 case TULIP_CHIP_WB89C840F: 1876 { 1877 /* XXX Do this with stream writes? */ 1878 bus_addr_t cpa = TULIP_CSR_OFFSET(sc, CSR_WINB_CPA0); 1879 1880 for (i = 0; i < ETHER_ADDR_LEN; i++) { 1881 bus_space_write_1(sc->sc_st, sc->sc_sh, 1882 cpa + i, CLLADDR(ifp->if_sadl)[i]); 1883 } 1884 break; 1885 } 1886 1887 case TULIP_CHIP_AL981: 1888 case TULIP_CHIP_AN983: 1889 case TULIP_CHIP_AN985: 1890 { 1891 uint32_t reg; 1892 const uint8_t *enaddr = CLLADDR(ifp->if_sadl); 1893 1894 reg = enaddr[0] | 1895 (enaddr[1] << 8) | 1896 (enaddr[2] << 16) | 1897 (enaddr[3] << 24); 1898 bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_PAR0, reg); 1899 1900 reg = enaddr[4] | 1901 (enaddr[5] << 8); 1902 bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_PAR1, reg); 1903 break; 1904 } 1905 1906 case TULIP_CHIP_AX88140: 1907 case TULIP_CHIP_AX88141: 1908 { 1909 uint32_t reg; 1910 const uint8_t *enaddr = CLLADDR(ifp->if_sadl); 1911 1912 reg = enaddr[0] | 1913 (enaddr[1] << 8) | 1914 (enaddr[2] << 16) | 1915 (enaddr[3] << 24); 1916 TULIP_WRITE(sc, CSR_AX_FILTIDX, AX_FILTIDX_PAR0); 1917 TULIP_WRITE(sc, CSR_AX_FILTDATA, reg); 1918 1919 reg = enaddr[4] | (enaddr[5] << 8); 1920 TULIP_WRITE(sc, CSR_AX_FILTIDX, AX_FILTIDX_PAR1); 1921 TULIP_WRITE(sc, CSR_AX_FILTDATA, reg); 1922 break; 1923 } 1924 1925 default: 1926 /* Nothing. */ 1927 break; 1928 } 1929 1930 /* 1931 * Set the receive filter. This will start the transmit and 1932 * receive processes. 1933 */ 1934 (*sc->sc_filter_setup)(sc); 1935 1936 /* 1937 * Set the current media. 1938 */ 1939 (void) (*sc->sc_mediasw->tmsw_set)(sc); 1940 1941 /* 1942 * Start the receive process. 1943 */ 1944 TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD); 1945 1946 if (sc->sc_tick != NULL) { 1947 /* Start the one second clock. */ 1948 callout_reset(&sc->sc_tick_callout, hz >> 3, sc->sc_tick, sc); 1949 } 1950 1951 /* 1952 * Note that the interface is now running. 1953 */ 1954 ifp->if_flags |= IFF_RUNNING; 1955 ifp->if_flags &= ~IFF_OACTIVE; 1956 sc->sc_if_flags = ifp->if_flags; 1957 1958 out: 1959 if (error) { 1960 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 1961 ifp->if_timer = 0; 1962 printf("%s: interface not running\n", device_xname(sc->sc_dev)); 1963 } 1964 return (error); 1965 } 1966 1967 /* 1968 * tlp_enable: 1969 * 1970 * Enable the Tulip chip. 1971 */ 1972 static int 1973 tlp_enable(struct tulip_softc *sc) 1974 { 1975 1976 if (TULIP_IS_ENABLED(sc) == 0 && sc->sc_enable != NULL) { 1977 if ((*sc->sc_enable)(sc) != 0) { 1978 aprint_error_dev(sc->sc_dev, "device enable failed\n"); 1979 return (EIO); 1980 } 1981 sc->sc_flags |= TULIPF_ENABLED; 1982 } 1983 return (0); 1984 } 1985 1986 /* 1987 * tlp_disable: 1988 * 1989 * Disable the Tulip chip. 1990 */ 1991 static void 1992 tlp_disable(struct tulip_softc *sc) 1993 { 1994 1995 if (TULIP_IS_ENABLED(sc) && sc->sc_disable != NULL) { 1996 (*sc->sc_disable)(sc); 1997 sc->sc_flags &= ~TULIPF_ENABLED; 1998 } 1999 } 2000 2001 /* 2002 * tlp_rxdrain: 2003 * 2004 * Drain the receive queue. 2005 */ 2006 static void 2007 tlp_rxdrain(struct tulip_softc *sc) 2008 { 2009 struct tulip_rxsoft *rxs; 2010 int i; 2011 2012 for (i = 0; i < TULIP_NRXDESC; i++) { 2013 rxs = &sc->sc_rxsoft[i]; 2014 if (rxs->rxs_mbuf != NULL) { 2015 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap); 2016 m_freem(rxs->rxs_mbuf); 2017 rxs->rxs_mbuf = NULL; 2018 } 2019 } 2020 } 2021 2022 /* 2023 * tlp_stop: [ ifnet interface function ] 2024 * 2025 * Stop transmission on the interface. 2026 */ 2027 static void 2028 tlp_stop(struct ifnet *ifp, int disable) 2029 { 2030 struct tulip_softc *sc = ifp->if_softc; 2031 struct tulip_txsoft *txs; 2032 2033 if (sc->sc_tick != NULL) { 2034 /* Stop the one second clock. */ 2035 callout_stop(&sc->sc_tick_callout); 2036 } 2037 2038 if (sc->sc_flags & TULIPF_HAS_MII) { 2039 /* Down the MII. */ 2040 mii_down(&sc->sc_mii); 2041 } 2042 2043 /* Disable interrupts. */ 2044 TULIP_WRITE(sc, CSR_INTEN, 0); 2045 2046 /* Stop the transmit and receive processes. */ 2047 sc->sc_opmode = 0; 2048 TULIP_WRITE(sc, CSR_OPMODE, 0); 2049 TULIP_WRITE(sc, CSR_RXLIST, 0); 2050 TULIP_WRITE(sc, CSR_TXLIST, 0); 2051 2052 /* 2053 * Release any queued transmit buffers. 2054 */ 2055 while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) { 2056 SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q); 2057 if (txs->txs_mbuf != NULL) { 2058 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap); 2059 m_freem(txs->txs_mbuf); 2060 txs->txs_mbuf = NULL; 2061 } 2062 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 2063 } 2064 2065 sc->sc_flags &= ~(TULIPF_WANT_SETUP|TULIPF_DOING_SETUP); 2066 2067 /* 2068 * Mark the interface down and cancel the watchdog timer. 2069 */ 2070 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 2071 sc->sc_if_flags = ifp->if_flags; 2072 ifp->if_timer = 0; 2073 2074 /* 2075 * Reset the chip (needed on some flavors to actually disable it). 2076 */ 2077 tlp_reset(sc); 2078 2079 if (disable) { 2080 tlp_rxdrain(sc); 2081 tlp_disable(sc); 2082 } 2083 } 2084 2085 #define SROM_EMIT(sc, x) \ 2086 do { \ 2087 TULIP_WRITE((sc), CSR_MIIROM, (x)); \ 2088 delay(2); \ 2089 } while (0) 2090 2091 /* 2092 * tlp_srom_idle: 2093 * 2094 * Put the SROM in idle state. 2095 */ 2096 static void 2097 tlp_srom_idle(struct tulip_softc *sc) 2098 { 2099 uint32_t miirom; 2100 int i; 2101 2102 miirom = MIIROM_SR; 2103 SROM_EMIT(sc, miirom); 2104 2105 miirom |= MIIROM_RD; 2106 SROM_EMIT(sc, miirom); 2107 2108 miirom |= MIIROM_SROMCS; 2109 SROM_EMIT(sc, miirom); 2110 2111 SROM_EMIT(sc, miirom|MIIROM_SROMSK); 2112 2113 /* Strobe the clock 32 times. */ 2114 for (i = 0; i < 32; i++) { 2115 SROM_EMIT(sc, miirom); 2116 SROM_EMIT(sc, miirom|MIIROM_SROMSK); 2117 } 2118 2119 SROM_EMIT(sc, miirom); 2120 2121 miirom &= ~MIIROM_SROMCS; 2122 SROM_EMIT(sc, miirom); 2123 2124 SROM_EMIT(sc, 0); 2125 } 2126 2127 /* 2128 * tlp_srom_size: 2129 * 2130 * Determine the number of address bits in the SROM. 2131 */ 2132 static int 2133 tlp_srom_size(struct tulip_softc *sc) 2134 { 2135 uint32_t miirom; 2136 int x; 2137 2138 /* Select the SROM. */ 2139 miirom = MIIROM_SR; 2140 SROM_EMIT(sc, miirom); 2141 2142 miirom |= MIIROM_RD; 2143 SROM_EMIT(sc, miirom); 2144 2145 /* Send CHIP SELECT for one clock tick. */ 2146 miirom |= MIIROM_SROMCS; 2147 SROM_EMIT(sc, miirom); 2148 2149 /* Shift in the READ opcode. */ 2150 for (x = 3; x > 0; x--) { 2151 if (TULIP_SROM_OPC_READ & (1 << (x - 1))) 2152 miirom |= MIIROM_SROMDI; 2153 else 2154 miirom &= ~MIIROM_SROMDI; 2155 SROM_EMIT(sc, miirom); 2156 SROM_EMIT(sc, miirom|MIIROM_SROMSK); 2157 SROM_EMIT(sc, miirom); 2158 } 2159 2160 /* Shift in address and look for dummy 0 bit. */ 2161 for (x = 1; x <= 12; x++) { 2162 miirom &= ~MIIROM_SROMDI; 2163 SROM_EMIT(sc, miirom); 2164 SROM_EMIT(sc, miirom|MIIROM_SROMSK); 2165 if (!TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO)) 2166 break; 2167 SROM_EMIT(sc, miirom); 2168 } 2169 2170 /* Clear CHIP SELECT. */ 2171 miirom &= ~MIIROM_SROMCS; 2172 SROM_EMIT(sc, miirom); 2173 2174 /* Deselect the SROM. */ 2175 SROM_EMIT(sc, 0); 2176 2177 if (x < 4 || x > 12) { 2178 aprint_debug_dev(sc->sc_dev, "broken MicroWire interface detected; " 2179 "setting SROM size to 1Kb\n"); 2180 return (6); 2181 } else { 2182 if (tlp_srom_debug) 2183 printf("%s: SROM size is 2^%d*16 bits (%d bytes)\n", 2184 device_xname(sc->sc_dev), x, (1 << (x + 4)) >> 3); 2185 return (x); 2186 } 2187 } 2188 2189 /* 2190 * tlp_read_srom: 2191 * 2192 * Read the Tulip SROM. 2193 */ 2194 int 2195 tlp_read_srom(struct tulip_softc *sc) 2196 { 2197 int size; 2198 uint32_t miirom; 2199 uint16_t datain; 2200 int i, x; 2201 2202 tlp_srom_idle(sc); 2203 2204 sc->sc_srom_addrbits = tlp_srom_size(sc); 2205 if (sc->sc_srom_addrbits == 0) 2206 return (0); 2207 size = TULIP_ROM_SIZE(sc->sc_srom_addrbits); 2208 sc->sc_srom = malloc(size, M_DEVBUF, M_NOWAIT); 2209 2210 /* Select the SROM. */ 2211 miirom = MIIROM_SR; 2212 SROM_EMIT(sc, miirom); 2213 2214 miirom |= MIIROM_RD; 2215 SROM_EMIT(sc, miirom); 2216 2217 for (i = 0; i < size; i += 2) { 2218 /* Send CHIP SELECT for one clock tick. */ 2219 miirom |= MIIROM_SROMCS; 2220 SROM_EMIT(sc, miirom); 2221 2222 /* Shift in the READ opcode. */ 2223 for (x = 3; x > 0; x--) { 2224 if (TULIP_SROM_OPC_READ & (1 << (x - 1))) 2225 miirom |= MIIROM_SROMDI; 2226 else 2227 miirom &= ~MIIROM_SROMDI; 2228 SROM_EMIT(sc, miirom); 2229 SROM_EMIT(sc, miirom|MIIROM_SROMSK); 2230 SROM_EMIT(sc, miirom); 2231 } 2232 2233 /* Shift in address. */ 2234 for (x = sc->sc_srom_addrbits; x > 0; x--) { 2235 if (i & (1 << x)) 2236 miirom |= MIIROM_SROMDI; 2237 else 2238 miirom &= ~MIIROM_SROMDI; 2239 SROM_EMIT(sc, miirom); 2240 SROM_EMIT(sc, miirom|MIIROM_SROMSK); 2241 SROM_EMIT(sc, miirom); 2242 } 2243 2244 /* Shift out data. */ 2245 miirom &= ~MIIROM_SROMDI; 2246 datain = 0; 2247 for (x = 16; x > 0; x--) { 2248 SROM_EMIT(sc, miirom|MIIROM_SROMSK); 2249 if (TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO)) 2250 datain |= (1 << (x - 1)); 2251 SROM_EMIT(sc, miirom); 2252 } 2253 sc->sc_srom[i] = datain & 0xff; 2254 sc->sc_srom[i + 1] = datain >> 8; 2255 2256 /* Clear CHIP SELECT. */ 2257 miirom &= ~MIIROM_SROMCS; 2258 SROM_EMIT(sc, miirom); 2259 } 2260 2261 /* Deselect the SROM. */ 2262 SROM_EMIT(sc, 0); 2263 2264 /* ...and idle it. */ 2265 tlp_srom_idle(sc); 2266 2267 if (tlp_srom_debug) { 2268 printf("SROM CONTENTS:"); 2269 for (i = 0; i < size; i++) { 2270 if ((i % 8) == 0) 2271 printf("\n\t"); 2272 printf("0x%02x ", sc->sc_srom[i]); 2273 } 2274 printf("\n"); 2275 } 2276 2277 return (1); 2278 } 2279 2280 #undef SROM_EMIT 2281 2282 /* 2283 * tlp_add_rxbuf: 2284 * 2285 * Add a receive buffer to the indicated descriptor. 2286 */ 2287 static int 2288 tlp_add_rxbuf(struct tulip_softc *sc, int idx) 2289 { 2290 struct tulip_rxsoft *rxs = &sc->sc_rxsoft[idx]; 2291 struct mbuf *m; 2292 int error; 2293 2294 MGETHDR(m, M_DONTWAIT, MT_DATA); 2295 if (m == NULL) 2296 return (ENOBUFS); 2297 2298 MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner); 2299 MCLGET(m, M_DONTWAIT); 2300 if ((m->m_flags & M_EXT) == 0) { 2301 m_freem(m); 2302 return (ENOBUFS); 2303 } 2304 2305 if (rxs->rxs_mbuf != NULL) 2306 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap); 2307 2308 rxs->rxs_mbuf = m; 2309 2310 error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap, 2311 m->m_ext.ext_buf, m->m_ext.ext_size, NULL, 2312 BUS_DMA_READ|BUS_DMA_NOWAIT); 2313 if (error) { 2314 aprint_error_dev(sc->sc_dev, "can't load rx DMA map %d, error = %d\n", 2315 idx, error); 2316 panic("tlp_add_rxbuf"); /* XXX */ 2317 } 2318 2319 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0, 2320 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD); 2321 2322 TULIP_INIT_RXDESC(sc, idx); 2323 2324 return (0); 2325 } 2326 2327 /* 2328 * tlp_srom_crcok: 2329 * 2330 * Check the CRC of the Tulip SROM. 2331 */ 2332 int 2333 tlp_srom_crcok(const uint8_t *romdata) 2334 { 2335 uint32_t crc; 2336 2337 crc = ether_crc32_le(romdata, TULIP_ROM_CRC32_CHECKSUM); 2338 crc = (crc & 0xffff) ^ 0xffff; 2339 if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM)) 2340 return (1); 2341 2342 /* 2343 * Try an alternate checksum. 2344 */ 2345 crc = ether_crc32_le(romdata, TULIP_ROM_CRC32_CHECKSUM1); 2346 crc = (crc & 0xffff) ^ 0xffff; 2347 if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM1)) 2348 return (1); 2349 2350 return (0); 2351 } 2352 2353 /* 2354 * tlp_isv_srom: 2355 * 2356 * Check to see if the SROM is in the new standardized format. 2357 */ 2358 int 2359 tlp_isv_srom(const uint8_t *romdata) 2360 { 2361 int i; 2362 uint16_t cksum; 2363 2364 if (tlp_srom_crcok(romdata)) { 2365 /* 2366 * SROM CRC checks out; must be in the new format. 2367 */ 2368 return (1); 2369 } 2370 2371 cksum = TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM); 2372 if (cksum == 0xffff || cksum == 0) { 2373 /* 2374 * No checksum present. Check the SROM ID; 18 bytes of 0 2375 * followed by 1 (version) followed by the number of 2376 * adapters which use this SROM (should be non-zero). 2377 */ 2378 for (i = 0; i < TULIP_ROM_SROM_FORMAT_VERION; i++) { 2379 if (romdata[i] != 0) 2380 return (0); 2381 } 2382 if (romdata[TULIP_ROM_SROM_FORMAT_VERION] != 1) 2383 return (0); 2384 if (romdata[TULIP_ROM_CHIP_COUNT] == 0) 2385 return (0); 2386 return (1); 2387 } 2388 2389 return (0); 2390 } 2391 2392 /* 2393 * tlp_isv_srom_enaddr: 2394 * 2395 * Get the Ethernet address from an ISV SROM. 2396 */ 2397 int 2398 tlp_isv_srom_enaddr(struct tulip_softc *sc, uint8_t *enaddr) 2399 { 2400 int i, devcnt; 2401 2402 if (tlp_isv_srom(sc->sc_srom) == 0) 2403 return (0); 2404 2405 devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT]; 2406 for (i = 0; i < devcnt; i++) { 2407 if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1) 2408 break; 2409 if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] == 2410 sc->sc_devno) 2411 break; 2412 } 2413 2414 if (i == devcnt) 2415 return (0); 2416 2417 memcpy(enaddr, &sc->sc_srom[TULIP_ROM_IEEE_NETWORK_ADDRESS], 2418 ETHER_ADDR_LEN); 2419 enaddr[5] += i; 2420 2421 return (1); 2422 } 2423 2424 /* 2425 * tlp_parse_old_srom: 2426 * 2427 * Parse old-format SROMs. 2428 * 2429 * This routine is largely lifted from Matt Thomas's `de' driver. 2430 */ 2431 int 2432 tlp_parse_old_srom(struct tulip_softc *sc, uint8_t *enaddr) 2433 { 2434 static const uint8_t testpat[] = 2435 { 0xff, 0, 0x55, 0xaa, 0xff, 0, 0x55, 0xaa }; 2436 int i; 2437 uint32_t cksum; 2438 2439 if (memcmp(&sc->sc_srom[0], &sc->sc_srom[16], 8) != 0) { 2440 /* 2441 * Phobos G100 interfaces have the address at 2442 * offsets 0 and 20, but each pair of bytes is 2443 * swapped. 2444 */ 2445 if (sc->sc_srom_addrbits == 6 && 2446 sc->sc_srom[1] == 0x00 && 2447 sc->sc_srom[0] == 0x60 && 2448 sc->sc_srom[3] == 0xf5 && 2449 memcmp(&sc->sc_srom[0], &sc->sc_srom[20], 6) == 0) { 2450 for (i = 0; i < 6; i += 2) { 2451 enaddr[i] = sc->sc_srom[i + 1]; 2452 enaddr[i + 1] = sc->sc_srom[i]; 2453 } 2454 return (1); 2455 } 2456 2457 /* 2458 * Phobos G130/G160 interfaces have the address at 2459 * offsets 20 and 84, but each pair of bytes is 2460 * swapped. 2461 */ 2462 if (sc->sc_srom_addrbits == 6 && 2463 sc->sc_srom[21] == 0x00 && 2464 sc->sc_srom[20] == 0x60 && 2465 sc->sc_srom[23] == 0xf5 && 2466 memcmp(&sc->sc_srom[20], &sc->sc_srom[84], 6) == 0) { 2467 for (i = 0; i < 6; i += 2) { 2468 enaddr[i] = sc->sc_srom[20 + i + 1]; 2469 enaddr[i + 1] = sc->sc_srom[20 + i]; 2470 } 2471 return (1); 2472 } 2473 2474 /* 2475 * Cobalt Networks interfaces simply have the address 2476 * in the first six bytes. The rest is zeroed out 2477 * on some models, but others contain unknown data. 2478 */ 2479 if (sc->sc_srom[0] == 0x00 && 2480 sc->sc_srom[1] == 0x10 && 2481 sc->sc_srom[2] == 0xe0) { 2482 memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN); 2483 return (1); 2484 } 2485 2486 /* 2487 * Some vendors (e.g. ZNYX) don't use the standard 2488 * DEC Address ROM format, but rather just have an 2489 * Ethernet address in the first 6 bytes, maybe a 2490 * 2 byte checksum, and then all 0xff's. 2491 */ 2492 for (i = 8; i < 32; i++) { 2493 if (sc->sc_srom[i] != 0xff && 2494 sc->sc_srom[i] != 0) 2495 return (0); 2496 } 2497 2498 /* 2499 * Sanity check the Ethernet address: 2500 * 2501 * - Make sure it's not multicast or locally 2502 * assigned 2503 * - Make sure it has a non-0 OUI 2504 */ 2505 if (sc->sc_srom[0] & 3) 2506 return (0); 2507 if (sc->sc_srom[0] == 0 && sc->sc_srom[1] == 0 && 2508 sc->sc_srom[2] == 0) 2509 return (0); 2510 2511 memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN); 2512 return (1); 2513 } 2514 2515 /* 2516 * Standard DEC Address ROM test. 2517 */ 2518 2519 if (memcmp(&sc->sc_srom[24], testpat, 8) != 0) 2520 return (0); 2521 2522 for (i = 0; i < 8; i++) { 2523 if (sc->sc_srom[i] != sc->sc_srom[15 - i]) 2524 return (0); 2525 } 2526 2527 memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN); 2528 2529 cksum = *(uint16_t *) &enaddr[0]; 2530 2531 cksum <<= 1; 2532 if (cksum > 0xffff) 2533 cksum -= 0xffff; 2534 2535 cksum += *(uint16_t *) &enaddr[2]; 2536 if (cksum > 0xffff) 2537 cksum -= 0xffff; 2538 2539 cksum <<= 1; 2540 if (cksum > 0xffff) 2541 cksum -= 0xffff; 2542 2543 cksum += *(uint16_t *) &enaddr[4]; 2544 if (cksum >= 0xffff) 2545 cksum -= 0xffff; 2546 2547 if (cksum != *(uint16_t *) &sc->sc_srom[6]) 2548 return (0); 2549 2550 return (1); 2551 } 2552 2553 /* 2554 * tlp_filter_setup: 2555 * 2556 * Set the Tulip's receive filter. 2557 */ 2558 static void 2559 tlp_filter_setup(struct tulip_softc *sc) 2560 { 2561 struct ethercom *ec = &sc->sc_ethercom; 2562 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 2563 struct ether_multi *enm; 2564 struct ether_multistep step; 2565 volatile uint32_t *sp; 2566 struct tulip_txsoft *txs; 2567 struct tulip_desc *txd; 2568 uint8_t enaddr[ETHER_ADDR_LEN]; 2569 uint32_t hash, hashsize; 2570 int cnt, nexttx; 2571 2572 DPRINTF(sc, ("%s: tlp_filter_setup: sc_flags 0x%08x\n", 2573 device_xname(sc->sc_dev), sc->sc_flags)); 2574 2575 memcpy(enaddr, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN); 2576 2577 /* 2578 * If there are transmissions pending, wait until they have 2579 * completed. 2580 */ 2581 if (! SIMPLEQ_EMPTY(&sc->sc_txdirtyq) || 2582 (sc->sc_flags & TULIPF_DOING_SETUP) != 0) { 2583 sc->sc_flags |= TULIPF_WANT_SETUP; 2584 DPRINTF(sc, ("%s: tlp_filter_setup: deferring\n", 2585 device_xname(sc->sc_dev))); 2586 return; 2587 } 2588 sc->sc_flags &= ~TULIPF_WANT_SETUP; 2589 2590 switch (sc->sc_chip) { 2591 case TULIP_CHIP_82C115: 2592 hashsize = TULIP_PNICII_HASHSIZE; 2593 break; 2594 2595 default: 2596 hashsize = TULIP_MCHASHSIZE; 2597 } 2598 2599 /* 2600 * If we're running, idle the transmit and receive engines. If 2601 * we're NOT running, we're being called from tlp_init(), and our 2602 * writing OPMODE will start the transmit and receive processes 2603 * in motion. 2604 */ 2605 if (ifp->if_flags & IFF_RUNNING) 2606 tlp_idle(sc, OPMODE_ST|OPMODE_SR); 2607 2608 sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM); 2609 2610 if (ifp->if_flags & IFF_PROMISC) { 2611 sc->sc_opmode |= OPMODE_PR; 2612 goto allmulti; 2613 } 2614 2615 /* 2616 * Try Perfect filtering first. 2617 */ 2618 2619 sc->sc_filtmode = TDCTL_Tx_FT_PERFECT; 2620 sp = TULIP_CDSP(sc); 2621 memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN); 2622 cnt = 0; 2623 ETHER_FIRST_MULTI(step, ec, enm); 2624 while (enm != NULL) { 2625 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) { 2626 /* 2627 * We must listen to a range of multicast addresses. 2628 * For now, just accept all multicasts, rather than 2629 * trying to set only those filter bits needed to match 2630 * the range. (At this time, the only use of address 2631 * ranges is for IP multicast routing, for which the 2632 * range is big enough to require all bits set.) 2633 */ 2634 goto allmulti; 2635 } 2636 if (cnt == (TULIP_MAXADDRS - 2)) { 2637 /* 2638 * We already have our multicast limit (still need 2639 * our station address and broadcast). Go to 2640 * Hash-Perfect mode. 2641 */ 2642 goto hashperfect; 2643 } 2644 cnt++; 2645 *sp++ = htole32(TULIP_SP_FIELD(enm->enm_addrlo, 0)); 2646 *sp++ = htole32(TULIP_SP_FIELD(enm->enm_addrlo, 1)); 2647 *sp++ = htole32(TULIP_SP_FIELD(enm->enm_addrlo, 2)); 2648 ETHER_NEXT_MULTI(step, enm); 2649 } 2650 2651 if (ifp->if_flags & IFF_BROADCAST) { 2652 /* ...and the broadcast address. */ 2653 cnt++; 2654 *sp++ = htole32(TULIP_SP_FIELD_C(0xff, 0xff)); 2655 *sp++ = htole32(TULIP_SP_FIELD_C(0xff, 0xff)); 2656 *sp++ = htole32(TULIP_SP_FIELD_C(0xff, 0xff)); 2657 } 2658 2659 /* Pad the rest with our station address. */ 2660 for (; cnt < TULIP_MAXADDRS; cnt++) { 2661 *sp++ = htole32(TULIP_SP_FIELD(enaddr, 0)); 2662 *sp++ = htole32(TULIP_SP_FIELD(enaddr, 1)); 2663 *sp++ = htole32(TULIP_SP_FIELD(enaddr, 2)); 2664 } 2665 ifp->if_flags &= ~IFF_ALLMULTI; 2666 goto setit; 2667 2668 hashperfect: 2669 /* 2670 * Try Hash-Perfect mode. 2671 */ 2672 2673 /* 2674 * Some 21140 chips have broken Hash-Perfect modes. On these 2675 * chips, we simply use Hash-Only mode, and put our station 2676 * address into the filter. 2677 */ 2678 if (sc->sc_chip == TULIP_CHIP_21140) 2679 sc->sc_filtmode = TDCTL_Tx_FT_HASHONLY; 2680 else 2681 sc->sc_filtmode = TDCTL_Tx_FT_HASH; 2682 sp = TULIP_CDSP(sc); 2683 memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN); 2684 ETHER_FIRST_MULTI(step, ec, enm); 2685 while (enm != NULL) { 2686 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) { 2687 /* 2688 * We must listen to a range of multicast addresses. 2689 * For now, just accept all multicasts, rather than 2690 * trying to set only those filter bits needed to match 2691 * the range. (At this time, the only use of address 2692 * ranges is for IP multicast routing, for which the 2693 * range is big enough to require all bits set.) 2694 */ 2695 goto allmulti; 2696 } 2697 hash = tlp_mchash(enm->enm_addrlo, hashsize); 2698 sp[hash >> 4] |= htole32(1 << (hash & 0xf)); 2699 ETHER_NEXT_MULTI(step, enm); 2700 } 2701 2702 if (ifp->if_flags & IFF_BROADCAST) { 2703 /* ...and the broadcast address. */ 2704 hash = tlp_mchash(etherbroadcastaddr, hashsize); 2705 sp[hash >> 4] |= htole32(1 << (hash & 0xf)); 2706 } 2707 2708 if (sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) { 2709 /* ...and our station address. */ 2710 hash = tlp_mchash(enaddr, hashsize); 2711 sp[hash >> 4] |= htole32(1 << (hash & 0xf)); 2712 } else { 2713 /* 2714 * Hash-Perfect mode; put our station address after 2715 * the hash table. 2716 */ 2717 sp[39] = htole32(TULIP_SP_FIELD(enaddr, 0)); 2718 sp[40] = htole32(TULIP_SP_FIELD(enaddr, 1)); 2719 sp[41] = htole32(TULIP_SP_FIELD(enaddr, 2)); 2720 } 2721 ifp->if_flags &= ~IFF_ALLMULTI; 2722 goto setit; 2723 2724 allmulti: 2725 /* 2726 * Use Perfect filter mode. First address is the broadcast address, 2727 * and pad the rest with our station address. We'll set Pass-all- 2728 * multicast in OPMODE below. 2729 */ 2730 sc->sc_filtmode = TDCTL_Tx_FT_PERFECT; 2731 sp = TULIP_CDSP(sc); 2732 memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN); 2733 cnt = 0; 2734 if (ifp->if_flags & IFF_BROADCAST) { 2735 cnt++; 2736 *sp++ = htole32(TULIP_SP_FIELD_C(0xff, 0xff)); 2737 *sp++ = htole32(TULIP_SP_FIELD_C(0xff, 0xff)); 2738 *sp++ = htole32(TULIP_SP_FIELD_C(0xff, 0xff)); 2739 } 2740 for (; cnt < TULIP_MAXADDRS; cnt++) { 2741 *sp++ = htole32(TULIP_SP_FIELD(enaddr, 0)); 2742 *sp++ = htole32(TULIP_SP_FIELD(enaddr, 1)); 2743 *sp++ = htole32(TULIP_SP_FIELD(enaddr, 2)); 2744 } 2745 ifp->if_flags |= IFF_ALLMULTI; 2746 2747 setit: 2748 if (ifp->if_flags & IFF_ALLMULTI) 2749 sc->sc_opmode |= OPMODE_PM; 2750 2751 /* Sync the setup packet buffer. */ 2752 TULIP_CDSPSYNC(sc, BUS_DMASYNC_PREWRITE); 2753 2754 /* 2755 * Fill in the setup packet descriptor. 2756 */ 2757 txs = SIMPLEQ_FIRST(&sc->sc_txfreeq); 2758 2759 txs->txs_firstdesc = sc->sc_txnext; 2760 txs->txs_lastdesc = sc->sc_txnext; 2761 txs->txs_ndescs = 1; 2762 txs->txs_mbuf = NULL; 2763 2764 nexttx = sc->sc_txnext; 2765 txd = &sc->sc_txdescs[nexttx]; 2766 txd->td_status = 0; 2767 txd->td_bufaddr1 = htole32(TULIP_CDSPADDR(sc)); 2768 txd->td_ctl = htole32((TULIP_SETUP_PACKET_LEN << TDCTL_SIZE1_SHIFT) | 2769 sc->sc_filtmode | TDCTL_Tx_SET | sc->sc_setup_fsls | 2770 TDCTL_Tx_IC | sc->sc_tdctl_ch | 2771 (nexttx == (TULIP_NTXDESC - 1) ? sc->sc_tdctl_er : 0)); 2772 TULIP_CDTXSYNC(sc, nexttx, 1, 2773 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 2774 2775 #ifdef TLP_DEBUG 2776 if (ifp->if_flags & IFF_DEBUG) { 2777 printf(" filter_setup %p transmit chain:\n", txs); 2778 printf(" descriptor %d:\n", nexttx); 2779 printf(" td_status: 0x%08x\n", le32toh(txd->td_status)); 2780 printf(" td_ctl: 0x%08x\n", le32toh(txd->td_ctl)); 2781 printf(" td_bufaddr1: 0x%08x\n", 2782 le32toh(txd->td_bufaddr1)); 2783 printf(" td_bufaddr2: 0x%08x\n", 2784 le32toh(txd->td_bufaddr2)); 2785 } 2786 #endif 2787 2788 txd->td_status = htole32(TDSTAT_OWN); 2789 TULIP_CDTXSYNC(sc, nexttx, 1, 2790 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 2791 2792 /* Advance the tx pointer. */ 2793 sc->sc_txfree -= 1; 2794 sc->sc_txnext = TULIP_NEXTTX(nexttx); 2795 2796 SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q); 2797 SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q); 2798 2799 /* 2800 * Set the OPMODE register. This will also resume the 2801 * transmit process we idled above. 2802 */ 2803 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode); 2804 2805 sc->sc_flags |= TULIPF_DOING_SETUP; 2806 2807 /* 2808 * Kick the transmitter; this will cause the Tulip to 2809 * read the setup descriptor. 2810 */ 2811 /* XXX USE AUTOPOLLING? */ 2812 TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD); 2813 2814 /* Set up a watchdog timer in case the chip flakes out. */ 2815 ifp->if_timer = 5; 2816 2817 DPRINTF(sc, ("%s: tlp_filter_setup: returning\n", device_xname(sc->sc_dev))); 2818 } 2819 2820 /* 2821 * tlp_winb_filter_setup: 2822 * 2823 * Set the Winbond 89C840F's receive filter. 2824 */ 2825 static void 2826 tlp_winb_filter_setup(struct tulip_softc *sc) 2827 { 2828 struct ethercom *ec = &sc->sc_ethercom; 2829 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 2830 struct ether_multi *enm; 2831 struct ether_multistep step; 2832 uint32_t hash, mchash[2]; 2833 2834 DPRINTF(sc, ("%s: tlp_winb_filter_setup: sc_flags 0x%08x\n", 2835 device_xname(sc->sc_dev), sc->sc_flags)); 2836 2837 sc->sc_opmode &= ~(OPMODE_WINB_APP|OPMODE_WINB_AMP|OPMODE_WINB_ABP); 2838 2839 if (ifp->if_flags & IFF_MULTICAST) 2840 sc->sc_opmode |= OPMODE_WINB_AMP; 2841 2842 if (ifp->if_flags & IFF_BROADCAST) 2843 sc->sc_opmode |= OPMODE_WINB_ABP; 2844 2845 if (ifp->if_flags & IFF_PROMISC) { 2846 sc->sc_opmode |= OPMODE_WINB_APP; 2847 goto allmulti; 2848 } 2849 2850 mchash[0] = mchash[1] = 0; 2851 2852 ETHER_FIRST_MULTI(step, ec, enm); 2853 while (enm != NULL) { 2854 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) { 2855 /* 2856 * We must listen to a range of multicast addresses. 2857 * For now, just accept all multicasts, rather than 2858 * trying to set only those filter bits needed to match 2859 * the range. (At this time, the only use of address 2860 * ranges is for IP multicast routing, for which the 2861 * range is big enough to require all bits set.) 2862 */ 2863 goto allmulti; 2864 } 2865 2866 /* 2867 * According to the FreeBSD `wb' driver, yes, you 2868 * really do invert the hash. 2869 */ 2870 hash = 2871 (~(ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26)) 2872 & 0x3f; 2873 mchash[hash >> 5] |= 1 << (hash & 0x1f); 2874 ETHER_NEXT_MULTI(step, enm); 2875 } 2876 ifp->if_flags &= ~IFF_ALLMULTI; 2877 goto setit; 2878 2879 allmulti: 2880 ifp->if_flags |= IFF_ALLMULTI; 2881 mchash[0] = mchash[1] = 0xffffffff; 2882 2883 setit: 2884 TULIP_WRITE(sc, CSR_WINB_CMA0, mchash[0]); 2885 TULIP_WRITE(sc, CSR_WINB_CMA1, mchash[1]); 2886 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode); 2887 DPRINTF(sc, ("%s: tlp_winb_filter_setup: returning\n", 2888 device_xname(sc->sc_dev))); 2889 } 2890 2891 /* 2892 * tlp_al981_filter_setup: 2893 * 2894 * Set the ADMtek AL981's receive filter. 2895 */ 2896 static void 2897 tlp_al981_filter_setup(struct tulip_softc *sc) 2898 { 2899 struct ethercom *ec = &sc->sc_ethercom; 2900 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 2901 struct ether_multi *enm; 2902 struct ether_multistep step; 2903 uint32_t hash, mchash[2]; 2904 2905 /* 2906 * If the chip is running, we need to reset the interface, 2907 * and will revisit here (with IFF_RUNNING) clear. The 2908 * chip seems to really not like to have its multicast 2909 * filter programmed without a reset. 2910 */ 2911 if (ifp->if_flags & IFF_RUNNING) { 2912 (void) tlp_init(ifp); 2913 return; 2914 } 2915 2916 DPRINTF(sc, ("%s: tlp_al981_filter_setup: sc_flags 0x%08x\n", 2917 device_xname(sc->sc_dev), sc->sc_flags)); 2918 2919 sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM); 2920 2921 if (ifp->if_flags & IFF_PROMISC) { 2922 sc->sc_opmode |= OPMODE_PR; 2923 goto allmulti; 2924 } 2925 2926 mchash[0] = mchash[1] = 0; 2927 2928 ETHER_FIRST_MULTI(step, ec, enm); 2929 while (enm != NULL) { 2930 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) { 2931 /* 2932 * We must listen to a range of multicast addresses. 2933 * For now, just accept all multicasts, rather than 2934 * trying to set only those filter bits needed to match 2935 * the range. (At this time, the only use of address 2936 * ranges is for IP multicast routing, for which the 2937 * range is big enough to require all bits set.) 2938 */ 2939 goto allmulti; 2940 } 2941 2942 hash = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN) & 0x3f; 2943 mchash[hash >> 5] |= 1 << (hash & 0x1f); 2944 ETHER_NEXT_MULTI(step, enm); 2945 } 2946 ifp->if_flags &= ~IFF_ALLMULTI; 2947 goto setit; 2948 2949 allmulti: 2950 ifp->if_flags |= IFF_ALLMULTI; 2951 mchash[0] = mchash[1] = 0xffffffff; 2952 2953 setit: 2954 bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_MAR0, mchash[0]); 2955 bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_MAR1, mchash[1]); 2956 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode); 2957 DPRINTF(sc, ("%s: tlp_al981_filter_setup: returning\n", 2958 device_xname(sc->sc_dev))); 2959 } 2960 2961 /* 2962 * tlp_asix_filter_setup: 2963 * 2964 * Set the ASIX AX8814x recieve filter. 2965 */ 2966 static void 2967 tlp_asix_filter_setup(struct tulip_softc *sc) 2968 { 2969 struct ethercom *ec = &sc->sc_ethercom; 2970 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 2971 struct ether_multi *enm; 2972 struct ether_multistep step; 2973 uint32_t hash, mchash[2]; 2974 2975 DPRINTF(sc, ("%s: tlp_asix_filter_setup: sc_flags 0x%08x\n", 2976 device_xname(sc->sc_dev), sc->sc_flags)); 2977 2978 sc->sc_opmode &= ~(OPMODE_PM|OPMODE_AX_RB|OPMODE_PR); 2979 2980 if (ifp->if_flags & IFF_MULTICAST) 2981 sc->sc_opmode |= OPMODE_PM; 2982 2983 if (ifp->if_flags & IFF_BROADCAST) 2984 sc->sc_opmode |= OPMODE_AX_RB; 2985 2986 if (ifp->if_flags & IFF_PROMISC) { 2987 sc->sc_opmode |= OPMODE_PR; 2988 goto allmulti; 2989 } 2990 2991 mchash[0] = mchash[1] = 0; 2992 2993 ETHER_FIRST_MULTI(step, ec, enm); 2994 while (enm != NULL) { 2995 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) { 2996 /* 2997 * We must listen to a range of multicast addresses. 2998 * For now, just accept all multicasts, rather than 2999 * trying to set only those filter bits needed to match 3000 * the range. (At this time, the only use of address 3001 * ranges is for IP multicast routing, for which the 3002 * range is big enough to require all bits set.) 3003 */ 3004 goto allmulti; 3005 } 3006 hash = (ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26) 3007 & 0x3f; 3008 if (hash < 32) 3009 mchash[0] |= (1 << hash); 3010 else 3011 mchash[1] |= (1 << (hash - 32)); 3012 ETHER_NEXT_MULTI(step, enm); 3013 } 3014 ifp->if_flags &= ~IFF_ALLMULTI; 3015 goto setit; 3016 3017 allmulti: 3018 ifp->if_flags |= IFF_ALLMULTI; 3019 mchash[0] = mchash[1] = 0xffffffff; 3020 3021 setit: 3022 TULIP_WRITE(sc, CSR_AX_FILTIDX, AX_FILTIDX_MAR0); 3023 TULIP_WRITE(sc, CSR_AX_FILTDATA, mchash[0]); 3024 TULIP_WRITE(sc, CSR_AX_FILTIDX, AX_FILTIDX_MAR1); 3025 TULIP_WRITE(sc, CSR_AX_FILTDATA, mchash[1]); 3026 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode); 3027 DPRINTF(sc, ("%s: tlp_asix_filter_setup: returning\n", 3028 device_xname(sc->sc_dev))); 3029 } 3030 3031 3032 /* 3033 * tlp_idle: 3034 * 3035 * Cause the transmit and/or receive processes to go idle. 3036 */ 3037 void 3038 tlp_idle(struct tulip_softc *sc, uint32_t bits) 3039 { 3040 static const char * const tlp_tx_state_names[] = { 3041 "STOPPED", 3042 "RUNNING - FETCH", 3043 "RUNNING - WAIT", 3044 "RUNNING - READING", 3045 "-- RESERVED --", 3046 "RUNNING - SETUP", 3047 "SUSPENDED", 3048 "RUNNING - CLOSE", 3049 }; 3050 static const char * const tlp_rx_state_names[] = { 3051 "STOPPED", 3052 "RUNNING - FETCH", 3053 "RUNNING - CHECK", 3054 "RUNNING - WAIT", 3055 "SUSPENDED", 3056 "RUNNING - CLOSE", 3057 "RUNNING - FLUSH", 3058 "RUNNING - QUEUE", 3059 }; 3060 static const char * const dm9102_tx_state_names[] = { 3061 "STOPPED", 3062 "RUNNING - FETCH", 3063 "RUNNING - SETUP", 3064 "RUNNING - READING", 3065 "RUNNING - CLOSE - CLEAR OWNER", 3066 "RUNNING - WAIT", 3067 "RUNNING - CLOSE - WRITE STATUS", 3068 "SUSPENDED", 3069 }; 3070 static const char * const dm9102_rx_state_names[] = { 3071 "STOPPED", 3072 "RUNNING - FETCH", 3073 "RUNNING - WAIT", 3074 "RUNNING - QUEUE", 3075 "RUNNING - CLOSE - CLEAR OWNER", 3076 "RUNNING - CLOSE - WRITE STATUS", 3077 "SUSPENDED", 3078 "RUNNING - FLUSH", 3079 }; 3080 3081 const char * const *tx_state_names, * const *rx_state_names; 3082 uint32_t csr, ackmask = 0; 3083 int i; 3084 3085 switch (sc->sc_chip) { 3086 case TULIP_CHIP_DM9102: 3087 case TULIP_CHIP_DM9102A: 3088 tx_state_names = dm9102_tx_state_names; 3089 rx_state_names = dm9102_rx_state_names; 3090 break; 3091 3092 default: 3093 tx_state_names = tlp_tx_state_names; 3094 rx_state_names = tlp_rx_state_names; 3095 break; 3096 } 3097 3098 if (bits & OPMODE_ST) 3099 ackmask |= STATUS_TPS; 3100 3101 if (bits & OPMODE_SR) 3102 ackmask |= STATUS_RPS; 3103 3104 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode & ~bits); 3105 3106 for (i = 0; i < 1000; i++) { 3107 if (TULIP_ISSET(sc, CSR_STATUS, ackmask) == ackmask) 3108 break; 3109 delay(10); 3110 } 3111 3112 csr = TULIP_READ(sc, CSR_STATUS); 3113 if ((csr & ackmask) != ackmask) { 3114 if ((bits & OPMODE_ST) != 0 && (csr & STATUS_TPS) == 0 && 3115 (csr & STATUS_TS) != STATUS_TS_STOPPED) { 3116 switch (sc->sc_chip) { 3117 case TULIP_CHIP_AX88140: 3118 case TULIP_CHIP_AX88141: 3119 /* 3120 * Filter the message out on noisy chips. 3121 */ 3122 break; 3123 default: 3124 printf("%s: transmit process failed to idle: " 3125 "state %s\n", device_xname(sc->sc_dev), 3126 tx_state_names[(csr & STATUS_TS) >> 20]); 3127 } 3128 } 3129 if ((bits & OPMODE_SR) != 0 && (csr & STATUS_RPS) == 0 && 3130 (csr & STATUS_RS) != STATUS_RS_STOPPED) { 3131 switch (sc->sc_chip) { 3132 case TULIP_CHIP_AN983: 3133 case TULIP_CHIP_AN985: 3134 case TULIP_CHIP_DM9102A: 3135 case TULIP_CHIP_RS7112: 3136 /* 3137 * Filter the message out on noisy chips. 3138 */ 3139 break; 3140 default: 3141 printf("%s: receive process failed to idle: " 3142 "state %s\n", device_xname(sc->sc_dev), 3143 rx_state_names[(csr & STATUS_RS) >> 17]); 3144 } 3145 } 3146 } 3147 TULIP_WRITE(sc, CSR_STATUS, ackmask); 3148 } 3149 3150 /***************************************************************************** 3151 * Generic media support functions. 3152 *****************************************************************************/ 3153 3154 /* 3155 * tlp_mediastatus: [ifmedia interface function] 3156 * 3157 * Query the current media. 3158 */ 3159 void 3160 tlp_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 3161 { 3162 struct tulip_softc *sc = ifp->if_softc; 3163 3164 if (TULIP_IS_ENABLED(sc) == 0) { 3165 ifmr->ifm_active = IFM_ETHER | IFM_NONE; 3166 ifmr->ifm_status = 0; 3167 return; 3168 } 3169 3170 (*sc->sc_mediasw->tmsw_get)(sc, ifmr); 3171 } 3172 3173 /* 3174 * tlp_mediachange: [ifmedia interface function] 3175 * 3176 * Update the current media. 3177 */ 3178 int 3179 tlp_mediachange(struct ifnet *ifp) 3180 { 3181 struct tulip_softc *sc = ifp->if_softc; 3182 3183 if ((ifp->if_flags & IFF_UP) == 0) 3184 return (0); 3185 return ((*sc->sc_mediasw->tmsw_set)(sc)); 3186 } 3187 3188 /***************************************************************************** 3189 * Support functions for MII-attached media. 3190 *****************************************************************************/ 3191 3192 /* 3193 * tlp_mii_tick: 3194 * 3195 * One second timer, used to tick the MII. 3196 */ 3197 static void 3198 tlp_mii_tick(void *arg) 3199 { 3200 struct tulip_softc *sc = arg; 3201 int s; 3202 3203 if (!device_is_active(sc->sc_dev)) 3204 return; 3205 3206 s = splnet(); 3207 mii_tick(&sc->sc_mii); 3208 splx(s); 3209 3210 callout_reset(&sc->sc_tick_callout, hz, sc->sc_tick, sc); 3211 } 3212 3213 /* 3214 * tlp_mii_statchg: [mii interface function] 3215 * 3216 * Callback from PHY when media changes. 3217 */ 3218 static void 3219 tlp_mii_statchg(device_t self) 3220 { 3221 struct tulip_softc *sc = device_private(self); 3222 3223 /* Idle the transmit and receive processes. */ 3224 tlp_idle(sc, OPMODE_ST|OPMODE_SR); 3225 3226 sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_HBD); 3227 3228 if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T) 3229 sc->sc_opmode |= OPMODE_TTM; 3230 else 3231 sc->sc_opmode |= OPMODE_HBD; 3232 3233 if (sc->sc_mii.mii_media_active & IFM_FDX) 3234 sc->sc_opmode |= OPMODE_FD|OPMODE_HBD; 3235 3236 /* 3237 * Write new OPMODE bits. This also restarts the transmit 3238 * and receive processes. 3239 */ 3240 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode); 3241 } 3242 3243 /* 3244 * tlp_winb_mii_statchg: [mii interface function] 3245 * 3246 * Callback from PHY when media changes. This version is 3247 * for the Winbond 89C840F, which has different OPMODE bits. 3248 */ 3249 static void 3250 tlp_winb_mii_statchg(device_t self) 3251 { 3252 struct tulip_softc *sc = device_private(self); 3253 3254 /* Idle the transmit and receive processes. */ 3255 tlp_idle(sc, OPMODE_ST|OPMODE_SR); 3256 3257 sc->sc_opmode &= ~(OPMODE_WINB_FES|OPMODE_FD); 3258 3259 if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_100_TX) 3260 sc->sc_opmode |= OPMODE_WINB_FES; 3261 3262 if (sc->sc_mii.mii_media_active & IFM_FDX) 3263 sc->sc_opmode |= OPMODE_FD; 3264 3265 /* 3266 * Write new OPMODE bits. This also restarts the transmit 3267 * and receive processes. 3268 */ 3269 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode); 3270 } 3271 3272 /* 3273 * tlp_dm9102_mii_statchg: [mii interface function] 3274 * 3275 * Callback from PHY when media changes. This version is 3276 * for the DM9102. 3277 */ 3278 static void 3279 tlp_dm9102_mii_statchg(device_t self) 3280 { 3281 struct tulip_softc *sc = device_private(self); 3282 3283 /* 3284 * Don't idle the transmit and receive processes, here. It 3285 * seems to fail, and just causes excess noise. 3286 */ 3287 sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD); 3288 3289 if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) != IFM_100_TX) 3290 sc->sc_opmode |= OPMODE_TTM; 3291 3292 if (sc->sc_mii.mii_media_active & IFM_FDX) 3293 sc->sc_opmode |= OPMODE_FD; 3294 3295 /* 3296 * Write new OPMODE bits. 3297 */ 3298 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode); 3299 } 3300 3301 /* 3302 * tlp_mii_getmedia: 3303 * 3304 * Callback from ifmedia to request current media status. 3305 */ 3306 static void 3307 tlp_mii_getmedia(struct tulip_softc *sc, struct ifmediareq *ifmr) 3308 { 3309 3310 mii_pollstat(&sc->sc_mii); 3311 ifmr->ifm_status = sc->sc_mii.mii_media_status; 3312 ifmr->ifm_active = sc->sc_mii.mii_media_active; 3313 } 3314 3315 /* 3316 * tlp_mii_setmedia: 3317 * 3318 * Callback from ifmedia to request new media setting. 3319 */ 3320 static int 3321 tlp_mii_setmedia(struct tulip_softc *sc) 3322 { 3323 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 3324 int rc; 3325 3326 if ((ifp->if_flags & IFF_UP) == 0) 3327 return 0; 3328 switch (sc->sc_chip) { 3329 case TULIP_CHIP_21142: 3330 case TULIP_CHIP_21143: 3331 /* Disable the internal Nway engine. */ 3332 TULIP_WRITE(sc, CSR_SIATXRX, 0); 3333 break; 3334 3335 default: 3336 /* Nothing. */ 3337 break; 3338 } 3339 if ((rc = mii_mediachg(&sc->sc_mii)) == ENXIO) 3340 return 0; 3341 return rc; 3342 } 3343 3344 /* 3345 * tlp_bitbang_mii_readreg: 3346 * 3347 * Read a PHY register via bit-bang'ing the MII. 3348 */ 3349 static int 3350 tlp_bitbang_mii_readreg(device_t self, int phy, int reg) 3351 { 3352 struct tulip_softc *sc = device_private(self); 3353 3354 return (mii_bitbang_readreg(self, sc->sc_bitbang_ops, phy, reg)); 3355 } 3356 3357 /* 3358 * tlp_bitbang_mii_writereg: 3359 * 3360 * Write a PHY register via bit-bang'ing the MII. 3361 */ 3362 static void 3363 tlp_bitbang_mii_writereg(device_t self, int phy, int reg, int val) 3364 { 3365 struct tulip_softc *sc = device_private(self); 3366 3367 mii_bitbang_writereg(self, sc->sc_bitbang_ops, phy, reg, val); 3368 } 3369 3370 /* 3371 * tlp_sio_mii_bitbang_read: 3372 * 3373 * Read the MII serial port for the MII bit-bang module. 3374 */ 3375 static uint32_t 3376 tlp_sio_mii_bitbang_read(device_t self) 3377 { 3378 struct tulip_softc *sc = device_private(self); 3379 3380 return (TULIP_READ(sc, CSR_MIIROM)); 3381 } 3382 3383 /* 3384 * tlp_sio_mii_bitbang_write: 3385 * 3386 * Write the MII serial port for the MII bit-bang module. 3387 */ 3388 static void 3389 tlp_sio_mii_bitbang_write(device_t self, uint32_t val) 3390 { 3391 struct tulip_softc *sc = device_private(self); 3392 3393 TULIP_WRITE(sc, CSR_MIIROM, val); 3394 } 3395 3396 /* 3397 * tlp_pnic_mii_readreg: 3398 * 3399 * Read a PHY register on the Lite-On PNIC. 3400 */ 3401 static int 3402 tlp_pnic_mii_readreg(device_t self, int phy, int reg) 3403 { 3404 struct tulip_softc *sc = device_private(self); 3405 uint32_t val; 3406 int i; 3407 3408 TULIP_WRITE(sc, CSR_PNIC_MII, 3409 PNIC_MII_MBO | PNIC_MII_RESERVED | 3410 PNIC_MII_READ | (phy << PNIC_MII_PHYSHIFT) | 3411 (reg << PNIC_MII_REGSHIFT)); 3412 3413 for (i = 0; i < 1000; i++) { 3414 delay(10); 3415 val = TULIP_READ(sc, CSR_PNIC_MII); 3416 if ((val & PNIC_MII_BUSY) == 0) { 3417 if ((val & PNIC_MII_DATA) == PNIC_MII_DATA) 3418 return (0); 3419 else 3420 return (val & PNIC_MII_DATA); 3421 } 3422 } 3423 printf("%s: MII read timed out\n", device_xname(sc->sc_dev)); 3424 return (0); 3425 } 3426 3427 /* 3428 * tlp_pnic_mii_writereg: 3429 * 3430 * Write a PHY register on the Lite-On PNIC. 3431 */ 3432 static void 3433 tlp_pnic_mii_writereg(device_t self, int phy, int reg, int val) 3434 { 3435 struct tulip_softc *sc = device_private(self); 3436 int i; 3437 3438 TULIP_WRITE(sc, CSR_PNIC_MII, 3439 PNIC_MII_MBO | PNIC_MII_RESERVED | 3440 PNIC_MII_WRITE | (phy << PNIC_MII_PHYSHIFT) | 3441 (reg << PNIC_MII_REGSHIFT) | val); 3442 3443 for (i = 0; i < 1000; i++) { 3444 delay(10); 3445 if (TULIP_ISSET(sc, CSR_PNIC_MII, PNIC_MII_BUSY) == 0) 3446 return; 3447 } 3448 printf("%s: MII write timed out\n", device_xname(sc->sc_dev)); 3449 } 3450 3451 static const bus_addr_t tlp_al981_phy_regmap[] = { 3452 CSR_ADM_BMCR, 3453 CSR_ADM_BMSR, 3454 CSR_ADM_PHYIDR1, 3455 CSR_ADM_PHYIDR2, 3456 CSR_ADM_ANAR, 3457 CSR_ADM_ANLPAR, 3458 CSR_ADM_ANER, 3459 3460 CSR_ADM_XMC, 3461 CSR_ADM_XCIIS, 3462 CSR_ADM_XIE, 3463 CSR_ADM_100CTR, 3464 }; 3465 static const int tlp_al981_phy_regmap_size = sizeof(tlp_al981_phy_regmap) / 3466 sizeof(tlp_al981_phy_regmap[0]); 3467 3468 /* 3469 * tlp_al981_mii_readreg: 3470 * 3471 * Read a PHY register on the ADMtek AL981. 3472 */ 3473 static int 3474 tlp_al981_mii_readreg(device_t self, int phy, int reg) 3475 { 3476 struct tulip_softc *sc = device_private(self); 3477 3478 /* AL981 only has an internal PHY. */ 3479 if (phy != 0) 3480 return (0); 3481 3482 if (reg >= tlp_al981_phy_regmap_size) 3483 return (0); 3484 3485 return (bus_space_read_4(sc->sc_st, sc->sc_sh, 3486 tlp_al981_phy_regmap[reg]) & 0xffff); 3487 } 3488 3489 /* 3490 * tlp_al981_mii_writereg: 3491 * 3492 * Write a PHY register on the ADMtek AL981. 3493 */ 3494 static void 3495 tlp_al981_mii_writereg(device_t self, int phy, int reg, int val) 3496 { 3497 struct tulip_softc *sc = device_private(self); 3498 3499 /* AL981 only has an internal PHY. */ 3500 if (phy != 0) 3501 return; 3502 3503 if (reg >= tlp_al981_phy_regmap_size) 3504 return; 3505 3506 bus_space_write_4(sc->sc_st, sc->sc_sh, 3507 tlp_al981_phy_regmap[reg], val); 3508 } 3509 3510 /***************************************************************************** 3511 * Chip-specific pre-init and reset functions. 3512 *****************************************************************************/ 3513 3514 /* 3515 * tlp_2114x_preinit: 3516 * 3517 * Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143. 3518 */ 3519 static void 3520 tlp_2114x_preinit(struct tulip_softc *sc) 3521 { 3522 struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur; 3523 struct tulip_21x4x_media *tm = ife->ifm_aux; 3524 3525 /* 3526 * Whether or not we're in MII or SIA/SYM mode, the media info 3527 * contains the appropriate OPMODE bits. 3528 * 3529 * Also, we always set the Must-Be-One bit. 3530 */ 3531 sc->sc_opmode |= OPMODE_MBO | tm->tm_opmode; 3532 3533 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode); 3534 } 3535 3536 /* 3537 * tlp_2114x_mii_preinit: 3538 * 3539 * Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143. 3540 * This version is used by boards which only have MII and don't have 3541 * an ISV SROM. 3542 */ 3543 static void 3544 tlp_2114x_mii_preinit(struct tulip_softc *sc) 3545 { 3546 3547 /* 3548 * Always set the Must-Be-One bit, and Port Select (to select MII). 3549 * We'll never be called during a media change. 3550 */ 3551 sc->sc_opmode |= OPMODE_MBO|OPMODE_PS; 3552 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode); 3553 } 3554 3555 /* 3556 * tlp_pnic_preinit: 3557 * 3558 * Pre-init function for the Lite-On 82c168 and 82c169. 3559 */ 3560 static void 3561 tlp_pnic_preinit(struct tulip_softc *sc) 3562 { 3563 3564 if (sc->sc_flags & TULIPF_HAS_MII) { 3565 /* 3566 * MII case: just set the port-select bit; we will never 3567 * be called during a media change. 3568 */ 3569 sc->sc_opmode |= OPMODE_PS; 3570 } else { 3571 /* 3572 * ENDEC/PCS/Nway mode; enable the Tx backoff counter. 3573 */ 3574 sc->sc_opmode |= OPMODE_PNIC_TBEN; 3575 } 3576 } 3577 3578 /* 3579 * tlp_asix_preinit: 3580 * 3581 * Pre-init function for the ASIX chipsets. 3582 */ 3583 static void 3584 tlp_asix_preinit(struct tulip_softc *sc) 3585 { 3586 3587 switch (sc->sc_chip) { 3588 case TULIP_CHIP_AX88140: 3589 case TULIP_CHIP_AX88141: 3590 /* XXX Handle PHY. */ 3591 sc->sc_opmode |= OPMODE_HBD|OPMODE_PS; 3592 break; 3593 default: 3594 /* Nothing */ 3595 break; 3596 } 3597 3598 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode); 3599 } 3600 3601 /* 3602 * tlp_dm9102_preinit: 3603 * 3604 * Pre-init function for the Davicom DM9102. 3605 */ 3606 static void 3607 tlp_dm9102_preinit(struct tulip_softc *sc) 3608 { 3609 3610 switch (sc->sc_chip) { 3611 case TULIP_CHIP_DM9102: 3612 sc->sc_opmode |= OPMODE_MBO|OPMODE_HBD|OPMODE_PS; 3613 break; 3614 3615 case TULIP_CHIP_DM9102A: 3616 /* 3617 * XXX Figure out how to actually deal with the HomePNA 3618 * XXX portion of the DM9102A. 3619 */ 3620 sc->sc_opmode |= OPMODE_MBO|OPMODE_HBD; 3621 break; 3622 3623 default: 3624 /* Nothing. */ 3625 break; 3626 } 3627 3628 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode); 3629 } 3630 3631 /* 3632 * tlp_21140_reset: 3633 * 3634 * Issue a reset sequence on the 21140 via the GPIO facility. 3635 */ 3636 static void 3637 tlp_21140_reset(struct tulip_softc *sc) 3638 { 3639 struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur; 3640 struct tulip_21x4x_media *tm = ife->ifm_aux; 3641 int i; 3642 3643 /* First, set the direction on the GPIO pins. */ 3644 TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir); 3645 3646 /* Now, issue the reset sequence. */ 3647 for (i = 0; i < tm->tm_reset_length; i++) { 3648 delay(10); 3649 TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_reset_offset + i]); 3650 } 3651 3652 /* Now, issue the selection sequence. */ 3653 for (i = 0; i < tm->tm_gp_length; i++) { 3654 delay(10); 3655 TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_gp_offset + i]); 3656 } 3657 3658 /* If there were no sequences, just lower the pins. */ 3659 if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) { 3660 delay(10); 3661 TULIP_WRITE(sc, CSR_GPP, 0); 3662 } 3663 } 3664 3665 /* 3666 * tlp_21142_reset: 3667 * 3668 * Issue a reset sequence on the 21142 via the GPIO facility. 3669 */ 3670 static void 3671 tlp_21142_reset(struct tulip_softc *sc) 3672 { 3673 struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur; 3674 struct tulip_21x4x_media *tm = ife->ifm_aux; 3675 const uint8_t *cp; 3676 int i; 3677 3678 cp = &sc->sc_srom[tm->tm_reset_offset]; 3679 for (i = 0; i < tm->tm_reset_length; i++, cp += 2) { 3680 delay(10); 3681 TULIP_WRITE(sc, CSR_SIAGEN, TULIP_ROM_GETW(cp, 0) << 16); 3682 } 3683 3684 cp = &sc->sc_srom[tm->tm_gp_offset]; 3685 for (i = 0; i < tm->tm_gp_length; i++, cp += 2) { 3686 delay(10); 3687 TULIP_WRITE(sc, CSR_SIAGEN, TULIP_ROM_GETW(cp, 0) << 16); 3688 } 3689 3690 /* If there were no sequences, just lower the pins. */ 3691 if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) { 3692 delay(10); 3693 TULIP_WRITE(sc, CSR_SIAGEN, 0); 3694 } 3695 } 3696 3697 /* 3698 * tlp_pmac_reset: 3699 * 3700 * Reset routine for Macronix chips. 3701 */ 3702 static void 3703 tlp_pmac_reset(struct tulip_softc *sc) 3704 { 3705 3706 switch (sc->sc_chip) { 3707 case TULIP_CHIP_82C115: 3708 case TULIP_CHIP_MX98715: 3709 case TULIP_CHIP_MX98715A: 3710 case TULIP_CHIP_MX98725: 3711 /* 3712 * Set the LED operating mode. This information is located 3713 * in the EEPROM at byte offset 0x77, per the MX98715A and 3714 * MX98725 application notes. 3715 */ 3716 TULIP_WRITE(sc, CSR_MIIROM, sc->sc_srom[0x77] << 24); 3717 break; 3718 case TULIP_CHIP_MX98715AEC_X: 3719 /* 3720 * Set the LED operating mode. This information is located 3721 * in the EEPROM at byte offset 0x76, per the MX98715AEC 3722 * application note. 3723 */ 3724 TULIP_WRITE(sc, CSR_MIIROM, ((0xf & sc->sc_srom[0x76]) << 28) 3725 | ((0xf0 & sc->sc_srom[0x76]) << 20)); 3726 break; 3727 3728 default: 3729 /* Nothing. */ 3730 break; 3731 } 3732 } 3733 3734 #if 0 3735 /* 3736 * tlp_dm9102_reset: 3737 * 3738 * Reset routine for the Davicom DM9102. 3739 */ 3740 static void 3741 tlp_dm9102_reset(struct tulip_softc *sc) 3742 { 3743 3744 TULIP_WRITE(sc, CSR_DM_PHYSTAT, DM_PHYSTAT_GEPC|DM_PHYSTAT_GPED); 3745 delay(100); 3746 TULIP_WRITE(sc, CSR_DM_PHYSTAT, 0); 3747 } 3748 #endif 3749 3750 /***************************************************************************** 3751 * Chip/board-specific media switches. The ones here are ones that 3752 * are potentially common to multiple front-ends. 3753 *****************************************************************************/ 3754 3755 /* 3756 * This table is a common place for all sorts of media information, 3757 * keyed off of the SROM media code for that media. 3758 * 3759 * Note that we explicitly configure the 21142/21143 to always advertise 3760 * NWay capabilities when using the UTP port. 3761 * XXX Actually, we don't yet. 3762 */ 3763 static const struct tulip_srom_to_ifmedia tulip_srom_to_ifmedia_table[] = { 3764 { TULIP_ROM_MB_MEDIA_TP, IFM_10_T, 0, 3765 "10baseT", 3766 OPMODE_TTM, 3767 BMSR_10THDX, 3768 { SIACONN_21040_10BASET, 3769 SIATXRX_21040_10BASET, 3770 SIAGEN_21040_10BASET }, 3771 3772 { SIACONN_21041_10BASET, 3773 SIATXRX_21041_10BASET, 3774 SIAGEN_21041_10BASET }, 3775 3776 { SIACONN_21142_10BASET, 3777 SIATXRX_21142_10BASET, 3778 SIAGEN_21142_10BASET } }, 3779 3780 { TULIP_ROM_MB_MEDIA_BNC, IFM_10_2, 0, 3781 "10base2", 3782 0, 3783 0, 3784 { 0, 3785 0, 3786 0 }, 3787 3788 { SIACONN_21041_BNC, 3789 SIATXRX_21041_BNC, 3790 SIAGEN_21041_BNC }, 3791 3792 { SIACONN_21142_BNC, 3793 SIATXRX_21142_BNC, 3794 SIAGEN_21142_BNC } }, 3795 3796 { TULIP_ROM_MB_MEDIA_AUI, IFM_10_5, 0, 3797 "10base5", 3798 0, 3799 0, 3800 { SIACONN_21040_AUI, 3801 SIATXRX_21040_AUI, 3802 SIAGEN_21040_AUI }, 3803 3804 { SIACONN_21041_AUI, 3805 SIATXRX_21041_AUI, 3806 SIAGEN_21041_AUI }, 3807 3808 { SIACONN_21142_AUI, 3809 SIATXRX_21142_AUI, 3810 SIAGEN_21142_AUI } }, 3811 3812 { TULIP_ROM_MB_MEDIA_100TX, IFM_100_TX, 0, 3813 "100baseTX", 3814 OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD, 3815 BMSR_100TXHDX, 3816 { 0, 3817 0, 3818 0 }, 3819 3820 { 0, 3821 0, 3822 0 }, 3823 3824 { 0, 3825 0, 3826 SIAGEN_ABM } }, 3827 3828 { TULIP_ROM_MB_MEDIA_TP_FDX, IFM_10_T, IFM_FDX, 3829 "10baseT-FDX", 3830 OPMODE_TTM|OPMODE_FD|OPMODE_HBD, 3831 BMSR_10TFDX, 3832 { SIACONN_21040_10BASET_FDX, 3833 SIATXRX_21040_10BASET_FDX, 3834 SIAGEN_21040_10BASET_FDX }, 3835 3836 { SIACONN_21041_10BASET_FDX, 3837 SIATXRX_21041_10BASET_FDX, 3838 SIAGEN_21041_10BASET_FDX }, 3839 3840 { SIACONN_21142_10BASET_FDX, 3841 SIATXRX_21142_10BASET_FDX, 3842 SIAGEN_21142_10BASET_FDX } }, 3843 3844 { TULIP_ROM_MB_MEDIA_100TX_FDX, IFM_100_TX, IFM_FDX, 3845 "100baseTX-FDX", 3846 OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_FD|OPMODE_HBD, 3847 BMSR_100TXFDX, 3848 { 0, 3849 0, 3850 0 }, 3851 3852 { 0, 3853 0, 3854 0 }, 3855 3856 { 0, 3857 0, 3858 SIAGEN_ABM } }, 3859 3860 { TULIP_ROM_MB_MEDIA_100T4, IFM_100_T4, 0, 3861 "100baseT4", 3862 OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD, 3863 BMSR_100T4, 3864 { 0, 3865 0, 3866 0 }, 3867 3868 { 0, 3869 0, 3870 0 }, 3871 3872 { 0, 3873 0, 3874 SIAGEN_ABM } }, 3875 3876 { TULIP_ROM_MB_MEDIA_100FX, IFM_100_FX, 0, 3877 "100baseFX", 3878 OPMODE_PS|OPMODE_PCS|OPMODE_HBD, 3879 0, 3880 { 0, 3881 0, 3882 0 }, 3883 3884 { 0, 3885 0, 3886 0 }, 3887 3888 { 0, 3889 0, 3890 SIAGEN_ABM } }, 3891 3892 { TULIP_ROM_MB_MEDIA_100FX_FDX, IFM_100_FX, IFM_FDX, 3893 "100baseFX-FDX", 3894 OPMODE_PS|OPMODE_PCS|OPMODE_FD|OPMODE_HBD, 3895 0, 3896 { 0, 3897 0, 3898 0 }, 3899 3900 { 0, 3901 0, 3902 0 }, 3903 3904 { 0, 3905 0, 3906 SIAGEN_ABM } }, 3907 3908 { 0, 0, 0, 3909 NULL, 3910 0, 3911 0, 3912 { 0, 3913 0, 3914 0 }, 3915 3916 { 0, 3917 0, 3918 0 }, 3919 3920 { 0, 3921 0, 3922 0 } }, 3923 }; 3924 3925 static const struct tulip_srom_to_ifmedia *tlp_srom_to_ifmedia(uint8_t); 3926 static void tlp_srom_media_info(struct tulip_softc *, 3927 const struct tulip_srom_to_ifmedia *, 3928 struct tulip_21x4x_media *); 3929 static void tlp_add_srom_media(struct tulip_softc *, int, 3930 void (*)(struct tulip_softc *, struct ifmediareq *), 3931 int (*)(struct tulip_softc *), const uint8_t *, int); 3932 static void tlp_print_media(struct tulip_softc *); 3933 static void tlp_nway_activate(struct tulip_softc *, int); 3934 static void tlp_get_minst(struct tulip_softc *); 3935 3936 static const struct tulip_srom_to_ifmedia * 3937 tlp_srom_to_ifmedia(uint8_t sm) 3938 { 3939 const struct tulip_srom_to_ifmedia *tsti; 3940 3941 for (tsti = tulip_srom_to_ifmedia_table; 3942 tsti->tsti_name != NULL; tsti++) { 3943 if (tsti->tsti_srom == sm) 3944 return (tsti); 3945 } 3946 3947 return (NULL); 3948 } 3949 3950 static void 3951 tlp_srom_media_info(struct tulip_softc *sc, 3952 const struct tulip_srom_to_ifmedia *tsti, struct tulip_21x4x_media *tm) 3953 { 3954 3955 tm->tm_name = tsti->tsti_name; 3956 tm->tm_opmode = tsti->tsti_opmode; 3957 3958 sc->sc_sia_cap |= tsti->tsti_sia_cap; 3959 3960 switch (sc->sc_chip) { 3961 case TULIP_CHIP_DE425: 3962 case TULIP_CHIP_21040: 3963 tm->tm_sia = tsti->tsti_21040; /* struct assignment */ 3964 break; 3965 3966 case TULIP_CHIP_21041: 3967 tm->tm_sia = tsti->tsti_21041; /* struct assignment */ 3968 break; 3969 3970 case TULIP_CHIP_21142: 3971 case TULIP_CHIP_21143: 3972 case TULIP_CHIP_82C115: 3973 case TULIP_CHIP_MX98715: 3974 case TULIP_CHIP_MX98715A: 3975 case TULIP_CHIP_MX98715AEC_X: 3976 case TULIP_CHIP_MX98725: 3977 tm->tm_sia = tsti->tsti_21142; /* struct assignment */ 3978 break; 3979 3980 default: 3981 /* Nothing. */ 3982 break; 3983 } 3984 } 3985 3986 static void 3987 tlp_add_srom_media(struct tulip_softc *sc, int type, 3988 void (*get)(struct tulip_softc *, struct ifmediareq *), 3989 int (*set)(struct tulip_softc *), const uint8_t *list, 3990 int cnt) 3991 { 3992 struct tulip_21x4x_media *tm; 3993 const struct tulip_srom_to_ifmedia *tsti; 3994 int i; 3995 3996 for (i = 0; i < cnt; i++) { 3997 tsti = tlp_srom_to_ifmedia(list[i]); 3998 tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO); 3999 tlp_srom_media_info(sc, tsti, tm); 4000 tm->tm_type = type; 4001 tm->tm_get = get; 4002 tm->tm_set = set; 4003 4004 ifmedia_add(&sc->sc_mii.mii_media, 4005 IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype, 4006 tsti->tsti_options, sc->sc_tlp_minst), 0, tm); 4007 } 4008 } 4009 4010 static void 4011 tlp_print_media(struct tulip_softc *sc) 4012 { 4013 struct ifmedia_entry *ife; 4014 struct tulip_21x4x_media *tm; 4015 const char *sep = ""; 4016 4017 #define PRINT(str) aprint_normal("%s%s", sep, str); sep = ", " 4018 4019 aprint_normal_dev(sc->sc_dev, ""); 4020 TAILQ_FOREACH(ife, &sc->sc_mii.mii_media.ifm_list, ifm_list) { 4021 tm = ife->ifm_aux; 4022 if (tm == NULL) { 4023 #ifdef DIAGNOSTIC 4024 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 4025 panic("tlp_print_media"); 4026 #endif 4027 PRINT("auto"); 4028 } else if (tm->tm_type != TULIP_ROM_MB_21140_MII && 4029 tm->tm_type != TULIP_ROM_MB_21142_MII) { 4030 PRINT(tm->tm_name); 4031 } 4032 } 4033 aprint_normal("\n"); 4034 4035 #undef PRINT 4036 } 4037 4038 static void 4039 tlp_nway_activate(struct tulip_softc *sc, int media) 4040 { 4041 struct ifmedia_entry *ife; 4042 4043 ife = ifmedia_match(&sc->sc_mii.mii_media, media, 0); 4044 #ifdef DIAGNOSTIC 4045 if (ife == NULL) 4046 panic("tlp_nway_activate"); 4047 #endif 4048 sc->sc_nway_active = ife; 4049 } 4050 4051 static void 4052 tlp_get_minst(struct tulip_softc *sc) 4053 { 4054 4055 if ((sc->sc_media_seen & 4056 ~((1 << TULIP_ROM_MB_21140_MII) | 4057 (1 << TULIP_ROM_MB_21142_MII))) == 0) { 4058 /* 4059 * We have not yet seen any SIA/SYM media (but are 4060 * about to; that's why we're called!), so assign 4061 * the current media instance to be the `internal media' 4062 * instance, and advance it so any MII media gets a 4063 * fresh one (used to selecting/isolating a PHY). 4064 */ 4065 sc->sc_tlp_minst = sc->sc_mii.mii_instance++; 4066 } 4067 } 4068 4069 /* 4070 * SIA Utility functions. 4071 */ 4072 static void tlp_sia_update_link(struct tulip_softc *); 4073 static void tlp_sia_get(struct tulip_softc *, struct ifmediareq *); 4074 static int tlp_sia_set(struct tulip_softc *); 4075 static int tlp_sia_media(struct tulip_softc *, struct ifmedia_entry *); 4076 static void tlp_sia_fixup(struct tulip_softc *); 4077 4078 static void 4079 tlp_sia_update_link(struct tulip_softc *sc) 4080 { 4081 struct ifmedia_entry *ife; 4082 struct tulip_21x4x_media *tm; 4083 uint32_t siastat; 4084 4085 ife = TULIP_CURRENT_MEDIA(sc); 4086 tm = ife->ifm_aux; 4087 4088 sc->sc_flags &= ~(TULIPF_LINK_UP|TULIPF_LINK_VALID); 4089 4090 siastat = TULIP_READ(sc, CSR_SIASTAT); 4091 4092 /* 4093 * Note that when we do SIA link tests, we are assuming that 4094 * the chip is really in the mode that the current media setting 4095 * reflects. If we're not, then the link tests will not be 4096 * accurate! 4097 */ 4098 switch (IFM_SUBTYPE(ife->ifm_media)) { 4099 case IFM_10_T: 4100 sc->sc_flags |= TULIPF_LINK_VALID; 4101 if ((siastat & SIASTAT_LS10) == 0) 4102 sc->sc_flags |= TULIPF_LINK_UP; 4103 break; 4104 4105 case IFM_100_TX: 4106 case IFM_100_T4: 4107 sc->sc_flags |= TULIPF_LINK_VALID; 4108 if ((siastat & SIASTAT_LS100) == 0) 4109 sc->sc_flags |= TULIPF_LINK_UP; 4110 break; 4111 } 4112 4113 switch (sc->sc_chip) { 4114 case TULIP_CHIP_21142: 4115 case TULIP_CHIP_21143: 4116 /* 4117 * On these chips, we can tell more information about 4118 * AUI/BNC. Note that the AUI/BNC selection is made 4119 * in a different register; for our purpose, it's all 4120 * AUI. 4121 */ 4122 switch (IFM_SUBTYPE(ife->ifm_media)) { 4123 case IFM_10_2: 4124 case IFM_10_5: 4125 sc->sc_flags |= TULIPF_LINK_VALID; 4126 if (siastat & SIASTAT_ARA) { 4127 TULIP_WRITE(sc, CSR_SIASTAT, SIASTAT_ARA); 4128 sc->sc_flags |= TULIPF_LINK_UP; 4129 } 4130 break; 4131 4132 default: 4133 /* 4134 * If we're SYM media and can detect the link 4135 * via the GPIO facility, prefer that status 4136 * over LS100. 4137 */ 4138 if (tm->tm_type == TULIP_ROM_MB_21143_SYM && 4139 tm->tm_actmask != 0) { 4140 sc->sc_flags = (sc->sc_flags & 4141 ~TULIPF_LINK_UP) | TULIPF_LINK_VALID; 4142 if (TULIP_ISSET(sc, CSR_SIAGEN, 4143 tm->tm_actmask) == tm->tm_actdata) 4144 sc->sc_flags |= TULIPF_LINK_UP; 4145 } 4146 } 4147 break; 4148 4149 default: 4150 /* Nothing. */ 4151 break; 4152 } 4153 } 4154 4155 static void 4156 tlp_sia_get(struct tulip_softc *sc, struct ifmediareq *ifmr) 4157 { 4158 struct ifmedia_entry *ife; 4159 4160 ifmr->ifm_status = 0; 4161 4162 tlp_sia_update_link(sc); 4163 4164 ife = TULIP_CURRENT_MEDIA(sc); 4165 4166 if (sc->sc_flags & TULIPF_LINK_VALID) 4167 ifmr->ifm_status |= IFM_AVALID; 4168 if (sc->sc_flags & TULIPF_LINK_UP) 4169 ifmr->ifm_status |= IFM_ACTIVE; 4170 ifmr->ifm_active = ife->ifm_media; 4171 } 4172 4173 static void 4174 tlp_sia_fixup(struct tulip_softc *sc) 4175 { 4176 struct ifmedia_entry *ife; 4177 struct tulip_21x4x_media *tm; 4178 uint32_t siaconn, siatxrx, siagen; 4179 4180 switch (sc->sc_chip) { 4181 case TULIP_CHIP_82C115: 4182 case TULIP_CHIP_MX98713A: 4183 case TULIP_CHIP_MX98715: 4184 case TULIP_CHIP_MX98715A: 4185 case TULIP_CHIP_MX98715AEC_X: 4186 case TULIP_CHIP_MX98725: 4187 siaconn = PMAC_SIACONN_MASK; 4188 siatxrx = PMAC_SIATXRX_MASK; 4189 siagen = PMAC_SIAGEN_MASK; 4190 break; 4191 4192 default: 4193 /* No fixups required on any other chips. */ 4194 return; 4195 } 4196 4197 TAILQ_FOREACH(ife, &sc->sc_mii.mii_media.ifm_list, ifm_list) { 4198 tm = ife->ifm_aux; 4199 if (tm == NULL) 4200 continue; 4201 4202 tm->tm_siaconn &= siaconn; 4203 tm->tm_siatxrx &= siatxrx; 4204 tm->tm_siagen &= siagen; 4205 } 4206 } 4207 4208 static int 4209 tlp_sia_set(struct tulip_softc *sc) 4210 { 4211 4212 return (tlp_sia_media(sc, TULIP_CURRENT_MEDIA(sc))); 4213 } 4214 4215 static int 4216 tlp_sia_media(struct tulip_softc *sc, struct ifmedia_entry *ife) 4217 { 4218 struct tulip_21x4x_media *tm; 4219 4220 tm = ife->ifm_aux; 4221 4222 /* 4223 * XXX This appears to be necessary on a bunch of the clone chips. 4224 */ 4225 delay(20000); 4226 4227 /* 4228 * Idle the chip. 4229 */ 4230 tlp_idle(sc, OPMODE_ST|OPMODE_SR); 4231 4232 /* 4233 * Program the SIA. It's important to write in this order, 4234 * resetting the SIA first. 4235 */ 4236 TULIP_WRITE(sc, CSR_SIACONN, 0); /* SRL bit clear */ 4237 delay(1000); 4238 4239 TULIP_WRITE(sc, CSR_SIATXRX, tm->tm_siatxrx); 4240 4241 switch (sc->sc_chip) { 4242 case TULIP_CHIP_21142: 4243 case TULIP_CHIP_21143: 4244 TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpctl); 4245 TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpdata); 4246 break; 4247 default: 4248 TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen); 4249 } 4250 4251 TULIP_WRITE(sc, CSR_SIACONN, tm->tm_siaconn); 4252 4253 /* 4254 * Set the OPMODE bits for this media and write OPMODE. 4255 * This will resume the transmit and receive processes. 4256 */ 4257 sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode; 4258 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode); 4259 4260 return (0); 4261 } 4262 4263 /* 4264 * 21140 GPIO utility functions. 4265 */ 4266 static void tlp_21140_gpio_update_link(struct tulip_softc *); 4267 4268 static void 4269 tlp_21140_gpio_update_link(struct tulip_softc *sc) 4270 { 4271 struct ifmedia_entry *ife; 4272 struct tulip_21x4x_media *tm; 4273 4274 ife = TULIP_CURRENT_MEDIA(sc); 4275 tm = ife->ifm_aux; 4276 4277 sc->sc_flags &= ~(TULIPF_LINK_UP|TULIPF_LINK_VALID); 4278 4279 if (tm->tm_actmask != 0) { 4280 sc->sc_flags |= TULIPF_LINK_VALID; 4281 if (TULIP_ISSET(sc, CSR_GPP, tm->tm_actmask) == 4282 tm->tm_actdata) 4283 sc->sc_flags |= TULIPF_LINK_UP; 4284 } 4285 } 4286 4287 void 4288 tlp_21140_gpio_get(struct tulip_softc *sc, struct ifmediareq *ifmr) 4289 { 4290 struct ifmedia_entry *ife; 4291 4292 ifmr->ifm_status = 0; 4293 4294 tlp_21140_gpio_update_link(sc); 4295 4296 ife = TULIP_CURRENT_MEDIA(sc); 4297 4298 if (sc->sc_flags & TULIPF_LINK_VALID) 4299 ifmr->ifm_status |= IFM_AVALID; 4300 if (sc->sc_flags & TULIPF_LINK_UP) 4301 ifmr->ifm_status |= IFM_ACTIVE; 4302 ifmr->ifm_active = ife->ifm_media; 4303 } 4304 4305 int 4306 tlp_21140_gpio_set(struct tulip_softc *sc) 4307 { 4308 struct ifmedia_entry *ife; 4309 struct tulip_21x4x_media *tm; 4310 4311 ife = TULIP_CURRENT_MEDIA(sc); 4312 tm = ife->ifm_aux; 4313 4314 /* 4315 * Idle the chip. 4316 */ 4317 tlp_idle(sc, OPMODE_ST|OPMODE_SR); 4318 4319 /* 4320 * Set the GPIO pins for this media, to flip any 4321 * relays, etc. 4322 */ 4323 TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir); 4324 delay(10); 4325 TULIP_WRITE(sc, CSR_GPP, tm->tm_gpdata); 4326 4327 /* 4328 * Set the OPMODE bits for this media and write OPMODE. 4329 * This will resume the transmit and receive processes. 4330 */ 4331 sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode; 4332 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode); 4333 4334 return (0); 4335 } 4336 4337 /* 4338 * 21040 and 21041 media switches. 4339 */ 4340 static void tlp_21040_tmsw_init(struct tulip_softc *); 4341 static void tlp_21040_tp_tmsw_init(struct tulip_softc *); 4342 static void tlp_21040_auibnc_tmsw_init(struct tulip_softc *); 4343 static void tlp_21041_tmsw_init(struct tulip_softc *); 4344 4345 const struct tulip_mediasw tlp_21040_mediasw = { 4346 tlp_21040_tmsw_init, tlp_sia_get, tlp_sia_set 4347 }; 4348 4349 const struct tulip_mediasw tlp_21040_tp_mediasw = { 4350 tlp_21040_tp_tmsw_init, tlp_sia_get, tlp_sia_set 4351 }; 4352 4353 const struct tulip_mediasw tlp_21040_auibnc_mediasw = { 4354 tlp_21040_auibnc_tmsw_init, tlp_sia_get, tlp_sia_set 4355 }; 4356 4357 const struct tulip_mediasw tlp_21041_mediasw = { 4358 tlp_21041_tmsw_init, tlp_sia_get, tlp_sia_set 4359 }; 4360 4361 static void 4362 tlp_21040_tmsw_init(struct tulip_softc *sc) 4363 { 4364 static const uint8_t media[] = { 4365 TULIP_ROM_MB_MEDIA_TP, 4366 TULIP_ROM_MB_MEDIA_TP_FDX, 4367 TULIP_ROM_MB_MEDIA_AUI, 4368 }; 4369 struct tulip_21x4x_media *tm; 4370 4371 ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange, 4372 tlp_mediastatus); 4373 4374 tlp_add_srom_media(sc, 0, NULL, NULL, media, 3); 4375 4376 /* 4377 * No SROM type for External SIA. 4378 */ 4379 tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO); 4380 tm->tm_name = "manual"; 4381 tm->tm_opmode = 0; 4382 tm->tm_siaconn = SIACONN_21040_EXTSIA; 4383 tm->tm_siatxrx = SIATXRX_21040_EXTSIA; 4384 tm->tm_siagen = SIAGEN_21040_EXTSIA; 4385 ifmedia_add(&sc->sc_mii.mii_media, 4386 IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, sc->sc_tlp_minst), 0, tm); 4387 4388 /* 4389 * XXX Autosense not yet supported. 4390 */ 4391 4392 /* XXX This should be auto-sense. */ 4393 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T); 4394 4395 tlp_print_media(sc); 4396 } 4397 4398 static void 4399 tlp_21040_tp_tmsw_init(struct tulip_softc *sc) 4400 { 4401 static const uint8_t media[] = { 4402 TULIP_ROM_MB_MEDIA_TP, 4403 TULIP_ROM_MB_MEDIA_TP_FDX, 4404 }; 4405 4406 ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange, 4407 tlp_mediastatus); 4408 4409 tlp_add_srom_media(sc, 0, NULL, NULL, media, 2); 4410 4411 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T); 4412 4413 tlp_print_media(sc); 4414 } 4415 4416 static void 4417 tlp_21040_auibnc_tmsw_init(struct tulip_softc *sc) 4418 { 4419 static const uint8_t media[] = { 4420 TULIP_ROM_MB_MEDIA_AUI, 4421 }; 4422 4423 ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange, 4424 tlp_mediastatus); 4425 4426 tlp_add_srom_media(sc, 0, NULL, NULL, media, 1); 4427 4428 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_5); 4429 4430 tlp_print_media(sc); 4431 } 4432 4433 static void 4434 tlp_21041_tmsw_init(struct tulip_softc *sc) 4435 { 4436 static const uint8_t media[] = { 4437 TULIP_ROM_MB_MEDIA_TP, 4438 TULIP_ROM_MB_MEDIA_TP_FDX, 4439 TULIP_ROM_MB_MEDIA_BNC, 4440 TULIP_ROM_MB_MEDIA_AUI, 4441 }; 4442 int i, defmedia, devcnt, leaf_offset, mb_offset, m_cnt; 4443 const struct tulip_srom_to_ifmedia *tsti; 4444 struct tulip_21x4x_media *tm; 4445 uint16_t romdef; 4446 uint8_t mb; 4447 4448 ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange, 4449 tlp_mediastatus); 4450 4451 if (tlp_isv_srom(sc->sc_srom) == 0) { 4452 not_isv_srom: 4453 /* 4454 * If we have a board without the standard 21041 SROM format, 4455 * we just assume all media are present and try and pick a 4456 * reasonable default. 4457 */ 4458 tlp_add_srom_media(sc, 0, NULL, NULL, media, 4); 4459 4460 /* 4461 * XXX Autosense not yet supported. 4462 */ 4463 4464 /* XXX This should be auto-sense. */ 4465 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T); 4466 4467 tlp_print_media(sc); 4468 return; 4469 } 4470 4471 devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT]; 4472 for (i = 0; i < devcnt; i++) { 4473 if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1) 4474 break; 4475 if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] == 4476 sc->sc_devno) 4477 break; 4478 } 4479 4480 if (i == devcnt) 4481 goto not_isv_srom; 4482 4483 leaf_offset = TULIP_ROM_GETW(sc->sc_srom, 4484 TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i)); 4485 mb_offset = leaf_offset + TULIP_ROM_IL_MEDIAn_BLOCK_BASE; 4486 m_cnt = sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT]; 4487 4488 for (; m_cnt != 0; 4489 m_cnt--, mb_offset += TULIP_ROM_MB_SIZE(mb)) { 4490 mb = sc->sc_srom[mb_offset]; 4491 tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO); 4492 switch (mb & TULIP_ROM_MB_MEDIA_CODE) { 4493 case TULIP_ROM_MB_MEDIA_TP_FDX: 4494 case TULIP_ROM_MB_MEDIA_TP: 4495 case TULIP_ROM_MB_MEDIA_BNC: 4496 case TULIP_ROM_MB_MEDIA_AUI: 4497 tsti = tlp_srom_to_ifmedia(mb & 4498 TULIP_ROM_MB_MEDIA_CODE); 4499 4500 tlp_srom_media_info(sc, tsti, tm); 4501 4502 /* 4503 * Override our default SIA settings if the 4504 * SROM contains its own. 4505 */ 4506 if (mb & TULIP_ROM_MB_EXT) { 4507 tm->tm_siaconn = TULIP_ROM_GETW(sc->sc_srom, 4508 mb_offset + TULIP_ROM_MB_CSR13); 4509 tm->tm_siatxrx = TULIP_ROM_GETW(sc->sc_srom, 4510 mb_offset + TULIP_ROM_MB_CSR14); 4511 tm->tm_siagen = TULIP_ROM_GETW(sc->sc_srom, 4512 mb_offset + TULIP_ROM_MB_CSR15); 4513 } 4514 4515 ifmedia_add(&sc->sc_mii.mii_media, 4516 IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype, 4517 tsti->tsti_options, sc->sc_tlp_minst), 0, tm); 4518 break; 4519 4520 default: 4521 aprint_error_dev(sc->sc_dev, 4522 "unknown media code 0x%02x\n", 4523 mb & TULIP_ROM_MB_MEDIA_CODE); 4524 free(tm, M_DEVBUF); 4525 } 4526 } 4527 4528 /* 4529 * XXX Autosense not yet supported. 4530 */ 4531 4532 romdef = TULIP_ROM_GETW(sc->sc_srom, leaf_offset + 4533 TULIP_ROM_IL_SELECT_CONN_TYPE); 4534 switch (romdef) { 4535 case SELECT_CONN_TYPE_TP: 4536 case SELECT_CONN_TYPE_TP_AUTONEG: 4537 case SELECT_CONN_TYPE_TP_NOLINKPASS: 4538 defmedia = IFM_ETHER|IFM_10_T; 4539 break; 4540 4541 case SELECT_CONN_TYPE_TP_FDX: 4542 defmedia = IFM_ETHER|IFM_10_T|IFM_FDX; 4543 break; 4544 4545 case SELECT_CONN_TYPE_BNC: 4546 defmedia = IFM_ETHER|IFM_10_2; 4547 break; 4548 4549 case SELECT_CONN_TYPE_AUI: 4550 defmedia = IFM_ETHER|IFM_10_5; 4551 break; 4552 #if 0 /* XXX */ 4553 case SELECT_CONN_TYPE_ASENSE: 4554 case SELECT_CONN_TYPE_ASENSE_AUTONEG: 4555 defmedia = IFM_ETHER|IFM_AUTO; 4556 break; 4557 #endif 4558 default: 4559 defmedia = 0; 4560 } 4561 4562 if (defmedia == 0) { 4563 /* 4564 * XXX We should default to auto-sense. 4565 */ 4566 defmedia = IFM_ETHER|IFM_10_T; 4567 } 4568 4569 ifmedia_set(&sc->sc_mii.mii_media, defmedia); 4570 4571 tlp_print_media(sc); 4572 } 4573 4574 /* 4575 * DECchip 2114x ISV media switch. 4576 */ 4577 static void tlp_2114x_isv_tmsw_init(struct tulip_softc *); 4578 static void tlp_2114x_isv_tmsw_get(struct tulip_softc *, 4579 struct ifmediareq *); 4580 static int tlp_2114x_isv_tmsw_set(struct tulip_softc *); 4581 4582 const struct tulip_mediasw tlp_2114x_isv_mediasw = { 4583 tlp_2114x_isv_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set 4584 }; 4585 4586 static void tlp_2114x_nway_get(struct tulip_softc *, struct ifmediareq *); 4587 static int tlp_2114x_nway_set(struct tulip_softc *); 4588 4589 static void tlp_2114x_nway_statchg(device_t); 4590 static int tlp_2114x_nway_service(struct tulip_softc *, int); 4591 static void tlp_2114x_nway_auto(struct tulip_softc *); 4592 static void tlp_2114x_nway_status(struct tulip_softc *); 4593 4594 static void 4595 tlp_2114x_isv_tmsw_init(struct tulip_softc *sc) 4596 { 4597 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 4598 struct ifmedia_entry *ife; 4599 struct mii_softc *phy; 4600 struct tulip_21x4x_media *tm; 4601 const struct tulip_srom_to_ifmedia *tsti; 4602 int i, devcnt, leaf_offset, m_cnt, type, length; 4603 int defmedia, miidef; 4604 uint16_t word; 4605 uint8_t *cp, *ncp; 4606 4607 defmedia = miidef = 0; 4608 4609 sc->sc_mii.mii_ifp = ifp; 4610 sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg; 4611 sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg; 4612 sc->sc_mii.mii_statchg = sc->sc_statchg; 4613 4614 /* 4615 * Ignore `instance'; we may get a mixture of SIA and MII 4616 * media, and `instance' is used to isolate or select the 4617 * PHY on the MII as appropriate. Note that duplicate media 4618 * are disallowed, so ignoring `instance' is safe. 4619 */ 4620 ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, tlp_mediachange, 4621 tlp_mediastatus); 4622 4623 devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT]; 4624 for (i = 0; i < devcnt; i++) { 4625 if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1) 4626 break; 4627 if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] == 4628 sc->sc_devno) 4629 break; 4630 } 4631 4632 if (i == devcnt) { 4633 aprint_error_dev(sc->sc_dev, "unable to locate info leaf in SROM\n"); 4634 return; 4635 } 4636 4637 leaf_offset = TULIP_ROM_GETW(sc->sc_srom, 4638 TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i)); 4639 4640 /* XXX SELECT CONN TYPE */ 4641 4642 cp = &sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT]; 4643 4644 /* 4645 * On some chips, the first thing in the Info Leaf is the 4646 * GPIO pin direction data. 4647 */ 4648 switch (sc->sc_chip) { 4649 case TULIP_CHIP_21140: 4650 case TULIP_CHIP_21140A: 4651 case TULIP_CHIP_MX98713: 4652 case TULIP_CHIP_AX88140: 4653 case TULIP_CHIP_AX88141: 4654 sc->sc_gp_dir = *cp++; 4655 break; 4656 4657 default: 4658 /* Nothing. */ 4659 break; 4660 } 4661 4662 /* Get the media count. */ 4663 m_cnt = *cp++; 4664 4665 if (m_cnt == 0) { 4666 sc->sc_mediasw = &tlp_sio_mii_mediasw; 4667 (*sc->sc_mediasw->tmsw_init)(sc); 4668 return; 4669 } 4670 4671 for (; m_cnt != 0; cp = ncp, m_cnt--) { 4672 /* 4673 * Determine the type and length of this media block. 4674 * The 21143 is spec'd to always use extended format blocks, 4675 * but some cards don't set the bit to indicate this. 4676 * Hopefully there are no cards which really don't use 4677 * extended format blocks. 4678 */ 4679 if ((*cp & 0x80) == 0 && sc->sc_chip != TULIP_CHIP_21143) { 4680 length = 4; 4681 type = TULIP_ROM_MB_21140_GPR; 4682 } else { 4683 length = (*cp++ & 0x7f) - 1; 4684 type = *cp++ & 0x3f; 4685 } 4686 4687 /* Compute the start of the next block. */ 4688 ncp = cp + length; 4689 4690 /* Now, parse the block. */ 4691 switch (type) { 4692 case TULIP_ROM_MB_21140_GPR: 4693 tlp_get_minst(sc); 4694 sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_GPR; 4695 4696 tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO); 4697 4698 tm->tm_type = TULIP_ROM_MB_21140_GPR; 4699 tm->tm_get = tlp_21140_gpio_get; 4700 tm->tm_set = tlp_21140_gpio_set; 4701 4702 /* First is the media type code. */ 4703 tsti = tlp_srom_to_ifmedia(cp[0] & 4704 TULIP_ROM_MB_MEDIA_CODE); 4705 if (tsti == NULL) { 4706 /* Invalid media code. */ 4707 free(tm, M_DEVBUF); 4708 break; 4709 } 4710 4711 /* Get defaults. */ 4712 tlp_srom_media_info(sc, tsti, tm); 4713 4714 /* Next is any GPIO info for this media. */ 4715 tm->tm_gpdata = cp[1]; 4716 4717 /* 4718 * Next is a word containing OPMODE information 4719 * and info on how to detect if this media is 4720 * active. 4721 */ 4722 word = TULIP_ROM_GETW(cp, 2); 4723 tm->tm_opmode &= OPMODE_FD; 4724 tm->tm_opmode |= TULIP_ROM_MB_OPMODE(word); 4725 if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) { 4726 tm->tm_actmask = 4727 TULIP_ROM_MB_BITPOS(word); 4728 tm->tm_actdata = 4729 (word & TULIP_ROM_MB_POLARITY) ? 4730 0 : tm->tm_actmask; 4731 } 4732 4733 ifmedia_add(&sc->sc_mii.mii_media, 4734 IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype, 4735 tsti->tsti_options, sc->sc_tlp_minst), 0, tm); 4736 break; 4737 4738 case TULIP_ROM_MB_21140_MII: 4739 sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_MII; 4740 4741 tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO); 4742 4743 tm->tm_type = TULIP_ROM_MB_21140_MII; 4744 tm->tm_get = tlp_mii_getmedia; 4745 tm->tm_set = tlp_mii_setmedia; 4746 tm->tm_opmode = OPMODE_PS; 4747 4748 if (sc->sc_reset == NULL) 4749 sc->sc_reset = tlp_21140_reset; 4750 4751 /* First is the PHY number. */ 4752 tm->tm_phyno = *cp++; 4753 4754 /* Next is the MII select sequence length and offset. */ 4755 tm->tm_gp_length = *cp++; 4756 tm->tm_gp_offset = cp - &sc->sc_srom[0]; 4757 cp += tm->tm_gp_length; 4758 4759 /* Next is the MII reset sequence length and offset. */ 4760 tm->tm_reset_length = *cp++; 4761 tm->tm_reset_offset = cp - &sc->sc_srom[0]; 4762 cp += tm->tm_reset_length; 4763 4764 /* 4765 * The following items are left in the media block 4766 * that we don't particularly care about: 4767 * 4768 * capabilities W 4769 * advertisement W 4770 * full duplex W 4771 * tx threshold W 4772 * 4773 * These appear to be bits in the PHY registers, 4774 * which our MII code handles on its own. 4775 */ 4776 4777 /* 4778 * Before we probe the MII bus, we need to reset 4779 * it and issue the selection sequence. 4780 */ 4781 4782 /* Set the direction of the pins... */ 4783 TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir); 4784 4785 for (i = 0; i < tm->tm_reset_length; i++) { 4786 delay(10); 4787 TULIP_WRITE(sc, CSR_GPP, 4788 sc->sc_srom[tm->tm_reset_offset + i]); 4789 } 4790 4791 for (i = 0; i < tm->tm_gp_length; i++) { 4792 delay(10); 4793 TULIP_WRITE(sc, CSR_GPP, 4794 sc->sc_srom[tm->tm_gp_offset + i]); 4795 } 4796 4797 /* If there were no sequences, just lower the pins. */ 4798 if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) { 4799 delay(10); 4800 TULIP_WRITE(sc, CSR_GPP, 0); 4801 } 4802 4803 /* 4804 * Now, probe the MII for the PHY. Note, we know 4805 * the location of the PHY on the bus, but we don't 4806 * particularly care; the MII code just likes to 4807 * search the whole thing anyhow. 4808 */ 4809 mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, 4810 MII_PHY_ANY, tm->tm_phyno, 0); 4811 4812 /* 4813 * Now, search for the PHY we hopefully just 4814 * configured. If it's not configured into the 4815 * kernel, we lose. The PHY's default media always 4816 * takes priority. 4817 */ 4818 LIST_FOREACH(phy, &sc->sc_mii.mii_phys, mii_list) { 4819 if (phy->mii_offset == tm->tm_phyno) 4820 break; 4821 } 4822 if (phy == NULL) { 4823 aprint_error_dev(sc->sc_dev, "unable to configure MII\n"); 4824 break; 4825 } 4826 4827 sc->sc_flags |= TULIPF_HAS_MII; 4828 sc->sc_tick = tlp_mii_tick; 4829 miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 4830 phy->mii_inst); 4831 4832 /* 4833 * Okay, now that we've found the PHY and the MII 4834 * layer has added all of the media associated 4835 * with that PHY, we need to traverse the media 4836 * list, and add our `tm' to each entry's `aux' 4837 * pointer. 4838 * 4839 * We do this by looking for media with our 4840 * PHY's `instance'. 4841 */ 4842 TAILQ_FOREACH(ife, &sc->sc_mii.mii_media.ifm_list, 4843 ifm_list) { 4844 if (IFM_INST(ife->ifm_media) != phy->mii_inst) 4845 continue; 4846 ife->ifm_aux = tm; 4847 } 4848 break; 4849 4850 case TULIP_ROM_MB_21142_SIA: 4851 tlp_get_minst(sc); 4852 sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_SIA; 4853 4854 tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO); 4855 4856 tm->tm_type = TULIP_ROM_MB_21142_SIA; 4857 tm->tm_get = tlp_sia_get; 4858 tm->tm_set = tlp_sia_set; 4859 4860 /* First is the media type code. */ 4861 tsti = tlp_srom_to_ifmedia(cp[0] & 4862 TULIP_ROM_MB_MEDIA_CODE); 4863 if (tsti == NULL) { 4864 /* Invalid media code. */ 4865 free(tm, M_DEVBUF); 4866 break; 4867 } 4868 4869 /* Get defaults. */ 4870 tlp_srom_media_info(sc, tsti, tm); 4871 4872 /* 4873 * Override our default SIA settings if the 4874 * SROM contains its own. 4875 */ 4876 if (cp[0] & 0x40) { 4877 tm->tm_siaconn = TULIP_ROM_GETW(cp, 1); 4878 tm->tm_siatxrx = TULIP_ROM_GETW(cp, 3); 4879 tm->tm_siagen = TULIP_ROM_GETW(cp, 5); 4880 cp += 7; 4881 } else 4882 cp++; 4883 4884 /* Next is GPIO control/data. */ 4885 tm->tm_gpctl = TULIP_ROM_GETW(cp, 0) << 16; 4886 tm->tm_gpdata = TULIP_ROM_GETW(cp, 2) << 16; 4887 4888 ifmedia_add(&sc->sc_mii.mii_media, 4889 IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype, 4890 tsti->tsti_options, sc->sc_tlp_minst), 0, tm); 4891 break; 4892 4893 case TULIP_ROM_MB_21142_MII: 4894 sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_MII; 4895 4896 tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO); 4897 4898 tm->tm_type = TULIP_ROM_MB_21142_MII; 4899 tm->tm_get = tlp_mii_getmedia; 4900 tm->tm_set = tlp_mii_setmedia; 4901 tm->tm_opmode = OPMODE_PS; 4902 4903 if (sc->sc_reset == NULL) 4904 sc->sc_reset = tlp_21142_reset; 4905 4906 /* First is the PHY number. */ 4907 tm->tm_phyno = *cp++; 4908 4909 /* Next is the MII select sequence length and offset. */ 4910 tm->tm_gp_length = *cp++; 4911 tm->tm_gp_offset = cp - &sc->sc_srom[0]; 4912 cp += tm->tm_gp_length * 2; 4913 4914 /* Next is the MII reset sequence length and offset. */ 4915 tm->tm_reset_length = *cp++; 4916 tm->tm_reset_offset = cp - &sc->sc_srom[0]; 4917 cp += tm->tm_reset_length * 2; 4918 4919 /* 4920 * The following items are left in the media block 4921 * that we don't particularly care about: 4922 * 4923 * capabilities W 4924 * advertisement W 4925 * full duplex W 4926 * tx threshold W 4927 * MII interrupt W 4928 * 4929 * These appear to be bits in the PHY registers, 4930 * which our MII code handles on its own. 4931 */ 4932 4933 /* 4934 * Before we probe the MII bus, we need to reset 4935 * it and issue the selection sequence. 4936 */ 4937 4938 cp = &sc->sc_srom[tm->tm_reset_offset]; 4939 for (i = 0; i < tm->tm_reset_length; i++, cp += 2) { 4940 delay(10); 4941 TULIP_WRITE(sc, CSR_SIAGEN, 4942 TULIP_ROM_GETW(cp, 0) << 16); 4943 } 4944 4945 cp = &sc->sc_srom[tm->tm_gp_offset]; 4946 for (i = 0; i < tm->tm_gp_length; i++, cp += 2) { 4947 delay(10); 4948 TULIP_WRITE(sc, CSR_SIAGEN, 4949 TULIP_ROM_GETW(cp, 0) << 16); 4950 } 4951 4952 /* If there were no sequences, just lower the pins. */ 4953 if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) { 4954 delay(10); 4955 TULIP_WRITE(sc, CSR_SIAGEN, 0); 4956 } 4957 4958 /* 4959 * Now, probe the MII for the PHY. Note, we know 4960 * the location of the PHY on the bus, but we don't 4961 * particularly care; the MII code just likes to 4962 * search the whole thing anyhow. 4963 */ 4964 mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, 4965 MII_PHY_ANY, tm->tm_phyno, 0); 4966 4967 /* 4968 * Now, search for the PHY we hopefully just 4969 * configured. If it's not configured into the 4970 * kernel, we lose. The PHY's default media always 4971 * takes priority. 4972 */ 4973 LIST_FOREACH(phy, &sc->sc_mii.mii_phys, mii_list) { 4974 if (phy->mii_offset == tm->tm_phyno) 4975 break; 4976 } 4977 if (phy == NULL) { 4978 aprint_error_dev(sc->sc_dev, "unable to configure MII\n"); 4979 break; 4980 } 4981 4982 sc->sc_flags |= TULIPF_HAS_MII; 4983 sc->sc_tick = tlp_mii_tick; 4984 miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 4985 phy->mii_inst); 4986 4987 /* 4988 * Okay, now that we've found the PHY and the MII 4989 * layer has added all of the media associated 4990 * with that PHY, we need to traverse the media 4991 * list, and add our `tm' to each entry's `aux' 4992 * pointer. 4993 * 4994 * We do this by looking for media with our 4995 * PHY's `instance'. 4996 */ 4997 TAILQ_FOREACH(ife, &sc->sc_mii.mii_media.ifm_list, 4998 ifm_list) { 4999 if (IFM_INST(ife->ifm_media) != phy->mii_inst) 5000 continue; 5001 ife->ifm_aux = tm; 5002 } 5003 break; 5004 5005 case TULIP_ROM_MB_21143_SYM: 5006 tlp_get_minst(sc); 5007 sc->sc_media_seen |= 1 << TULIP_ROM_MB_21143_SYM; 5008 5009 tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO); 5010 5011 tm->tm_type = TULIP_ROM_MB_21143_SYM; 5012 tm->tm_get = tlp_sia_get; 5013 tm->tm_set = tlp_sia_set; 5014 5015 /* First is the media type code. */ 5016 tsti = tlp_srom_to_ifmedia(cp[0] & 5017 TULIP_ROM_MB_MEDIA_CODE); 5018 if (tsti == NULL) { 5019 /* Invalid media code. */ 5020 free(tm, M_DEVBUF); 5021 break; 5022 } 5023 5024 /* Get defaults. */ 5025 tlp_srom_media_info(sc, tsti, tm); 5026 5027 /* Next is GPIO control/data. */ 5028 tm->tm_gpctl = TULIP_ROM_GETW(cp, 1) << 16; 5029 tm->tm_gpdata = TULIP_ROM_GETW(cp, 3) << 16; 5030 5031 /* 5032 * Next is a word containing OPMODE information 5033 * and info on how to detect if this media is 5034 * active. 5035 */ 5036 word = TULIP_ROM_GETW(cp, 5); 5037 tm->tm_opmode &= OPMODE_FD; 5038 tm->tm_opmode |= TULIP_ROM_MB_OPMODE(word); 5039 if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) { 5040 tm->tm_actmask = 5041 TULIP_ROM_MB_BITPOS(word); 5042 tm->tm_actdata = 5043 (word & TULIP_ROM_MB_POLARITY) ? 5044 0 : tm->tm_actmask; 5045 } 5046 5047 ifmedia_add(&sc->sc_mii.mii_media, 5048 IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype, 5049 tsti->tsti_options, sc->sc_tlp_minst), 0, tm); 5050 break; 5051 5052 case TULIP_ROM_MB_21143_RESET: 5053 aprint_normal_dev(sc->sc_dev, "21143 reset block\n"); 5054 break; 5055 5056 default: 5057 aprint_error_dev(sc->sc_dev, 5058 "unknown ISV media block type 0x%02x\n", type); 5059 } 5060 } 5061 5062 /* 5063 * Deal with the case where no media is configured. 5064 */ 5065 if (TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list) == NULL) { 5066 aprint_error_dev(sc->sc_dev, "no media found!\n"); 5067 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL); 5068 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE); 5069 return; 5070 } 5071 5072 /* 5073 * Pick the default media. 5074 */ 5075 if (miidef != 0) 5076 defmedia = miidef; 5077 else { 5078 switch (sc->sc_chip) { 5079 case TULIP_CHIP_21140: 5080 case TULIP_CHIP_21140A: 5081 /* XXX should come from SROM */ 5082 defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0); 5083 if (ifmedia_match(&sc->sc_mii.mii_media, defmedia, 5084 sc->sc_mii.mii_media.ifm_mask) == NULL) { 5085 /* 5086 * There is not a 10baseT media. 5087 * Fall back to the first found one. 5088 */ 5089 ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list); 5090 defmedia = ife->ifm_media; 5091 } 5092 break; 5093 5094 case TULIP_CHIP_21142: 5095 case TULIP_CHIP_21143: 5096 case TULIP_CHIP_MX98713A: 5097 case TULIP_CHIP_MX98715: 5098 case TULIP_CHIP_MX98715A: 5099 case TULIP_CHIP_MX98715AEC_X: 5100 case TULIP_CHIP_MX98725: 5101 tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO); 5102 tm->tm_name = "auto"; 5103 tm->tm_get = tlp_2114x_nway_get; 5104 tm->tm_set = tlp_2114x_nway_set; 5105 5106 defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0); 5107 ifmedia_add(&sc->sc_mii.mii_media, defmedia, 0, tm); 5108 5109 sc->sc_statchg = tlp_2114x_nway_statchg; 5110 sc->sc_tick = tlp_2114x_nway_tick; 5111 break; 5112 5113 default: 5114 defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0); 5115 break; 5116 } 5117 } 5118 5119 ifmedia_set(&sc->sc_mii.mii_media, defmedia); 5120 5121 /* 5122 * Display any non-MII media we've located. 5123 */ 5124 if (sc->sc_media_seen & 5125 ~((1 << TULIP_ROM_MB_21140_MII) | (1 << TULIP_ROM_MB_21142_MII))) 5126 tlp_print_media(sc); 5127 5128 tlp_sia_fixup(sc); 5129 } 5130 5131 static void 5132 tlp_2114x_nway_get(struct tulip_softc *sc, struct ifmediareq *ifmr) 5133 { 5134 5135 (void) tlp_2114x_nway_service(sc, MII_POLLSTAT); 5136 ifmr->ifm_status = sc->sc_mii.mii_media_status; 5137 ifmr->ifm_active = sc->sc_mii.mii_media_active; 5138 } 5139 5140 static int 5141 tlp_2114x_nway_set(struct tulip_softc *sc) 5142 { 5143 5144 return (tlp_2114x_nway_service(sc, MII_MEDIACHG)); 5145 } 5146 5147 static void 5148 tlp_2114x_nway_statchg(device_t self) 5149 { 5150 struct tulip_softc *sc = device_private(self); 5151 struct mii_data *mii = &sc->sc_mii; 5152 struct ifmedia_entry *ife; 5153 5154 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE) 5155 return; 5156 5157 if ((ife = ifmedia_match(&mii->mii_media, mii->mii_media_active, 5158 mii->mii_media.ifm_mask)) == NULL) { 5159 printf("tlp_2114x_nway_statchg: no match for media 0x%x/0x%x\n", 5160 mii->mii_media_active, ~mii->mii_media.ifm_mask); 5161 panic("tlp_2114x_nway_statchg"); 5162 } 5163 5164 tlp_sia_media(sc, ife); 5165 } 5166 5167 static void 5168 tlp_2114x_nway_tick(void *arg) 5169 { 5170 struct tulip_softc *sc = arg; 5171 struct mii_data *mii = &sc->sc_mii; 5172 int s, ticks; 5173 5174 if (!device_is_active(sc->sc_dev)) 5175 return; 5176 5177 s = splnet(); 5178 tlp_2114x_nway_service(sc, MII_TICK); 5179 if ((sc->sc_flags & TULIPF_LINK_UP) == 0 && 5180 (mii->mii_media_status & IFM_ACTIVE) != 0 && 5181 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 5182 sc->sc_flags |= TULIPF_LINK_UP; 5183 tlp_start(&sc->sc_ethercom.ec_if); 5184 } else if ((sc->sc_flags & TULIPF_LINK_UP) != 0 && 5185 (mii->mii_media_status & IFM_ACTIVE) == 0) { 5186 sc->sc_flags &= ~TULIPF_LINK_UP; 5187 } 5188 splx(s); 5189 5190 if ((sc->sc_flags & TULIPF_LINK_UP) == 0) 5191 ticks = hz >> 3; 5192 else 5193 ticks = hz; 5194 callout_reset(&sc->sc_tick_callout, ticks, tlp_2114x_nway_tick, sc); 5195 } 5196 5197 /* 5198 * Support for the 2114X internal NWay block. This is constructed 5199 * somewhat like a PHY driver for simplicity. 5200 */ 5201 5202 static int 5203 tlp_2114x_nway_service(struct tulip_softc *sc, int cmd) 5204 { 5205 struct mii_data *mii = &sc->sc_mii; 5206 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 5207 5208 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 5209 return (0); 5210 5211 switch (cmd) { 5212 case MII_POLLSTAT: 5213 /* Nothing special to do here. */ 5214 break; 5215 5216 case MII_MEDIACHG: 5217 switch (IFM_SUBTYPE(ife->ifm_media)) { 5218 case IFM_AUTO: 5219 goto restart; 5220 default: 5221 /* Manual setting doesn't go through here. */ 5222 printf("tlp_2114x_nway_service: oops!\n"); 5223 return (EINVAL); 5224 } 5225 break; 5226 5227 case MII_TICK: 5228 /* 5229 * Only used for autonegotiation. 5230 */ 5231 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 5232 break; 5233 5234 /* 5235 * Check to see if we have link. If we do, we don't 5236 * need to restart the autonegotiation process. 5237 */ 5238 #if 0 5239 if (mii->mii_media_status & IFM_ACTIVE) 5240 #else 5241 if (sc->sc_flags & TULIPF_LINK_UP) 5242 #endif 5243 break; 5244 5245 /* 5246 * Only retry autonegotiation every 5 seconds. 5247 */ 5248 if (++sc->sc_nway_ticks != (5 << 3)) 5249 break; 5250 5251 restart: 5252 sc->sc_nway_ticks = 0; 5253 ife->ifm_data = IFM_NONE; 5254 tlp_2114x_nway_auto(sc); 5255 break; 5256 } 5257 5258 /* Update the media status. */ 5259 tlp_2114x_nway_status(sc); 5260 5261 /* 5262 * Callback if something changed. Manually configuration goes through 5263 * tlp_sia_set() anyway, so ignore that here. 5264 */ 5265 if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO && 5266 ife->ifm_data != mii->mii_media_active) { 5267 (*sc->sc_statchg)(sc->sc_dev); 5268 ife->ifm_data = mii->mii_media_active; 5269 } 5270 return (0); 5271 } 5272 5273 static void 5274 tlp_2114x_nway_auto(struct tulip_softc *sc) 5275 { 5276 uint32_t siastat, siatxrx; 5277 5278 tlp_idle(sc, OPMODE_ST|OPMODE_SR); 5279 5280 sc->sc_opmode &= ~(OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_FD); 5281 sc->sc_opmode |= OPMODE_TTM|OPMODE_HBD; 5282 siatxrx = 0xffbf; /* XXX magic number */ 5283 5284 /* Compute the link code word to advertise. */ 5285 if (sc->sc_sia_cap & BMSR_100T4) 5286 siatxrx |= SIATXRX_T4; 5287 if (sc->sc_sia_cap & BMSR_100TXFDX) 5288 siatxrx |= SIATXRX_TXF; 5289 if (sc->sc_sia_cap & BMSR_100TXHDX) 5290 siatxrx |= SIATXRX_THX; 5291 if (sc->sc_sia_cap & BMSR_10TFDX) 5292 sc->sc_opmode |= OPMODE_FD; 5293 if (sc->sc_sia_cap & BMSR_10THDX) 5294 siatxrx |= SIATXRX_TH; 5295 5296 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode); 5297 5298 TULIP_WRITE(sc, CSR_SIACONN, 0); 5299 delay(1000); 5300 TULIP_WRITE(sc, CSR_SIATXRX, siatxrx); 5301 TULIP_WRITE(sc, CSR_SIACONN, SIACONN_SRL); 5302 5303 siastat = TULIP_READ(sc, CSR_SIASTAT); 5304 siastat &= ~(SIASTAT_ANS|SIASTAT_LPC|SIASTAT_TRA|SIASTAT_ARA| 5305 SIASTAT_LS100|SIASTAT_LS10|SIASTAT_MRA); 5306 siastat |= SIASTAT_ANS_TXDIS; 5307 TULIP_WRITE(sc, CSR_SIASTAT, siastat); 5308 } 5309 5310 static void 5311 tlp_2114x_nway_status(struct tulip_softc *sc) 5312 { 5313 struct mii_data *mii = &sc->sc_mii; 5314 uint32_t siatxrx, siastat, anlpar; 5315 5316 mii->mii_media_status = IFM_AVALID; 5317 mii->mii_media_active = IFM_ETHER; 5318 5319 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 5320 return; 5321 5322 siastat = TULIP_READ(sc, CSR_SIASTAT); 5323 siatxrx = TULIP_READ(sc, CSR_SIATXRX); 5324 5325 if (siatxrx & SIATXRX_ANE) { 5326 if ((siastat & SIASTAT_ANS) != SIASTAT_ANS_FLPGOOD) { 5327 /* Erg, still trying, I guess... */ 5328 mii->mii_media_active |= IFM_NONE; 5329 return; 5330 } 5331 5332 if (~siastat & (SIASTAT_LS10 | SIASTAT_LS100)) 5333 mii->mii_media_status |= IFM_ACTIVE; 5334 5335 if (siastat & SIASTAT_LPN) { 5336 anlpar = SIASTAT_GETLPC(siastat); 5337 if (anlpar & ANLPAR_T4 && 5338 sc->sc_sia_cap & BMSR_100T4) 5339 mii->mii_media_active |= IFM_100_T4; 5340 else if (anlpar & ANLPAR_TX_FD && 5341 sc->sc_sia_cap & BMSR_100TXFDX) 5342 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 5343 else if (anlpar & ANLPAR_TX && 5344 sc->sc_sia_cap & BMSR_100TXHDX) 5345 mii->mii_media_active |= IFM_100_TX; 5346 else if (anlpar & ANLPAR_10_FD && 5347 sc->sc_sia_cap & BMSR_10TFDX) 5348 mii->mii_media_active |= IFM_10_T|IFM_FDX; 5349 else if (anlpar & ANLPAR_10 && 5350 sc->sc_sia_cap & BMSR_10THDX) 5351 mii->mii_media_active |= IFM_10_T; 5352 else 5353 mii->mii_media_active |= IFM_NONE; 5354 } else { 5355 /* 5356 * If the other side doesn't support NWAY, then the 5357 * best we can do is determine if we have a 10Mbps or 5358 * 100Mbps link. There's no way to know if the link 5359 * is full or half duplex, so we default to half duplex 5360 * and hope that the user is clever enough to manually 5361 * change the media settings if we're wrong. 5362 */ 5363 if ((siastat & SIASTAT_LS100) == 0) 5364 mii->mii_media_active |= IFM_100_TX; 5365 else if ((siastat & SIASTAT_LS10) == 0) 5366 mii->mii_media_active |= IFM_10_T; 5367 else 5368 mii->mii_media_active |= IFM_NONE; 5369 } 5370 } else { 5371 if (~siastat & (SIASTAT_LS10 | SIASTAT_LS100)) 5372 mii->mii_media_status |= IFM_ACTIVE; 5373 5374 if (sc->sc_opmode & OPMODE_TTM) 5375 mii->mii_media_active |= IFM_10_T; 5376 else 5377 mii->mii_media_active |= IFM_100_TX; 5378 if (sc->sc_opmode & OPMODE_FD) 5379 mii->mii_media_active |= IFM_FDX; 5380 } 5381 } 5382 5383 static void 5384 tlp_2114x_isv_tmsw_get(struct tulip_softc *sc, struct ifmediareq *ifmr) 5385 { 5386 struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur; 5387 struct tulip_21x4x_media *tm = ife->ifm_aux; 5388 5389 (*tm->tm_get)(sc, ifmr); 5390 } 5391 5392 static int 5393 tlp_2114x_isv_tmsw_set(struct tulip_softc *sc) 5394 { 5395 struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur; 5396 struct tulip_21x4x_media *tm = ife->ifm_aux; 5397 5398 /* 5399 * Check to see if we need to reset the chip, and do it. The 5400 * reset path will get the OPMODE register right the next 5401 * time through. 5402 */ 5403 if (TULIP_MEDIA_NEEDSRESET(sc, tm->tm_opmode)) 5404 return (tlp_init(&sc->sc_ethercom.ec_if)); 5405 5406 return ((*tm->tm_set)(sc)); 5407 } 5408 5409 /* 5410 * MII-on-SIO media switch. Handles only MII attached to the SIO. 5411 */ 5412 static void tlp_sio_mii_tmsw_init(struct tulip_softc *); 5413 5414 const struct tulip_mediasw tlp_sio_mii_mediasw = { 5415 tlp_sio_mii_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia 5416 }; 5417 5418 static void 5419 tlp_sio_mii_tmsw_init(struct tulip_softc *sc) 5420 { 5421 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 5422 5423 /* 5424 * We don't attach any media info structures to the ifmedia 5425 * entries, so if we're using a pre-init function that needs 5426 * that info, override it to one that doesn't. 5427 */ 5428 if (sc->sc_preinit == tlp_2114x_preinit) 5429 sc->sc_preinit = tlp_2114x_mii_preinit; 5430 5431 sc->sc_mii.mii_ifp = ifp; 5432 sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg; 5433 sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg; 5434 sc->sc_mii.mii_statchg = sc->sc_statchg; 5435 ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange, 5436 tlp_mediastatus); 5437 mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY, 5438 MII_OFFSET_ANY, 0); 5439 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) { 5440 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL); 5441 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE); 5442 } else { 5443 sc->sc_flags |= TULIPF_HAS_MII; 5444 sc->sc_tick = tlp_mii_tick; 5445 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO); 5446 } 5447 } 5448 5449 /* 5450 * Lite-On PNIC media switch. Must handle MII or internal NWAY. 5451 */ 5452 static void tlp_pnic_tmsw_init(struct tulip_softc *); 5453 static void tlp_pnic_tmsw_get(struct tulip_softc *, struct ifmediareq *); 5454 static int tlp_pnic_tmsw_set(struct tulip_softc *); 5455 5456 const struct tulip_mediasw tlp_pnic_mediasw = { 5457 tlp_pnic_tmsw_init, tlp_pnic_tmsw_get, tlp_pnic_tmsw_set 5458 }; 5459 5460 static void tlp_pnic_nway_statchg(device_t); 5461 static void tlp_pnic_nway_tick(void *); 5462 static int tlp_pnic_nway_service(struct tulip_softc *, int); 5463 static void tlp_pnic_nway_reset(struct tulip_softc *); 5464 static int tlp_pnic_nway_auto(struct tulip_softc *, int); 5465 static void tlp_pnic_nway_auto_timeout(void *); 5466 static void tlp_pnic_nway_status(struct tulip_softc *); 5467 static void tlp_pnic_nway_acomp(struct tulip_softc *); 5468 5469 static void 5470 tlp_pnic_tmsw_init(struct tulip_softc *sc) 5471 { 5472 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 5473 const char *sep = ""; 5474 5475 #define ADD(m, c) ifmedia_add(&sc->sc_mii.mii_media, (m), (c), NULL) 5476 #define PRINT(str) aprint_normal("%s%s", sep, str); sep = ", " 5477 5478 sc->sc_mii.mii_ifp = ifp; 5479 sc->sc_mii.mii_readreg = tlp_pnic_mii_readreg; 5480 sc->sc_mii.mii_writereg = tlp_pnic_mii_writereg; 5481 sc->sc_mii.mii_statchg = sc->sc_statchg; 5482 ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange, 5483 tlp_mediastatus); 5484 mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY, 5485 MII_OFFSET_ANY, 0); 5486 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) { 5487 /* XXX What about AUI/BNC support? */ 5488 aprint_normal_dev(sc->sc_dev, ""); 5489 5490 tlp_pnic_nway_reset(sc); 5491 5492 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0), 5493 PNIC_NWAY_TW|PNIC_NWAY_CAP10T); 5494 PRINT("10baseT"); 5495 5496 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0), 5497 PNIC_NWAY_TW|PNIC_NWAY_FD|PNIC_NWAY_CAP10TFDX); 5498 PRINT("10baseT-FDX"); 5499 5500 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, 0), 5501 PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_CAP100TX); 5502 PRINT("100baseTX"); 5503 5504 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, 0), 5505 PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_FD| 5506 PNIC_NWAY_CAP100TXFDX); 5507 PRINT("100baseTX-FDX"); 5508 5509 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0), 5510 PNIC_NWAY_TW|PNIC_NWAY_RN|PNIC_NWAY_NW| 5511 PNIC_NWAY_CAP10T|PNIC_NWAY_CAP10TFDX| 5512 PNIC_NWAY_CAP100TXFDX|PNIC_NWAY_CAP100TX); 5513 PRINT("auto"); 5514 5515 aprint_normal("\n"); 5516 5517 sc->sc_statchg = tlp_pnic_nway_statchg; 5518 sc->sc_tick = tlp_pnic_nway_tick; 5519 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO); 5520 } else { 5521 sc->sc_flags |= TULIPF_HAS_MII; 5522 sc->sc_tick = tlp_mii_tick; 5523 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO); 5524 } 5525 5526 #undef ADD 5527 #undef PRINT 5528 } 5529 5530 static void 5531 tlp_pnic_tmsw_get(struct tulip_softc *sc, struct ifmediareq *ifmr) 5532 { 5533 struct mii_data *mii = &sc->sc_mii; 5534 5535 if (sc->sc_flags & TULIPF_HAS_MII) 5536 tlp_mii_getmedia(sc, ifmr); 5537 else { 5538 mii->mii_media_status = 0; 5539 mii->mii_media_active = IFM_NONE; 5540 tlp_pnic_nway_service(sc, MII_POLLSTAT); 5541 ifmr->ifm_status = sc->sc_mii.mii_media_status; 5542 ifmr->ifm_active = sc->sc_mii.mii_media_active; 5543 } 5544 } 5545 5546 static int 5547 tlp_pnic_tmsw_set(struct tulip_softc *sc) 5548 { 5549 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 5550 struct mii_data *mii = &sc->sc_mii; 5551 5552 if (sc->sc_flags & TULIPF_HAS_MII) { 5553 /* 5554 * Make sure the built-in Tx jabber timer is disabled. 5555 */ 5556 TULIP_WRITE(sc, CSR_PNIC_ENDEC, PNIC_ENDEC_JDIS); 5557 5558 return (tlp_mii_setmedia(sc)); 5559 } 5560 5561 if (ifp->if_flags & IFF_UP) { 5562 mii->mii_media_status = 0; 5563 mii->mii_media_active = IFM_NONE; 5564 return (tlp_pnic_nway_service(sc, MII_MEDIACHG)); 5565 } 5566 5567 return (0); 5568 } 5569 5570 static void 5571 tlp_pnic_nway_statchg(device_t self) 5572 { 5573 struct tulip_softc *sc = device_private(self); 5574 5575 /* Idle the transmit and receive processes. */ 5576 tlp_idle(sc, OPMODE_ST|OPMODE_SR); 5577 5578 sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_PS|OPMODE_PCS| 5579 OPMODE_SCR|OPMODE_HBD); 5580 5581 if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T) { 5582 sc->sc_opmode |= OPMODE_TTM; 5583 TULIP_WRITE(sc, CSR_GPP, 5584 GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 0) | 5585 GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1)); 5586 } else { 5587 sc->sc_opmode |= OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD; 5588 TULIP_WRITE(sc, CSR_GPP, 5589 GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 1) | 5590 GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1)); 5591 } 5592 5593 if (sc->sc_mii.mii_media_active & IFM_FDX) 5594 sc->sc_opmode |= OPMODE_FD|OPMODE_HBD; 5595 5596 /* 5597 * Write new OPMODE bits. This also restarts the transmit 5598 * and receive processes. 5599 */ 5600 TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode); 5601 } 5602 5603 static void 5604 tlp_pnic_nway_tick(void *arg) 5605 { 5606 struct tulip_softc *sc = arg; 5607 int s; 5608 5609 if (!device_is_active(sc->sc_dev)) 5610 return; 5611 5612 s = splnet(); 5613 tlp_pnic_nway_service(sc, MII_TICK); 5614 splx(s); 5615 5616 callout_reset(&sc->sc_tick_callout, hz, tlp_pnic_nway_tick, sc); 5617 } 5618 5619 /* 5620 * Support for the Lite-On PNIC internal NWay block. This is constructed 5621 * somewhat like a PHY driver for simplicity. 5622 */ 5623 5624 static int 5625 tlp_pnic_nway_service(struct tulip_softc *sc, int cmd) 5626 { 5627 struct mii_data *mii = &sc->sc_mii; 5628 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 5629 5630 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 5631 return (0); 5632 5633 switch (cmd) { 5634 case MII_POLLSTAT: 5635 /* Nothing special to do here. */ 5636 break; 5637 5638 case MII_MEDIACHG: 5639 switch (IFM_SUBTYPE(ife->ifm_media)) { 5640 case IFM_AUTO: 5641 (void) tlp_pnic_nway_auto(sc, 1); 5642 break; 5643 case IFM_100_T4: 5644 /* 5645 * XXX Not supported as a manual setting right now. 5646 */ 5647 return (EINVAL); 5648 default: 5649 /* 5650 * NWAY register data is stored in the ifmedia entry. 5651 */ 5652 TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data); 5653 } 5654 break; 5655 5656 case MII_TICK: 5657 /* 5658 * Only used for autonegotiation. 5659 */ 5660 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) 5661 return (0); 5662 5663 /* 5664 * Check to see if we have link. If we do, we don't 5665 * need to restart the autonegotiation process. 5666 */ 5667 if (sc->sc_flags & TULIPF_LINK_UP) 5668 return (0); 5669 5670 /* 5671 * Only retry autonegotiation every 5 seconds. 5672 */ 5673 if (++sc->sc_nway_ticks != 5) 5674 return (0); 5675 5676 sc->sc_nway_ticks = 0; 5677 tlp_pnic_nway_reset(sc); 5678 if (tlp_pnic_nway_auto(sc, 0) == EJUSTRETURN) 5679 return (0); 5680 break; 5681 } 5682 5683 /* Update the media status. */ 5684 tlp_pnic_nway_status(sc); 5685 5686 /* Callback if something changed. */ 5687 if ((sc->sc_nway_active == NULL || 5688 sc->sc_nway_active->ifm_media != mii->mii_media_active) || 5689 cmd == MII_MEDIACHG) { 5690 (*sc->sc_statchg)(sc->sc_dev); 5691 tlp_nway_activate(sc, mii->mii_media_active); 5692 } 5693 return (0); 5694 } 5695 5696 static void 5697 tlp_pnic_nway_reset(struct tulip_softc *sc) 5698 { 5699 5700 TULIP_WRITE(sc, CSR_PNIC_NWAY, PNIC_NWAY_RS); 5701 delay(100); 5702 TULIP_WRITE(sc, CSR_PNIC_NWAY, 0); 5703 } 5704 5705 static int 5706 tlp_pnic_nway_auto(struct tulip_softc *sc, int waitfor) 5707 { 5708 struct mii_data *mii = &sc->sc_mii; 5709 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 5710 uint32_t reg; 5711 int i; 5712 5713 if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0) 5714 TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data); 5715 5716 if (waitfor) { 5717 /* Wait 500ms for it to complete. */ 5718 for (i = 0; i < 500; i++) { 5719 reg = TULIP_READ(sc, CSR_PNIC_NWAY); 5720 if (reg & PNIC_NWAY_LPAR_MASK) { 5721 tlp_pnic_nway_acomp(sc); 5722 return (0); 5723 } 5724 delay(1000); 5725 } 5726 #if 0 5727 if ((reg & PNIC_NWAY_LPAR_MASK) == 0) 5728 aprint_error_dev(sc->sc_dev, "autonegotiation failed to complete\n"); 5729 #endif 5730 5731 /* 5732 * Don't need to worry about clearing DOINGAUTO. 5733 * If that's set, a timeout is pending, and it will 5734 * clear the flag. 5735 */ 5736 return (EIO); 5737 } 5738 5739 /* 5740 * Just let it finish asynchronously. This is for the benefit of 5741 * the tick handler driving autonegotiation. Don't want 500ms 5742 * delays all the time while the system is running! 5743 */ 5744 if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0) { 5745 sc->sc_flags |= TULIPF_DOINGAUTO; 5746 callout_reset(&sc->sc_nway_callout, hz >> 1, 5747 tlp_pnic_nway_auto_timeout, sc); 5748 } 5749 return (EJUSTRETURN); 5750 } 5751 5752 static void 5753 tlp_pnic_nway_auto_timeout(void *arg) 5754 { 5755 struct tulip_softc *sc = arg; 5756 uint32_t reg; 5757 int s; 5758 5759 s = splnet(); 5760 sc->sc_flags &= ~TULIPF_DOINGAUTO; 5761 reg = TULIP_READ(sc, CSR_PNIC_NWAY); 5762 #if 0 5763 if ((reg & PNIC_NWAY_LPAR_MASK) == 0) 5764 aprint_error_dev(sc->sc_dev, "autonegotiation failed to complete\n"); 5765 #endif 5766 5767 tlp_pnic_nway_acomp(sc); 5768 5769 /* Update the media status. */ 5770 (void) tlp_pnic_nway_service(sc, MII_POLLSTAT); 5771 splx(s); 5772 } 5773 5774 static void 5775 tlp_pnic_nway_status(struct tulip_softc *sc) 5776 { 5777 struct mii_data *mii = &sc->sc_mii; 5778 uint32_t reg; 5779 5780 mii->mii_media_status = IFM_AVALID; 5781 mii->mii_media_active = IFM_ETHER; 5782 5783 reg = TULIP_READ(sc, CSR_PNIC_NWAY); 5784 5785 if (sc->sc_flags & TULIPF_LINK_UP) 5786 mii->mii_media_status |= IFM_ACTIVE; 5787 5788 if (reg & PNIC_NWAY_NW) { 5789 if ((reg & PNIC_NWAY_LPAR_MASK) == 0) { 5790 /* Erg, still trying, I guess... */ 5791 mii->mii_media_active |= IFM_NONE; 5792 return; 5793 } 5794 5795 #if 0 5796 if (reg & PNIC_NWAY_LPAR100T4) 5797 mii->mii_media_active |= IFM_100_T4; 5798 else 5799 #endif 5800 if (reg & PNIC_NWAY_LPAR100TXFDX) 5801 mii->mii_media_active |= IFM_100_TX|IFM_FDX; 5802 else if (reg & PNIC_NWAY_LPAR100TX) 5803 mii->mii_media_active |= IFM_100_TX; 5804 else if (reg & PNIC_NWAY_LPAR10TFDX) 5805 mii->mii_media_active |= IFM_10_T|IFM_FDX; 5806 else if (reg & PNIC_NWAY_LPAR10T) 5807 mii->mii_media_active |= IFM_10_T; 5808 else 5809 mii->mii_media_active |= IFM_NONE; 5810 } else { 5811 if (reg & PNIC_NWAY_100) 5812 mii->mii_media_active |= IFM_100_TX; 5813 else 5814 mii->mii_media_active |= IFM_10_T; 5815 if (reg & PNIC_NWAY_FD) 5816 mii->mii_media_active |= IFM_FDX; 5817 } 5818 } 5819 5820 static void 5821 tlp_pnic_nway_acomp(struct tulip_softc *sc) 5822 { 5823 uint32_t reg; 5824 5825 reg = TULIP_READ(sc, CSR_PNIC_NWAY); 5826 reg &= ~(PNIC_NWAY_FD|PNIC_NWAY_100|PNIC_NWAY_RN); 5827 5828 if (reg & (PNIC_NWAY_LPAR100TXFDX|PNIC_NWAY_LPAR100TX)) 5829 reg |= PNIC_NWAY_100; 5830 if (reg & (PNIC_NWAY_LPAR10TFDX|PNIC_NWAY_LPAR100TXFDX)) 5831 reg |= PNIC_NWAY_FD; 5832 5833 TULIP_WRITE(sc, CSR_PNIC_NWAY, reg); 5834 } 5835 5836 /* 5837 * Macronix PMAC and Lite-On PNIC-II media switch: 5838 * 5839 * MX98713 and MX98713A 21140-like MII or GPIO media. 5840 * 5841 * MX98713A 21143-like MII or SIA/SYM media. 5842 * 5843 * MX98715, MX98715A, MX98725, 21143-like SIA/SYM media. 5844 * 82C115, MX98715AEC-C, -E 5845 * 5846 * So, what we do here is fake MII-on-SIO or ISV media info, and 5847 * use the ISV media switch get/set functions to handle the rest. 5848 */ 5849 5850 static void tlp_pmac_tmsw_init(struct tulip_softc *); 5851 5852 const struct tulip_mediasw tlp_pmac_mediasw = { 5853 tlp_pmac_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set 5854 }; 5855 5856 const struct tulip_mediasw tlp_pmac_mii_mediasw = { 5857 tlp_pmac_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia 5858 }; 5859 5860 static void 5861 tlp_pmac_tmsw_init(struct tulip_softc *sc) 5862 { 5863 static const uint8_t media[] = { 5864 TULIP_ROM_MB_MEDIA_TP, 5865 TULIP_ROM_MB_MEDIA_TP_FDX, 5866 TULIP_ROM_MB_MEDIA_100TX, 5867 TULIP_ROM_MB_MEDIA_100TX_FDX, 5868 }; 5869 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 5870 struct tulip_21x4x_media *tm; 5871 5872 sc->sc_mii.mii_ifp = ifp; 5873 sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg; 5874 sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg; 5875 sc->sc_mii.mii_statchg = sc->sc_statchg; 5876 ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange, 5877 tlp_mediastatus); 5878 if (sc->sc_chip == TULIP_CHIP_MX98713 || 5879 sc->sc_chip == TULIP_CHIP_MX98713A) { 5880 mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, 5881 MII_PHY_ANY, MII_OFFSET_ANY, 0); 5882 if (LIST_FIRST(&sc->sc_mii.mii_phys) != NULL) { 5883 sc->sc_flags |= TULIPF_HAS_MII; 5884 sc->sc_tick = tlp_mii_tick; 5885 sc->sc_preinit = tlp_2114x_mii_preinit; 5886 sc->sc_mediasw = &tlp_pmac_mii_mediasw; 5887 ifmedia_set(&sc->sc_mii.mii_media, 5888 IFM_ETHER|IFM_AUTO); 5889 return; 5890 } 5891 } 5892 5893 switch (sc->sc_chip) { 5894 case TULIP_CHIP_MX98713: 5895 tlp_add_srom_media(sc, TULIP_ROM_MB_21140_GPR, 5896 tlp_21140_gpio_get, tlp_21140_gpio_set, media, 4); 5897 5898 /* 5899 * XXX Should implement auto-sense for this someday, 5900 * XXX when we do the same for the 21140. 5901 */ 5902 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T); 5903 break; 5904 5905 default: 5906 tlp_add_srom_media(sc, TULIP_ROM_MB_21142_SIA, 5907 tlp_sia_get, tlp_sia_set, media, 2); 5908 tlp_add_srom_media(sc, TULIP_ROM_MB_21143_SYM, 5909 tlp_sia_get, tlp_sia_set, media + 2, 2); 5910 5911 tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO); 5912 tm->tm_name = "auto"; 5913 tm->tm_get = tlp_2114x_nway_get; 5914 tm->tm_set = tlp_2114x_nway_set; 5915 ifmedia_add(&sc->sc_mii.mii_media, 5916 IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0), 0, tm); 5917 5918 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO); 5919 sc->sc_statchg = tlp_2114x_nway_statchg; 5920 sc->sc_tick = tlp_2114x_nway_tick; 5921 break; 5922 } 5923 5924 tlp_print_media(sc); 5925 tlp_sia_fixup(sc); 5926 5927 /* Set the LED modes. */ 5928 tlp_pmac_reset(sc); 5929 5930 sc->sc_reset = tlp_pmac_reset; 5931 } 5932 5933 /* 5934 * ADMtek AL981 media switch. Only has internal PHY. 5935 */ 5936 static void tlp_al981_tmsw_init(struct tulip_softc *); 5937 5938 const struct tulip_mediasw tlp_al981_mediasw = { 5939 tlp_al981_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia 5940 }; 5941 5942 static void 5943 tlp_al981_tmsw_init(struct tulip_softc *sc) 5944 { 5945 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 5946 5947 sc->sc_mii.mii_ifp = ifp; 5948 sc->sc_mii.mii_readreg = tlp_al981_mii_readreg; 5949 sc->sc_mii.mii_writereg = tlp_al981_mii_writereg; 5950 sc->sc_mii.mii_statchg = sc->sc_statchg; 5951 ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange, 5952 tlp_mediastatus); 5953 mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY, 5954 MII_OFFSET_ANY, 0); 5955 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) { 5956 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL); 5957 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE); 5958 } else { 5959 sc->sc_flags |= TULIPF_HAS_MII; 5960 sc->sc_tick = tlp_mii_tick; 5961 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO); 5962 } 5963 } 5964 5965 /* 5966 * ADMtek AN983/985 media switch. Only has internal PHY, but 5967 * on an SIO-like interface. Unfortunately, we can't use the 5968 * standard SIO media switch, because the AN985 "ghosts" the 5969 * singly PHY at every address. 5970 */ 5971 static void tlp_an985_tmsw_init(struct tulip_softc *); 5972 5973 const struct tulip_mediasw tlp_an985_mediasw = { 5974 tlp_an985_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia 5975 }; 5976 5977 static void 5978 tlp_an985_tmsw_init(struct tulip_softc *sc) 5979 { 5980 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 5981 5982 sc->sc_mii.mii_ifp = ifp; 5983 sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg; 5984 sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg; 5985 sc->sc_mii.mii_statchg = sc->sc_statchg; 5986 ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange, 5987 tlp_mediastatus); 5988 mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, 1, 5989 MII_OFFSET_ANY, 0); 5990 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) { 5991 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL); 5992 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE); 5993 } else { 5994 sc->sc_flags |= TULIPF_HAS_MII; 5995 sc->sc_tick = tlp_mii_tick; 5996 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO); 5997 } 5998 } 5999 6000 /* 6001 * Davicom DM9102 media switch. Internal PHY and possibly HomePNA. 6002 */ 6003 static void tlp_dm9102_tmsw_init(struct tulip_softc *); 6004 static void tlp_dm9102_tmsw_getmedia(struct tulip_softc *, 6005 struct ifmediareq *); 6006 static int tlp_dm9102_tmsw_setmedia(struct tulip_softc *); 6007 6008 const struct tulip_mediasw tlp_dm9102_mediasw = { 6009 tlp_dm9102_tmsw_init, tlp_dm9102_tmsw_getmedia, 6010 tlp_dm9102_tmsw_setmedia 6011 }; 6012 6013 static void 6014 tlp_dm9102_tmsw_init(struct tulip_softc *sc) 6015 { 6016 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 6017 uint32_t opmode; 6018 6019 sc->sc_mii.mii_ifp = ifp; 6020 sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg; 6021 sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg; 6022 sc->sc_mii.mii_statchg = sc->sc_statchg; 6023 ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange, 6024 tlp_mediastatus); 6025 6026 /* PHY block already reset via tlp_reset(). */ 6027 6028 /* 6029 * Configure OPMODE properly for the internal MII interface. 6030 */ 6031 switch (sc->sc_chip) { 6032 case TULIP_CHIP_DM9102: 6033 opmode = OPMODE_MBO|OPMODE_HBD|OPMODE_PS; 6034 break; 6035 6036 case TULIP_CHIP_DM9102A: 6037 opmode = OPMODE_MBO|OPMODE_HBD; 6038 break; 6039 6040 default: 6041 opmode = 0; 6042 break; 6043 } 6044 6045 TULIP_WRITE(sc, CSR_OPMODE, opmode); 6046 6047 /* Now, probe the internal MII for the internal PHY. */ 6048 mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY, 6049 MII_OFFSET_ANY, 0); 6050 6051 /* 6052 * XXX Figure out what to do about the HomePNA portion 6053 * XXX of the DM9102A. 6054 */ 6055 6056 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) { 6057 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL); 6058 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE); 6059 } else { 6060 sc->sc_flags |= TULIPF_HAS_MII; 6061 sc->sc_tick = tlp_mii_tick; 6062 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO); 6063 } 6064 } 6065 6066 static void 6067 tlp_dm9102_tmsw_getmedia(struct tulip_softc *sc, struct ifmediareq *ifmr) 6068 { 6069 6070 /* XXX HomePNA on DM9102A. */ 6071 tlp_mii_getmedia(sc, ifmr); 6072 } 6073 6074 static int 6075 tlp_dm9102_tmsw_setmedia(struct tulip_softc *sc) 6076 { 6077 6078 /* XXX HomePNA on DM9102A. */ 6079 return (tlp_mii_setmedia(sc)); 6080 } 6081 6082 /* 6083 * ASIX AX88140A/AX88141 media switch. Internal PHY or MII. 6084 */ 6085 6086 static void tlp_asix_tmsw_init(struct tulip_softc *); 6087 static void tlp_asix_tmsw_getmedia(struct tulip_softc *, 6088 struct ifmediareq *); 6089 static int tlp_asix_tmsw_setmedia(struct tulip_softc *); 6090 6091 const struct tulip_mediasw tlp_asix_mediasw = { 6092 tlp_asix_tmsw_init, tlp_asix_tmsw_getmedia, 6093 tlp_asix_tmsw_setmedia 6094 }; 6095 6096 static void 6097 tlp_asix_tmsw_init(struct tulip_softc *sc) 6098 { 6099 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 6100 uint32_t opmode; 6101 6102 sc->sc_mii.mii_ifp = ifp; 6103 sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg; 6104 sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg; 6105 sc->sc_mii.mii_statchg = sc->sc_statchg; 6106 ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange, 6107 tlp_mediastatus); 6108 6109 /* 6110 * Configure OPMODE properly for the internal MII interface. 6111 */ 6112 switch (sc->sc_chip) { 6113 case TULIP_CHIP_AX88140: 6114 case TULIP_CHIP_AX88141: 6115 opmode = OPMODE_HBD|OPMODE_PS; 6116 break; 6117 default: 6118 opmode = 0; 6119 break; 6120 } 6121 6122 TULIP_WRITE(sc, CSR_OPMODE, opmode); 6123 6124 /* Now, probe the internal MII for the internal PHY. */ 6125 mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY, 6126 MII_OFFSET_ANY, 0); 6127 6128 /* XXX Figure how to handle the PHY. */ 6129 6130 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) { 6131 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL); 6132 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE); 6133 } else { 6134 sc->sc_flags |= TULIPF_HAS_MII; 6135 sc->sc_tick = tlp_mii_tick; 6136 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO); 6137 } 6138 6139 6140 } 6141 6142 static void 6143 tlp_asix_tmsw_getmedia(struct tulip_softc *sc, struct ifmediareq *ifmr) 6144 { 6145 6146 /* XXX PHY handling. */ 6147 tlp_mii_getmedia(sc, ifmr); 6148 } 6149 6150 static int 6151 tlp_asix_tmsw_setmedia(struct tulip_softc *sc) 6152 { 6153 6154 /* XXX PHY handling. */ 6155 return (tlp_mii_setmedia(sc)); 6156 } 6157 6158 /* 6159 * RS7112 media switch. Handles only MII attached to the SIO. 6160 * We only have a PHY at 1. 6161 */ 6162 void tlp_rs7112_tmsw_init(struct tulip_softc *); 6163 6164 const struct tulip_mediasw tlp_rs7112_mediasw = { 6165 tlp_rs7112_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia 6166 }; 6167 6168 void 6169 tlp_rs7112_tmsw_init(struct tulip_softc *sc) 6170 { 6171 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 6172 6173 /* 6174 * We don't attach any media info structures to the ifmedia 6175 * entries, so if we're using a pre-init function that needs 6176 * that info, override it to one that doesn't. 6177 */ 6178 if (sc->sc_preinit == tlp_2114x_preinit) 6179 sc->sc_preinit = tlp_2114x_mii_preinit; 6180 6181 sc->sc_mii.mii_ifp = ifp; 6182 sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg; 6183 sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg; 6184 sc->sc_mii.mii_statchg = sc->sc_statchg; 6185 ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange, 6186 tlp_mediastatus); 6187 6188 /* 6189 * The RS7112 reports a PHY at 0 (possibly HomePNA?) 6190 * and 1 (ethernet). We attach ethernet only. 6191 */ 6192 mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, 1, 6193 MII_OFFSET_ANY, 0); 6194 6195 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) { 6196 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL); 6197 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE); 6198 } else { 6199 sc->sc_flags |= TULIPF_HAS_MII; 6200 sc->sc_tick = tlp_mii_tick; 6201 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO); 6202 } 6203 } 6204 6205 const char * 6206 tlp_chip_name(tulip_chip_t t) { 6207 if ((int)t < 0 || (int)t >= __arraycount(tlp_chip_names)) { 6208 static char buf[256]; 6209 (void)snprintf(buf, sizeof(buf), "[unknown 0x%x]", t); 6210 return buf; 6211 } 6212 return tlp_chip_names[t]; 6213 } 6214