xref: /dragonfly/sys/dev/netif/mii_layer/mii.c (revision 2cd2d2b5)
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  * $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.6 2004/04/07 05:45:28 dillon 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_readreg	(device_t, int, int);
66 static int miibus_writereg	(device_t, int, int, int);
67 static void miibus_statchg	(device_t);
68 static void miibus_mediainit	(device_t);
69 
70 static device_method_t miibus_methods[] = {
71 	/* device interface */
72 	DEVMETHOD(device_probe,		miibus_probe),
73 	DEVMETHOD(device_attach,	miibus_attach),
74 	DEVMETHOD(device_detach,	miibus_detach),
75 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
76 
77 	/* bus interface */
78 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
79 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
80 
81 	/* MII interface */
82 	DEVMETHOD(miibus_readreg,	miibus_readreg),
83 	DEVMETHOD(miibus_writereg,	miibus_writereg),
84 	DEVMETHOD(miibus_statchg,	miibus_statchg),
85 	DEVMETHOD(miibus_mediainit,	miibus_mediainit),
86 
87 	{ 0, 0 }
88 };
89 
90 devclass_t miibus_devclass;
91 
92 driver_t miibus_driver = {
93 	"miibus",
94 	miibus_methods,
95 	sizeof(struct mii_data)
96 };
97 
98 /*
99  * Helper function used by network interface drivers, attaches PHYs
100  * to the network interface driver parent.
101  */
102 
103 int miibus_probe(dev)
104 	device_t		dev;
105 {
106 	struct mii_attach_args	ma, *args;
107 	struct mii_data		*mii;
108 	device_t		child = NULL, parent;
109 	int			bmsr, capmask = 0xFFFFFFFF;
110 
111 	mii = device_get_softc(dev);
112 	parent = device_get_parent(dev);
113 	LIST_INIT(&mii->mii_phys);
114 
115 	for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) {
116 		/*
117 		 * Check to see if there is a PHY at this address.  Note,
118 		 * many braindead PHYs report 0/0 in their ID registers,
119 		 * so we test for media in the BMSR.
120 	 	 */
121 		bmsr = MIIBUS_READREG(parent, ma.mii_phyno, MII_BMSR);
122 		if (bmsr == 0 || bmsr == 0xffff ||
123 		    (bmsr & BMSR_MEDIAMASK) == 0) {
124 			/* Assume no PHY at this address. */
125 			continue;
126 		}
127 
128 		/*
129 		 * Extract the IDs. Braindead PHYs will be handled by
130 		 * the `ukphy' driver, as we have no ID information to
131 		 * match on.
132 	 	 */
133 		ma.mii_id1 = MIIBUS_READREG(parent, ma.mii_phyno,
134 		    MII_PHYIDR1);
135 		ma.mii_id2 = MIIBUS_READREG(parent, ma.mii_phyno,
136 		    MII_PHYIDR2);
137 
138 		ma.mii_data = mii;
139 		ma.mii_capmask = capmask;
140 
141 		args = malloc(sizeof(struct mii_attach_args),
142 		    M_DEVBUF, M_INTWAIT);
143 		bcopy((char *)&ma, (char *)args, sizeof(ma));
144 		child = device_add_child(dev, NULL, -1);
145 		device_set_ivars(child, args);
146 	}
147 
148 	if (child == NULL)
149 		return(ENXIO);
150 
151 	device_set_desc(dev, "MII bus");
152 
153 	return(0);
154 }
155 
156 int miibus_attach(dev)
157 	device_t		dev;
158 {
159 	void			**v;
160 	ifm_change_cb_t		ifmedia_upd;
161 	ifm_stat_cb_t		ifmedia_sts;
162 	struct mii_data		*mii;
163 
164 	mii = device_get_softc(dev);
165 	mii->mii_ifp = device_get_softc(device_get_parent(dev));
166 	v = device_get_ivars(dev);
167 	ifmedia_upd = v[0];
168 	ifmedia_sts = v[1];
169 	ifmedia_init(&mii->mii_media, IFM_IMASK, ifmedia_upd, ifmedia_sts);
170 	bus_generic_attach(dev);
171 
172 	return(0);
173 }
174 
175 int miibus_detach(dev)
176 	device_t		dev;
177 {
178 	struct mii_data		*mii;
179 
180 	bus_generic_detach(dev);
181 	mii = device_get_softc(dev);
182 	ifmedia_removeall(&mii->mii_media);
183 	mii->mii_ifp = NULL;
184 
185 	return(0);
186 }
187 
188 static int miibus_readreg(dev, phy, reg)
189 	device_t		dev;
190 	int			phy, reg;
191 {
192 	device_t		parent;
193 
194 	parent = device_get_parent(dev);
195 	return(MIIBUS_READREG(parent, phy, reg));
196 }
197 
198 static int miibus_writereg(dev, phy, reg, data)
199 	device_t		dev;
200 	int			phy, reg, data;
201 {
202 	device_t		parent;
203 
204 	parent = device_get_parent(dev);
205 	return(MIIBUS_WRITEREG(parent, phy, reg, data));
206 }
207 
208 static void miibus_statchg(dev)
209 	device_t		dev;
210 {
211 	device_t		parent;
212 
213 	parent = device_get_parent(dev);
214 	MIIBUS_STATCHG(parent);
215 	return;
216 }
217 
218 static void miibus_mediainit(dev)
219 	device_t		dev;
220 {
221 	struct mii_data		*mii;
222 	struct ifmedia_entry	*m;
223 	int			media = 0;
224 
225 	/* Poke the parent in case it has any media of its own to add. */
226 	MIIBUS_MEDIAINIT(device_get_parent(dev));
227 
228 	mii = device_get_softc(dev);
229 	for (m = LIST_FIRST(&mii->mii_media.ifm_list); m != NULL;
230 	     m = LIST_NEXT(m, ifm_list)) {
231 		media = m->ifm_media;
232 		if (media == (IFM_ETHER|IFM_AUTO))
233 			break;
234 	}
235 
236 	ifmedia_set(&mii->mii_media, media);
237 
238 	return;
239 }
240 
241 int mii_phy_probe(dev, child, ifmedia_upd, ifmedia_sts)
242 	device_t		dev;
243 	device_t		*child;
244 	ifm_change_cb_t		ifmedia_upd;
245 	ifm_stat_cb_t		ifmedia_sts;
246 {
247 	void			**v;
248 	int			bmsr, i;
249 
250 	v = malloc(sizeof(vm_offset_t) * 2, M_DEVBUF, M_INTWAIT);
251 	v[0] = ifmedia_upd;
252 	v[1] = ifmedia_sts;
253 	*child = device_add_child(dev, "miibus", -1);
254 	device_set_ivars(*child, v);
255 
256 	for (i = 0; i < MII_NPHY; i++) {
257 		bmsr = MIIBUS_READREG(dev, i, MII_BMSR);
258                 if (bmsr == 0 || bmsr == 0xffff ||
259                     (bmsr & BMSR_MEDIAMASK) == 0) {
260                         /* Assume no PHY at this address. */
261                         continue;
262                 } else
263 			break;
264 	}
265 
266 	if (i == MII_NPHY) {
267 		device_delete_child(dev, *child);
268 		*child = NULL;
269 		return(ENXIO);
270 	}
271 
272 	bus_generic_attach(dev);
273 
274 	return(0);
275 }
276 
277 /*
278  * Media changed; notify all PHYs.
279  */
280 int
281 mii_mediachg(mii)
282 	struct mii_data *mii;
283 {
284 	struct mii_softc *child;
285 	int rv;
286 
287 	mii->mii_media_status = 0;
288 	mii->mii_media_active = IFM_NONE;
289 
290 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
291 	     child = LIST_NEXT(child, mii_list)) {
292 		rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
293 		if (rv)
294 			return (rv);
295 	}
296 	return (0);
297 }
298 
299 /*
300  * Call the PHY tick routines, used during autonegotiation.
301  */
302 void
303 mii_tick(mii)
304 	struct mii_data *mii;
305 {
306 	struct mii_softc *child;
307 
308 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
309 	     child = LIST_NEXT(child, mii_list))
310 		(void) (*child->mii_service)(child, mii, MII_TICK);
311 }
312 
313 /*
314  * Get media status from PHYs.
315  */
316 void
317 mii_pollstat(mii)
318 	struct mii_data *mii;
319 {
320 	struct mii_softc *child;
321 
322 	mii->mii_media_status = 0;
323 	mii->mii_media_active = IFM_NONE;
324 
325 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
326 	     child = LIST_NEXT(child, mii_list))
327 		(void) (*child->mii_service)(child, mii, MII_POLLSTAT);
328 }
329 
330 static moduledata_t miibus_mod = { "miibus" };
331 
332 DECLARE_MODULE(miibus, miibus_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
333 
334