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.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * MII bus layer, glues MII-capable network interface drivers to sharable
38 * PHY drivers. This exports an interface compatible with BSD/OS 3.0's,
39 * plus some NetBSD extensions.
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/socket.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
50 #include <net/if_media.h>
51 #include <net/route.h>
53 #include <dev/mii/mii.h>
54 #include <dev/mii/miivar.h>
56 MODULE_VERSION(miibus, 1);
58 #include "miibus_if.h"
60 static int miibus_print_child(device_t dev, device_t child);
61 static int miibus_child_location_str(device_t bus, device_t child, char *buf,
63 static int miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
65 static int miibus_readreg(device_t, int, int);
66 static int miibus_writereg(device_t, int, int, int);
67 static void miibus_statchg(device_t);
68 static void miibus_linkchg(device_t);
69 static void miibus_mediainit(device_t);
71 static device_method_t miibus_methods[] = {
72 /* device interface */
73 DEVMETHOD(device_probe, miibus_probe),
74 DEVMETHOD(device_attach, miibus_attach),
75 DEVMETHOD(device_detach, miibus_detach),
76 DEVMETHOD(device_shutdown, bus_generic_shutdown),
79 DEVMETHOD(bus_print_child, miibus_print_child),
80 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
81 DEVMETHOD(bus_child_pnpinfo_str, miibus_child_pnpinfo_str),
82 DEVMETHOD(bus_child_location_str, miibus_child_location_str),
85 DEVMETHOD(miibus_readreg, miibus_readreg),
86 DEVMETHOD(miibus_writereg, miibus_writereg),
87 DEVMETHOD(miibus_statchg, miibus_statchg),
88 DEVMETHOD(miibus_linkchg, miibus_linkchg),
89 DEVMETHOD(miibus_mediainit, miibus_mediainit),
94 devclass_t miibus_devclass;
96 driver_t miibus_driver = {
99 sizeof(struct mii_data)
102 struct miibus_ivars {
103 ifm_change_cb_t ifmedia_upd;
104 ifm_stat_cb_t ifmedia_sts;
108 * Helper function used by network interface drivers, attaches PHYs
109 * to the network interface driver parent.
112 miibus_probe(device_t dev)
114 struct mii_attach_args ma, *args;
115 struct mii_data *mii;
116 device_t child = NULL, parent;
117 int bmsr, capmask = 0xFFFFFFFF;
119 mii = device_get_softc(dev);
120 parent = device_get_parent(dev);
121 LIST_INIT(&mii->mii_phys);
123 for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) {
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.
129 bmsr = MIIBUS_READREG(parent, ma.mii_phyno, MII_BMSR);
130 if (bmsr == 0 || bmsr == 0xffff ||
131 (bmsr & (BMSR_EXTSTAT | BMSR_MEDIAMASK)) == 0) {
132 /* Assume no PHY at this address. */
137 * Extract the IDs. Braindead PHYs will be handled by
138 * the `ukphy' driver, as we have no ID information to
141 ma.mii_id1 = MIIBUS_READREG(parent, ma.mii_phyno,
143 ma.mii_id2 = MIIBUS_READREG(parent, ma.mii_phyno,
147 ma.mii_capmask = capmask;
149 args = malloc(sizeof(struct mii_attach_args),
151 bcopy((char *)&ma, (char *)args, sizeof(ma));
152 child = device_add_child(dev, NULL, -1);
153 device_set_ivars(child, args);
159 device_set_desc(dev, "MII bus");
165 miibus_attach(device_t dev)
167 struct miibus_ivars *ivars;
168 struct mii_data *mii;
170 mii = device_get_softc(dev);
172 * Note that each NIC's softc must start with an ifnet pointer.
175 mii->mii_ifp = *(struct ifnet**)device_get_softc(device_get_parent(dev));
176 mii->mii_ifp->if_capabilities |= IFCAP_LINKSTATE;
177 mii->mii_ifp->if_capenable |= IFCAP_LINKSTATE;
178 ivars = device_get_ivars(dev);
179 ifmedia_init(&mii->mii_media, IFM_IMASK, ivars->ifmedia_upd,
181 bus_generic_attach(dev);
187 miibus_detach(device_t dev)
189 struct mii_data *mii;
191 bus_generic_detach(dev);
192 mii = device_get_softc(dev);
193 ifmedia_removeall(&mii->mii_media);
200 miibus_print_child(device_t dev, device_t child)
202 struct mii_attach_args *ma;
205 ma = device_get_ivars(child);
206 retval = bus_print_child_header(dev, child);
207 retval += printf(" PHY %d", ma->mii_phyno);
208 retval += bus_print_child_footer(dev, child);
214 miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
217 struct mii_attach_args *ma;
219 ma = device_get_ivars(child);
220 snprintf(buf, buflen, "oui=0x%x model=0x%x rev=0x%x",
221 MII_OUI(ma->mii_id1, ma->mii_id2),
222 MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2));
227 miibus_child_location_str(device_t bus, device_t child, char *buf,
230 struct mii_attach_args *ma;
232 ma = device_get_ivars(child);
233 snprintf(buf, buflen, "phyno=%d", ma->mii_phyno);
238 miibus_readreg(device_t dev, int phy, int reg)
242 parent = device_get_parent(dev);
243 return (MIIBUS_READREG(parent, phy, reg));
247 miibus_writereg(device_t dev, int phy, int reg, int data)
251 parent = device_get_parent(dev);
252 return (MIIBUS_WRITEREG(parent, phy, reg, data));
256 miibus_statchg(device_t dev)
259 struct mii_data *mii;
262 parent = device_get_parent(dev);
263 MIIBUS_STATCHG(parent);
265 mii = device_get_softc(dev);
268 * Note that each NIC's softc must start with an ifnet pointer.
271 ifp = *(struct ifnet **)device_get_softc(parent);
272 ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
276 miibus_linkchg(device_t dev)
278 struct mii_data *mii;
282 parent = device_get_parent(dev);
283 MIIBUS_LINKCHG(parent);
285 mii = device_get_softc(dev);
287 if (mii->mii_media_status & IFM_AVALID) {
288 if (mii->mii_media_status & IFM_ACTIVE)
289 link_state = LINK_STATE_UP;
291 link_state = LINK_STATE_DOWN;
293 link_state = LINK_STATE_UNKNOWN;
295 * Note that each NIC's softc must start with an ifnet pointer.
298 if_link_state_change(*(struct ifnet**)device_get_softc(parent), link_state);
302 miibus_mediainit(device_t dev)
304 struct mii_data *mii;
305 struct ifmedia_entry *m;
308 /* Poke the parent in case it has any media of its own to add. */
309 MIIBUS_MEDIAINIT(device_get_parent(dev));
311 mii = device_get_softc(dev);
312 LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) {
313 media = m->ifm_media;
314 if (media == (IFM_ETHER | IFM_AUTO))
318 ifmedia_set(&mii->mii_media, media);
322 mii_phy_probe(device_t dev, device_t *child, ifm_change_cb_t ifmedia_upd,
323 ifm_stat_cb_t ifmedia_sts)
325 struct miibus_ivars *ivars;
328 ivars = malloc(sizeof(*ivars), M_DEVBUF, M_NOWAIT);
331 ivars->ifmedia_upd = ifmedia_upd;
332 ivars->ifmedia_sts = ifmedia_sts;
333 *child = device_add_child(dev, "miibus", -1);
334 device_set_ivars(*child, ivars);
336 for (i = 0; i < MII_NPHY; i++) {
337 bmsr = MIIBUS_READREG(dev, i, MII_BMSR);
338 if (bmsr == 0 || bmsr == 0xffff ||
339 (bmsr & (BMSR_EXTSTAT | BMSR_MEDIAMASK)) == 0) {
340 /* Assume no PHY at this address. */
347 device_delete_child(dev, *child);
352 bus_generic_attach(dev);
358 * Media changed; notify all PHYs.
361 mii_mediachg(struct mii_data *mii)
363 struct mii_softc *child;
364 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
367 mii->mii_media_status = 0;
368 mii->mii_media_active = IFM_NONE;
370 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
372 * If the media indicates a different PHY instance,
375 if (IFM_INST(ife->ifm_media) != child->mii_inst) {
376 if ((child->mii_flags & MIIF_NOISOLATE) != 0) {
377 device_printf(child->mii_dev, "%s: "
378 "can't handle non-zero PHY instance %d\n",
379 __func__, child->mii_inst);
382 PHY_WRITE(child, MII_BMCR, PHY_READ(child, MII_BMCR) |
386 rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
394 * Call the PHY tick routines, used during autonegotiation.
397 mii_tick(struct mii_data *mii)
399 struct mii_softc *child;
400 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
402 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
404 * If this PHY instance isn't currently selected, just skip
407 if (IFM_INST(ife->ifm_media) != child->mii_inst)
409 (void)(*child->mii_service)(child, mii, MII_TICK);
414 * Get media status from PHYs.
417 mii_pollstat(struct mii_data *mii)
419 struct mii_softc *child;
420 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
422 mii->mii_media_status = 0;
423 mii->mii_media_active = IFM_NONE;
425 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
427 * If we're not polling this PHY instance, just skip it.
429 if (IFM_INST(ife->ifm_media) != child->mii_inst)
431 (void)(*child->mii_service)(child, mii, MII_POLLSTAT);
436 * Inform the PHYs that the interface is down.
439 mii_down(struct mii_data *mii)
441 struct mii_softc *child;
443 LIST_FOREACH(child, &mii->mii_phys, mii_list)