1 /* $NetBSD: mii.c,v 1.12 1999/08/03 19:41:49 drochner Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
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.
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>
57 #include <net/if_media.h>
58 #include <net/route.h>
60 #include <dev/mii/mii.h>
61 #include <dev/mii/miivar.h>
63 MODULE_VERSION(miibus, 1);
65 #include "miibus_if.h"
67 static int miibus_print_child(device_t dev, device_t child);
68 static int miibus_child_location_str(device_t bus, device_t child, char *buf,
70 static int miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
72 static int miibus_readreg(device_t, int, int);
73 static int miibus_writereg(device_t, int, int, int);
74 static void miibus_statchg(device_t);
75 static void miibus_linkchg(device_t);
76 static void miibus_mediainit(device_t);
78 static device_method_t miibus_methods[] = {
79 /* device interface */
80 DEVMETHOD(device_probe, miibus_probe),
81 DEVMETHOD(device_attach, miibus_attach),
82 DEVMETHOD(device_detach, miibus_detach),
83 DEVMETHOD(device_shutdown, bus_generic_shutdown),
86 DEVMETHOD(bus_print_child, miibus_print_child),
87 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
88 DEVMETHOD(bus_child_pnpinfo_str, miibus_child_pnpinfo_str),
89 DEVMETHOD(bus_child_location_str, miibus_child_location_str),
92 DEVMETHOD(miibus_readreg, miibus_readreg),
93 DEVMETHOD(miibus_writereg, miibus_writereg),
94 DEVMETHOD(miibus_statchg, miibus_statchg),
95 DEVMETHOD(miibus_linkchg, miibus_linkchg),
96 DEVMETHOD(miibus_mediainit, miibus_mediainit),
101 devclass_t miibus_devclass;
103 driver_t miibus_driver = {
106 sizeof(struct mii_data)
109 struct miibus_ivars {
110 ifm_change_cb_t ifmedia_upd;
111 ifm_stat_cb_t ifmedia_sts;
115 * Helper function used by network interface drivers, attaches PHYs
116 * to the network interface driver parent.
119 miibus_probe(device_t dev)
121 struct mii_attach_args ma, *args;
122 struct mii_data *mii;
123 device_t child = NULL, parent;
124 int bmsr, capmask = 0xFFFFFFFF;
126 mii = device_get_softc(dev);
127 parent = device_get_parent(dev);
128 LIST_INIT(&mii->mii_phys);
130 for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) {
132 * Check to see if there is a PHY at this address. Note,
133 * many braindead PHYs report 0/0 in their ID registers,
134 * so we test for media in the BMSR.
136 bmsr = MIIBUS_READREG(parent, ma.mii_phyno, MII_BMSR);
137 if (bmsr == 0 || bmsr == 0xffff ||
138 (bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) {
139 /* Assume no PHY at this address. */
144 * Extract the IDs. Braindead PHYs will be handled by
145 * the `ukphy' driver, as we have no ID information to
148 ma.mii_id1 = MIIBUS_READREG(parent, ma.mii_phyno,
150 ma.mii_id2 = MIIBUS_READREG(parent, ma.mii_phyno,
154 ma.mii_capmask = capmask;
156 args = malloc(sizeof(struct mii_attach_args),
158 bcopy((char *)&ma, (char *)args, sizeof(ma));
159 child = device_add_child(dev, NULL, -1);
160 device_set_ivars(child, args);
166 device_set_desc(dev, "MII bus");
172 miibus_attach(device_t dev)
174 struct miibus_ivars *ivars;
175 struct mii_data *mii;
177 mii = device_get_softc(dev);
179 * Note that each NIC's softc must start with an ifnet pointer.
182 mii->mii_ifp = *(struct ifnet**)device_get_softc(device_get_parent(dev));
183 ivars = device_get_ivars(dev);
184 ifmedia_init(&mii->mii_media, IFM_IMASK, ivars->ifmedia_upd,
186 bus_generic_attach(dev);
192 miibus_detach(device_t dev)
194 struct mii_data *mii;
196 bus_generic_detach(dev);
197 mii = device_get_softc(dev);
198 ifmedia_removeall(&mii->mii_media);
205 miibus_print_child(device_t dev, device_t child)
207 struct mii_attach_args *ma;
210 ma = device_get_ivars(child);
211 retval = bus_print_child_header(dev, child);
212 retval += printf(" PHY %d", ma->mii_phyno);
213 retval += bus_print_child_footer(dev, child);
219 miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
222 struct mii_attach_args *maa = device_get_ivars(child);
223 snprintf(buf, buflen, "oui=0x%x model=0x%x rev=0x%x",
224 MII_OUI(maa->mii_id1, maa->mii_id2),
225 MII_MODEL(maa->mii_id2), MII_REV(maa->mii_id2));
230 miibus_child_location_str(device_t bus, device_t child, char *buf,
233 struct mii_attach_args *maa = device_get_ivars(child);
234 snprintf(buf, buflen, "phyno=%d", maa->mii_phyno);
239 miibus_readreg(device_t dev, int phy, int reg)
243 parent = device_get_parent(dev);
244 return(MIIBUS_READREG(parent, phy, reg));
248 miibus_writereg(device_t dev, int phy, int reg, int data)
252 parent = device_get_parent(dev);
253 return(MIIBUS_WRITEREG(parent, phy, reg, data));
257 miibus_statchg(device_t dev)
260 struct mii_data *mii;
263 parent = device_get_parent(dev);
264 MIIBUS_STATCHG(parent);
266 mii = device_get_softc(dev);
269 * Note that each NIC's softc must start with an ifnet pointer.
272 ifp = *(struct ifnet **)device_get_softc(parent);
273 ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
278 miibus_linkchg(device_t dev)
280 struct mii_data *mii;
284 parent = device_get_parent(dev);
285 MIIBUS_LINKCHG(parent);
287 mii = device_get_softc(dev);
289 if (mii->mii_media_status & IFM_AVALID) {
290 if (mii->mii_media_status & IFM_ACTIVE)
291 link_state = LINK_STATE_UP;
293 link_state = LINK_STATE_DOWN;
295 link_state = LINK_STATE_UNKNOWN;
297 * Note that each NIC's softc must start with an ifnet pointer.
300 if_link_state_change(*(struct ifnet**)device_get_softc(parent), link_state);
304 miibus_mediainit(device_t dev)
306 struct mii_data *mii;
307 struct ifmedia_entry *m;
310 /* Poke the parent in case it has any media of its own to add. */
311 MIIBUS_MEDIAINIT(device_get_parent(dev));
313 mii = device_get_softc(dev);
314 LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) {
315 media = m->ifm_media;
316 if (media == (IFM_ETHER|IFM_AUTO))
320 ifmedia_set(&mii->mii_media, media);
326 mii_phy_probe(device_t dev, device_t *child, ifm_change_cb_t ifmedia_upd,
327 ifm_stat_cb_t ifmedia_sts)
329 struct miibus_ivars *ivars;
332 ivars = malloc(sizeof(*ivars), M_DEVBUF, M_NOWAIT);
335 ivars->ifmedia_upd = ifmedia_upd;
336 ivars->ifmedia_sts = ifmedia_sts;
337 *child = device_add_child(dev, "miibus", -1);
338 device_set_ivars(*child, ivars);
340 for (i = 0; i < MII_NPHY; i++) {
341 bmsr = MIIBUS_READREG(dev, i, MII_BMSR);
342 if (bmsr == 0 || bmsr == 0xffff ||
343 (bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) {
344 /* Assume no PHY at this address. */
351 device_delete_child(dev, *child);
356 bus_generic_attach(dev);
362 * Media changed; notify all PHYs.
365 mii_mediachg(struct mii_data *mii)
367 struct mii_softc *child;
370 mii->mii_media_status = 0;
371 mii->mii_media_active = IFM_NONE;
373 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
374 rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
382 * Call the PHY tick routines, used during autonegotiation.
385 mii_tick(struct mii_data *mii)
387 struct mii_softc *child;
389 LIST_FOREACH(child, &mii->mii_phys, mii_list)
390 (void) (*child->mii_service)(child, mii, MII_TICK);
394 * Get media status from PHYs.
397 mii_pollstat(struct mii_data *mii)
399 struct mii_softc *child;
401 mii->mii_media_status = 0;
402 mii->mii_media_active = IFM_NONE;
404 LIST_FOREACH(child, &mii->mii_phys, mii_list)
405 (void) (*child->mii_service)(child, mii, MII_POLLSTAT);
409 * Inform the PHYs that the interface is down.
412 mii_down(struct mii_data *mii)
414 struct mii_softc *child;
416 LIST_FOREACH(child, &mii->mii_phys, mii_list)