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 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 #include <sys/cdefs.h> 41 __FBSDID("$FreeBSD$"); 42 43 /* 44 * MII bus layer, glues MII-capable network interface drivers to sharable 45 * PHY drivers. This exports an interface compatible with BSD/OS 3.0's, 46 * plus some NetBSD extensions. 47 */ 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/socket.h> 52 #include <sys/malloc.h> 53 #include <sys/module.h> 54 #include <sys/bus.h> 55 56 #include <net/if.h> 57 #include <net/if_media.h> 58 #include <net/route.h> 59 60 #include <dev/mii/mii.h> 61 #include <dev/mii/miivar.h> 62 63 MODULE_VERSION(miibus, 1); 64 65 #include "miibus_if.h" 66 67 static int miibus_print_child(device_t dev, device_t child); 68 static int miibus_child_location_str(device_t bus, device_t child, char *buf, 69 size_t buflen); 70 static int miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf, 71 size_t buflen); 72 static int miibus_readreg(device_t, int, int); 73 static int miibus_writereg(device_t, int, int, int); 74 static void miibus_statchg(device_t); 75 static void miibus_linkchg(device_t); 76 static void miibus_mediainit(device_t); 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_driver_added, bus_generic_driver_added), 88 DEVMETHOD(bus_child_pnpinfo_str, miibus_child_pnpinfo_str), 89 DEVMETHOD(bus_child_location_str, miibus_child_location_str), 90 91 /* MII interface */ 92 DEVMETHOD(miibus_readreg, miibus_readreg), 93 DEVMETHOD(miibus_writereg, miibus_writereg), 94 DEVMETHOD(miibus_statchg, miibus_statchg), 95 DEVMETHOD(miibus_linkchg, miibus_linkchg), 96 DEVMETHOD(miibus_mediainit, miibus_mediainit), 97 98 { 0, 0 } 99 }; 100 101 devclass_t miibus_devclass; 102 103 driver_t miibus_driver = { 104 "miibus", 105 miibus_methods, 106 sizeof(struct mii_data) 107 }; 108 109 /* 110 * Helper function used by network interface drivers, attaches PHYs 111 * to the network interface driver parent. 112 */ 113 int 114 miibus_probe(device_t dev) 115 { 116 struct mii_attach_args ma, *args; 117 struct mii_data *mii; 118 device_t child = NULL, parent; 119 int bmsr, capmask = 0xFFFFFFFF; 120 121 mii = device_get_softc(dev); 122 parent = device_get_parent(dev); 123 LIST_INIT(&mii->mii_phys); 124 125 for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) { 126 /* 127 * Check to see if there is a PHY at this address. Note, 128 * many braindead PHYs report 0/0 in their ID registers, 129 * so we test for media in the BMSR. 130 */ 131 bmsr = MIIBUS_READREG(parent, ma.mii_phyno, MII_BMSR); 132 if (bmsr == 0 || bmsr == 0xffff || 133 (bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) { 134 /* Assume no PHY at this address. */ 135 continue; 136 } 137 138 /* 139 * Extract the IDs. Braindead PHYs will be handled by 140 * the `ukphy' driver, as we have no ID information to 141 * match on. 142 */ 143 ma.mii_id1 = MIIBUS_READREG(parent, ma.mii_phyno, 144 MII_PHYIDR1); 145 ma.mii_id2 = MIIBUS_READREG(parent, ma.mii_phyno, 146 MII_PHYIDR2); 147 148 ma.mii_data = mii; 149 ma.mii_capmask = capmask; 150 151 args = malloc(sizeof(struct mii_attach_args), 152 M_DEVBUF, M_NOWAIT); 153 bcopy((char *)&ma, (char *)args, sizeof(ma)); 154 child = device_add_child(dev, NULL, -1); 155 device_set_ivars(child, args); 156 } 157 158 if (child == NULL) 159 return(ENXIO); 160 161 device_set_desc(dev, "MII bus"); 162 163 return(0); 164 } 165 166 int 167 miibus_attach(device_t dev) 168 { 169 void **v; 170 ifm_change_cb_t ifmedia_upd; 171 ifm_stat_cb_t ifmedia_sts; 172 struct mii_data *mii; 173 174 mii = device_get_softc(dev); 175 /* 176 * Note that each NIC's softc must start with an ifnet pointer. 177 * XXX: EVIL HACK! 178 */ 179 mii->mii_ifp = *(struct ifnet**)device_get_softc(device_get_parent(dev)); 180 v = device_get_ivars(dev); 181 ifmedia_upd = v[0]; 182 ifmedia_sts = v[1]; 183 ifmedia_init(&mii->mii_media, 0, ifmedia_upd, ifmedia_sts); 184 bus_generic_attach(dev); 185 186 return(0); 187 } 188 189 int 190 miibus_detach(device_t dev) 191 { 192 struct mii_data *mii; 193 194 bus_generic_detach(dev); 195 mii = device_get_softc(dev); 196 ifmedia_removeall(&mii->mii_media); 197 mii->mii_ifp = NULL; 198 199 return(0); 200 } 201 202 static int 203 miibus_print_child(device_t dev, device_t child) 204 { 205 struct mii_attach_args *ma; 206 int retval; 207 208 ma = device_get_ivars(child); 209 retval = bus_print_child_header(dev, child); 210 retval += printf(" PHY %d", ma->mii_phyno); 211 retval += bus_print_child_footer(dev, child); 212 213 return (retval); 214 } 215 216 static int 217 miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf, 218 size_t buflen) 219 { 220 struct mii_attach_args *maa = device_get_ivars(child); 221 snprintf(buf, buflen, "oui=0x%x model=0x%x rev=0x%x", 222 MII_OUI(maa->mii_id1, maa->mii_id2), 223 MII_MODEL(maa->mii_id2), MII_REV(maa->mii_id2)); 224 return (0); 225 } 226 227 static int 228 miibus_child_location_str(device_t bus, device_t child, char *buf, 229 size_t buflen) 230 { 231 struct mii_attach_args *maa = device_get_ivars(child); 232 snprintf(buf, buflen, "phyno=%d", maa->mii_phyno); 233 return (0); 234 } 235 236 static int 237 miibus_readreg(device_t dev, int phy, int reg) 238 { 239 device_t parent; 240 241 parent = device_get_parent(dev); 242 return(MIIBUS_READREG(parent, phy, reg)); 243 } 244 245 static int 246 miibus_writereg(device_t dev, int phy, int reg, int data) 247 { 248 device_t parent; 249 250 parent = device_get_parent(dev); 251 return(MIIBUS_WRITEREG(parent, phy, reg, data)); 252 } 253 254 static void 255 miibus_statchg(device_t dev) 256 { 257 device_t parent; 258 struct mii_data *mii; 259 struct ifnet *ifp; 260 261 parent = device_get_parent(dev); 262 MIIBUS_STATCHG(parent); 263 264 mii = device_get_softc(dev); 265 266 /* 267 * Note that each NIC's softc must start with an ifnet pointer. 268 * XXX: EVIL HACK! 269 */ 270 ifp = *(struct ifnet **)device_get_softc(parent); 271 ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active); 272 return; 273 } 274 275 static void 276 miibus_linkchg(device_t dev) 277 { 278 struct mii_data *mii; 279 device_t parent; 280 int link_state; 281 282 parent = device_get_parent(dev); 283 MIIBUS_LINKCHG(parent); 284 285 mii = device_get_softc(dev); 286 287 if (mii->mii_media_status & IFM_AVALID) { 288 if (mii->mii_media_status & IFM_ACTIVE) 289 link_state = LINK_STATE_UP; 290 else 291 link_state = LINK_STATE_DOWN; 292 } else 293 link_state = LINK_STATE_UNKNOWN; 294 /* 295 * Note that each NIC's softc must start with an ifnet pointer. 296 * XXX: EVIL HACK! 297 */ 298 if_link_state_change(*(struct ifnet**)device_get_softc(parent), link_state); 299 } 300 301 static void 302 miibus_mediainit(device_t dev) 303 { 304 struct mii_data *mii; 305 struct ifmedia_entry *m; 306 int media = 0; 307 308 /* Poke the parent in case it has any media of its own to add. */ 309 MIIBUS_MEDIAINIT(device_get_parent(dev)); 310 311 mii = device_get_softc(dev); 312 LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) { 313 media = m->ifm_media; 314 if (media == (IFM_ETHER|IFM_AUTO)) 315 break; 316 } 317 318 ifmedia_set(&mii->mii_media, media); 319 320 return; 321 } 322 323 int 324 mii_phy_probe(device_t dev, device_t *child, ifm_change_cb_t ifmedia_upd, 325 ifm_stat_cb_t ifmedia_sts) 326 { 327 void **v; 328 int bmsr, i; 329 330 v = malloc(sizeof(vm_offset_t) * 2, M_DEVBUF, M_NOWAIT); 331 if (v == 0) { 332 return (ENOMEM); 333 } 334 v[0] = ifmedia_upd; 335 v[1] = ifmedia_sts; 336 *child = device_add_child(dev, "miibus", -1); 337 device_set_ivars(*child, v); 338 339 for (i = 0; i < MII_NPHY; i++) { 340 bmsr = MIIBUS_READREG(dev, i, MII_BMSR); 341 if (bmsr == 0 || bmsr == 0xffff || 342 (bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) { 343 /* Assume no PHY at this address. */ 344 continue; 345 } else 346 break; 347 } 348 349 if (i == MII_NPHY) { 350 device_delete_child(dev, *child); 351 *child = NULL; 352 return(ENXIO); 353 } 354 355 bus_generic_attach(dev); 356 357 return(0); 358 } 359 360 /* 361 * Media changed; notify all PHYs. 362 */ 363 int 364 mii_mediachg(struct mii_data *mii) 365 { 366 struct mii_softc *child; 367 int rv; 368 369 mii->mii_media_status = 0; 370 mii->mii_media_active = IFM_NONE; 371 372 LIST_FOREACH(child, &mii->mii_phys, mii_list) { 373 rv = (*child->mii_service)(child, mii, MII_MEDIACHG); 374 if (rv) 375 return (rv); 376 } 377 return (0); 378 } 379 380 /* 381 * Call the PHY tick routines, used during autonegotiation. 382 */ 383 void 384 mii_tick(struct mii_data *mii) 385 { 386 struct mii_softc *child; 387 388 LIST_FOREACH(child, &mii->mii_phys, mii_list) 389 (void) (*child->mii_service)(child, mii, MII_TICK); 390 } 391 392 /* 393 * Get media status from PHYs. 394 */ 395 void 396 mii_pollstat(struct mii_data *mii) 397 { 398 struct mii_softc *child; 399 400 mii->mii_media_status = 0; 401 mii->mii_media_active = IFM_NONE; 402 403 LIST_FOREACH(child, &mii->mii_phys, mii_list) 404 (void) (*child->mii_service)(child, mii, MII_POLLSTAT); 405 } 406 407 /* 408 * Inform the PHYs that the interface is down. 409 */ 410 void 411 mii_down(struct mii_data *mii) 412 { 413 struct mii_softc *child; 414 415 LIST_FOREACH(child, &mii->mii_phys, mii_list) 416 mii_phy_down(child); 417 } 418