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);
72 static unsigned char mii_bitreverse(unsigned char x);
74 static 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),
82 DEVMETHOD(bus_print_child, miibus_print_child),
83 DEVMETHOD(bus_read_ivar, miibus_read_ivar),
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;
250 parent = device_get_parent(dev);
251 MIIBUS_STATCHG(parent);
253 mii = device_get_softc(dev);
254 mii->mii_ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
258 miibus_linkchg(device_t dev)
260 struct mii_data *mii;
264 parent = device_get_parent(dev);
265 MIIBUS_LINKCHG(parent);
267 mii = device_get_softc(dev);
269 if (mii->mii_media_status & IFM_AVALID) {
270 if (mii->mii_media_status & IFM_ACTIVE)
271 link_state = LINK_STATE_UP;
273 link_state = LINK_STATE_DOWN;
275 link_state = LINK_STATE_UNKNOWN;
276 if_link_state_change(mii->mii_ifp, link_state);
280 miibus_mediainit(device_t dev)
282 struct mii_data *mii;
283 struct ifmedia_entry *m;
286 /* Poke the parent in case it has any media of its own to add. */
287 MIIBUS_MEDIAINIT(device_get_parent(dev));
289 mii = device_get_softc(dev);
290 LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) {
291 media = m->ifm_media;
292 if (media == (IFM_ETHER | IFM_AUTO))
296 ifmedia_set(&mii->mii_media, media);
300 * Helper function used by network interface drivers, attaches the miibus and
301 * the PHYs to the network interface driver parent.
304 mii_attach(device_t dev, device_t *miibus, struct ifnet *ifp,
305 ifm_change_cb_t ifmedia_upd, ifm_stat_cb_t ifmedia_sts, int capmask,
306 int phyloc, int offloc, int flags)
308 struct miibus_ivars *ivars;
309 struct mii_attach_args ma, *args;
310 device_t *children, phy;
311 int bmsr, first, i, nchildren, offset, phymax, phymin, rv;
313 if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY) {
314 printf("%s: phyloc and offloc specified\n", __func__);
318 if (offloc != MII_OFFSET_ANY && (offloc < 0 || offloc >= MII_NPHY)) {
319 printf("%s: ivalid offloc %d\n", __func__, offloc);
323 if (phyloc == MII_PHY_ANY) {
325 phymax = MII_NPHY - 1;
327 if (phyloc < 0 || phyloc >= MII_NPHY) {
328 printf("%s: ivalid phyloc %d\n", __func__, phyloc);
331 phymin = phymax = phyloc;
335 if (*miibus == NULL) {
337 ivars = malloc(sizeof(*ivars), M_DEVBUF, M_NOWAIT);
341 ivars->ifmedia_upd = ifmedia_upd;
342 ivars->ifmedia_sts = ifmedia_sts;
343 ivars->mii_flags = flags;
344 *miibus = device_add_child(dev, "miibus", -1);
345 if (*miibus == NULL) {
349 device_set_ivars(*miibus, ivars);
351 ivars = device_get_ivars(*miibus);
352 if (ivars->ifp != ifp || ivars->ifmedia_upd != ifmedia_upd ||
353 ivars->ifmedia_sts != ifmedia_sts ||
354 ivars->mii_flags != flags) {
355 printf("%s: non-matching invariant\n", __func__);
359 * Assignment of the attach arguments mii_data for the first
360 * pass is done in miibus_attach(), i.e. once the miibus softc
361 * has been allocated.
363 ma.mii_data = device_get_softc(*miibus);
366 ma.mii_capmask = capmask;
370 for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) {
372 * Make sure we haven't already configured a PHY at this
373 * address. This allows mii_attach() to be called
376 if (device_get_children(*miibus, &children, &nchildren) == 0) {
377 for (i = 0; i < nchildren; i++) {
378 args = device_get_ivars(children[i]);
379 if (args->mii_phyno == ma.mii_phyno) {
381 * Yes, there is already something
382 * configured at this address.
384 free(children, M_TEMP);
388 free(children, M_TEMP);
392 * Check to see if there is a PHY at this address. Note,
393 * many braindead PHYs report 0/0 in their ID registers,
394 * so we test for media in the BMSR.
396 bmsr = MIIBUS_READREG(dev, ma.mii_phyno, MII_BMSR);
397 if (bmsr == 0 || bmsr == 0xffff ||
398 (bmsr & (BMSR_EXTSTAT | BMSR_MEDIAMASK)) == 0) {
399 /* Assume no PHY at this address. */
404 * There is a PHY at this address. If we were given an
405 * `offset' locator, skip this PHY if it doesn't match.
407 if (offloc != MII_OFFSET_ANY && offloc != offset)
411 * Extract the IDs. Braindead PHYs will be handled by
412 * the `ukphy' driver, as we have no ID information to
415 ma.mii_id1 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR1);
416 ma.mii_id2 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR2);
418 ma.mii_offset = offset;
419 args = malloc(sizeof(struct mii_attach_args), M_DEVBUF,
423 bcopy((char *)&ma, (char *)args, sizeof(ma));
424 phy = device_add_child(*miibus, NULL, -1);
426 free(args, M_DEVBUF);
429 device_set_ivars(phy, args);
439 rv = bus_generic_attach(dev);
443 /* Attaching of the PHY drivers is done in miibus_attach(). */
446 rv = bus_generic_attach(*miibus);
454 device_delete_child(dev, *miibus);
455 free(ivars, M_DEVBUF);
462 * Media changed; notify all PHYs.
465 mii_mediachg(struct mii_data *mii)
467 struct mii_softc *child;
468 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
471 mii->mii_media_status = 0;
472 mii->mii_media_active = IFM_NONE;
474 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
476 * If the media indicates a different PHY instance,
479 if (IFM_INST(ife->ifm_media) != child->mii_inst) {
480 if ((child->mii_flags & MIIF_NOISOLATE) != 0) {
481 device_printf(child->mii_dev, "%s: "
482 "can't handle non-zero PHY instance %d\n",
483 __func__, child->mii_inst);
486 PHY_WRITE(child, MII_BMCR, PHY_READ(child, MII_BMCR) |
490 rv = PHY_SERVICE(child, mii, MII_MEDIACHG);
498 * Call the PHY tick routines, used during autonegotiation.
501 mii_tick(struct mii_data *mii)
503 struct mii_softc *child;
504 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
506 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
508 * If this PHY instance isn't currently selected, just skip
511 if (IFM_INST(ife->ifm_media) != child->mii_inst)
513 (void)PHY_SERVICE(child, mii, MII_TICK);
518 * Get media status from PHYs.
521 mii_pollstat(struct mii_data *mii)
523 struct mii_softc *child;
524 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
526 mii->mii_media_status = 0;
527 mii->mii_media_active = IFM_NONE;
529 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
531 * If we're not polling this PHY instance, just skip it.
533 if (IFM_INST(ife->ifm_media) != child->mii_inst)
535 (void)PHY_SERVICE(child, mii, MII_POLLSTAT);
540 * Inform the PHYs that the interface is down.
543 mii_down(struct mii_data *mii)
545 struct mii_softc *child;
547 LIST_FOREACH(child, &mii->mii_phys, mii_list)
552 mii_bitreverse(unsigned char x)
554 unsigned const char const nibbletab[16] = {
555 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
558 return ((nibbletab[x & 15] << 4) | nibbletab[x >> 4]);
562 mii_oui(u_int id1, u_int id2)
566 h = (id1 << 6) | (id2 >> 10);
568 return ((mii_bitreverse(h >> 16) << 16) |
569 (mii_bitreverse((h >> 8) & 0xff) << 8) |
570 mii_bitreverse(h & 0xff));