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