mii.c revision 84333
180708Sjake/* $NetBSD: mii.c,v 1.12 1999/08/03 19:41:49 drochner Exp $ */ 280708Sjake 380708Sjake/*- 480708Sjake * Copyright (c) 1998 The NetBSD Foundation, Inc. 580708Sjake * All rights reserved. 680708Sjake * 780708Sjake * This code is derived from software contributed to The NetBSD Foundation 880708Sjake * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 980708Sjake * NASA Ames Research Center. 1080708Sjake * 1180708Sjake * Redistribution and use in source and binary forms, with or without 1280708Sjake * modification, are permitted provided that the following conditions 1380708Sjake * are met: 1480708Sjake * 1. Redistributions of source code must retain the above copyright 1580708Sjake * notice, this list of conditions and the following disclaimer. 1680708Sjake * 2. Redistributions in binary form must reproduce the above copyright 1780708Sjake * notice, this list of conditions and the following disclaimer in the 1880708Sjake * documentation and/or other materials provided with the distribution. 1980708Sjake * 3. All advertising materials mentioning features or use of this software 2080708Sjake * must display the following acknowledgement: 2180708Sjake * This product includes software developed by the NetBSD 2280708Sjake * Foundation, Inc. and its contributors. 2380708Sjake * 4. Neither the name of The NetBSD Foundation nor the names of its 2480708Sjake * contributors may be used to endorse or promote products derived 2580708Sjake * from this software without specific prior written permission. 2680708Sjake * 2780708Sjake * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2880708Sjake * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2980708Sjake * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 3080708Sjake * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 3180708Sjake * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 3280708Sjake * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 3380708Sjake * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3480708Sjake * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3580708Sjake * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3680708Sjake * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3780708Sjake * POSSIBILITY OF SUCH DAMAGE. 3880708Sjake */ 3980708Sjake 4080708Sjake/* 4180708Sjake * MII bus layer, glues MII-capable network interface drivers to sharable 4280708Sjake * PHY drivers. This exports an interface compatible with BSD/OS 3.0's, 4380708Sjake * plus some NetBSD extensions. 4480708Sjake */ 45 46#include <sys/param.h> 47#include <sys/systm.h> 48#include <sys/socket.h> 49#include <sys/malloc.h> 50#include <sys/module.h> 51#include <sys/bus.h> 52 53#include <net/if.h> 54#include <net/if_media.h> 55 56#include <dev/mii/mii.h> 57#include <dev/mii/miivar.h> 58 59MODULE_VERSION(miibus, 1); 60 61#include "miibus_if.h" 62 63#if !defined(lint) 64static const char rcsid[] = 65 "$FreeBSD: head/sys/dev/mii/mii.c 84333 2001-10-01 22:57:57Z mjacob $"; 66#endif 67 68static int miibus_readreg __P((device_t, int, int)); 69static int miibus_writereg __P((device_t, int, int, int)); 70static void miibus_statchg __P((device_t)); 71static void miibus_linkchg __P((device_t)); 72static void miibus_mediainit __P((device_t)); 73 74static device_method_t miibus_methods[] = { 75 /* device interface */ 76 DEVMETHOD(device_probe, miibus_probe), 77 DEVMETHOD(device_attach, miibus_attach), 78 DEVMETHOD(device_detach, miibus_detach), 79 DEVMETHOD(device_shutdown, bus_generic_shutdown), 80 81 /* bus interface */ 82 DEVMETHOD(bus_print_child, bus_generic_print_child), 83 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 84 85 /* MII interface */ 86 DEVMETHOD(miibus_readreg, miibus_readreg), 87 DEVMETHOD(miibus_writereg, miibus_writereg), 88 DEVMETHOD(miibus_statchg, miibus_statchg), 89 DEVMETHOD(miibus_linkchg, miibus_linkchg), 90 DEVMETHOD(miibus_mediainit, miibus_mediainit), 91 92 { 0, 0 } 93}; 94 95devclass_t miibus_devclass; 96 97driver_t miibus_driver = { 98 "miibus", 99 miibus_methods, 100 sizeof(struct mii_data) 101}; 102 103/* 104 * Helper function used by network interface drivers, attaches PHYs 105 * to the network interface driver parent. 106 */ 107 108int miibus_probe(dev) 109 device_t dev; 110{ 111 struct mii_attach_args ma, *args; 112 struct mii_data *mii; 113 device_t child = NULL, parent; 114 int bmsr, capmask = 0xFFFFFFFF; 115 116 mii = device_get_softc(dev); 117 parent = device_get_parent(dev); 118 LIST_INIT(&mii->mii_phys); 119 120 for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) { 121 /* 122 * Check to see if there is a PHY at this address. Note, 123 * many braindead PHYs report 0/0 in their ID registers, 124 * so we test for media in the BMSR. 125 */ 126 bmsr = MIIBUS_READREG(parent, ma.mii_phyno, MII_BMSR); 127 if (bmsr == 0 || bmsr == 0xffff || 128 (bmsr & BMSR_MEDIAMASK) == 0) { 129 /* Assume no PHY at this address. */ 130 continue; 131 } 132 133 /* 134 * Extract the IDs. Braindead PHYs will be handled by 135 * the `ukphy' driver, as we have no ID information to 136 * match on. 137 */ 138 ma.mii_id1 = MIIBUS_READREG(parent, ma.mii_phyno, 139 MII_PHYIDR1); 140 ma.mii_id2 = MIIBUS_READREG(parent, ma.mii_phyno, 141 MII_PHYIDR2); 142 143 ma.mii_data = mii; 144 ma.mii_capmask = capmask; 145 146 args = malloc(sizeof(struct mii_attach_args), 147 M_DEVBUF, M_NOWAIT); 148 bcopy((char *)&ma, (char *)args, sizeof(ma)); 149 child = device_add_child(dev, NULL, -1); 150 device_set_ivars(child, args); 151 } 152 153 if (child == NULL) 154 return(ENXIO); 155 156 device_set_desc(dev, "MII bus"); 157 158 return(0); 159} 160 161int 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 183int miibus_detach(dev) 184 device_t dev; 185{ 186 struct mii_data *mii; 187 188 bus_generic_detach(dev); 189 mii = device_get_softc(dev); 190 ifmedia_removeall(&mii->mii_media); 191 mii->mii_ifp = NULL; 192 193 return(0); 194} 195 196static int miibus_readreg(dev, phy, reg) 197 device_t dev; 198 int phy, reg; 199{ 200 device_t parent; 201 202 parent = device_get_parent(dev); 203 return(MIIBUS_READREG(parent, phy, reg)); 204} 205 206static int miibus_writereg(dev, phy, reg, data) 207 device_t dev; 208 int phy, reg, data; 209{ 210 device_t parent; 211 212 parent = device_get_parent(dev); 213 return(MIIBUS_WRITEREG(parent, phy, reg, data)); 214} 215 216static void miibus_statchg(dev) 217 device_t dev; 218{ 219 device_t parent; 220 221 parent = device_get_parent(dev); 222 MIIBUS_STATCHG(parent); 223 return; 224} 225 226static void 227miibus_linkchg(dev) 228 device_t dev; 229{ 230 struct mii_data *mii; 231 struct ifnet *ifp; 232 device_t parent; 233 int link; 234 235 parent = device_get_parent(dev); 236 MIIBUS_LINKCHG(parent); 237 238 mii = device_get_softc(dev); 239 /* 240 * Note that each NIC's softc must start with an ifnet structure. 241 */ 242 ifp = device_get_softc(parent); 243 244 if (mii->mii_media_status & IFM_AVALID) { 245 if (mii->mii_media_status & IFM_ACTIVE) 246 link = NOTE_LINKUP; 247 else 248 link = NOTE_LINKDOWN; 249 } else { 250 link = NOTE_LINKINV; 251 } 252 253 KNOTE(&ifp->if_klist, link); 254} 255 256static void miibus_mediainit(dev) 257 device_t dev; 258{ 259 struct mii_data *mii; 260 struct ifmedia_entry *m; 261 int media = 0; 262 263 /* Poke the parent in case it has any media of its own to add. */ 264 MIIBUS_MEDIAINIT(device_get_parent(dev)); 265 266 mii = device_get_softc(dev); 267 LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) { 268 media = m->ifm_media; 269 if (media == (IFM_ETHER|IFM_AUTO)) 270 break; 271 } 272 273 ifmedia_set(&mii->mii_media, media); 274 275 return; 276} 277 278int mii_phy_probe(dev, child, ifmedia_upd, ifmedia_sts) 279 device_t dev; 280 device_t *child; 281 ifm_change_cb_t ifmedia_upd; 282 ifm_stat_cb_t ifmedia_sts; 283{ 284 void **v; 285 int bmsr, i; 286 287 v = malloc(sizeof(vm_offset_t) * 2, M_DEVBUF, M_NOWAIT); 288 if (v == 0) { 289 return (ENOMEM); 290 } 291 v[0] = ifmedia_upd; 292 v[1] = ifmedia_sts; 293 *child = device_add_child(dev, "miibus", -1); 294 device_set_ivars(*child, v); 295 296 for (i = 0; i < MII_NPHY; i++) { 297 bmsr = MIIBUS_READREG(dev, i, MII_BMSR); 298 if (bmsr == 0 || bmsr == 0xffff || 299 (bmsr & BMSR_MEDIAMASK) == 0) { 300 /* Assume no PHY at this address. */ 301 continue; 302 } else 303 break; 304 } 305 306 if (i == MII_NPHY) { 307 device_delete_child(dev, *child); 308 *child = NULL; 309 return(ENXIO); 310 } 311 312 bus_generic_attach(dev); 313 314 return(0); 315} 316 317/* 318 * Media changed; notify all PHYs. 319 */ 320int 321mii_mediachg(mii) 322 struct mii_data *mii; 323{ 324 struct mii_softc *child; 325 int rv; 326 327 mii->mii_media_status = 0; 328 mii->mii_media_active = IFM_NONE; 329 330 LIST_FOREACH(child, &mii->mii_phys, mii_list) { 331 rv = (*child->mii_service)(child, mii, MII_MEDIACHG); 332 if (rv) 333 return (rv); 334 } 335 return (0); 336} 337 338/* 339 * Call the PHY tick routines, used during autonegotiation. 340 */ 341void 342mii_tick(mii) 343 struct mii_data *mii; 344{ 345 struct mii_softc *child; 346 347 LIST_FOREACH(child, &mii->mii_phys, mii_list) 348 (void) (*child->mii_service)(child, mii, MII_TICK); 349} 350 351/* 352 * Get media status from PHYs. 353 */ 354void 355mii_pollstat(mii) 356 struct mii_data *mii; 357{ 358 struct mii_softc *child; 359 360 mii->mii_media_status = 0; 361 mii->mii_media_active = IFM_NONE; 362 363 LIST_FOREACH(child, &mii->mii_phys, mii_list) 364 (void) (*child->mii_service)(child, mii, MII_POLLSTAT); 365} 366