1 /* $NetBSD: mii.c,v 1.12 1999/08/03 19:41:49 drochner Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 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 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 /* 37 * MII bus layer, glues MII-capable network interface drivers to sharable 38 * PHY drivers. This exports an interface compatible with BSD/OS 3.0's, 39 * plus some NetBSD extensions. 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/socket.h> 45 #include <sys/malloc.h> 46 #include <sys/module.h> 47 #include <sys/bus.h> 48 49 #include <net/if.h> 50 #include <net/if_media.h> 51 #include <net/route.h> 52 53 #include <dev/mii/mii.h> 54 #include <dev/mii/miivar.h> 55 56 MODULE_VERSION(miibus, 1); 57 58 #include "miibus_if.h" 59 60 static int miibus_print_child(device_t dev, device_t child); 61 static int miibus_read_ivar(device_t dev, device_t child, int which, 62 uintptr_t *result); 63 static int miibus_child_location_str(device_t bus, device_t child, char *buf, 64 size_t buflen); 65 static int miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf, 66 size_t buflen); 67 static int miibus_readreg(device_t, int, int); 68 static int miibus_writereg(device_t, int, int, int); 69 static void miibus_statchg(device_t); 70 static void miibus_linkchg(device_t); 71 static void miibus_mediainit(device_t); 72 73 static device_method_t miibus_methods[] = { 74 /* device interface */ 75 DEVMETHOD(device_probe, miibus_probe), 76 DEVMETHOD(device_attach, miibus_attach), 77 DEVMETHOD(device_detach, miibus_detach), 78 DEVMETHOD(device_shutdown, bus_generic_shutdown), 79 80 /* bus interface */ 81 DEVMETHOD(bus_print_child, miibus_print_child), 82 DEVMETHOD(bus_read_ivar, miibus_read_ivar), 83 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 84 DEVMETHOD(bus_child_pnpinfo_str, miibus_child_pnpinfo_str), 85 DEVMETHOD(bus_child_location_str, miibus_child_location_str), 86 87 /* MII interface */ 88 DEVMETHOD(miibus_readreg, miibus_readreg), 89 DEVMETHOD(miibus_writereg, miibus_writereg), 90 DEVMETHOD(miibus_statchg, miibus_statchg), 91 DEVMETHOD(miibus_linkchg, miibus_linkchg), 92 DEVMETHOD(miibus_mediainit, miibus_mediainit), 93 94 { 0, 0 } 95 }; 96 97 devclass_t miibus_devclass; 98 99 driver_t miibus_driver = { 100 "miibus", 101 miibus_methods, 102 sizeof(struct mii_data) 103 }; 104 105 struct miibus_ivars { 106 struct ifnet *ifp; 107 ifm_change_cb_t ifmedia_upd; 108 ifm_stat_cb_t ifmedia_sts; 109 int mii_flags; 110 }; 111 112 int 113 miibus_probe(device_t dev) 114 { 115 116 device_set_desc(dev, "MII bus"); 117 118 return (BUS_PROBE_SPECIFIC); 119 } 120 121 int 122 miibus_attach(device_t dev) 123 { 124 struct miibus_ivars *ivars; 125 struct mii_attach_args *ma; 126 struct mii_data *mii; 127 device_t *children; 128 int i, nchildren; 129 130 mii = device_get_softc(dev); 131 nchildren = 0; 132 if (device_get_children(dev, &children, &nchildren) == 0) { 133 for (i = 0; i < nchildren; i++) { 134 ma = device_get_ivars(children[i]); 135 ma->mii_data = mii; 136 } 137 free(children, M_TEMP); 138 } 139 if (nchildren == 0) { 140 device_printf(dev, "cannot get children"); 141 return (ENXIO); 142 } 143 ivars = device_get_ivars(dev); 144 ifmedia_init(&mii->mii_media, IFM_IMASK, ivars->ifmedia_upd, 145 ivars->ifmedia_sts); 146 mii->mii_ifp = ivars->ifp; 147 mii->mii_ifp->if_capabilities |= IFCAP_LINKSTATE; 148 mii->mii_ifp->if_capenable |= IFCAP_LINKSTATE; 149 LIST_INIT(&mii->mii_phys); 150 151 return (bus_generic_attach(dev)); 152 } 153 154 int 155 miibus_detach(device_t dev) 156 { 157 struct mii_data *mii; 158 159 bus_generic_detach(dev); 160 mii = device_get_softc(dev); 161 ifmedia_removeall(&mii->mii_media); 162 mii->mii_ifp = NULL; 163 164 return (0); 165 } 166 167 static int 168 miibus_print_child(device_t dev, device_t child) 169 { 170 struct mii_attach_args *ma; 171 int retval; 172 173 ma = device_get_ivars(child); 174 retval = bus_print_child_header(dev, child); 175 retval += printf(" PHY %d", ma->mii_phyno); 176 retval += bus_print_child_footer(dev, child); 177 178 return (retval); 179 } 180 181 static int 182 miibus_read_ivar(device_t dev, device_t child __unused, int which, 183 uintptr_t *result) 184 { 185 struct miibus_ivars *ivars; 186 187 /* 188 * NB: this uses the instance variables of the miibus rather than 189 * its PHY children. 190 */ 191 ivars = device_get_ivars(dev); 192 switch (which) { 193 case MIIBUS_IVAR_FLAGS: 194 *result = ivars->mii_flags; 195 break; 196 default: 197 return (ENOENT); 198 } 199 return (0); 200 } 201 202 static int 203 miibus_child_pnpinfo_str(device_t bus __unused, device_t child, char *buf, 204 size_t buflen) 205 { 206 struct mii_attach_args *ma; 207 208 ma = device_get_ivars(child); 209 snprintf(buf, buflen, "oui=0x%x model=0x%x rev=0x%x", 210 MII_OUI(ma->mii_id1, ma->mii_id2), 211 MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2)); 212 return (0); 213 } 214 215 static int 216 miibus_child_location_str(device_t bus __unused, device_t child, char *buf, 217 size_t buflen) 218 { 219 struct mii_attach_args *ma; 220 221 ma = device_get_ivars(child); 222 snprintf(buf, buflen, "phyno=%d", ma->mii_phyno); 223 return (0); 224 } 225 226 static int 227 miibus_readreg(device_t dev, int phy, int reg) 228 { 229 device_t parent; 230 231 parent = device_get_parent(dev); 232 return (MIIBUS_READREG(parent, phy, reg)); 233 } 234 235 static int 236 miibus_writereg(device_t dev, int phy, int reg, int data) 237 { 238 device_t parent; 239 240 parent = device_get_parent(dev); 241 return (MIIBUS_WRITEREG(parent, phy, reg, data)); 242 } 243 244 static void 245 miibus_statchg(device_t dev) 246 { 247 device_t parent; 248 struct mii_data *mii; 249 250 parent = device_get_parent(dev); 251 MIIBUS_STATCHG(parent); 252 253 mii = device_get_softc(dev); 254 mii->mii_ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active); 255 } 256 257 static void 258 miibus_linkchg(device_t dev) 259 { 260 struct mii_data *mii; 261 device_t parent; 262 int link_state; 263 264 parent = device_get_parent(dev); 265 MIIBUS_LINKCHG(parent); 266 267 mii = device_get_softc(dev); 268 269 if (mii->mii_media_status & IFM_AVALID) { 270 if (mii->mii_media_status & IFM_ACTIVE) 271 link_state = LINK_STATE_UP; 272 else 273 link_state = LINK_STATE_DOWN; 274 } else 275 link_state = LINK_STATE_UNKNOWN; 276 if_link_state_change(mii->mii_ifp, link_state); 277 } 278 279 static void 280 miibus_mediainit(device_t dev) 281 { 282 struct mii_data *mii; 283 struct ifmedia_entry *m; 284 int media = 0; 285 286 /* Poke the parent in case it has any media of its own to add. */ 287 MIIBUS_MEDIAINIT(device_get_parent(dev)); 288 289 mii = device_get_softc(dev); 290 LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) { 291 media = m->ifm_media; 292 if (media == (IFM_ETHER | IFM_AUTO)) 293 break; 294 } 295 296 ifmedia_set(&mii->mii_media, media); 297 } 298 299 /* 300 * Helper function used by network interface drivers, attaches the miibus and 301 * the PHYs to the network interface driver parent. 302 */ 303 int 304 mii_attach(device_t dev, device_t *miibus, struct ifnet *ifp, 305 ifm_change_cb_t ifmedia_upd, ifm_stat_cb_t ifmedia_sts, int capmask, 306 int phyloc, int offloc, int flags) 307 { 308 struct miibus_ivars *ivars; 309 struct mii_attach_args ma, *args; 310 device_t *children, phy; 311 int bmsr, first, i, nchildren, offset, phymax, phymin, rv; 312 313 if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY) { 314 printf("%s: phyloc and offloc specified", __func__); 315 return (EINVAL); 316 } 317 318 if (offloc != MII_OFFSET_ANY && (offloc < 0 || offloc >= MII_NPHY)) { 319 printf("%s: ivalid offloc %d", __func__, offloc); 320 return (EINVAL); 321 } 322 323 if (phyloc == MII_PHY_ANY) { 324 phymin = 0; 325 phymax = MII_NPHY - 1; 326 } else { 327 if (phyloc < 0 || phyloc >= MII_NPHY) { 328 printf("%s: ivalid phyloc %d", __func__, phyloc); 329 return (EINVAL); 330 } 331 phymin = phymax = phyloc; 332 } 333 334 first = 0; 335 if (*miibus == NULL) { 336 first = 1; 337 ivars = malloc(sizeof(*ivars), M_DEVBUF, M_NOWAIT); 338 if (ivars == NULL) 339 return (ENOMEM); 340 ivars->ifp = ifp; 341 ivars->ifmedia_upd = ifmedia_upd; 342 ivars->ifmedia_sts = ifmedia_sts; 343 ivars->mii_flags = flags; 344 *miibus = device_add_child(dev, "miibus", -1); 345 if (*miibus == NULL) { 346 rv = ENXIO; 347 goto fail; 348 } 349 device_set_ivars(*miibus, ivars); 350 } else { 351 ivars = device_get_ivars(*miibus); 352 if (ivars->ifp != ifp || ivars->ifmedia_upd != ifmedia_upd || 353 ivars->ifmedia_sts != ifmedia_sts || 354 ivars->mii_flags != flags) { 355 printf("%s: non-matching invariant", __func__); 356 return (EINVAL); 357 } 358 /* 359 * Assignment of the attach arguments mii_data for the first 360 * pass is done in miibus_attach(), i.e. once the miibus softc 361 * has been allocated. 362 */ 363 ma.mii_data = device_get_softc(*miibus); 364 } 365 366 ma.mii_capmask = capmask; 367 368 phy = NULL; 369 offset = 0; 370 for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) { 371 /* 372 * Make sure we haven't already configured a PHY at this 373 * address. This allows mii_attach() to be called 374 * multiple times. 375 */ 376 if (device_get_children(*miibus, &children, &nchildren) == 0) { 377 for (i = 0; i < nchildren; i++) { 378 args = device_get_ivars(children[i]); 379 if (args->mii_phyno == ma.mii_phyno) { 380 /* 381 * Yes, there is already something 382 * configured at this address. 383 */ 384 free(children, M_TEMP); 385 goto skip; 386 } 387 } 388 free(children, M_TEMP); 389 } 390 391 /* 392 * Check to see if there is a PHY at this address. Note, 393 * many braindead PHYs report 0/0 in their ID registers, 394 * so we test for media in the BMSR. 395 */ 396 bmsr = MIIBUS_READREG(dev, ma.mii_phyno, MII_BMSR); 397 if (bmsr == 0 || bmsr == 0xffff || 398 (bmsr & (BMSR_EXTSTAT | BMSR_MEDIAMASK)) == 0) { 399 /* Assume no PHY at this address. */ 400 continue; 401 } 402 403 /* 404 * There is a PHY at this address. If we were given an 405 * `offset' locator, skip this PHY if it doesn't match. 406 */ 407 if (offloc != MII_OFFSET_ANY && offloc != offset) 408 goto skip; 409 410 /* 411 * Extract the IDs. Braindead PHYs will be handled by 412 * the `ukphy' driver, as we have no ID information to 413 * match on. 414 */ 415 ma.mii_id1 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR1); 416 ma.mii_id2 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR2); 417 418 args = malloc(sizeof(struct mii_attach_args), M_DEVBUF, 419 M_NOWAIT); 420 if (args == NULL) 421 goto skip; 422 bcopy((char *)&ma, (char *)args, sizeof(ma)); 423 phy = device_add_child(*miibus, NULL, -1); 424 if (phy == NULL) { 425 free(args, M_DEVBUF); 426 goto skip; 427 } 428 device_set_ivars(phy, args); 429 skip: 430 offset++; 431 } 432 433 if (first != 0) { 434 if (phy == NULL) { 435 rv = ENXIO; 436 goto fail; 437 } 438 rv = bus_generic_attach(dev); 439 if (rv != 0) 440 goto fail; 441 } 442 rv = bus_generic_attach(*miibus); 443 if (rv != 0) 444 goto fail; 445 446 return (0); 447 448 fail: 449 if (*miibus != NULL) 450 device_delete_child(dev, *miibus); 451 free(ivars, M_DEVBUF); 452 if (first != 0) 453 *miibus = NULL; 454 return (rv); 455 } 456 457 /* 458 * Media changed; notify all PHYs. 459 */ 460 int 461 mii_mediachg(struct mii_data *mii) 462 { 463 struct mii_softc *child; 464 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 465 int rv; 466 467 mii->mii_media_status = 0; 468 mii->mii_media_active = IFM_NONE; 469 470 LIST_FOREACH(child, &mii->mii_phys, mii_list) { 471 /* 472 * If the media indicates a different PHY instance, 473 * isolate this one. 474 */ 475 if (IFM_INST(ife->ifm_media) != child->mii_inst) { 476 if ((child->mii_flags & MIIF_NOISOLATE) != 0) { 477 device_printf(child->mii_dev, "%s: " 478 "can't handle non-zero PHY instance %d\n", 479 __func__, child->mii_inst); 480 continue; 481 } 482 PHY_WRITE(child, MII_BMCR, PHY_READ(child, MII_BMCR) | 483 BMCR_ISO); 484 continue; 485 } 486 rv = (*child->mii_service)(child, mii, MII_MEDIACHG); 487 if (rv) 488 return (rv); 489 } 490 return (0); 491 } 492 493 /* 494 * Call the PHY tick routines, used during autonegotiation. 495 */ 496 void 497 mii_tick(struct mii_data *mii) 498 { 499 struct mii_softc *child; 500 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 501 502 LIST_FOREACH(child, &mii->mii_phys, mii_list) { 503 /* 504 * If this PHY instance isn't currently selected, just skip 505 * it. 506 */ 507 if (IFM_INST(ife->ifm_media) != child->mii_inst) 508 continue; 509 (void)(*child->mii_service)(child, mii, MII_TICK); 510 } 511 } 512 513 /* 514 * Get media status from PHYs. 515 */ 516 void 517 mii_pollstat(struct mii_data *mii) 518 { 519 struct mii_softc *child; 520 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 521 522 mii->mii_media_status = 0; 523 mii->mii_media_active = IFM_NONE; 524 525 LIST_FOREACH(child, &mii->mii_phys, mii_list) { 526 /* 527 * If we're not polling this PHY instance, just skip it. 528 */ 529 if (IFM_INST(ife->ifm_media) != child->mii_inst) 530 continue; 531 (void)(*child->mii_service)(child, mii, MII_POLLSTAT); 532 } 533 } 534 535 /* 536 * Inform the PHYs that the interface is down. 537 */ 538 void 539 mii_down(struct mii_data *mii) 540 { 541 struct mii_softc *child; 542 543 LIST_FOREACH(child, &mii->mii_phys, mii_list) 544 mii_phy_down(child); 545 } 546