]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/mii/mii.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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  * 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.
26  *
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.
38  */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 /*
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.
47  */
48
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>
54 #include <sys/bus.h> 
55
56 #include <net/if.h>
57 #include <net/if_media.h>
58 #include <net/route.h>
59
60 #include <dev/mii/mii.h>
61 #include <dev/mii/miivar.h>
62
63 MODULE_VERSION(miibus, 1);
64
65 #include "miibus_if.h"
66
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,
69     size_t buflen);
70 static int miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
71     size_t buflen);
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);
77
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),
84
85         /* bus interface */
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),
90
91         /* MII interface */
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),    
97
98         { 0, 0 }
99 };
100
101 devclass_t miibus_devclass;
102
103 driver_t miibus_driver = {
104         "miibus",
105         miibus_methods,
106         sizeof(struct mii_data)
107 };
108
109 struct miibus_ivars {
110         ifm_change_cb_t ifmedia_upd;
111         ifm_stat_cb_t   ifmedia_sts;
112 };
113
114 /*
115  * Helper function used by network interface drivers, attaches PHYs
116  * to the network interface driver parent.
117  */
118 int
119 miibus_probe(device_t dev)
120 {
121         struct mii_attach_args  ma, *args;
122         struct mii_data         *mii;
123         device_t                child = NULL, parent;
124         int                     bmsr, capmask = 0xFFFFFFFF;
125
126         mii = device_get_softc(dev);
127         parent = device_get_parent(dev);
128         LIST_INIT(&mii->mii_phys);
129
130         for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) {
131                 /*
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.
135                  */
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. */
140                         continue;
141                 }
142
143                 /*
144                  * Extract the IDs. Braindead PHYs will be handled by
145                  * the `ukphy' driver, as we have no ID information to
146                  * match on.
147                  */
148                 ma.mii_id1 = MIIBUS_READREG(parent, ma.mii_phyno,
149                     MII_PHYIDR1);
150                 ma.mii_id2 = MIIBUS_READREG(parent, ma.mii_phyno,
151                     MII_PHYIDR2);
152
153                 ma.mii_data = mii;
154                 ma.mii_capmask = capmask;
155
156                 args = malloc(sizeof(struct mii_attach_args),
157                     M_DEVBUF, M_NOWAIT);
158                 bcopy((char *)&ma, (char *)args, sizeof(ma));
159                 child = device_add_child(dev, NULL, -1);
160                 device_set_ivars(child, args);
161         }
162
163         if (child == NULL)
164                 return(ENXIO);
165
166         device_set_desc(dev, "MII bus");
167
168         return(0);
169 }
170
171 int
172 miibus_attach(device_t dev)
173 {
174         struct miibus_ivars     *ivars;
175         struct mii_data         *mii;
176
177         mii = device_get_softc(dev);
178         /*
179          * Note that each NIC's softc must start with an ifnet pointer.
180          * XXX: EVIL HACK!
181          */
182         mii->mii_ifp = *(struct ifnet**)device_get_softc(device_get_parent(dev));
183         mii->mii_ifp->if_capabilities |= IFCAP_LINKSTATE;
184         mii->mii_ifp->if_capenable |= IFCAP_LINKSTATE;
185         ivars = device_get_ivars(dev);
186         ifmedia_init(&mii->mii_media, IFM_IMASK, ivars->ifmedia_upd,
187             ivars->ifmedia_sts);
188         bus_generic_attach(dev);
189
190         return(0);
191 }
192
193 int
194 miibus_detach(device_t dev)
195 {
196         struct mii_data         *mii;
197
198         bus_generic_detach(dev);
199         mii = device_get_softc(dev);
200         ifmedia_removeall(&mii->mii_media);
201         mii->mii_ifp = NULL;
202
203         return(0);
204 }
205
206 static int
207 miibus_print_child(device_t dev, device_t child)
208 {
209         struct mii_attach_args *ma;
210         int retval;
211
212         ma = device_get_ivars(child);
213         retval = bus_print_child_header(dev, child);
214         retval += printf(" PHY %d", ma->mii_phyno);
215         retval += bus_print_child_footer(dev, child);
216
217         return (retval);
218 }
219
220 static int
221 miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
222     size_t buflen)
223 {
224         struct mii_attach_args *maa = device_get_ivars(child);
225         snprintf(buf, buflen, "oui=0x%x model=0x%x rev=0x%x",
226             MII_OUI(maa->mii_id1, maa->mii_id2),
227             MII_MODEL(maa->mii_id2), MII_REV(maa->mii_id2));
228         return (0);
229 }
230
231 static int
232 miibus_child_location_str(device_t bus, device_t child, char *buf,
233     size_t buflen)
234 {
235         struct mii_attach_args *maa = device_get_ivars(child);
236         snprintf(buf, buflen, "phyno=%d", maa->mii_phyno);
237         return (0);
238 }
239
240 static int
241 miibus_readreg(device_t dev, int phy, int reg)
242 {
243         device_t                parent;
244
245         parent = device_get_parent(dev);
246         return(MIIBUS_READREG(parent, phy, reg));
247 }
248
249 static int
250 miibus_writereg(device_t dev, int phy, int reg, int data)
251 {
252         device_t                parent;
253
254         parent = device_get_parent(dev);
255         return(MIIBUS_WRITEREG(parent, phy, reg, data));
256 }
257
258 static void
259 miibus_statchg(device_t dev)
260 {
261         device_t                parent;
262         struct mii_data         *mii;
263         struct ifnet            *ifp;
264
265         parent = device_get_parent(dev);
266         MIIBUS_STATCHG(parent);
267
268         mii = device_get_softc(dev);
269
270         /*
271          * Note that each NIC's softc must start with an ifnet pointer.
272          * XXX: EVIL HACK!
273          */
274         ifp = *(struct ifnet **)device_get_softc(parent);
275         ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
276         return;
277 }
278
279 static void
280 miibus_linkchg(device_t dev)
281 {
282         struct mii_data         *mii;
283         device_t                parent;
284         int                     link_state;
285
286         parent = device_get_parent(dev);
287         MIIBUS_LINKCHG(parent);
288
289         mii = device_get_softc(dev);
290         
291         if (mii->mii_media_status & IFM_AVALID) {
292                 if (mii->mii_media_status & IFM_ACTIVE)
293                         link_state = LINK_STATE_UP;
294                 else
295                         link_state = LINK_STATE_DOWN;
296         } else
297                 link_state = LINK_STATE_UNKNOWN;
298         /*
299          * Note that each NIC's softc must start with an ifnet pointer.
300          * XXX: EVIL HACK!
301          */
302         if_link_state_change(*(struct ifnet**)device_get_softc(parent), link_state);
303 }
304
305 static void
306 miibus_mediainit(device_t dev)
307 {
308         struct mii_data         *mii;
309         struct ifmedia_entry    *m;
310         int                     media = 0;
311
312         /* Poke the parent in case it has any media of its own to add. */
313         MIIBUS_MEDIAINIT(device_get_parent(dev));
314
315         mii = device_get_softc(dev);
316         LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) {
317                 media = m->ifm_media;
318                 if (media == (IFM_ETHER|IFM_AUTO))
319                         break;
320         }
321
322         ifmedia_set(&mii->mii_media, media);
323
324         return;
325 }
326
327 int
328 mii_phy_probe(device_t dev, device_t *child, ifm_change_cb_t ifmedia_upd,
329     ifm_stat_cb_t ifmedia_sts)
330 {
331         struct miibus_ivars     *ivars;
332         int                     bmsr, i;
333
334         ivars = malloc(sizeof(*ivars), M_DEVBUF, M_NOWAIT);
335         if (ivars == NULL)
336                 return (ENOMEM);
337         ivars->ifmedia_upd = ifmedia_upd;
338         ivars->ifmedia_sts = ifmedia_sts;
339         *child = device_add_child(dev, "miibus", -1);
340         device_set_ivars(*child, ivars);
341
342         for (i = 0; i < MII_NPHY; i++) {
343                 bmsr = MIIBUS_READREG(dev, i, MII_BMSR);
344                 if (bmsr == 0 || bmsr == 0xffff ||
345                     (bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) {
346                         /* Assume no PHY at this address. */
347                         continue;
348                 } else
349                         break;
350         }
351
352         if (i == MII_NPHY) {
353                 device_delete_child(dev, *child);
354                 *child = NULL;
355                 return(ENXIO);
356         }
357
358         bus_generic_attach(dev);
359
360         return(0);
361 }
362
363 /*
364  * Media changed; notify all PHYs.
365  */
366 int
367 mii_mediachg(struct mii_data *mii)
368 {
369         struct mii_softc *child;
370         int rv;
371
372         mii->mii_media_status = 0;
373         mii->mii_media_active = IFM_NONE;
374
375         LIST_FOREACH(child, &mii->mii_phys, mii_list) {
376                 rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
377                 if (rv)
378                         return (rv);
379         }
380         return (0);
381 }
382
383 /*
384  * Call the PHY tick routines, used during autonegotiation.
385  */
386 void
387 mii_tick(struct mii_data *mii)
388 {
389         struct mii_softc *child;
390
391         LIST_FOREACH(child, &mii->mii_phys, mii_list)
392                 (void) (*child->mii_service)(child, mii, MII_TICK);
393 }
394
395 /*
396  * Get media status from PHYs.
397  */
398 void
399 mii_pollstat(struct mii_data *mii)
400 {
401         struct mii_softc *child;
402
403         mii->mii_media_status = 0;
404         mii->mii_media_active = IFM_NONE;
405
406         LIST_FOREACH(child, &mii->mii_phys, mii_list)
407                 (void) (*child->mii_service)(child, mii, MII_POLLSTAT);
408 }
409
410 /*
411  * Inform the PHYs that the interface is down.
412  */
413 void
414 mii_down(struct mii_data *mii)
415 {
416         struct mii_softc *child;
417
418         LIST_FOREACH(child, &mii->mii_phys, mii_list)
419                 mii_phy_down(child);
420 }