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 structure. 176 */ 177 mii->mii_ifp = device_get_softc(device_get_parent(dev)); 178 v = device_get_ivars(dev); 179 ifmedia_upd = v[0]; 180 ifmedia_sts = v[1]; 181 ifmedia_init(&mii->mii_media, IFM_IMASK, ifmedia_upd, ifmedia_sts); 182 bus_generic_attach(dev); 183 184 return(0); 185 } 186 187 int 188 miibus_detach(device_t dev) 189 { 190 struct mii_data *mii; 191 192 bus_generic_detach(dev); 193 mii = device_get_softc(dev); 194 ifmedia_removeall(&mii->mii_media); 195 mii->mii_ifp = NULL; 196 197 return(0); 198 } 199 200 static int 201 miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf, 202 size_t buflen) 203 { 204 struct mii_attach_args *maa = device_get_ivars(child); 205 snprintf(buf, buflen, "oui=0x%x model=0x%x rev=0x%x", 206 MII_OUI(maa->mii_id1, maa->mii_id2), 207 MII_MODEL(maa->mii_id2), MII_REV(maa->mii_id2)); 208 return (0); 209 } 210 211 static int 212 miibus_child_location_str(device_t bus, device_t child, char *buf, 213 size_t buflen) 214 { 215 struct mii_attach_args *maa = device_get_ivars(child); 216 snprintf(buf, buflen, "phyno=%d", maa->mii_phyno); 217 return (0); 218 } 219 220 static int 221 miibus_readreg(device_t dev, int phy, int reg) 222 { 223 device_t parent; 224 225 parent = device_get_parent(dev); 226 return(MIIBUS_READREG(parent, phy, reg)); 227 } 228 229 static int 230 miibus_writereg(device_t dev, int phy, int reg, int data) 231 { 232 device_t parent; 233 234 parent = device_get_parent(dev); 235 return(MIIBUS_WRITEREG(parent, phy, reg, data)); 236 } 237 238 static void 239 miibus_statchg(device_t dev) 240 { 241 device_t parent; 242 243 parent = device_get_parent(dev); 244 MIIBUS_STATCHG(parent); 245 return; 246 } 247 248 static void 249 miibus_linkchg(device_t dev) 250 { 251 struct mii_data *mii; 252 device_t parent; 253 int link_state; 254 255 parent = device_get_parent(dev); 256 MIIBUS_LINKCHG(parent); 257 258 mii = device_get_softc(dev); 259 260 if (mii->mii_media_status & IFM_AVALID) { 261 if (mii->mii_media_status & IFM_ACTIVE) 262 link_state = LINK_STATE_UP; 263 else 264 link_state = LINK_STATE_DOWN; 265 } else 266 link_state = LINK_STATE_UNKNOWN; 267 /* 268 * Note that each NIC's softc must start with an ifnet structure. 269 */ 270 if_link_state_change(device_get_softc(parent), link_state); 271 } 272 273 static void 274 miibus_mediainit(device_t dev) 275 { 276 struct mii_data *mii; 277 struct ifmedia_entry *m; 278 int media = 0; 279 280 /* Poke the parent in case it has any media of its own to add. */ 281 MIIBUS_MEDIAINIT(device_get_parent(dev)); 282 283 mii = device_get_softc(dev); 284 LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) { 285 media = m->ifm_media; 286 if (media == (IFM_ETHER|IFM_AUTO)) 287 break; 288 } 289 290 ifmedia_set(&mii->mii_media, media); 291 292 return; 293 } 294 295 int 296 mii_phy_probe(device_t dev, device_t *child, ifm_change_cb_t ifmedia_upd, 297 ifm_stat_cb_t ifmedia_sts) 298 { 299 void **v; 300 int bmsr, i; 301 302 v = malloc(sizeof(vm_offset_t) * 2, M_DEVBUF, M_NOWAIT); 303 if (v == 0) { 304 return (ENOMEM); 305 } 306 v[0] = ifmedia_upd; 307 v[1] = ifmedia_sts; 308 *child = device_add_child(dev, "miibus", -1); 309 device_set_ivars(*child, v); 310 311 for (i = 0; i < MII_NPHY; i++) { 312 bmsr = MIIBUS_READREG(dev, i, MII_BMSR); 313 if (bmsr == 0 || bmsr == 0xffff || 314 (bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) { 315 /* Assume no PHY at this address. */ 316 continue; 317 } else 318 break; 319 } 320 321 if (i == MII_NPHY) { 322 device_delete_child(dev, *child); 323 *child = NULL; 324 return(ENXIO); 325 } 326 327 bus_generic_attach(dev); 328 329 return(0); 330 } 331 332 /* 333 * Media changed; notify all PHYs. 334 */ 335 int 336 mii_mediachg(struct mii_data *mii) 337 { 338 struct mii_softc *child; 339 int rv; 340 341 mii->mii_media_status = 0; 342 mii->mii_media_active = IFM_NONE; 343 344 LIST_FOREACH(child, &mii->mii_phys, mii_list) { 345 rv = (*child->mii_service)(child, mii, MII_MEDIACHG); 346 if (rv) 347 return (rv); 348 } 349 return (0); 350 } 351 352 /* 353 * Call the PHY tick routines, used during autonegotiation. 354 */ 355 void 356 mii_tick(struct mii_data *mii) 357 { 358 struct mii_softc *child; 359 360 LIST_FOREACH(child, &mii->mii_phys, mii_list) 361 (void) (*child->mii_service)(child, mii, MII_TICK); 362 } 363 364 /* 365 * Get media status from PHYs. 366 */ 367 void 368 mii_pollstat(struct mii_data *mii) 369 { 370 struct mii_softc *child; 371 372 mii->mii_media_status = 0; 373 mii->mii_media_active = IFM_NONE; 374 375 LIST_FOREACH(child, &mii->mii_phys, mii_list) 376 (void) (*child->mii_service)(child, mii, MII_POLLSTAT); 377 } 378 379 /* 380 * Inform the PHYs that the interface is down. 381 */ 382 void 383 mii_down(struct mii_data *mii) 384 { 385 struct mii_softc *child; 386 387 LIST_FOREACH(child, &mii->mii_phys, mii_list) 388 mii_phy_down(child); 389 } 390