Remove the <sys/syscall-hide.h> header file.
[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  */
41
42 /*
43  * MII bus layer, glues MII-capable network interface drivers to sharable
44  * PHY drivers.  This exports an interface compatible with BSD/OS 3.0's,
45  * plus some NetBSD extensions.
46  */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/socket.h>
51 #include <sys/malloc.h>
52 #include <sys/kernel.h>
53 #include <sys/module.h>
54 #include <sys/bus.h> 
55
56 #include <net/if.h>
57 #include <net/if_media.h>
58
59 #include "mii.h"
60 #include "miivar.h"
61
62 #include "miibus_if.h"
63
64 static int      miibus_probe(device_t);
65 static int      miibus_attach(device_t);
66 static int      miibus_detach(device_t);
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_mediainit(device_t);
71
72 static device_method_t miibus_methods[] = {
73         /* device interface */
74         DEVMETHOD(device_probe,         miibus_probe),
75         DEVMETHOD(device_attach,        miibus_attach),
76         DEVMETHOD(device_detach,        miibus_detach),
77         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
78
79         /* bus interface */
80         DEVMETHOD(bus_print_child,      bus_generic_print_child),
81         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
82
83         /* MII interface */
84         DEVMETHOD(miibus_readreg,       miibus_readreg),
85         DEVMETHOD(miibus_writereg,      miibus_writereg),
86         DEVMETHOD(miibus_statchg,       miibus_statchg),    
87         DEVMETHOD(miibus_mediainit,     miibus_mediainit),    
88
89         DEVMETHOD_END
90 };
91
92 devclass_t miibus_devclass;
93
94 driver_t miibus_driver = {
95         "miibus",
96         miibus_methods,
97         sizeof(struct mii_data)
98 };
99
100 /*
101  * Check to see if there is a PHY at this address.  Note,
102  * many braindead PHYs report 0/0 in their ID registers,
103  * so we test for media in the BMSR.
104  */
105 static __inline int
106 miibus_no_phy(device_t dev, const struct mii_probe_args *args, int phyno)
107 {
108         int bmsr;
109
110         if ((args->mii_probemask & (1 << phyno)) == 0)
111                 return 1;
112
113         bmsr = MIIBUS_READREG(dev, phyno, MII_BMSR);
114         return (bmsr == 0 || bmsr == 0xffff ||
115                 (bmsr & (BMSR_MEDIAMASK | BMSR_EXTSTAT)) == 0);
116 }
117
118 /*
119  * Helper function used by network interface drivers, attaches PHYs
120  * to the network interface driver parent.
121  */
122
123 int
124 miibus_probe(device_t dev)
125 {
126         struct mii_attach_args ma, *args;
127         const struct mii_probe_args *probe_args;
128         struct mii_data *mii;
129         device_t child = NULL, parent;
130
131         probe_args = device_get_ivars(dev);
132
133         mii = device_get_softc(dev);
134         parent = device_get_parent(dev);
135         LIST_INIT(&mii->mii_phys);
136         bzero(&ma, sizeof(ma));
137
138         for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) {
139                 if (miibus_no_phy(parent, probe_args, ma.mii_phyno))
140                         continue;
141
142                 /*
143                  * Extract the IDs. Braindead PHYs will be handled by
144                  * the `ukphy' driver, as we have no ID information to
145                  * match on.
146                  */
147                 ma.mii_id1 = MIIBUS_READREG(parent, ma.mii_phyno,
148                     MII_PHYIDR1);
149                 ma.mii_id2 = MIIBUS_READREG(parent, ma.mii_phyno,
150                     MII_PHYIDR2);
151
152                 ma.mii_data = mii;
153                 ma.mii_capmask = probe_args->mii_capmask;
154                 ma.mii_privtag = probe_args->mii_privtag;
155                 ma.mii_priv = probe_args->mii_priv;
156
157                 args = kmalloc(sizeof(struct mii_attach_args), M_DEVBUF,
158                     M_WAITOK);
159                 *args = ma;
160                 child = device_add_child(dev, NULL, -1);
161                 device_set_ivars(child, args);
162         }
163         if (child == NULL)
164                 return ENXIO;
165
166         device_set_desc(dev, "MII bus");
167         return 0;
168 }
169
170 int
171 miibus_attach(device_t dev)
172 {
173         const struct mii_probe_args *probe_args;
174         struct mii_data *mii;
175
176         mii = device_get_softc(dev);
177         mii->mii_ifp = device_get_softc(device_get_parent(dev));
178
179         probe_args = device_get_ivars(dev);
180         ifmedia_init(&mii->mii_media, IFM_IMASK,
181             probe_args->mii_ifmedia_upd, probe_args->mii_ifmedia_sts);
182
183         bus_generic_attach(dev);
184         return 0;
185 }
186
187 int
188 miibus_detach(device_t dev)
189 {
190         struct mii_data *mii;
191
192         bus_generic_detach(dev);
193         mii = device_get_softc(dev);
194         ifmedia_removeall(&mii->mii_media);
195         mii->mii_ifp = NULL;
196
197         return 0;
198 }
199
200 static int
201 miibus_readreg(device_t dev, int phy, int reg)
202 {
203         device_t parent;
204
205         parent = device_get_parent(dev);
206         return MIIBUS_READREG(parent, phy, reg);
207 }
208
209 static int
210 miibus_writereg(device_t dev, int phy, int reg, int data)
211 {
212         device_t parent;
213
214         parent = device_get_parent(dev);
215         return MIIBUS_WRITEREG(parent, phy, reg, data);
216 }
217
218 static void
219 miibus_statchg(device_t dev)
220 {
221         device_t parent;
222
223         parent = device_get_parent(dev);
224         MIIBUS_STATCHG(parent);
225 }
226
227 static void
228 miibus_mediainit(device_t dev)
229 {
230         struct mii_data *mii;
231         struct ifmedia_entry *m;
232         int media = 0;
233
234         /* Poke the parent in case it has any media of its own to add. */
235         MIIBUS_MEDIAINIT(device_get_parent(dev));
236
237         mii = device_get_softc(dev);
238         for (m = LIST_FIRST(&mii->mii_media.ifm_list); m != NULL;
239              m = LIST_NEXT(m, ifm_list)) {
240                 media = m->ifm_media;
241                 if (media == (IFM_ETHER|IFM_AUTO))
242                         break;
243         }
244         ifmedia_set(&mii->mii_media, media);
245 }
246
247 int
248 mii_phy_probe(device_t dev, device_t *child,
249     ifm_change_cb_t ifmedia_upd, ifm_stat_cb_t ifmedia_sts)
250 {
251         struct mii_probe_args args;
252
253         mii_probe_args_init(&args, ifmedia_upd, ifmedia_sts);
254         return mii_probe(dev, child, &args);
255 }
256
257 int
258 mii_probe(device_t dev, device_t *child, const struct mii_probe_args *args0)
259 {
260         struct mii_probe_args *args;
261         int i;
262
263         args = kmalloc(sizeof(*args), M_DEVBUF, M_WAITOK);
264         *args = *args0;
265
266         *child = device_add_child(dev, "miibus", -1);
267         device_set_ivars(*child, args);
268
269         for (i = 0; i < MII_NPHY; i++) {
270                 if (!miibus_no_phy(dev, args0, i))
271                         break;
272         }
273
274         if (i == MII_NPHY) {
275                 device_delete_child(dev, *child);
276                 *child = NULL;
277                 return ENXIO;
278         }
279
280         bus_generic_attach(dev);
281         return 0;
282 }
283
284 void
285 mii_probe_args_init(struct mii_probe_args *args,
286     ifm_change_cb_t ifmedia_upd, ifm_stat_cb_t ifmedia_sts)
287 {
288         memset(args, 0, sizeof(*args));
289
290         args->mii_ifmedia_upd = ifmedia_upd;
291         args->mii_ifmedia_sts = ifmedia_sts;
292         args->mii_probemask = MII_PROBEMASK_DEFAULT;
293         args->mii_capmask = MII_CAPMASK_DEFAULT;
294         args->mii_privtag = MII_PRIVTAG_NONE;
295 }
296
297 /*
298  * Media changed; notify all PHYs.
299  */
300 int
301 mii_mediachg(struct mii_data *mii)
302 {
303         struct mii_softc *child;
304         int rv;
305
306         mii->mii_media_status = 0;
307         mii->mii_media_active = IFM_NONE;
308
309         for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
310              child = LIST_NEXT(child, mii_list)) {
311                 rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
312                 if (rv) {
313                         return rv;
314                 } else {
315                         /* Reset autonegotiation timer. */
316                         child->mii_ticks = 0;
317                 }
318         }
319         return 0;
320 }
321
322 /*
323  * Call the PHY tick routines, used during autonegotiation.
324  */
325 void
326 mii_tick(struct mii_data *mii)
327 {
328         struct mii_softc *child;
329
330         for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
331              child = LIST_NEXT(child, mii_list))
332                 (*child->mii_service)(child, mii, MII_TICK);
333 }
334
335 /*
336  * Get media status from PHYs.
337  */
338 void
339 mii_pollstat(struct mii_data *mii)
340 {
341         struct mii_softc *child;
342
343         mii->mii_media_status = 0;
344         mii->mii_media_active = IFM_NONE;
345
346         for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
347              child = LIST_NEXT(child, mii_list))
348                 (*child->mii_service)(child, mii, MII_POLLSTAT);
349 }
350
351 static moduledata_t miibus_mod = { "miibus" };
352
353 DECLARE_MODULE(miibus, miibus_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
354 MODULE_VERSION(miibus, 1);