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