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_read_ivar(device_t dev, device_t child, int which,
63 static int miibus_child_location_str(device_t bus, device_t child, char *buf,
65 static int miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
67 static int miibus_readreg(device_t, int, int);
68 static int miibus_writereg(device_t, int, int, int);
69 static void miibus_statchg(device_t);
70 static void miibus_linkchg(device_t);
71 static void miibus_mediainit(device_t);
73 static device_method_t miibus_methods[] = {
74 /* device interface */
75 DEVMETHOD(device_probe, miibus_probe),
76 DEVMETHOD(device_attach, miibus_attach),
77 DEVMETHOD(device_detach, miibus_detach),
78 DEVMETHOD(device_shutdown, bus_generic_shutdown),
81 DEVMETHOD(bus_print_child, miibus_print_child),
82 DEVMETHOD(bus_read_ivar, miibus_read_ivar),
83 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
84 DEVMETHOD(bus_child_pnpinfo_str, miibus_child_pnpinfo_str),
85 DEVMETHOD(bus_child_location_str, miibus_child_location_str),
88 DEVMETHOD(miibus_readreg, miibus_readreg),
89 DEVMETHOD(miibus_writereg, miibus_writereg),
90 DEVMETHOD(miibus_statchg, miibus_statchg),
91 DEVMETHOD(miibus_linkchg, miibus_linkchg),
92 DEVMETHOD(miibus_mediainit, miibus_mediainit),
97 devclass_t miibus_devclass;
99 driver_t miibus_driver = {
102 sizeof(struct mii_data)
105 struct miibus_ivars {
107 ifm_change_cb_t ifmedia_upd;
108 ifm_stat_cb_t ifmedia_sts;
113 miibus_probe(device_t dev)
116 device_set_desc(dev, "MII bus");
118 return (BUS_PROBE_SPECIFIC);
122 miibus_attach(device_t dev)
124 struct miibus_ivars *ivars;
125 struct mii_attach_args *ma;
126 struct mii_data *mii;
130 mii = device_get_softc(dev);
132 if (device_get_children(dev, &children, &nchildren) == 0) {
133 for (i = 0; i < nchildren; i++) {
134 ma = device_get_ivars(children[i]);
137 free(children, M_TEMP);
139 if (nchildren == 0) {
140 device_printf(dev, "cannot get children\n");
143 ivars = device_get_ivars(dev);
144 ifmedia_init(&mii->mii_media, IFM_IMASK, ivars->ifmedia_upd,
146 mii->mii_ifp = ivars->ifp;
147 mii->mii_ifp->if_capabilities |= IFCAP_LINKSTATE;
148 mii->mii_ifp->if_capenable |= IFCAP_LINKSTATE;
149 LIST_INIT(&mii->mii_phys);
151 return (bus_generic_attach(dev));
155 miibus_detach(device_t dev)
157 struct mii_data *mii;
159 bus_generic_detach(dev);
160 mii = device_get_softc(dev);
161 ifmedia_removeall(&mii->mii_media);
168 miibus_print_child(device_t dev, device_t child)
170 struct mii_attach_args *ma;
173 ma = device_get_ivars(child);
174 retval = bus_print_child_header(dev, child);
175 retval += printf(" PHY %d", ma->mii_phyno);
176 retval += bus_print_child_footer(dev, child);
182 miibus_read_ivar(device_t dev, device_t child __unused, int which,
185 struct miibus_ivars *ivars;
188 * NB: this uses the instance variables of the miibus rather than
191 ivars = device_get_ivars(dev);
193 case MIIBUS_IVAR_FLAGS:
194 *result = ivars->mii_flags;
203 miibus_child_pnpinfo_str(device_t bus __unused, device_t child, char *buf,
206 struct mii_attach_args *ma;
208 ma = device_get_ivars(child);
209 snprintf(buf, buflen, "oui=0x%x model=0x%x rev=0x%x",
210 MII_OUI(ma->mii_id1, ma->mii_id2),
211 MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2));
216 miibus_child_location_str(device_t bus __unused, device_t child, char *buf,
219 struct mii_attach_args *ma;
221 ma = device_get_ivars(child);
222 snprintf(buf, buflen, "phyno=%d", ma->mii_phyno);
227 miibus_readreg(device_t dev, int phy, int reg)
231 parent = device_get_parent(dev);
232 return (MIIBUS_READREG(parent, phy, reg));
236 miibus_writereg(device_t dev, int phy, int reg, int data)
240 parent = device_get_parent(dev);
241 return (MIIBUS_WRITEREG(parent, phy, reg, data));
245 miibus_statchg(device_t dev)
248 struct mii_data *mii;
251 parent = device_get_parent(dev);
252 MIIBUS_STATCHG(parent);
254 mii = device_get_softc(dev);
257 * Note that each NIC's softc must start with an ifnet pointer.
260 ifp = *(struct ifnet **)device_get_softc(parent);
261 ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
265 miibus_linkchg(device_t dev)
267 struct mii_data *mii;
271 parent = device_get_parent(dev);
272 MIIBUS_LINKCHG(parent);
274 mii = device_get_softc(dev);
276 if (mii->mii_media_status & IFM_AVALID) {
277 if (mii->mii_media_status & IFM_ACTIVE)
278 link_state = LINK_STATE_UP;
280 link_state = LINK_STATE_DOWN;
282 link_state = LINK_STATE_UNKNOWN;
284 * Note that each NIC's softc must start with an ifnet pointer.
287 if_link_state_change(*(struct ifnet**)device_get_softc(parent), link_state);
291 miibus_mediainit(device_t dev)
293 struct mii_data *mii;
294 struct ifmedia_entry *m;
297 /* Poke the parent in case it has any media of its own to add. */
298 MIIBUS_MEDIAINIT(device_get_parent(dev));
300 mii = device_get_softc(dev);
301 LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) {
302 media = m->ifm_media;
303 if (media == (IFM_ETHER | IFM_AUTO))
307 ifmedia_set(&mii->mii_media, media);
311 * Helper function used by network interface drivers, attaches the miibus and
312 * the PHYs to the network interface driver parent.
315 mii_attach(device_t dev, device_t *miibus, struct ifnet *ifp,
316 ifm_change_cb_t ifmedia_upd, ifm_stat_cb_t ifmedia_sts, int capmask,
317 int phyloc, int offloc, int flags)
319 struct miibus_ivars *ivars;
320 struct mii_attach_args ma, *args;
321 device_t *children, phy;
322 int bmsr, first, i, nchildren, offset, phymax, phymin, rv;
324 if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY) {
325 printf("%s: phyloc and offloc specified\n", __func__);
329 if (offloc != MII_OFFSET_ANY && (offloc < 0 || offloc >= MII_NPHY)) {
330 printf("%s: ivalid offloc %d\n", __func__, offloc);
334 if (phyloc == MII_PHY_ANY) {
336 phymax = MII_NPHY - 1;
338 if (phyloc < 0 || phyloc >= MII_NPHY) {
339 printf("%s: ivalid phyloc %d\n", __func__, phyloc);
342 phymin = phymax = phyloc;
346 if (*miibus == NULL) {
348 ivars = malloc(sizeof(*ivars), M_DEVBUF, M_NOWAIT);
352 ivars->ifmedia_upd = ifmedia_upd;
353 ivars->ifmedia_sts = ifmedia_sts;
354 ivars->mii_flags = flags;
355 *miibus = device_add_child(dev, "miibus", -1);
356 if (*miibus == NULL) {
360 device_set_ivars(*miibus, ivars);
362 ivars = device_get_ivars(*miibus);
363 if (ivars->ifp != ifp || ivars->ifmedia_upd != ifmedia_upd ||
364 ivars->ifmedia_sts != ifmedia_sts ||
365 ivars->mii_flags != flags) {
366 printf("%s: non-matching invariant\n", __func__);
370 * Assignment of the attach arguments mii_data for the first
371 * pass is done in miibus_attach(), i.e. once the miibus softc
372 * has been allocated.
374 ma.mii_data = device_get_softc(*miibus);
377 ma.mii_capmask = capmask;
381 for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) {
383 * Make sure we haven't already configured a PHY at this
384 * address. This allows mii_attach() to be called
387 if (device_get_children(*miibus, &children, &nchildren) == 0) {
388 for (i = 0; i < nchildren; i++) {
389 args = device_get_ivars(children[i]);
390 if (args->mii_phyno == ma.mii_phyno) {
392 * Yes, there is already something
393 * configured at this address.
395 free(children, M_TEMP);
399 free(children, M_TEMP);
403 * Check to see if there is a PHY at this address. Note,
404 * many braindead PHYs report 0/0 in their ID registers,
405 * so we test for media in the BMSR.
407 bmsr = MIIBUS_READREG(dev, ma.mii_phyno, MII_BMSR);
408 if (bmsr == 0 || bmsr == 0xffff ||
409 (bmsr & (BMSR_EXTSTAT | BMSR_MEDIAMASK)) == 0) {
410 /* Assume no PHY at this address. */
415 * There is a PHY at this address. If we were given an
416 * `offset' locator, skip this PHY if it doesn't match.
418 if (offloc != MII_OFFSET_ANY && offloc != offset)
422 * Extract the IDs. Braindead PHYs will be handled by
423 * the `ukphy' driver, as we have no ID information to
426 ma.mii_id1 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR1);
427 ma.mii_id2 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR2);
429 args = malloc(sizeof(struct mii_attach_args), M_DEVBUF,
433 bcopy((char *)&ma, (char *)args, sizeof(ma));
434 phy = device_add_child(*miibus, NULL, -1);
436 free(args, M_DEVBUF);
439 device_set_ivars(phy, args);
449 rv = bus_generic_attach(dev);
453 /* Attaching of the PHY drivers is done in miibus_attach(). */
456 rv = bus_generic_attach(*miibus);
464 device_delete_child(dev, *miibus);
465 free(ivars, M_DEVBUF);
472 mii_phy_probe(device_t dev, device_t *child, ifm_change_cb_t ifmedia_upd,
473 ifm_stat_cb_t ifmedia_sts)
478 * Note that each NIC's softc must start with an ifnet pointer.
481 ifp = *(struct ifnet **)device_get_softc(dev);
482 return (mii_attach(dev, child, ifp, ifmedia_upd, ifmedia_sts,
483 BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0));
487 * Media changed; notify all PHYs.
490 mii_mediachg(struct mii_data *mii)
492 struct mii_softc *child;
493 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
496 mii->mii_media_status = 0;
497 mii->mii_media_active = IFM_NONE;
499 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
501 * If the media indicates a different PHY instance,
504 if (IFM_INST(ife->ifm_media) != child->mii_inst) {
505 if ((child->mii_flags & MIIF_NOISOLATE) != 0) {
506 device_printf(child->mii_dev, "%s: "
507 "can't handle non-zero PHY instance %d\n",
508 __func__, child->mii_inst);
511 PHY_WRITE(child, MII_BMCR, PHY_READ(child, MII_BMCR) |
515 rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
523 * Call the PHY tick routines, used during autonegotiation.
526 mii_tick(struct mii_data *mii)
528 struct mii_softc *child;
529 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
531 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
533 * If this PHY instance isn't currently selected, just skip
536 if (IFM_INST(ife->ifm_media) != child->mii_inst)
538 (void)(*child->mii_service)(child, mii, MII_TICK);
543 * Get media status from PHYs.
546 mii_pollstat(struct mii_data *mii)
548 struct mii_softc *child;
549 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
551 mii->mii_media_status = 0;
552 mii->mii_media_active = IFM_NONE;
554 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
556 * If we're not polling this PHY instance, just skip it.
558 if (IFM_INST(ife->ifm_media) != child->mii_inst)
560 (void)(*child->mii_service)(child, mii, MII_POLLSTAT);
565 * Inform the PHYs that the interface is down.
568 mii_down(struct mii_data *mii)
570 struct mii_softc *child;
572 LIST_FOREACH(child, &mii->mii_phys, mii_list)