xref: /freebsd/sys/dev/mii/mii.c (revision 74bf4e164ba5851606a27d4feff27717452583e5)
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$");
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_readreg(device_t, int, int);
68 static int miibus_writereg(device_t, int, int, int);
69 static void miibus_statchg(device_t);
70 static void miibus_linkchg(device_t);
71 static void miibus_mediainit(device_t);
72 
73 static device_method_t miibus_methods[] = {
74 	/* device interface */
75 	DEVMETHOD(device_probe,		miibus_probe),
76 	DEVMETHOD(device_attach,	miibus_attach),
77 	DEVMETHOD(device_detach,	miibus_detach),
78 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
79 
80 	/* bus interface */
81 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
82 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
83 
84 	/* MII interface */
85 	DEVMETHOD(miibus_readreg,	miibus_readreg),
86 	DEVMETHOD(miibus_writereg,	miibus_writereg),
87 	DEVMETHOD(miibus_statchg,	miibus_statchg),
88 	DEVMETHOD(miibus_linkchg,	miibus_linkchg),
89 	DEVMETHOD(miibus_mediainit,	miibus_mediainit),
90 
91 	{ 0, 0 }
92 };
93 
94 devclass_t miibus_devclass;
95 
96 driver_t miibus_driver = {
97 	"miibus",
98 	miibus_methods,
99 	sizeof(struct mii_data)
100 };
101 
102 /*
103  * Helper function used by network interface drivers, attaches PHYs
104  * to the network interface driver parent.
105  */
106 
107 int
108 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
162 miibus_attach(dev)
163 	device_t		dev;
164 {
165 	void			**v;
166 	ifm_change_cb_t		ifmedia_upd;
167 	ifm_stat_cb_t		ifmedia_sts;
168 	struct mii_data		*mii;
169 
170 	mii = device_get_softc(dev);
171 	/*
172 	 * Note that each NIC's softc must start with an ifnet structure.
173 	 */
174 	mii->mii_ifp = device_get_softc(device_get_parent(dev));
175 	v = device_get_ivars(dev);
176 	ifmedia_upd = v[0];
177 	ifmedia_sts = v[1];
178 	ifmedia_init(&mii->mii_media, IFM_IMASK, ifmedia_upd, ifmedia_sts);
179 	bus_generic_attach(dev);
180 
181 	return(0);
182 }
183 
184 int
185 miibus_detach(dev)
186 	device_t		dev;
187 {
188 	struct mii_data		*mii;
189 
190 	bus_generic_detach(dev);
191 	mii = device_get_softc(dev);
192 	ifmedia_removeall(&mii->mii_media);
193 	mii->mii_ifp = NULL;
194 
195 	return(0);
196 }
197 
198 static int
199 miibus_readreg(dev, phy, reg)
200 	device_t		dev;
201 	int			phy, reg;
202 {
203 	device_t		parent;
204 
205 	parent = device_get_parent(dev);
206 	return(MIIBUS_READREG(parent, phy, reg));
207 }
208 
209 static int
210 miibus_writereg(dev, phy, reg, data)
211 	device_t		dev;
212 	int			phy, reg, data;
213 {
214 	device_t		parent;
215 
216 	parent = device_get_parent(dev);
217 	return(MIIBUS_WRITEREG(parent, phy, reg, data));
218 }
219 
220 static void
221 miibus_statchg(dev)
222 	device_t		dev;
223 {
224 	device_t		parent;
225 
226 	parent = device_get_parent(dev);
227 	MIIBUS_STATCHG(parent);
228 	return;
229 }
230 
231 void	(*vlan_link_state_p)(struct ifnet *, int);	/* XXX: private from if_vlan */
232 
233 static void
234 miibus_linkchg(dev)
235 	device_t dev;
236 {
237 	struct mii_data *mii;
238 	struct ifnet *ifp;
239 	device_t parent;
240 	int link, link_state;
241 
242 	parent = device_get_parent(dev);
243 	MIIBUS_LINKCHG(parent);
244 
245 	mii = device_get_softc(dev);
246 	/*
247 	 * Note that each NIC's softc must start with an ifnet structure.
248 	 */
249 	ifp = device_get_softc(parent);
250 
251 	if (mii->mii_media_status & IFM_AVALID) {
252 		if (mii->mii_media_status & IFM_ACTIVE) {
253 			link = NOTE_LINKUP;
254 			link_state = LINK_STATE_UP;
255 		} else {
256 			link = NOTE_LINKDOWN;
257 			link_state = LINK_STATE_DOWN;
258 		}
259 	} else {
260 		link = NOTE_LINKINV;
261 		link_state = LINK_STATE_UNKNOWN;
262 	}
263 
264 	/* Notify that the link state has changed. */
265 	if (ifp->if_link_state != link_state) {
266 		ifp->if_link_state = link_state;
267 		rt_ifmsg(ifp);
268 		KNOTE_UNLOCKED(&ifp->if_klist, link);
269 		if (ifp->if_nvlans != 0)
270 			(*vlan_link_state_p)(ifp, link);
271 	}
272 }
273 
274 static void
275 miibus_mediainit(dev)
276 	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(dev, child, ifmedia_upd, ifmedia_sts)
299 	device_t		dev;
300 	device_t		*child;
301 	ifm_change_cb_t		ifmedia_upd;
302 	ifm_stat_cb_t		ifmedia_sts;
303 {
304 	void			**v;
305 	int			bmsr, i;
306 
307 	v = 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_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(mii)
342 	struct mii_data *mii;
343 {
344 	struct mii_softc *child;
345 	int rv;
346 
347 	mii->mii_media_status = 0;
348 	mii->mii_media_active = IFM_NONE;
349 
350 	LIST_FOREACH(child, &mii->mii_phys, mii_list) {
351 		rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
352 		if (rv)
353 			return (rv);
354 	}
355 	return (0);
356 }
357 
358 /*
359  * Call the PHY tick routines, used during autonegotiation.
360  */
361 void
362 mii_tick(mii)
363 	struct mii_data *mii;
364 {
365 	struct mii_softc *child;
366 
367 	LIST_FOREACH(child, &mii->mii_phys, mii_list)
368 		(void) (*child->mii_service)(child, mii, MII_TICK);
369 }
370 
371 /*
372  * Get media status from PHYs.
373  */
374 void
375 mii_pollstat(mii)
376 	struct mii_data *mii;
377 {
378 	struct mii_softc *child;
379 
380 	mii->mii_media_status = 0;
381 	mii->mii_media_active = IFM_NONE;
382 
383 	LIST_FOREACH(child, &mii->mii_phys, mii_list)
384 		(void) (*child->mii_service)(child, mii, MII_POLLSTAT);
385 }
386 
387 /*
388  * Inform the PHYs that the interface is down.
389  */
390 void
391 mii_down(struct mii_data *mii)
392 {
393 	struct mii_softc *child;
394 
395 	LIST_FOREACH(child, &mii->mii_phys, mii_list)
396 		mii_phy_down(child);
397 }
398