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/cdefs.h> 50 __FBSDID("$FreeBSD$"); 51 52 #include <sys/param.h> 53 #include <sys/systm.h> 54 #include <sys/socket.h> 55 #include <sys/malloc.h> 56 #include <sys/module.h> 57 #include <sys/bus.h> 58 59 #include <net/if.h> 60 #include <net/if_media.h> 61 #include <net/route.h> 62 63 #include <dev/mii/mii.h> 64 #include <dev/mii/miivar.h> 65 66 MODULE_VERSION(miibus, 1); 67 68 #include "miibus_if.h" 69 70 static int miibus_readreg(device_t, int, int); 71 static int miibus_writereg(device_t, int, int, int); 72 static void miibus_statchg(device_t); 73 static void miibus_linkchg(device_t); 74 static void miibus_mediainit(device_t); 75 76 static device_method_t miibus_methods[] = { 77 /* device interface */ 78 DEVMETHOD(device_probe, miibus_probe), 79 DEVMETHOD(device_attach, miibus_attach), 80 DEVMETHOD(device_detach, miibus_detach), 81 DEVMETHOD(device_shutdown, bus_generic_shutdown), 82 83 /* bus interface */ 84 DEVMETHOD(bus_print_child, bus_generic_print_child), 85 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 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 /* 106 * Helper function used by network interface drivers, attaches PHYs 107 * to the network interface driver parent. 108 */ 109 110 int 111 miibus_probe(dev) 112 device_t dev; 113 { 114 struct mii_attach_args ma, *args; 115 struct mii_data *mii; 116 device_t child = NULL, parent; 117 int bmsr, capmask = 0xFFFFFFFF; 118 119 mii = device_get_softc(dev); 120 parent = device_get_parent(dev); 121 LIST_INIT(&mii->mii_phys); 122 123 for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) { 124 /* 125 * Check to see if there is a PHY at this address. Note, 126 * many braindead PHYs report 0/0 in their ID registers, 127 * so we test for media in the BMSR. 128 */ 129 bmsr = MIIBUS_READREG(parent, ma.mii_phyno, MII_BMSR); 130 if (bmsr == 0 || bmsr == 0xffff || 131 (bmsr & BMSR_MEDIAMASK) == 0) { 132 /* Assume no PHY at this address. */ 133 continue; 134 } 135 136 /* 137 * Extract the IDs. Braindead PHYs will be handled by 138 * the `ukphy' driver, as we have no ID information to 139 * match on. 140 */ 141 ma.mii_id1 = MIIBUS_READREG(parent, ma.mii_phyno, 142 MII_PHYIDR1); 143 ma.mii_id2 = MIIBUS_READREG(parent, ma.mii_phyno, 144 MII_PHYIDR2); 145 146 ma.mii_data = mii; 147 ma.mii_capmask = capmask; 148 149 args = malloc(sizeof(struct mii_attach_args), 150 M_DEVBUF, M_NOWAIT); 151 bcopy((char *)&ma, (char *)args, sizeof(ma)); 152 child = device_add_child(dev, NULL, -1); 153 device_set_ivars(child, args); 154 } 155 156 if (child == NULL) 157 return(ENXIO); 158 159 device_set_desc(dev, "MII bus"); 160 161 return(0); 162 } 163 164 int 165 miibus_attach(dev) 166 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(dev) 189 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_readreg(dev, phy, reg) 203 device_t dev; 204 int phy, reg; 205 { 206 device_t parent; 207 208 parent = device_get_parent(dev); 209 return(MIIBUS_READREG(parent, phy, reg)); 210 } 211 212 static int 213 miibus_writereg(dev, phy, reg, data) 214 device_t dev; 215 int phy, reg, data; 216 { 217 device_t parent; 218 219 parent = device_get_parent(dev); 220 return(MIIBUS_WRITEREG(parent, phy, reg, data)); 221 } 222 223 static void 224 miibus_statchg(dev) 225 device_t dev; 226 { 227 device_t parent; 228 229 parent = device_get_parent(dev); 230 MIIBUS_STATCHG(parent); 231 return; 232 } 233 234 void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */ 235 236 static void 237 miibus_linkchg(dev) 238 device_t dev; 239 { 240 struct mii_data *mii; 241 struct ifnet *ifp; 242 device_t parent; 243 int link, link_state; 244 245 parent = device_get_parent(dev); 246 MIIBUS_LINKCHG(parent); 247 248 mii = device_get_softc(dev); 249 /* 250 * Note that each NIC's softc must start with an ifnet structure. 251 */ 252 ifp = device_get_softc(parent); 253 254 if (mii->mii_media_status & IFM_AVALID) { 255 if (mii->mii_media_status & IFM_ACTIVE) { 256 link = NOTE_LINKUP; 257 link_state = LINK_STATE_UP; 258 } else { 259 link = NOTE_LINKDOWN; 260 link_state = LINK_STATE_DOWN; 261 } 262 } else { 263 link = NOTE_LINKINV; 264 link_state = LINK_STATE_UNKNOWN; 265 } 266 267 /* Notify that the link state has changed. */ 268 if (ifp->if_link_state != link_state) { 269 ifp->if_link_state = link_state; 270 rt_ifmsg(ifp); 271 KNOTE(&ifp->if_klist, link); 272 if (ifp->if_nvlans != 0) 273 (*vlan_link_state_p)(ifp, link); 274 } 275 } 276 277 static void 278 miibus_mediainit(dev) 279 device_t dev; 280 { 281 struct mii_data *mii; 282 struct ifmedia_entry *m; 283 int media = 0; 284 285 /* Poke the parent in case it has any media of its own to add. */ 286 MIIBUS_MEDIAINIT(device_get_parent(dev)); 287 288 mii = device_get_softc(dev); 289 LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) { 290 media = m->ifm_media; 291 if (media == (IFM_ETHER|IFM_AUTO)) 292 break; 293 } 294 295 ifmedia_set(&mii->mii_media, media); 296 297 return; 298 } 299 300 int 301 mii_phy_probe(dev, child, ifmedia_upd, ifmedia_sts) 302 device_t dev; 303 device_t *child; 304 ifm_change_cb_t ifmedia_upd; 305 ifm_stat_cb_t ifmedia_sts; 306 { 307 void **v; 308 int bmsr, i; 309 310 v = malloc(sizeof(vm_offset_t) * 2, M_DEVBUF, M_NOWAIT); 311 if (v == 0) { 312 return (ENOMEM); 313 } 314 v[0] = ifmedia_upd; 315 v[1] = ifmedia_sts; 316 *child = device_add_child(dev, "miibus", -1); 317 device_set_ivars(*child, v); 318 319 for (i = 0; i < MII_NPHY; i++) { 320 bmsr = MIIBUS_READREG(dev, i, MII_BMSR); 321 if (bmsr == 0 || bmsr == 0xffff || 322 (bmsr & BMSR_MEDIAMASK) == 0) { 323 /* Assume no PHY at this address. */ 324 continue; 325 } else 326 break; 327 } 328 329 if (i == MII_NPHY) { 330 device_delete_child(dev, *child); 331 *child = NULL; 332 return(ENXIO); 333 } 334 335 bus_generic_attach(dev); 336 337 return(0); 338 } 339 340 /* 341 * Media changed; notify all PHYs. 342 */ 343 int 344 mii_mediachg(mii) 345 struct mii_data *mii; 346 { 347 struct mii_softc *child; 348 int rv; 349 350 mii->mii_media_status = 0; 351 mii->mii_media_active = IFM_NONE; 352 353 LIST_FOREACH(child, &mii->mii_phys, mii_list) { 354 rv = (*child->mii_service)(child, mii, MII_MEDIACHG); 355 if (rv) 356 return (rv); 357 } 358 return (0); 359 } 360 361 /* 362 * Call the PHY tick routines, used during autonegotiation. 363 */ 364 void 365 mii_tick(mii) 366 struct mii_data *mii; 367 { 368 struct mii_softc *child; 369 370 LIST_FOREACH(child, &mii->mii_phys, mii_list) 371 (void) (*child->mii_service)(child, mii, MII_TICK); 372 } 373 374 /* 375 * Get media status from PHYs. 376 */ 377 void 378 mii_pollstat(mii) 379 struct mii_data *mii; 380 { 381 struct mii_softc *child; 382 383 mii->mii_media_status = 0; 384 mii->mii_media_active = IFM_NONE; 385 386 LIST_FOREACH(child, &mii->mii_phys, mii_list) 387 (void) (*child->mii_service)(child, mii, MII_POLLSTAT); 388 } 389 390 /* 391 * Inform the PHYs that the interface is down. 392 */ 393 void 394 mii_down(struct mii_data *mii) 395 { 396 struct mii_softc *child; 397 398 LIST_FOREACH(child, &mii->mii_phys, mii_list) 399 mii_phy_down(child); 400 } 401