FreeBSD/Linux Kernel Cross Reference
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: releng/6.0/sys/dev/mii/mii.c 147287 2005-06-11 00:20:38Z brooks $");
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_child_location_str(device_t bus, device_t child, char *buf,
68 size_t buflen);
69 static int miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
70 size_t buflen);
71 static int miibus_readreg(device_t, int, int);
72 static int miibus_writereg(device_t, int, int, int);
73 static void miibus_statchg(device_t);
74 static void miibus_linkchg(device_t);
75 static void miibus_mediainit(device_t);
76
77 static device_method_t miibus_methods[] = {
78 /* device interface */
79 DEVMETHOD(device_probe, miibus_probe),
80 DEVMETHOD(device_attach, miibus_attach),
81 DEVMETHOD(device_detach, miibus_detach),
82 DEVMETHOD(device_shutdown, bus_generic_shutdown),
83
84 /* bus interface */
85 DEVMETHOD(bus_print_child, bus_generic_print_child),
86 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
87 DEVMETHOD(bus_child_pnpinfo_str, miibus_child_pnpinfo_str),
88 DEVMETHOD(bus_child_location_str, miibus_child_location_str),
89
90 /* MII interface */
91 DEVMETHOD(miibus_readreg, miibus_readreg),
92 DEVMETHOD(miibus_writereg, miibus_writereg),
93 DEVMETHOD(miibus_statchg, miibus_statchg),
94 DEVMETHOD(miibus_linkchg, miibus_linkchg),
95 DEVMETHOD(miibus_mediainit, miibus_mediainit),
96
97 { 0, 0 }
98 };
99
100 devclass_t miibus_devclass;
101
102 driver_t miibus_driver = {
103 "miibus",
104 miibus_methods,
105 sizeof(struct mii_data)
106 };
107
108 /*
109 * Helper function used by network interface drivers, attaches PHYs
110 * to the network interface driver parent.
111 */
112 int
113 miibus_probe(device_t dev)
114 {
115 struct mii_attach_args ma, *args;
116 struct mii_data *mii;
117 device_t child = NULL, parent;
118 int bmsr, capmask = 0xFFFFFFFF;
119
120 mii = device_get_softc(dev);
121 parent = device_get_parent(dev);
122 LIST_INIT(&mii->mii_phys);
123
124 for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) {
125 /*
126 * Check to see if there is a PHY at this address. Note,
127 * many braindead PHYs report 0/0 in their ID registers,
128 * so we test for media in the BMSR.
129 */
130 bmsr = MIIBUS_READREG(parent, ma.mii_phyno, MII_BMSR);
131 if (bmsr == 0 || bmsr == 0xffff ||
132 (bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) {
133 /* Assume no PHY at this address. */
134 continue;
135 }
136
137 /*
138 * Extract the IDs. Braindead PHYs will be handled by
139 * the `ukphy' driver, as we have no ID information to
140 * match on.
141 */
142 ma.mii_id1 = MIIBUS_READREG(parent, ma.mii_phyno,
143 MII_PHYIDR1);
144 ma.mii_id2 = MIIBUS_READREG(parent, ma.mii_phyno,
145 MII_PHYIDR2);
146
147 ma.mii_data = mii;
148 ma.mii_capmask = capmask;
149
150 args = malloc(sizeof(struct mii_attach_args),
151 M_DEVBUF, M_NOWAIT);
152 bcopy((char *)&ma, (char *)args, sizeof(ma));
153 child = device_add_child(dev, NULL, -1);
154 device_set_ivars(child, args);
155 }
156
157 if (child == NULL)
158 return(ENXIO);
159
160 device_set_desc(dev, "MII bus");
161
162 return(0);
163 }
164
165 int
166 miibus_attach(device_t dev)
167 {
168 void **v;
169 ifm_change_cb_t ifmedia_upd;
170 ifm_stat_cb_t ifmedia_sts;
171 struct mii_data *mii;
172
173 mii = device_get_softc(dev);
174 /*
175 * Note that each NIC's softc must start with an ifnet pointer.
176 * XXX: EVIL HACK!
177 */
178 mii->mii_ifp = *(struct ifnet**)device_get_softc(device_get_parent(dev));
179 v = device_get_ivars(dev);
180 ifmedia_upd = v[0];
181 ifmedia_sts = v[1];
182 ifmedia_init(&mii->mii_media, IFM_IMASK, ifmedia_upd, ifmedia_sts);
183 bus_generic_attach(dev);
184
185 return(0);
186 }
187
188 int
189 miibus_detach(device_t dev)
190 {
191 struct mii_data *mii;
192
193 bus_generic_detach(dev);
194 mii = device_get_softc(dev);
195 ifmedia_removeall(&mii->mii_media);
196 mii->mii_ifp = NULL;
197
198 return(0);
199 }
200
201 static int
202 miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
203 size_t buflen)
204 {
205 struct mii_attach_args *maa = device_get_ivars(child);
206 snprintf(buf, buflen, "oui=0x%x model=0x%x rev=0x%x",
207 MII_OUI(maa->mii_id1, maa->mii_id2),
208 MII_MODEL(maa->mii_id2), MII_REV(maa->mii_id2));
209 return (0);
210 }
211
212 static int
213 miibus_child_location_str(device_t bus, device_t child, char *buf,
214 size_t buflen)
215 {
216 struct mii_attach_args *maa = device_get_ivars(child);
217 snprintf(buf, buflen, "phyno=%d", maa->mii_phyno);
218 return (0);
219 }
220
221 static int
222 miibus_readreg(device_t dev, int phy, int reg)
223 {
224 device_t parent;
225
226 parent = device_get_parent(dev);
227 return(MIIBUS_READREG(parent, phy, reg));
228 }
229
230 static int
231 miibus_writereg(device_t dev, int phy, int reg, int data)
232 {
233 device_t parent;
234
235 parent = device_get_parent(dev);
236 return(MIIBUS_WRITEREG(parent, phy, reg, data));
237 }
238
239 static void
240 miibus_statchg(device_t dev)
241 {
242 device_t parent;
243
244 parent = device_get_parent(dev);
245 MIIBUS_STATCHG(parent);
246 return;
247 }
248
249 static void
250 miibus_linkchg(device_t dev)
251 {
252 struct mii_data *mii;
253 device_t parent;
254 int link_state;
255
256 parent = device_get_parent(dev);
257 MIIBUS_LINKCHG(parent);
258
259 mii = device_get_softc(dev);
260
261 if (mii->mii_media_status & IFM_AVALID) {
262 if (mii->mii_media_status & IFM_ACTIVE)
263 link_state = LINK_STATE_UP;
264 else
265 link_state = LINK_STATE_DOWN;
266 } else
267 link_state = LINK_STATE_UNKNOWN;
268 /*
269 * Note that each NIC's softc must start with an ifnet pointer.
270 * XXX: EVIL HACK!
271 */
272 if_link_state_change(*(struct ifnet**)device_get_softc(parent), link_state);
273 }
274
275 static void
276 miibus_mediainit(device_t dev)
277 {
278 struct mii_data *mii;
279 struct ifmedia_entry *m;
280 int media = 0;
281
282 /* Poke the parent in case it has any media of its own to add. */
283 MIIBUS_MEDIAINIT(device_get_parent(dev));
284
285 mii = device_get_softc(dev);
286 LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) {
287 media = m->ifm_media;
288 if (media == (IFM_ETHER|IFM_AUTO))
289 break;
290 }
291
292 ifmedia_set(&mii->mii_media, media);
293
294 return;
295 }
296
297 int
298 mii_phy_probe(device_t dev, device_t *child, ifm_change_cb_t ifmedia_upd,
299 ifm_stat_cb_t ifmedia_sts)
300 {
301 void **v;
302 int bmsr, i;
303
304 v = malloc(sizeof(vm_offset_t) * 2, M_DEVBUF, M_NOWAIT);
305 if (v == 0) {
306 return (ENOMEM);
307 }
308 v[0] = ifmedia_upd;
309 v[1] = ifmedia_sts;
310 *child = device_add_child(dev, "miibus", -1);
311 device_set_ivars(*child, v);
312
313 for (i = 0; i < MII_NPHY; i++) {
314 bmsr = MIIBUS_READREG(dev, i, MII_BMSR);
315 if (bmsr == 0 || bmsr == 0xffff ||
316 (bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) {
317 /* Assume no PHY at this address. */
318 continue;
319 } else
320 break;
321 }
322
323 if (i == MII_NPHY) {
324 device_delete_child(dev, *child);
325 *child = NULL;
326 return(ENXIO);
327 }
328
329 bus_generic_attach(dev);
330
331 return(0);
332 }
333
334 /*
335 * Media changed; notify all PHYs.
336 */
337 int
338 mii_mediachg(struct mii_data *mii)
339 {
340 struct mii_softc *child;
341 int rv;
342
343 mii->mii_media_status = 0;
344 mii->mii_media_active = IFM_NONE;
345
346 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
347 rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
348 if (rv)
349 return (rv);
350 }
351 return (0);
352 }
353
354 /*
355 * Call the PHY tick routines, used during autonegotiation.
356 */
357 void
358 mii_tick(struct mii_data *mii)
359 {
360 struct mii_softc *child;
361
362 LIST_FOREACH(child, &mii->mii_phys, mii_list)
363 (void) (*child->mii_service)(child, mii, MII_TICK);
364 }
365
366 /*
367 * Get media status from PHYs.
368 */
369 void
370 mii_pollstat(struct mii_data *mii)
371 {
372 struct mii_softc *child;
373
374 mii->mii_media_status = 0;
375 mii->mii_media_active = IFM_NONE;
376
377 LIST_FOREACH(child, &mii->mii_phys, mii_list)
378 (void) (*child->mii_service)(child, mii, MII_POLLSTAT);
379 }
380
381 /*
382 * Inform the PHYs that the interface is down.
383 */
384 void
385 mii_down(struct mii_data *mii)
386 {
387 struct mii_softc *child;
388
389 LIST_FOREACH(child, &mii->mii_phys, mii_list)
390 mii_phy_down(child);
391 }
Cache object: 5c25eb59a409ae4ed79353c627d24ecd
|