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