1 /* $NetBSD: mii.c,v 1.12 1999/08/03 19:41:49 drochner Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD 5 * 6 * Copyright (c) 1998 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 11 * NASA Ames Research Center. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 /* 39 * MII bus layer, glues MII-capable network interface drivers to sharable 40 * PHY drivers. This exports an interface compatible with BSD/OS 3.0's, 41 * plus some NetBSD extensions. 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/socket.h> 47 #include <sys/malloc.h> 48 #include <sys/module.h> 49 #include <sys/bus.h> 50 51 #include <net/if.h> 52 #include <net/if_var.h> 53 #include <net/if_media.h> 54 55 #include <dev/mii/mii.h> 56 #include <dev/mii/miivar.h> 57 58 MODULE_VERSION(miibus, 1); 59 60 #include "miibus_if.h" 61 62 static device_attach_t miibus_attach; 63 static bus_child_location_str_t miibus_child_location_str; 64 static bus_child_pnpinfo_str_t miibus_child_pnpinfo_str; 65 static device_detach_t miibus_detach; 66 static bus_hinted_child_t miibus_hinted_child; 67 static bus_print_child_t miibus_print_child; 68 static device_probe_t miibus_probe; 69 static bus_read_ivar_t miibus_read_ivar; 70 static miibus_readreg_t miibus_readreg; 71 static miibus_statchg_t miibus_statchg; 72 static miibus_writereg_t miibus_writereg; 73 static miibus_linkchg_t miibus_linkchg; 74 static miibus_mediainit_t miibus_mediainit; 75 76 static unsigned char mii_bitreverse(unsigned char x); 77 78 static device_method_t miibus_methods[] = { 79 /* device interface */ 80 DEVMETHOD(device_probe, miibus_probe), 81 DEVMETHOD(device_attach, miibus_attach), 82 DEVMETHOD(device_detach, miibus_detach), 83 DEVMETHOD(device_shutdown, bus_generic_shutdown), 84 85 /* bus interface */ 86 DEVMETHOD(bus_print_child, miibus_print_child), 87 DEVMETHOD(bus_read_ivar, miibus_read_ivar), 88 DEVMETHOD(bus_child_pnpinfo_str, miibus_child_pnpinfo_str), 89 DEVMETHOD(bus_child_location_str, miibus_child_location_str), 90 DEVMETHOD(bus_hinted_child, miibus_hinted_child), 91 92 /* MII interface */ 93 DEVMETHOD(miibus_readreg, miibus_readreg), 94 DEVMETHOD(miibus_writereg, miibus_writereg), 95 DEVMETHOD(miibus_statchg, miibus_statchg), 96 DEVMETHOD(miibus_linkchg, miibus_linkchg), 97 DEVMETHOD(miibus_mediainit, miibus_mediainit), 98 99 DEVMETHOD_END 100 }; 101 102 devclass_t miibus_devclass; 103 104 driver_t miibus_driver = { 105 "miibus", 106 miibus_methods, 107 sizeof(struct mii_data) 108 }; 109 110 struct miibus_ivars { 111 if_t ifp; 112 ifm_change_cb_t ifmedia_upd; 113 ifm_stat_cb_t ifmedia_sts; 114 u_int mii_flags; 115 u_int mii_offset; 116 }; 117 118 static int 119 miibus_probe(device_t dev) 120 { 121 122 device_set_desc(dev, "MII bus"); 123 124 return (BUS_PROBE_SPECIFIC); 125 } 126 127 static int 128 miibus_attach(device_t dev) 129 { 130 struct miibus_ivars *ivars; 131 struct mii_attach_args *ma; 132 struct mii_data *mii; 133 device_t *children; 134 int i, nchildren; 135 136 mii = device_get_softc(dev); 137 if (device_get_children(dev, &children, &nchildren) == 0) { 138 for (i = 0; i < nchildren; i++) { 139 ma = device_get_ivars(children[i]); 140 ma->mii_data = mii; 141 } 142 free(children, M_TEMP); 143 } 144 if (nchildren == 0) { 145 device_printf(dev, "cannot get children\n"); 146 return (ENXIO); 147 } 148 ivars = device_get_ivars(dev); 149 ifmedia_init(&mii->mii_media, IFM_IMASK, ivars->ifmedia_upd, 150 ivars->ifmedia_sts); 151 mii->mii_ifp = ivars->ifp; 152 if_setcapabilitiesbit(mii->mii_ifp, IFCAP_LINKSTATE, 0); 153 if_setcapenablebit(mii->mii_ifp, IFCAP_LINKSTATE, 0); 154 LIST_INIT(&mii->mii_phys); 155 156 return (bus_generic_attach(dev)); 157 } 158 159 static int 160 miibus_detach(device_t dev) 161 { 162 struct mii_data *mii; 163 164 bus_generic_detach(dev); 165 mii = device_get_softc(dev); 166 ifmedia_removeall(&mii->mii_media); 167 mii->mii_ifp = NULL; 168 169 return (0); 170 } 171 172 static int 173 miibus_print_child(device_t dev, device_t child) 174 { 175 struct mii_attach_args *ma; 176 int retval; 177 178 ma = device_get_ivars(child); 179 retval = bus_print_child_header(dev, child); 180 retval += printf(" PHY %d", ma->mii_phyno); 181 retval += bus_print_child_footer(dev, child); 182 183 return (retval); 184 } 185 186 static int 187 miibus_read_ivar(device_t dev, device_t child __unused, int which, 188 uintptr_t *result) 189 { 190 struct miibus_ivars *ivars; 191 192 /* 193 * NB: this uses the instance variables of the miibus rather than 194 * its PHY children. 195 */ 196 ivars = device_get_ivars(dev); 197 switch (which) { 198 case MIIBUS_IVAR_FLAGS: 199 *result = ivars->mii_flags; 200 break; 201 default: 202 return (ENOENT); 203 } 204 return (0); 205 } 206 207 static int 208 miibus_child_pnpinfo_str(device_t dev __unused, device_t child, char *buf, 209 size_t buflen) 210 { 211 struct mii_attach_args *ma; 212 213 ma = device_get_ivars(child); 214 snprintf(buf, buflen, "oui=0x%x model=0x%x rev=0x%x", 215 MII_OUI(ma->mii_id1, ma->mii_id2), 216 MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2)); 217 return (0); 218 } 219 220 static int 221 miibus_child_location_str(device_t dev __unused, device_t child, char *buf, 222 size_t buflen) 223 { 224 struct mii_attach_args *ma; 225 226 ma = device_get_ivars(child); 227 snprintf(buf, buflen, "phyno=%d", ma->mii_phyno); 228 return (0); 229 } 230 231 static void 232 miibus_hinted_child(device_t dev, const char *name, int unit) 233 { 234 struct miibus_ivars *ivars; 235 struct mii_attach_args *args, *ma; 236 device_t *children, phy; 237 int i, nchildren; 238 u_int val; 239 240 if (resource_int_value(name, unit, "phyno", &val) != 0) 241 return; 242 if (device_get_children(dev, &children, &nchildren) != 0) 243 return; 244 ma = NULL; 245 for (i = 0; i < nchildren; i++) { 246 args = device_get_ivars(children[i]); 247 if (args->mii_phyno == val) { 248 ma = args; 249 break; 250 } 251 } 252 free(children, M_TEMP); 253 254 /* 255 * Don't add a PHY that was automatically identified by having media 256 * in its BMSR twice, only allow to alter its attach arguments. 257 */ 258 if (ma == NULL) { 259 ma = malloc(sizeof(struct mii_attach_args), M_DEVBUF, 260 M_NOWAIT); 261 if (ma == NULL) 262 return; 263 phy = device_add_child(dev, name, unit); 264 if (phy == NULL) { 265 free(ma, M_DEVBUF); 266 return; 267 } 268 ivars = device_get_ivars(dev); 269 ma->mii_phyno = val; 270 ma->mii_offset = ivars->mii_offset++; 271 ma->mii_id1 = 0; 272 ma->mii_id2 = 0; 273 ma->mii_capmask = BMSR_DEFCAPMASK; 274 device_set_ivars(phy, ma); 275 } 276 277 if (resource_int_value(name, unit, "id1", &val) == 0) 278 ma->mii_id1 = val; 279 if (resource_int_value(name, unit, "id2", &val) == 0) 280 ma->mii_id2 = val; 281 if (resource_int_value(name, unit, "capmask", &val) == 0) 282 ma->mii_capmask = val; 283 } 284 285 static int 286 miibus_readreg(device_t dev, int phy, int reg) 287 { 288 device_t parent; 289 290 parent = device_get_parent(dev); 291 return (MIIBUS_READREG(parent, phy, reg)); 292 } 293 294 static int 295 miibus_writereg(device_t dev, int phy, int reg, int data) 296 { 297 device_t parent; 298 299 parent = device_get_parent(dev); 300 return (MIIBUS_WRITEREG(parent, phy, reg, data)); 301 } 302 303 static void 304 miibus_statchg(device_t dev) 305 { 306 device_t parent; 307 struct mii_data *mii; 308 309 parent = device_get_parent(dev); 310 MIIBUS_STATCHG(parent); 311 312 mii = device_get_softc(dev); 313 if_setbaudrate(mii->mii_ifp, ifmedia_baudrate(mii->mii_media_active)); 314 } 315 316 static void 317 miibus_linkchg(device_t dev) 318 { 319 struct mii_data *mii; 320 device_t parent; 321 int link_state; 322 323 parent = device_get_parent(dev); 324 MIIBUS_LINKCHG(parent); 325 326 mii = device_get_softc(dev); 327 328 if (mii->mii_media_status & IFM_AVALID) { 329 if (mii->mii_media_status & IFM_ACTIVE) 330 link_state = LINK_STATE_UP; 331 else 332 link_state = LINK_STATE_DOWN; 333 } else 334 link_state = LINK_STATE_UNKNOWN; 335 if_link_state_change(mii->mii_ifp, link_state); 336 } 337 338 static void 339 miibus_mediainit(device_t dev) 340 { 341 struct mii_data *mii; 342 struct ifmedia_entry *m; 343 int media = 0; 344 345 /* Poke the parent in case it has any media of its own to add. */ 346 MIIBUS_MEDIAINIT(device_get_parent(dev)); 347 348 mii = device_get_softc(dev); 349 LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) { 350 media = m->ifm_media; 351 if (media == (IFM_ETHER | IFM_AUTO)) 352 break; 353 } 354 355 ifmedia_set(&mii->mii_media, media); 356 } 357 358 /* 359 * Helper function used by network interface drivers, attaches the miibus and 360 * the PHYs to the network interface driver parent. 361 */ 362 int 363 mii_attach(device_t dev, device_t *miibus, if_t ifp, 364 ifm_change_cb_t ifmedia_upd, ifm_stat_cb_t ifmedia_sts, int capmask, 365 int phyloc, int offloc, int flags) 366 { 367 struct miibus_ivars *ivars; 368 struct mii_attach_args *args, ma; 369 device_t *children, phy; 370 int bmsr, first, i, nchildren, phymax, phymin, rv; 371 uint32_t phymask; 372 373 if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY) { 374 printf("%s: phyloc and offloc specified\n", __func__); 375 return (EINVAL); 376 } 377 378 if (offloc != MII_OFFSET_ANY && (offloc < 0 || offloc >= MII_NPHY)) { 379 printf("%s: invalid offloc %d\n", __func__, offloc); 380 return (EINVAL); 381 } 382 383 if (phyloc == MII_PHY_ANY) { 384 phymin = 0; 385 phymax = MII_NPHY - 1; 386 } else { 387 if (phyloc < 0 || phyloc >= MII_NPHY) { 388 printf("%s: invalid phyloc %d\n", __func__, phyloc); 389 return (EINVAL); 390 } 391 phymin = phymax = phyloc; 392 } 393 394 first = 0; 395 if (*miibus == NULL) { 396 first = 1; 397 ivars = malloc(sizeof(*ivars), M_DEVBUF, M_NOWAIT); 398 if (ivars == NULL) 399 return (ENOMEM); 400 ivars->ifp = ifp; 401 ivars->ifmedia_upd = ifmedia_upd; 402 ivars->ifmedia_sts = ifmedia_sts; 403 ivars->mii_flags = flags; 404 *miibus = device_add_child(dev, "miibus", -1); 405 if (*miibus == NULL) { 406 rv = ENXIO; 407 goto fail; 408 } 409 device_set_ivars(*miibus, ivars); 410 } else { 411 ivars = device_get_ivars(*miibus); 412 if (ivars->ifp != ifp || ivars->ifmedia_upd != ifmedia_upd || 413 ivars->ifmedia_sts != ifmedia_sts || 414 ivars->mii_flags != flags) { 415 printf("%s: non-matching invariant\n", __func__); 416 return (EINVAL); 417 } 418 /* 419 * Assignment of the attach arguments mii_data for the first 420 * pass is done in miibus_attach(), i.e. once the miibus softc 421 * has been allocated. 422 */ 423 ma.mii_data = device_get_softc(*miibus); 424 } 425 426 ma.mii_capmask = capmask; 427 428 if (resource_int_value(device_get_name(*miibus), 429 device_get_unit(*miibus), "phymask", &phymask) != 0) 430 phymask = 0xffffffff; 431 432 if (device_get_children(*miibus, &children, &nchildren) != 0) { 433 children = NULL; 434 nchildren = 0; 435 } 436 ivars->mii_offset = 0; 437 for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) { 438 /* 439 * Make sure we haven't already configured a PHY at this 440 * address. This allows mii_attach() to be called 441 * multiple times. 442 */ 443 for (i = 0; i < nchildren; i++) { 444 args = device_get_ivars(children[i]); 445 if (args->mii_phyno == ma.mii_phyno) { 446 /* 447 * Yes, there is already something 448 * configured at this address. 449 */ 450 goto skip; 451 } 452 } 453 454 /* 455 * Check to see if there is a PHY at this address. Note, 456 * many braindead PHYs report 0/0 in their ID registers, 457 * so we test for media in the BMSR. 458 */ 459 bmsr = MIIBUS_READREG(dev, ma.mii_phyno, MII_BMSR); 460 if (bmsr == 0 || bmsr == 0xffff || 461 (bmsr & (BMSR_EXTSTAT | BMSR_MEDIAMASK)) == 0) { 462 /* Assume no PHY at this address. */ 463 continue; 464 } 465 466 /* 467 * There is a PHY at this address. If we were given an 468 * `offset' locator, skip this PHY if it doesn't match. 469 */ 470 if (offloc != MII_OFFSET_ANY && offloc != ivars->mii_offset) 471 goto skip; 472 473 /* 474 * Skip this PHY if it's not included in the phymask hint. 475 */ 476 if ((phymask & (1 << ma.mii_phyno)) == 0) 477 goto skip; 478 479 /* 480 * Extract the IDs. Braindead PHYs will be handled by 481 * the `ukphy' driver, as we have no ID information to 482 * match on. 483 */ 484 ma.mii_id1 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR1); 485 ma.mii_id2 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR2); 486 487 ma.mii_offset = ivars->mii_offset; 488 args = malloc(sizeof(struct mii_attach_args), M_DEVBUF, 489 M_NOWAIT); 490 if (args == NULL) 491 goto skip; 492 bcopy((char *)&ma, (char *)args, sizeof(ma)); 493 phy = device_add_child(*miibus, NULL, -1); 494 if (phy == NULL) { 495 free(args, M_DEVBUF); 496 goto skip; 497 } 498 device_set_ivars(phy, args); 499 skip: 500 ivars->mii_offset++; 501 } 502 free(children, M_TEMP); 503 504 if (first != 0) { 505 rv = device_set_driver(*miibus, &miibus_driver); 506 if (rv != 0) 507 goto fail; 508 bus_enumerate_hinted_children(*miibus); 509 rv = device_get_children(*miibus, &children, &nchildren); 510 if (rv != 0) 511 goto fail; 512 free(children, M_TEMP); 513 if (nchildren == 0) { 514 rv = ENXIO; 515 goto fail; 516 } 517 rv = bus_generic_attach(dev); 518 if (rv != 0) 519 goto fail; 520 521 /* Attaching of the PHY drivers is done in miibus_attach(). */ 522 return (0); 523 } 524 rv = bus_generic_attach(*miibus); 525 if (rv != 0) 526 goto fail; 527 528 return (0); 529 530 fail: 531 if (*miibus != NULL) 532 device_delete_child(dev, *miibus); 533 free(ivars, M_DEVBUF); 534 if (first != 0) 535 *miibus = NULL; 536 return (rv); 537 } 538 539 /* 540 * Media changed; notify all PHYs. 541 */ 542 int 543 mii_mediachg(struct mii_data *mii) 544 { 545 struct mii_softc *child; 546 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 547 int rv; 548 549 mii->mii_media_status = 0; 550 mii->mii_media_active = IFM_NONE; 551 552 LIST_FOREACH(child, &mii->mii_phys, mii_list) { 553 /* 554 * If the media indicates a different PHY instance, 555 * isolate this one. 556 */ 557 if (IFM_INST(ife->ifm_media) != child->mii_inst) { 558 if ((child->mii_flags & MIIF_NOISOLATE) != 0) { 559 device_printf(child->mii_dev, "%s: " 560 "can't handle non-zero PHY instance %d\n", 561 __func__, child->mii_inst); 562 continue; 563 } 564 PHY_WRITE(child, MII_BMCR, PHY_READ(child, MII_BMCR) | 565 BMCR_ISO); 566 continue; 567 } 568 rv = PHY_SERVICE(child, mii, MII_MEDIACHG); 569 if (rv) 570 return (rv); 571 } 572 return (0); 573 } 574 575 /* 576 * Call the PHY tick routines, used during autonegotiation. 577 */ 578 void 579 mii_tick(struct mii_data *mii) 580 { 581 struct mii_softc *child; 582 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 583 584 LIST_FOREACH(child, &mii->mii_phys, mii_list) { 585 /* 586 * If this PHY instance isn't currently selected, just skip 587 * it. 588 */ 589 if (IFM_INST(ife->ifm_media) != child->mii_inst) 590 continue; 591 (void)PHY_SERVICE(child, mii, MII_TICK); 592 } 593 } 594 595 /* 596 * Get media status from PHYs. 597 */ 598 void 599 mii_pollstat(struct mii_data *mii) 600 { 601 struct mii_softc *child; 602 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 603 604 mii->mii_media_status = 0; 605 mii->mii_media_active = IFM_NONE; 606 607 LIST_FOREACH(child, &mii->mii_phys, mii_list) { 608 /* 609 * If we're not polling this PHY instance, just skip it. 610 */ 611 if (IFM_INST(ife->ifm_media) != child->mii_inst) 612 continue; 613 (void)PHY_SERVICE(child, mii, MII_POLLSTAT); 614 } 615 } 616 617 static unsigned char 618 mii_bitreverse(unsigned char x) 619 { 620 static unsigned const char nibbletab[16] = { 621 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15 622 }; 623 624 return ((nibbletab[x & 15] << 4) | nibbletab[x >> 4]); 625 } 626 627 u_int 628 mii_oui(u_int id1, u_int id2) 629 { 630 u_int h; 631 632 h = (id1 << 6) | (id2 >> 10); 633 634 return ((mii_bitreverse(h >> 16) << 16) | 635 (mii_bitreverse((h >> 8) & 0xff) << 8) | 636 mii_bitreverse(h & 0xff)); 637 } 638 639 int 640 mii_phy_mac_match(struct mii_softc *mii, const char *name) 641 { 642 643 return (strcmp(device_get_name(device_get_parent(mii->mii_dev)), 644 name) == 0); 645 } 646 647 int 648 mii_dev_mac_match(device_t parent, const char *name) 649 { 650 651 return (strcmp(device_get_name(device_get_parent( 652 device_get_parent(parent))), name) == 0); 653 } 654 655 void * 656 mii_phy_mac_softc(struct mii_softc *mii) 657 { 658 659 return (device_get_softc(device_get_parent(mii->mii_dev))); 660 } 661 662 void * 663 mii_dev_mac_softc(device_t parent) 664 { 665 666 return (device_get_softc(device_get_parent(device_get_parent(parent)))); 667 } 668