1 /* $NetBSD: mii_physubr.c,v 1.103 2023/05/22 02:12:13 msaitoh Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999, 2000, 2001 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. 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 * Subroutines common to all PHYs. 35 */ 36 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: mii_physubr.c,v 1.103 2023/05/22 02:12:13 msaitoh Exp $"); 39 40 #include <sys/param.h> 41 #include <sys/device.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/socket.h> 45 #include <sys/errno.h> 46 #include <sys/module.h> 47 #include <sys/module_hook.h> 48 #include <sys/proc.h> 49 50 #include <net/if.h> 51 #include <net/if_media.h> 52 #include <net/route.h> 53 54 #include <dev/dev_verbose.h> 55 56 #include <dev/mii/mii.h> 57 #include <dev/mii/miidevs.h> 58 #include <dev/mii/miivar.h> 59 60 DEV_VERBOSE_DEFINE(mii); 61 62 const char * 63 mii_get_descr(char *descr, size_t len, uint32_t oui, uint32_t model) 64 { 65 char temp[MII_MAX_DESCR_LEN]; 66 67 mii_load_verbose(); 68 if (miiverbose_loaded) { 69 if (mii_findvendor(temp, sizeof(temp), oui) == NULL) { 70 descr[0] = '\0'; 71 return NULL; 72 } 73 strlcpy(descr, temp, len); 74 strlcat(descr, " ", len); 75 if (mii_findproduct(temp, sizeof(temp), oui, model) == NULL) { 76 descr[0] = '\0'; 77 return NULL; 78 } 79 strlcat(descr, temp, len); 80 return descr; 81 } 82 snprintf(descr, len, "OUI 0x%06x model 0x%04x", oui, model); 83 return NULL; 84 } 85 86 static void mii_phy_statusmsg(struct mii_softc *); 87 88 /* 89 * Media to register setting conversion table. Order matters. 90 */ 91 static const struct mii_media mii_media_table[MII_NMEDIA] = { 92 /* None */ 93 { BMCR_ISO, ANAR_CSMA, 94 0, }, 95 96 /* 10baseT */ 97 { BMCR_S10, ANAR_CSMA | ANAR_10, 98 0, }, 99 100 /* 10baseT-FDX */ 101 { BMCR_S10|BMCR_FDX, ANAR_CSMA | ANAR_10_FD, 102 0, }, 103 104 /* 100baseT4 */ 105 { BMCR_S100, ANAR_CSMA | ANAR_T4, 106 0, }, 107 108 /* 100baseTX */ 109 { BMCR_S100, ANAR_CSMA | ANAR_TX, 110 0, }, 111 112 /* 100baseTX-FDX */ 113 { BMCR_S100|BMCR_FDX, ANAR_CSMA | ANAR_TX_FD, 114 0, }, 115 116 /* 1000baseX */ 117 { BMCR_S1000, ANAR_CSMA, 118 0, }, 119 120 /* 1000baseX-FDX */ 121 { BMCR_S1000|BMCR_FDX, ANAR_CSMA, 122 0, }, 123 124 /* 1000baseT */ 125 { BMCR_S1000, ANAR_CSMA, 126 GTCR_ADV_1000THDX }, 127 128 /* 1000baseT-FDX */ 129 { BMCR_S1000, ANAR_CSMA, 130 GTCR_ADV_1000TFDX }, 131 }; 132 133 static void mii_phy_auto_timeout(void *); 134 static void mii_phy_auto_timeout_locked(struct mii_softc *); 135 136 void 137 mii_phy_setmedia(struct mii_softc *sc) 138 { 139 struct mii_data *mii = sc->mii_pdata; 140 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 141 u_int subtype; 142 uint16_t bmcr, anar, gtcr; 143 144 KASSERT(mii_locked(mii)); 145 146 subtype = IFM_SUBTYPE(ife->ifm_media); 147 if ((subtype == IFM_AUTO) || (subtype == IFM_1000_T)) { 148 (void) mii_phy_auto(sc); 149 return; 150 } 151 152 /* Table index is stored in the media entry. */ 153 154 #ifdef DIAGNOSTIC 155 if (/* ife->ifm_data < 0 || */ ife->ifm_data >= MII_NMEDIA) 156 panic("mii_phy_setmedia"); 157 #endif 158 159 anar = mii_media_table[ife->ifm_data].mm_anar; 160 bmcr = mii_media_table[ife->ifm_data].mm_bmcr; 161 gtcr = mii_media_table[ife->ifm_data].mm_gtcr; 162 163 if (mii->mii_media.ifm_media & IFM_ETH_MASTER) { 164 switch (subtype) { 165 case IFM_1000_T: 166 gtcr |= GTCR_MAN_MS | GTCR_ADV_MS; 167 break; 168 169 default: 170 panic("mii_phy_setmedia: MASTER on wrong media"); 171 } 172 } 173 174 if (mii->mii_media.ifm_media & IFM_FLOW) { 175 if (sc->mii_flags & MIIF_IS_1000X) 176 anar |= ANAR_X_PAUSE_SYM | ANAR_X_PAUSE_ASYM; 177 else { 178 anar |= ANAR_FC; 179 /* XXX Only 1000BASE-T has PAUSE_ASYM? */ 180 if ((sc->mii_flags & MIIF_HAVE_GTCR) && 181 (sc->mii_extcapabilities & 182 (EXTSR_1000THDX | EXTSR_1000TFDX))) 183 anar |= ANAR_PAUSE_ASYM; 184 } 185 } 186 187 if (ife->ifm_media & IFM_LOOP) 188 bmcr |= BMCR_LOOP; 189 190 PHY_WRITE(sc, MII_ANAR, anar); 191 if (sc->mii_flags & MIIF_HAVE_GTCR) 192 PHY_WRITE(sc, MII_100T2CR, gtcr); 193 PHY_WRITE(sc, MII_BMCR, bmcr); 194 } 195 196 /* Setup autonegotiation and start it. */ 197 int 198 mii_phy_auto(struct mii_softc *sc) 199 { 200 struct mii_data *mii = sc->mii_pdata; 201 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 202 203 KASSERT(mii_locked(mii)); 204 205 sc->mii_ticks = 0; 206 if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) { 207 /* 208 * Check for 1000BASE-X. Autonegotiation is a bit 209 * different on such devices. 210 */ 211 if (sc->mii_flags & MIIF_IS_1000X) { 212 uint16_t anar = 0; 213 214 if (sc->mii_extcapabilities & EXTSR_1000XFDX) 215 anar |= ANAR_X_FD; 216 if (sc->mii_extcapabilities & EXTSR_1000XHDX) 217 anar |= ANAR_X_HD; 218 219 if (sc->mii_flags & MIIF_DOPAUSE) { 220 /* XXX Asymmetric vs. symmetric? */ 221 anar |= ANLPAR_X_PAUSE_TOWARDS; 222 } 223 224 PHY_WRITE(sc, MII_ANAR, anar); 225 } else { 226 uint16_t anar; 227 228 anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | 229 ANAR_CSMA; 230 if (sc->mii_flags & MIIF_DOPAUSE) { 231 anar |= ANAR_FC; 232 /* XXX Only 1000BASE-T has PAUSE_ASYM? */ 233 if ((sc->mii_flags & MIIF_HAVE_GTCR) && 234 (sc->mii_extcapabilities & 235 (EXTSR_1000THDX | EXTSR_1000TFDX))) 236 anar |= ANAR_PAUSE_ASYM; 237 } 238 239 /* 240 * For 1000-base-T, autonegotiation must be enabled, 241 * but if we're not set to auto, only advertise 242 * 1000-base-T with the link partner. 243 */ 244 if (IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T) { 245 anar &= ~(ANAR_T4 | ANAR_TX_FD | ANAR_TX | 246 ANAR_10_FD | ANAR_10); 247 } 248 249 PHY_WRITE(sc, MII_ANAR, anar); 250 if (sc->mii_flags & MIIF_HAVE_GTCR) { 251 uint16_t gtcr = 0; 252 253 if (sc->mii_extcapabilities & EXTSR_1000TFDX) 254 gtcr |= GTCR_ADV_1000TFDX; 255 if (sc->mii_extcapabilities & EXTSR_1000THDX) 256 gtcr |= GTCR_ADV_1000THDX; 257 258 PHY_WRITE(sc, MII_100T2CR, gtcr); 259 } 260 } 261 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG); 262 } 263 264 /* 265 * Just let it finish asynchronously. This is for the benefit of 266 * the tick handler driving autonegotiation. Don't want 500ms 267 * delays all the time while the system is running! 268 */ 269 if (sc->mii_flags & MIIF_AUTOTSLEEP) { 270 ASSERT_SLEEPABLE(); 271 sc->mii_flags |= MIIF_DOINGAUTO; 272 kpause("miiaut", false, hz >> 1, mii->mii_media.ifm_lock); 273 mii_phy_auto_timeout_locked(sc); 274 KASSERT((sc->mii_flags & MIIF_DOINGAUTO) == 0); 275 cv_broadcast(&sc->mii_nway_cv); 276 } else if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) { 277 sc->mii_flags |= MIIF_DOINGAUTO; 278 callout_reset(&sc->mii_nway_ch, hz >> 1, 279 mii_phy_auto_timeout, sc); 280 } 281 return EJUSTRETURN; 282 } 283 284 /* Just restart autonegotiation without changing any setting */ 285 int 286 mii_phy_auto_restart(struct mii_softc *sc) 287 { 288 uint16_t reg; 289 290 PHY_READ(sc, MII_BMCR, ®); 291 reg |= BMCR_STARTNEG; 292 PHY_WRITE(sc, MII_BMCR, reg); 293 sc->mii_ticks = 0; 294 295 return EJUSTRETURN; 296 } 297 298 static void 299 mii_phy_auto_timeout_locked(struct mii_softc *sc) 300 { 301 302 KASSERT(mii_locked(sc->mii_pdata)); 303 KASSERT(sc->mii_flags & MIIF_DOINGAUTO); 304 305 if (!device_is_active(sc->mii_dev)) 306 return; 307 308 sc->mii_flags &= ~MIIF_DOINGAUTO; 309 310 /* Update the media status. */ 311 (void) PHY_SERVICE(sc, sc->mii_pdata, MII_POLLSTAT); 312 } 313 314 static void 315 mii_phy_auto_timeout(void *arg) 316 { 317 struct mii_softc *sc = arg; 318 319 KASSERT((sc->mii_flags & MIIF_AUTOTSLEEP) == 0); 320 321 if (!device_is_active(sc->mii_dev)) 322 return; 323 324 mii_lock(sc->mii_pdata); 325 mii_phy_auto_timeout_locked(sc); 326 mii_unlock(sc->mii_pdata); 327 } 328 329 int 330 mii_phy_tick(struct mii_softc *sc) 331 { 332 struct mii_data *mii = sc->mii_pdata; 333 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 334 uint16_t reg; 335 336 KASSERT(mii_locked(mii)); 337 338 /* Just bail now if the interface is down. */ 339 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 340 return EJUSTRETURN; 341 342 /* 343 * If we're not doing autonegotiation, we don't need to do any extra 344 * work here. However, we need to check the link status so we can 345 * generate an announcement by returning with 0 if the status changes. 346 */ 347 if ((IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) && 348 (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)) { 349 /* 350 * Reset autonegotiation timer to 0 just to make sure 351 * the future autonegotiation start with 0. 352 */ 353 sc->mii_ticks = 0; 354 return 0; 355 } 356 357 /* Read the status register twice; BMSR_LINK is latch-low. */ 358 PHY_READ(sc, MII_BMSR, ®); 359 PHY_READ(sc, MII_BMSR, ®); 360 if (reg & BMSR_LINK) { 361 /* 362 * Reset autonegotiation timer to 0 in case the link 363 * goes down in the next tick. 364 */ 365 sc->mii_ticks = 0; 366 /* See above. */ 367 return 0; 368 } 369 370 /* 371 * mii_ticks == 0 means it's the first tick after changing the media or 372 * the link became down since the last tick (see above), so return with 373 * 0 to update the status. 374 */ 375 if (sc->mii_ticks++ == 0) 376 return 0; 377 378 /* 379 * Only retry autonegotiation every N seconds. 380 */ 381 KASSERT(sc->mii_anegticks != 0); 382 if (sc->mii_ticks < sc->mii_anegticks) 383 return EJUSTRETURN; 384 385 if (mii_phy_auto_restart(sc) == EJUSTRETURN) 386 return EJUSTRETURN; 387 388 /* 389 * Might need to generate a status message if autonegotiation 390 * failed. 391 */ 392 return 0; 393 } 394 395 void 396 mii_phy_reset(struct mii_softc *sc) 397 { 398 int i; 399 uint16_t reg; 400 401 KASSERT(mii_locked(sc->mii_pdata)); 402 403 if (sc->mii_flags & MIIF_NOISOLATE) 404 reg = BMCR_RESET; 405 else 406 reg = BMCR_RESET | BMCR_ISO; 407 PHY_WRITE(sc, MII_BMCR, reg); 408 409 /* Wait another 500ms for it to complete. */ 410 for (i = 0; i < 500; i++) { 411 PHY_READ(sc, MII_BMCR, ®); 412 if ((reg & BMCR_RESET) == 0) 413 break; 414 delay(1000); 415 } 416 417 if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0)) 418 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO); 419 } 420 421 void 422 mii_phy_down(struct mii_softc *sc) 423 { 424 425 KASSERT(mii_locked(sc->mii_pdata)); 426 427 if (sc->mii_flags & MIIF_AUTOTSLEEP) { 428 while (sc->mii_flags & MIIF_DOINGAUTO) { 429 cv_wait(&sc->mii_nway_cv, 430 sc->mii_pdata->mii_media.ifm_lock); 431 } 432 } else { 433 if ((sc->mii_flags & MIIF_DOINGAUTO) != 0 && 434 callout_halt(&sc->mii_nway_ch, 435 sc->mii_pdata->mii_media.ifm_lock) == 0) { 436 /* 437 * The callout was scheduled, and we prevented 438 * it from running before it expired, so we are 439 * now responsible for clearing the flag. 440 */ 441 sc->mii_flags &= ~MIIF_DOINGAUTO; 442 } 443 } 444 KASSERT((sc->mii_flags & MIIF_DOINGAUTO) == 0); 445 } 446 447 void 448 mii_phy_status(struct mii_softc *sc) 449 { 450 451 KASSERT(mii_locked(sc->mii_pdata)); 452 PHY_STATUS(sc); 453 } 454 455 void 456 mii_phy_update(struct mii_softc *sc, int cmd) 457 { 458 struct mii_data *mii = sc->mii_pdata; 459 u_int mii_media_active; 460 int mii_media_status; 461 462 KASSERT(mii_locked(mii)); 463 464 mii_media_active = mii->mii_media_active; 465 mii_media_status = mii->mii_media_status; 466 467 if (sc->mii_media_active != mii_media_active || 468 sc->mii_media_status != mii_media_status || 469 cmd == MII_MEDIACHG) { 470 mii_phy_statusmsg(sc); 471 (*mii->mii_statchg)(mii->mii_ifp); 472 sc->mii_media_active = mii_media_active; 473 sc->mii_media_status = mii_media_status; 474 } 475 } 476 477 static void 478 mii_phy_statusmsg(struct mii_softc *sc) 479 { 480 struct mii_data *mii = sc->mii_pdata; 481 struct ifnet *ifp = mii->mii_ifp; 482 483 KASSERT(mii_locked(mii)); 484 485 if (mii->mii_media_status & IFM_AVALID) { 486 if (mii->mii_media_status & IFM_ACTIVE) 487 if_link_state_change(ifp, LINK_STATE_UP); 488 else 489 if_link_state_change(ifp, LINK_STATE_DOWN); 490 } else 491 if_link_state_change(ifp, LINK_STATE_UNKNOWN); 492 493 /* XXX NET_MPSAFE */ 494 ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active); 495 } 496 497 /* 498 * Initialize generic PHY media based on BMSR, called when a PHY is 499 * attached. We expect to be set up to print a comma-separated list 500 * of media names. Does not print a newline. 501 */ 502 void 503 mii_phy_add_media(struct mii_softc *sc) 504 { 505 struct mii_data *mii = sc->mii_pdata; 506 device_t self = sc->mii_dev; 507 const char *sep = ""; 508 int fdx = 0; 509 510 aprint_normal_dev(self, ""); 511 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 && 512 (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0) { 513 aprint_error("no media present\n"); 514 goto out; 515 } 516 517 /* 518 * Set the autonegotiation timer for 10/100 media. Gigabit media is 519 * handled below. 520 */ 521 mii_lock(mii); 522 sc->mii_anegticks = MII_ANEGTICKS; 523 mii_unlock(mii); 524 525 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL) 526 #define PRINT(n) aprint_normal("%s%s", sep, (n)); sep = ", " 527 528 /* This flag is static; no need to lock. */ 529 if ((sc->mii_flags & MIIF_NOISOLATE) == 0) 530 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst), 531 MII_MEDIA_NONE); 532 533 /* 534 * There are different interpretations for the bits in 535 * HomePNA PHYs. And there is really only one media type 536 * that is supported. This flag is also static, and so 537 * no need to lock. 538 */ 539 if (sc->mii_flags & MIIF_IS_HPNA) { 540 if (sc->mii_capabilities & BMSR_10THDX) { 541 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0, 542 sc->mii_inst), 543 MII_MEDIA_10_T); 544 PRINT("HomePNA1"); 545 } 546 goto out; 547 } 548 549 if (sc->mii_capabilities & BMSR_10THDX) { 550 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst), 551 MII_MEDIA_10_T); 552 PRINT("10baseT"); 553 } 554 if (sc->mii_capabilities & BMSR_10TFDX) { 555 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst), 556 MII_MEDIA_10_T_FDX); 557 PRINT("10baseT-FDX"); 558 fdx = 1; 559 } 560 if (sc->mii_capabilities & BMSR_100TXHDX) { 561 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst), 562 MII_MEDIA_100_TX); 563 PRINT("100baseTX"); 564 } 565 if (sc->mii_capabilities & BMSR_100TXFDX) { 566 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst), 567 MII_MEDIA_100_TX_FDX); 568 PRINT("100baseTX-FDX"); 569 fdx = 1; 570 } 571 if (sc->mii_capabilities & BMSR_100T4) { 572 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst), 573 MII_MEDIA_100_T4); 574 PRINT("100baseT4"); 575 } 576 577 if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) { 578 /* 579 * XXX Right now only handle 1000SX and 1000TX. Need 580 * XXX to handle 1000LX and 1000CX some how. 581 * 582 * Note since it can take 5 seconds to auto-negotiate 583 * a gigabit link, we make anegticks 10 seconds for 584 * all the gigabit media types. 585 */ 586 if (sc->mii_extcapabilities & EXTSR_1000XHDX) { 587 mii_lock(mii); 588 sc->mii_anegticks = MII_ANEGTICKS_GIGE; 589 sc->mii_flags |= MIIF_IS_1000X; 590 mii_unlock(mii); 591 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0, 592 sc->mii_inst), MII_MEDIA_1000_X); 593 PRINT("1000baseSX"); 594 } 595 if (sc->mii_extcapabilities & EXTSR_1000XFDX) { 596 mii_lock(mii); 597 sc->mii_anegticks = MII_ANEGTICKS_GIGE; 598 sc->mii_flags |= MIIF_IS_1000X; 599 mii_unlock(mii); 600 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX, 601 sc->mii_inst), MII_MEDIA_1000_X_FDX); 602 PRINT("1000baseSX-FDX"); 603 fdx = 1; 604 } 605 606 /* 607 * 1000baseT media needs to be able to manipulate 608 * master/slave mode. We set IFM_ETH_MASTER in 609 * the "don't care mask" and filter it out when 610 * the media is set. 611 * 612 * All 1000baseT PHYs have a 1000baseT control register. 613 */ 614 if (sc->mii_extcapabilities & EXTSR_1000THDX) { 615 mii_lock(mii); 616 sc->mii_anegticks = MII_ANEGTICKS_GIGE; 617 sc->mii_flags |= MIIF_HAVE_GTCR; 618 mii->mii_media.ifm_mask |= IFM_ETH_MASTER; 619 mii_unlock(mii); 620 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0, 621 sc->mii_inst), MII_MEDIA_1000_T); 622 PRINT("1000baseT"); 623 } 624 if (sc->mii_extcapabilities & EXTSR_1000TFDX) { 625 mii_lock(mii); 626 sc->mii_anegticks = MII_ANEGTICKS_GIGE; 627 sc->mii_flags |= MIIF_HAVE_GTCR; 628 mii->mii_media.ifm_mask |= IFM_ETH_MASTER; 629 mii_unlock(mii); 630 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX, 631 sc->mii_inst), MII_MEDIA_1000_T_FDX); 632 PRINT("1000baseT-FDX"); 633 fdx = 1; 634 } 635 } 636 637 if (sc->mii_capabilities & BMSR_ANEG) { 638 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 639 MII_NMEDIA); /* intentionally invalid index */ 640 PRINT("auto"); 641 } 642 #undef ADD 643 #undef PRINT 644 /* This flag is static; no need to lock. */ 645 if (fdx != 0 && (sc->mii_flags & MIIF_DOPAUSE)) { 646 mii_lock(mii); 647 mii->mii_media.ifm_mask |= IFM_ETH_FMASK; 648 mii_unlock(mii); 649 } 650 out: 651 aprint_normal("\n"); 652 if (!pmf_device_register(self, NULL, mii_phy_resume)) { 653 aprint_error_dev(self, "couldn't establish power handler\n"); 654 } 655 } 656 657 void 658 mii_phy_delete_media(struct mii_softc *sc) 659 { 660 struct mii_data *mii = sc->mii_pdata; 661 662 ifmedia_delete_instance(&mii->mii_media, sc->mii_inst); 663 } 664 665 int 666 mii_phy_activate(device_t self, enum devact act) 667 { 668 669 switch (act) { 670 case DVACT_DEACTIVATE: 671 /* XXX Invalidate parent's media setting? */ 672 return 0; 673 default: 674 return EOPNOTSUPP; 675 } 676 } 677 678 /* ARGSUSED1 */ 679 int 680 mii_phy_detach(device_t self, int flags) 681 { 682 struct mii_softc *sc = device_private(self); 683 684 /* No mii_lock because mii_flags should be stable by now. */ 685 KASSERT((sc->mii_flags & MIIF_DOINGAUTO) == 0); 686 687 if (sc->mii_flags & MIIF_AUTOTSLEEP) 688 cv_destroy(&sc->mii_nway_cv); 689 else 690 callout_destroy(&sc->mii_nway_ch); 691 692 mii_phy_delete_media(sc); 693 694 return 0; 695 } 696 697 const struct mii_phydesc * 698 mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd) 699 { 700 701 for (; mpd->mpd_oui != 0; mpd++) { 702 if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui && 703 MII_MODEL(ma->mii_id2) == mpd->mpd_model) 704 return mpd; 705 } 706 return NULL; 707 } 708 709 /* 710 * Return the flow control status flag from MII_ANAR & MII_ANLPAR. 711 */ 712 u_int 713 mii_phy_flowstatus(struct mii_softc *sc) 714 { 715 uint16_t anar, anlpar; 716 717 KASSERT(mii_locked(sc->mii_pdata)); 718 719 if ((sc->mii_flags & MIIF_DOPAUSE) == 0) 720 return 0; 721 722 PHY_READ(sc, MII_ANAR, &anar); 723 PHY_READ(sc, MII_ANLPAR, &anlpar); 724 725 /* For 1000baseX, the bits are in a different location. */ 726 if (sc->mii_flags & MIIF_IS_1000X) { 727 anar <<= 3; 728 anlpar <<= 3; 729 } 730 731 if ((anar & ANAR_PAUSE_SYM) & (anlpar & ANLPAR_PAUSE_SYM)) 732 return (IFM_FLOW | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE); 733 734 if ((anar & ANAR_PAUSE_SYM) == 0) { 735 if ((anar & ANAR_PAUSE_ASYM) && 736 ((anlpar & ANLPAR_PAUSE_TOWARDS) == ANLPAR_PAUSE_TOWARDS)) 737 return (IFM_FLOW | IFM_ETH_TXPAUSE); 738 else 739 return 0; 740 } 741 742 if ((anar & ANAR_PAUSE_ASYM) == 0) { 743 if (anlpar & ANLPAR_PAUSE_SYM) 744 return (IFM_FLOW | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE); 745 else 746 return 0; 747 } 748 749 switch ((anlpar & ANLPAR_PAUSE_TOWARDS)) { 750 case ANLPAR_PAUSE_NONE: 751 return 0; 752 753 case ANLPAR_PAUSE_ASYM: 754 return (IFM_FLOW | IFM_ETH_RXPAUSE); 755 756 default: 757 return (IFM_FLOW | IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE); 758 } 759 /* NOTREACHED */ 760 } 761 762 bool 763 mii_phy_resume(device_t dv, const pmf_qual_t *qual) 764 { 765 struct mii_softc *sc = device_private(dv); 766 767 mii_lock(sc->mii_pdata); 768 PHY_RESET(sc); 769 bool rv = PHY_SERVICE(sc, sc->mii_pdata, MII_MEDIACHG) == 0; 770 mii_unlock(sc->mii_pdata); 771 772 return rv; 773 } 774 775 776 /* 777 * Given an ifmedia_entry, return the corresponding ANAR value. 778 */ 779 uint16_t 780 mii_anar(struct ifmedia_entry *ife) 781 { 782 783 #ifdef DIAGNOSTIC 784 if (ife->ifm_data >= MII_NMEDIA) 785 panic("mii_anar"); 786 #endif 787 788 return mii_media_table[ife->ifm_data].mm_anar; 789 } 790