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_child_location_str(device_t bus, device_t child, char *buf, 68 size_t buflen); 69 static int miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf, 70 size_t buflen); 71 static int miibus_readreg(device_t, int, int); 72 static int miibus_writereg(device_t, int, int, int); 73 static void miibus_statchg(device_t); 74 static void miibus_linkchg(device_t); 75 static void miibus_mediainit(device_t); 76 77 static device_method_t miibus_methods[] = { 78 /* device interface */ 79 DEVMETHOD(device_probe, miibus_probe), 80 DEVMETHOD(device_attach, miibus_attach), 81 DEVMETHOD(device_detach, miibus_detach), 82 DEVMETHOD(device_shutdown, bus_generic_shutdown), 83 84 /* bus interface */ 85 DEVMETHOD(bus_print_child, bus_generic_print_child), 86 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 87 DEVMETHOD(bus_child_pnpinfo_str, miibus_child_pnpinfo_str), 88 DEVMETHOD(bus_child_location_str, miibus_child_location_str), 89 90 /* MII interface */ 91 DEVMETHOD(miibus_readreg, miibus_readreg), 92 DEVMETHOD(miibus_writereg, miibus_writereg), 93 DEVMETHOD(miibus_statchg, miibus_statchg), 94 DEVMETHOD(miibus_linkchg, miibus_linkchg), 95 DEVMETHOD(miibus_mediainit, miibus_mediainit), 96 97 { 0, 0 } 98 }; 99 100 devclass_t miibus_devclass; 101 102 driver_t miibus_driver = { 103 "miibus", 104 miibus_methods, 105 sizeof(struct mii_data) 106 }; 107 108 /* 109 * Helper function used by network interface drivers, attaches PHYs 110 * to the network interface driver parent. 111 */ 112 int 113 miibus_probe(device_t dev) 114 { 115 struct mii_attach_args ma, *args; 116 struct mii_data *mii; 117 device_t child = NULL, parent; 118 int bmsr, capmask = 0xFFFFFFFF; 119 120 mii = device_get_softc(dev); 121 parent = device_get_parent(dev); 122 LIST_INIT(&mii->mii_phys); 123 124 for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) { 125 /* 126 * Check to see if there is a PHY at this address. Note, 127 * many braindead PHYs report 0/0 in their ID registers, 128 * so we test for media in the BMSR. 129 */ 130 bmsr = MIIBUS_READREG(parent, ma.mii_phyno, MII_BMSR); 131 if (bmsr == 0 || bmsr == 0xffff || 132 (bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) { 133 /* Assume no PHY at this address. */ 134 continue; 135 } 136 137 /* 138 * Extract the IDs. Braindead PHYs will be handled by 139 * the `ukphy' driver, as we have no ID information to 140 * match on. 141 */ 142 ma.mii_id1 = MIIBUS_READREG(parent, ma.mii_phyno, 143 MII_PHYIDR1); 144 ma.mii_id2 = MIIBUS_READREG(parent, ma.mii_phyno, 145 MII_PHYIDR2); 146 147 ma.mii_data = mii; 148 ma.mii_capmask = capmask; 149 150 args = malloc(sizeof(struct mii_attach_args), 151 M_DEVBUF, M_NOWAIT); 152 bcopy((char *)&ma, (char *)args, sizeof(ma)); 153 child = device_add_child(dev, NULL, -1); 154 device_set_ivars(child, args); 155 } 156 157 if (child == NULL) 158 return(ENXIO); 159 160 device_set_desc(dev, "MII bus"); 161 162 return(0); 163 } 164 165 int 166 miibus_attach(device_t dev) 167 { 168 void **v; 169 ifm_change_cb_t ifmedia_upd; 170 ifm_stat_cb_t ifmedia_sts; 171 struct mii_data *mii; 172 173 mii = device_get_softc(dev); 174 /* 175 * Note that each NIC's softc must start with an ifnet pointer. 176 * XXX: EVIL HACK! 177 */ 178 mii->mii_ifp = *(struct ifnet**)device_get_softc(device_get_parent(dev)); 179 v = device_get_ivars(dev); 180 ifmedia_upd = v[0]; 181 ifmedia_sts = v[1]; 182 ifmedia_init(&mii->mii_media, IFM_IMASK, ifmedia_upd, ifmedia_sts); 183 bus_generic_attach(dev); 184 185 return(0); 186 } 187 188 int 189 miibus_detach(device_t dev) 190 { 191 struct mii_data *mii; 192 193 bus_generic_detach(dev); 194 mii = device_get_softc(dev); 195 ifmedia_removeall(&mii->mii_media); 196 mii->mii_ifp = NULL; 197 198 return(0); 199 } 200 201 static int 202 miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf, 203 size_t buflen) 204 { 205 struct mii_attach_args *maa = device_get_ivars(child); 206 snprintf(buf, buflen, "oui=0x%x model=0x%x rev=0x%x", 207 MII_OUI(maa->mii_id1, maa->mii_id2), 208 MII_MODEL(maa->mii_id2), MII_REV(maa->mii_id2)); 209 return (0); 210 } 211 212 static int 213 miibus_child_location_str(device_t bus, device_t child, char *buf, 214 size_t buflen) 215 { 216 struct mii_attach_args *maa = device_get_ivars(child); 217 snprintf(buf, buflen, "phyno=%d", maa->mii_phyno); 218 return (0); 219 } 220 221 static int 222 miibus_readreg(device_t dev, int phy, int reg) 223 { 224 device_t parent; 225 226 parent = device_get_parent(dev); 227 return(MIIBUS_READREG(parent, phy, reg)); 228 } 229 230 static int 231 miibus_writereg(device_t dev, int phy, int reg, int data) 232 { 233 device_t parent; 234 235 parent = device_get_parent(dev); 236 return(MIIBUS_WRITEREG(parent, phy, reg, data)); 237 } 238 239 static void 240 miibus_statchg(device_t dev) 241 { 242 device_t parent; 243 struct mii_data *mii; 244 struct ifnet *ifp; 245 246 parent = device_get_parent(dev); 247 MIIBUS_STATCHG(parent); 248 249 mii = device_get_softc(dev); 250 251 /* 252 * Note that each NIC's softc must start with an ifnet pointer. 253 * XXX: EVIL HACK! 254 */ 255 ifp = *(struct ifnet **)device_get_softc(parent); 256 ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active); 257 return; 258 } 259 260 static void 261 miibus_linkchg(device_t dev) 262 { 263 struct mii_data *mii; 264 device_t parent; 265 int link_state; 266 267 parent = device_get_parent(dev); 268 MIIBUS_LINKCHG(parent); 269 270 mii = device_get_softc(dev); 271 272 if (mii->mii_media_status & IFM_AVALID) { 273 if (mii->mii_media_status & IFM_ACTIVE) 274 link_state = LINK_STATE_UP; 275 else 276 link_state = LINK_STATE_DOWN; 277 } else 278 link_state = LINK_STATE_UNKNOWN; 279 /* 280 * Note that each NIC's softc must start with an ifnet pointer. 281 * XXX: EVIL HACK! 282 */ 283 if_link_state_change(*(struct ifnet**)device_get_softc(parent), link_state); 284 } 285 286 static void 287 miibus_mediainit(device_t dev) 288 { 289 struct mii_data *mii; 290 struct ifmedia_entry *m; 291 int media = 0; 292 293 /* Poke the parent in case it has any media of its own to add. */ 294 MIIBUS_MEDIAINIT(device_get_parent(dev)); 295 296 mii = device_get_softc(dev); 297 LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) { 298 media = m->ifm_media; 299 if (media == (IFM_ETHER|IFM_AUTO)) 300 break; 301 } 302 303 ifmedia_set(&mii->mii_media, media); 304 305 return; 306 } 307 308 int 309 mii_phy_probe(device_t dev, device_t *child, ifm_change_cb_t ifmedia_upd, 310 ifm_stat_cb_t ifmedia_sts) 311 { 312 void **v; 313 int bmsr, i; 314 315 v = malloc(sizeof(vm_offset_t) * 2, M_DEVBUF, M_NOWAIT); 316 if (v == 0) { 317 return (ENOMEM); 318 } 319 v[0] = ifmedia_upd; 320 v[1] = ifmedia_sts; 321 *child = device_add_child(dev, "miibus", -1); 322 device_set_ivars(*child, v); 323 324 for (i = 0; i < MII_NPHY; i++) { 325 bmsr = MIIBUS_READREG(dev, i, MII_BMSR); 326 if (bmsr == 0 || bmsr == 0xffff || 327 (bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) { 328 /* Assume no PHY at this address. */ 329 continue; 330 } else 331 break; 332 } 333 334 if (i == MII_NPHY) { 335 device_delete_child(dev, *child); 336 *child = NULL; 337 return(ENXIO); 338 } 339 340 bus_generic_attach(dev); 341 342 return(0); 343 } 344 345 /* 346 * Media changed; notify all PHYs. 347 */ 348 int 349 mii_mediachg(struct mii_data *mii) 350 { 351 struct mii_softc *child; 352 int rv; 353 354 mii->mii_media_status = 0; 355 mii->mii_media_active = IFM_NONE; 356 357 LIST_FOREACH(child, &mii->mii_phys, mii_list) { 358 rv = (*child->mii_service)(child, mii, MII_MEDIACHG); 359 if (rv) 360 return (rv); 361 } 362 return (0); 363 } 364 365 /* 366 * Call the PHY tick routines, used during autonegotiation. 367 */ 368 void 369 mii_tick(struct mii_data *mii) 370 { 371 struct mii_softc *child; 372 373 LIST_FOREACH(child, &mii->mii_phys, mii_list) 374 (void) (*child->mii_service)(child, mii, MII_TICK); 375 } 376 377 /* 378 * Get media status from PHYs. 379 */ 380 void 381 mii_pollstat(struct mii_data *mii) 382 { 383 struct mii_softc *child; 384 385 mii->mii_media_status = 0; 386 mii->mii_media_active = IFM_NONE; 387 388 LIST_FOREACH(child, &mii->mii_phys, mii_list) 389 (void) (*child->mii_service)(child, mii, MII_POLLSTAT); 390 } 391 392 /* 393 * Inform the PHYs that the interface is down. 394 */ 395 void 396 mii_down(struct mii_data *mii) 397 { 398 struct mii_softc *child; 399 400 LIST_FOREACH(child, &mii->mii_phys, mii_list) 401 mii_phy_down(child); 402 } 403