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