mii: Wrong sizeof
[dragonfly.git] / sys / dev / netif / mii_layer / mii.c
1 /*      $NetBSD: mii.c,v 1.40 2005/12/11 12:22:42 christos Exp $        */
2
3 /*-
4  * Copyright (c) 1998, 2000 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  * $FreeBSD: src/sys/dev/mii/mii.c,v 1.6.2.2 2002/08/19 16:56:33 ambrisko Exp $
40  * $DragonFly: src/sys/dev/netif/mii_layer/mii.c,v 1.12 2008/07/22 10:59:16 sephe Exp $
41  */
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/kernel.h>
54 #include <sys/module.h>
55 #include <sys/bus.h> 
56
57 #include <net/if.h>
58 #include <net/if_media.h>
59
60 #include "mii.h"
61 #include "miivar.h"
62
63 #include "miibus_if.h"
64
65 static int      miibus_probe(device_t);
66 static int      miibus_attach(device_t);
67 static int      miibus_detach(device_t);
68 static int      miibus_readreg(device_t, int, int);
69 static int      miibus_writereg(device_t, int, int, int);
70 static void     miibus_statchg(device_t);
71 static void     miibus_mediainit(device_t);
72
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),
79
80         /* bus interface */
81         DEVMETHOD(bus_print_child,      bus_generic_print_child),
82         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
83
84         /* MII interface */
85         DEVMETHOD(miibus_readreg,       miibus_readreg),
86         DEVMETHOD(miibus_writereg,      miibus_writereg),
87         DEVMETHOD(miibus_statchg,       miibus_statchg),    
88         DEVMETHOD(miibus_mediainit,     miibus_mediainit),    
89
90         { 0, 0 }
91 };
92
93 devclass_t miibus_devclass;
94
95 driver_t miibus_driver = {
96         "miibus",
97         miibus_methods,
98         sizeof(struct mii_data)
99 };
100
101 /*
102  * Check to see if there is a PHY at this address.  Note,
103  * many braindead PHYs report 0/0 in their ID registers,
104  * so we test for media in the BMSR.
105  */
106 static __inline int
107 miibus_no_phy(device_t dev, const struct mii_probe_args *args, int phyno)
108 {
109         int bmsr;
110
111         if ((args->mii_probemask & (1 << phyno)) == 0)
112                 return 1;
113
114         bmsr = MIIBUS_READREG(dev, phyno, MII_BMSR);
115         return (bmsr == 0 || bmsr == 0xffff ||
116                 (bmsr & (BMSR_MEDIAMASK | BMSR_EXTSTAT)) == 0);
117 }
118
119 /*
120  * Helper function used by network interface drivers, attaches PHYs
121  * to the network interface driver parent.
122  */
123
124 int
125 miibus_probe(device_t dev)
126 {
127         struct mii_attach_args ma, *args;
128         const struct mii_probe_args *probe_args;
129         struct mii_data *mii;
130         device_t child = NULL, parent;
131
132         probe_args = device_get_ivars(dev);
133
134         mii = device_get_softc(dev);
135         parent = device_get_parent(dev);
136         LIST_INIT(&mii->mii_phys);
137         bzero(&ma, sizeof(ma));
138
139         for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) {
140                 if (miibus_no_phy(parent, probe_args, ma.mii_phyno))
141                         continue;
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 = probe_args->mii_capmask;
155
156                 args = kmalloc(sizeof(struct mii_attach_args), M_DEVBUF,
157                     M_WAITOK);
158                 *args = ma;
159                 child = device_add_child(dev, NULL, -1);
160                 device_set_ivars(child, args);
161         }
162         if (child == NULL)
163                 return ENXIO;
164
165         device_set_desc(dev, "MII bus");
166         return 0;
167 }
168
169 int
170 miibus_attach(device_t dev)
171 {
172         const struct mii_probe_args *probe_args;
173         struct mii_data *mii;
174
175         mii = device_get_softc(dev);
176         mii->mii_ifp = device_get_softc(device_get_parent(dev));
177
178         probe_args = device_get_ivars(dev);
179         ifmedia_init(&mii->mii_media, IFM_IMASK,
180             probe_args->mii_ifmedia_upd, probe_args->mii_ifmedia_sts);
181
182         bus_generic_attach(dev);
183         return 0;
184 }
185
186 int
187 miibus_detach(device_t dev)
188 {
189         struct mii_data *mii;
190
191         bus_generic_detach(dev);
192         mii = device_get_softc(dev);
193         ifmedia_removeall(&mii->mii_media);
194         mii->mii_ifp = NULL;
195
196         return 0;
197 }
198
199 static int
200 miibus_readreg(device_t dev, int phy, int reg)
201 {
202         device_t parent;
203
204         parent = device_get_parent(dev);
205         return MIIBUS_READREG(parent, phy, reg);
206 }
207
208 static int
209 miibus_writereg(device_t dev, int phy, int reg, int data)
210 {
211         device_t parent;
212
213         parent = device_get_parent(dev);
214         return MIIBUS_WRITEREG(parent, phy, reg, data);
215 }
216
217 static void
218 miibus_statchg(device_t dev)
219 {
220         device_t parent;
221
222         parent = device_get_parent(dev);
223         MIIBUS_STATCHG(parent);
224 }
225
226 static void
227 miibus_mediainit(device_t dev)
228 {
229         struct mii_data *mii;
230         struct ifmedia_entry *m;
231         int media = 0;
232
233         /* Poke the parent in case it has any media of its own to add. */
234         MIIBUS_MEDIAINIT(device_get_parent(dev));
235
236         mii = device_get_softc(dev);
237         for (m = LIST_FIRST(&mii->mii_media.ifm_list); m != NULL;
238              m = LIST_NEXT(m, ifm_list)) {
239                 media = m->ifm_media;
240                 if (media == (IFM_ETHER|IFM_AUTO))
241                         break;
242         }
243         ifmedia_set(&mii->mii_media, media);
244 }
245
246 int
247 mii_phy_probe(device_t dev, device_t *child,
248     ifm_change_cb_t ifmedia_upd, ifm_stat_cb_t ifmedia_sts)
249 {
250         struct mii_probe_args args;
251
252         mii_probe_args_init(&args, ifmedia_upd, ifmedia_sts);
253         return mii_probe(dev, child, &args);
254 }
255
256 int
257 mii_probe(device_t dev, device_t *child, const struct mii_probe_args *args0)
258 {
259         struct mii_probe_args *args;
260         int i;
261
262         args = kmalloc(sizeof(*args), M_DEVBUF, M_WAITOK);
263         *args = *args0;
264
265         *child = device_add_child(dev, "miibus", -1);
266         device_set_ivars(*child, args);
267
268         for (i = 0; i < MII_NPHY; i++) {
269                 if (!miibus_no_phy(dev, args0, i))
270                         break;
271         }
272
273         if (i == MII_NPHY) {
274                 device_delete_child(dev, *child);
275                 *child = NULL;
276                 return ENXIO;
277         }
278
279         bus_generic_attach(dev);
280         return 0;
281 }
282
283 void
284 mii_probe_args_init(struct mii_probe_args *args,
285     ifm_change_cb_t ifmedia_upd, ifm_stat_cb_t ifmedia_sts)
286 {
287         memset(args, 0, sizeof(*args));
288
289         args->mii_ifmedia_upd = ifmedia_upd;
290         args->mii_ifmedia_sts = ifmedia_sts;
291         args->mii_probemask = MII_PROBEMASK_DEFAULT;
292         args->mii_capmask = MII_CAPMASK_DEFAULT;
293 }
294
295 /*
296  * Media changed; notify all PHYs.
297  */
298 int
299 mii_mediachg(struct mii_data *mii)
300 {
301         struct mii_softc *child;
302         int rv;
303
304         mii->mii_media_status = 0;
305         mii->mii_media_active = IFM_NONE;
306
307         for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
308              child = LIST_NEXT(child, mii_list)) {
309                 rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
310                 if (rv) {
311                         return rv;
312                 } else {
313                         /* Reset autonegotiation timer. */
314                         child->mii_ticks = 0;
315                 }
316         }
317         return 0;
318 }
319
320 /*
321  * Call the PHY tick routines, used during autonegotiation.
322  */
323 void
324 mii_tick(struct mii_data *mii)
325 {
326         struct mii_softc *child;
327
328         for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
329              child = LIST_NEXT(child, mii_list))
330                 (*child->mii_service)(child, mii, MII_TICK);
331 }
332
333 /*
334  * Get media status from PHYs.
335  */
336 void
337 mii_pollstat(struct mii_data *mii)
338 {
339         struct mii_softc *child;
340
341         mii->mii_media_status = 0;
342         mii->mii_media_active = IFM_NONE;
343
344         for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
345              child = LIST_NEXT(child, mii_list))
346                 (*child->mii_service)(child, mii, MII_POLLSTAT);
347 }
348
349 static moduledata_t miibus_mod = { "miibus" };
350
351 DECLARE_MODULE(miibus, miibus_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
352 MODULE_VERSION(miibus, 1);