]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/dev/mii/mii.c
MFC r226173, r227843, r227848 and r227908:
[FreeBSD/stable/9.git] / sys / dev / mii / mii.c
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  *
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.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 /*
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.
40  */
41
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>
47 #include <sys/bus.h>
48
49 #include <net/if.h>
50 #include <net/if_media.h>
51 #include <net/route.h>
52
53 #include <dev/mii/mii.h>
54 #include <dev/mii/miivar.h>
55
56 MODULE_VERSION(miibus, 1);
57
58 #include "miibus_if.h"
59
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,
62     uintptr_t *result);
63 static int miibus_child_location_str(device_t bus, device_t child, char *buf,
64     size_t buflen);
65 static int miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
66     size_t buflen);
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);
73
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),
80
81         /* bus interface */
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),
86
87         /* MII interface */
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),
93
94         DEVMETHOD_END
95 };
96
97 devclass_t miibus_devclass;
98
99 driver_t miibus_driver = {
100         "miibus",
101         miibus_methods,
102         sizeof(struct mii_data)
103 };
104
105 struct miibus_ivars {
106         struct ifnet    *ifp;
107         ifm_change_cb_t ifmedia_upd;
108         ifm_stat_cb_t   ifmedia_sts;
109         int             mii_flags;
110 };
111
112 int
113 miibus_probe(device_t dev)
114 {
115
116         device_set_desc(dev, "MII bus");
117
118         return (BUS_PROBE_SPECIFIC);
119 }
120
121 int
122 miibus_attach(device_t dev)
123 {
124         struct miibus_ivars     *ivars;
125         struct mii_attach_args  *ma;
126         struct mii_data         *mii;
127         device_t                *children;
128         int                     i, nchildren;
129
130         mii = device_get_softc(dev);
131         nchildren = 0;
132         if (device_get_children(dev, &children, &nchildren) == 0) {
133                 for (i = 0; i < nchildren; i++) {
134                         ma = device_get_ivars(children[i]);
135                         ma->mii_data = mii;
136                 }
137                 free(children, M_TEMP);
138         }
139         if (nchildren == 0) {
140                 device_printf(dev, "cannot get children\n");
141                 return (ENXIO);
142         }
143         ivars = device_get_ivars(dev);
144         ifmedia_init(&mii->mii_media, IFM_IMASK, ivars->ifmedia_upd,
145             ivars->ifmedia_sts);
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);
150
151         return (bus_generic_attach(dev));
152 }
153
154 int
155 miibus_detach(device_t dev)
156 {
157         struct mii_data         *mii;
158
159         bus_generic_detach(dev);
160         mii = device_get_softc(dev);
161         ifmedia_removeall(&mii->mii_media);
162         mii->mii_ifp = NULL;
163
164         return (0);
165 }
166
167 static int
168 miibus_print_child(device_t dev, device_t child)
169 {
170         struct mii_attach_args *ma;
171         int retval;
172
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);
177
178         return (retval);
179 }
180
181 static int
182 miibus_read_ivar(device_t dev, device_t child __unused, int which,
183     uintptr_t *result)
184 {
185         struct miibus_ivars *ivars;
186
187         /*
188          * NB: this uses the instance variables of the miibus rather than
189          * its PHY children.
190          */
191         ivars = device_get_ivars(dev);
192         switch (which) {
193         case MIIBUS_IVAR_FLAGS:
194                 *result = ivars->mii_flags;
195                 break;
196         default:
197                 return (ENOENT);
198         }
199         return (0);
200 }
201
202 static int
203 miibus_child_pnpinfo_str(device_t bus __unused, device_t child, char *buf,
204     size_t buflen)
205 {
206         struct mii_attach_args *ma;
207
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));
212         return (0);
213 }
214
215 static int
216 miibus_child_location_str(device_t bus __unused, device_t child, char *buf,
217     size_t buflen)
218 {
219         struct mii_attach_args *ma;
220
221         ma = device_get_ivars(child);
222         snprintf(buf, buflen, "phyno=%d", ma->mii_phyno);
223         return (0);
224 }
225
226 static int
227 miibus_readreg(device_t dev, int phy, int reg)
228 {
229         device_t                parent;
230
231         parent = device_get_parent(dev);
232         return (MIIBUS_READREG(parent, phy, reg));
233 }
234
235 static int
236 miibus_writereg(device_t dev, int phy, int reg, int data)
237 {
238         device_t                parent;
239
240         parent = device_get_parent(dev);
241         return (MIIBUS_WRITEREG(parent, phy, reg, data));
242 }
243
244 static void
245 miibus_statchg(device_t dev)
246 {
247         device_t                parent;
248         struct mii_data         *mii;
249
250         parent = device_get_parent(dev);
251         MIIBUS_STATCHG(parent);
252
253         mii = device_get_softc(dev);
254         mii->mii_ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
255 }
256
257 static void
258 miibus_linkchg(device_t dev)
259 {
260         struct mii_data         *mii;
261         device_t                parent;
262         int                     link_state;
263
264         parent = device_get_parent(dev);
265         MIIBUS_LINKCHG(parent);
266
267         mii = device_get_softc(dev);
268
269         if (mii->mii_media_status & IFM_AVALID) {
270                 if (mii->mii_media_status & IFM_ACTIVE)
271                         link_state = LINK_STATE_UP;
272                 else
273                         link_state = LINK_STATE_DOWN;
274         } else
275                 link_state = LINK_STATE_UNKNOWN;
276         if_link_state_change(mii->mii_ifp, link_state);
277 }
278
279 static void
280 miibus_mediainit(device_t dev)
281 {
282         struct mii_data         *mii;
283         struct ifmedia_entry    *m;
284         int                     media = 0;
285
286         /* Poke the parent in case it has any media of its own to add. */
287         MIIBUS_MEDIAINIT(device_get_parent(dev));
288
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))
293                         break;
294         }
295
296         ifmedia_set(&mii->mii_media, media);
297 }
298
299 /*
300  * Helper function used by network interface drivers, attaches the miibus and
301  * the PHYs to the network interface driver parent.
302  */
303 int
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)
307 {
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;
312
313         if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY) {
314                 printf("%s: phyloc and offloc specified\n", __func__);
315                 return (EINVAL);
316         }
317
318         if (offloc != MII_OFFSET_ANY && (offloc < 0 || offloc >= MII_NPHY)) {
319                 printf("%s: ivalid offloc %d\n", __func__, offloc);
320                 return (EINVAL);
321         }
322
323         if (phyloc == MII_PHY_ANY) {
324                 phymin = 0;
325                 phymax = MII_NPHY - 1;
326         } else {
327                 if (phyloc < 0 || phyloc >= MII_NPHY) {
328                         printf("%s: ivalid phyloc %d\n", __func__, phyloc);
329                         return (EINVAL);
330                 }
331                 phymin = phymax = phyloc;
332         }
333
334         first = 0;
335         if (*miibus == NULL) {
336                 first = 1;
337                 ivars = malloc(sizeof(*ivars), M_DEVBUF, M_NOWAIT);
338                 if (ivars == NULL)
339                         return (ENOMEM);
340                 ivars->ifp = ifp;
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) {
346                         rv = ENXIO;
347                         goto fail;
348                 }
349                 device_set_ivars(*miibus, ivars);
350         } else {
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__);
356                         return (EINVAL);
357                 }
358                 /*
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.
362                  */
363                 ma.mii_data = device_get_softc(*miibus);
364         }
365
366         ma.mii_capmask = capmask;
367
368         phy = NULL;
369         offset = 0;
370         for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) {
371                 /*
372                  * Make sure we haven't already configured a PHY at this
373                  * address.  This allows mii_attach() to be called
374                  * multiple times.
375                  */
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) {
380                                         /*
381                                          * Yes, there is already something
382                                          * configured at this address.
383                                          */
384                                         free(children, M_TEMP);
385                                         goto skip;
386                                 }
387                         }
388                         free(children, M_TEMP);
389                 }
390
391                 /*
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.
395                  */
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. */
400                         continue;
401                 }
402
403                 /*
404                  * There is a PHY at this address.  If we were given an
405                  * `offset' locator, skip this PHY if it doesn't match.
406                  */
407                 if (offloc != MII_OFFSET_ANY && offloc != offset)
408                         goto skip;
409
410                 /*
411                  * Extract the IDs. Braindead PHYs will be handled by
412                  * the `ukphy' driver, as we have no ID information to
413                  * match on.
414                  */
415                 ma.mii_id1 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR1);
416                 ma.mii_id2 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR2);
417
418                 ma.mii_offset = offset;
419                 args = malloc(sizeof(struct mii_attach_args), M_DEVBUF,
420                     M_NOWAIT);
421                 if (args == NULL)
422                         goto skip;
423                 bcopy((char *)&ma, (char *)args, sizeof(ma));
424                 phy = device_add_child(*miibus, NULL, -1);
425                 if (phy == NULL) {
426                         free(args, M_DEVBUF);
427                         goto skip;
428                 }
429                 device_set_ivars(phy, args);
430  skip:
431                 offset++;
432         }
433
434         if (first != 0) {
435                 if (phy == NULL) {
436                         rv = ENXIO;
437                         goto fail;
438                 }
439                 rv = bus_generic_attach(dev);
440                 if (rv != 0)
441                         goto fail;
442
443                 /* Attaching of the PHY drivers is done in miibus_attach(). */
444                 return (0);
445         }
446         rv = bus_generic_attach(*miibus);
447         if (rv != 0)
448                 goto fail;
449
450         return (0);
451
452  fail:
453         if (*miibus != NULL)
454                 device_delete_child(dev, *miibus);
455         free(ivars, M_DEVBUF);
456         if (first != 0)
457                 *miibus = NULL;
458         return (rv);
459 }
460
461 /*
462  * Media changed; notify all PHYs.
463  */
464 int
465 mii_mediachg(struct mii_data *mii)
466 {
467         struct mii_softc *child;
468         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
469         int rv;
470
471         mii->mii_media_status = 0;
472         mii->mii_media_active = IFM_NONE;
473
474         LIST_FOREACH(child, &mii->mii_phys, mii_list) {
475                 /*
476                  * If the media indicates a different PHY instance,
477                  * isolate this one.
478                  */
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);
484                                 continue;
485                         }
486                         PHY_WRITE(child, MII_BMCR, PHY_READ(child, MII_BMCR) |
487                             BMCR_ISO);
488                         continue;
489                 }
490                 rv = PHY_SERVICE(child, mii, MII_MEDIACHG);
491                 if (rv)
492                         return (rv);
493         }
494         return (0);
495 }
496
497 /*
498  * Call the PHY tick routines, used during autonegotiation.
499  */
500 void
501 mii_tick(struct mii_data *mii)
502 {
503         struct mii_softc *child;
504         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
505
506         LIST_FOREACH(child, &mii->mii_phys, mii_list) {
507                 /*
508                  * If this PHY instance isn't currently selected, just skip
509                  * it.
510                  */
511                 if (IFM_INST(ife->ifm_media) != child->mii_inst)
512                         continue;
513                 (void)PHY_SERVICE(child, mii, MII_TICK);
514         }
515 }
516
517 /*
518  * Get media status from PHYs.
519  */
520 void
521 mii_pollstat(struct mii_data *mii)
522 {
523         struct mii_softc *child;
524         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
525
526         mii->mii_media_status = 0;
527         mii->mii_media_active = IFM_NONE;
528
529         LIST_FOREACH(child, &mii->mii_phys, mii_list) {
530                 /*
531                  * If we're not polling this PHY instance, just skip it.
532                  */
533                 if (IFM_INST(ife->ifm_media) != child->mii_inst)
534                         continue;
535                 (void)PHY_SERVICE(child, mii, MII_POLLSTAT);
536         }
537 }
538
539 /*
540  * Inform the PHYs that the interface is down.
541  */
542 void
543 mii_down(struct mii_data *mii)
544 {
545         struct mii_softc *child;
546
547         LIST_FOREACH(child, &mii->mii_phys, mii_list)
548                 mii_phy_down(child);
549 }
550
551 static unsigned char
552 mii_bitreverse(unsigned char x)
553 {
554         unsigned const char const nibbletab[16] = {
555                 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
556         };
557
558         return ((nibbletab[x & 15] << 4) | nibbletab[x >> 4]);
559 }
560
561 u_int
562 mii_oui(u_int id1, u_int id2)
563 {
564         u_int h;
565
566         h = (id1 << 6) | (id2 >> 10);
567
568         return ((mii_bitreverse(h >> 16) << 16) |
569             (mii_bitreverse((h >> 8) & 0xff) << 8) |
570             mii_bitreverse(h & 0xff));
571 }