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