mii.c revision 205275
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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/dev/mii/mii.c 205275 2010-03-18 07:35:20Z ed $");
35
36/*
37 * MII bus layer, glues MII-capable network interface drivers to sharable
38 * PHY drivers.  This exports an interface compatible with BSD/OS 3.0's,
39 * plus some NetBSD extensions.
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/socket.h>
45#include <sys/malloc.h>
46#include <sys/module.h>
47#include <sys/bus.h>
48
49#include <net/if.h>
50#include <net/if_media.h>
51#include <net/route.h>
52
53#include <dev/mii/mii.h>
54#include <dev/mii/miivar.h>
55
56MODULE_VERSION(miibus, 1);
57
58#include "miibus_if.h"
59
60static int miibus_print_child(device_t dev, device_t child);
61static int miibus_child_location_str(device_t bus, device_t child, char *buf,
62    size_t buflen);
63static int miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
64    size_t buflen);
65static int miibus_readreg(device_t, int, int);
66static int miibus_writereg(device_t, int, int, int);
67static void miibus_statchg(device_t);
68static void miibus_linkchg(device_t);
69static void miibus_mediainit(device_t);
70
71static device_method_t miibus_methods[] = {
72	/* device interface */
73	DEVMETHOD(device_probe,		miibus_probe),
74	DEVMETHOD(device_attach,	miibus_attach),
75	DEVMETHOD(device_detach,	miibus_detach),
76	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
77
78	/* bus interface */
79	DEVMETHOD(bus_print_child,	miibus_print_child),
80	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
81	DEVMETHOD(bus_child_pnpinfo_str, miibus_child_pnpinfo_str),
82	DEVMETHOD(bus_child_location_str, miibus_child_location_str),
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
94devclass_t miibus_devclass;
95
96driver_t miibus_driver = {
97	"miibus",
98	miibus_methods,
99	sizeof(struct mii_data)
100};
101
102struct miibus_ivars {
103	ifm_change_cb_t	ifmedia_upd;
104	ifm_stat_cb_t	ifmedia_sts;
105};
106
107/*
108 * Helper function used by network interface drivers, attaches PHYs
109 * to the network interface driver parent.
110 */
111int
112miibus_probe(device_t dev)
113{
114	struct mii_attach_args	ma, *args;
115	struct mii_data		*mii;
116	device_t		child = NULL, parent;
117	int			bmsr, capmask = 0xFFFFFFFF;
118
119	mii = device_get_softc(dev);
120	parent = device_get_parent(dev);
121	LIST_INIT(&mii->mii_phys);
122
123	for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) {
124		/*
125		 * Check to see if there is a PHY at this address.  Note,
126		 * many braindead PHYs report 0/0 in their ID registers,
127		 * so we test for media in the BMSR.
128	 	 */
129		bmsr = MIIBUS_READREG(parent, ma.mii_phyno, MII_BMSR);
130		if (bmsr == 0 || bmsr == 0xffff ||
131		    (bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) {
132			/* Assume no PHY at this address. */
133			continue;
134		}
135
136		/*
137		 * Extract the IDs. Braindead PHYs will be handled by
138		 * the `ukphy' driver, as we have no ID information to
139		 * match on.
140	 	 */
141		ma.mii_id1 = MIIBUS_READREG(parent, ma.mii_phyno,
142		    MII_PHYIDR1);
143		ma.mii_id2 = MIIBUS_READREG(parent, ma.mii_phyno,
144		    MII_PHYIDR2);
145
146		ma.mii_data = mii;
147		ma.mii_capmask = capmask;
148
149		args = malloc(sizeof(struct mii_attach_args),
150		    M_DEVBUF, M_NOWAIT);
151		bcopy((char *)&ma, (char *)args, sizeof(ma));
152		child = device_add_child(dev, NULL, -1);
153		device_set_ivars(child, args);
154	}
155
156	if (child == NULL)
157		return(ENXIO);
158
159	device_set_desc(dev, "MII bus");
160
161	return(0);
162}
163
164int
165miibus_attach(device_t dev)
166{
167	struct miibus_ivars	*ivars;
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 pointer.
173	 * XXX: EVIL HACK!
174	 */
175	mii->mii_ifp = *(struct ifnet**)device_get_softc(device_get_parent(dev));
176	mii->mii_ifp->if_capabilities |= IFCAP_LINKSTATE;
177	mii->mii_ifp->if_capenable |= IFCAP_LINKSTATE;
178	ivars = device_get_ivars(dev);
179	ifmedia_init(&mii->mii_media, IFM_IMASK, ivars->ifmedia_upd,
180	    ivars->ifmedia_sts);
181	bus_generic_attach(dev);
182
183	return(0);
184}
185
186int
187miibus_detach(device_t dev)
188{
189	struct mii_data		*mii;
190
191	bus_generic_detach(dev);
192	mii = device_get_softc(dev);
193	ifmedia_removeall(&mii->mii_media);
194	mii->mii_ifp = NULL;
195
196	return(0);
197}
198
199static int
200miibus_print_child(device_t dev, device_t child)
201{
202	struct mii_attach_args *ma;
203	int retval;
204
205	ma = device_get_ivars(child);
206	retval = bus_print_child_header(dev, child);
207	retval += printf(" PHY %d", ma->mii_phyno);
208	retval += bus_print_child_footer(dev, child);
209
210	return (retval);
211}
212
213static int
214miibus_child_pnpinfo_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, "oui=0x%x model=0x%x rev=0x%x",
219	    MII_OUI(maa->mii_id1, maa->mii_id2),
220	    MII_MODEL(maa->mii_id2), MII_REV(maa->mii_id2));
221	return (0);
222}
223
224static int
225miibus_child_location_str(device_t bus, device_t child, char *buf,
226    size_t buflen)
227{
228	struct mii_attach_args *maa = device_get_ivars(child);
229	snprintf(buf, buflen, "phyno=%d", maa->mii_phyno);
230	return (0);
231}
232
233static int
234miibus_readreg(device_t dev, int phy, int reg)
235{
236	device_t		parent;
237
238	parent = device_get_parent(dev);
239	return(MIIBUS_READREG(parent, phy, reg));
240}
241
242static int
243miibus_writereg(device_t dev, int phy, int reg, int data)
244{
245	device_t		parent;
246
247	parent = device_get_parent(dev);
248	return(MIIBUS_WRITEREG(parent, phy, reg, data));
249}
250
251static void
252miibus_statchg(device_t dev)
253{
254	device_t		parent;
255	struct mii_data		*mii;
256
257	parent = device_get_parent(dev);
258	MIIBUS_STATCHG(parent);
259
260	mii = device_get_softc(dev);
261	mii->mii_ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
262	return;
263}
264
265static void
266miibus_linkchg(device_t dev)
267{
268	struct mii_data		*mii;
269	device_t		parent;
270	int			link_state;
271
272	parent = device_get_parent(dev);
273	MIIBUS_LINKCHG(parent);
274
275	mii = device_get_softc(dev);
276
277	if (mii->mii_media_status & IFM_AVALID) {
278		if (mii->mii_media_status & IFM_ACTIVE)
279			link_state = LINK_STATE_UP;
280		else
281			link_state = LINK_STATE_DOWN;
282	} else
283		link_state = LINK_STATE_UNKNOWN;
284	if_link_state_change(mii->mii_ifp, link_state);
285}
286
287static void
288miibus_mediainit(device_t dev)
289{
290	struct mii_data		*mii;
291	struct ifmedia_entry	*m;
292	int			media = 0;
293
294	/* Poke the parent in case it has any media of its own to add. */
295	MIIBUS_MEDIAINIT(device_get_parent(dev));
296
297	mii = device_get_softc(dev);
298	LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) {
299		media = m->ifm_media;
300		if (media == (IFM_ETHER|IFM_AUTO))
301			break;
302	}
303
304	ifmedia_set(&mii->mii_media, media);
305
306	return;
307}
308
309int
310mii_phy_probe(device_t dev, device_t *child, ifm_change_cb_t ifmedia_upd,
311    ifm_stat_cb_t ifmedia_sts)
312{
313	struct miibus_ivars	*ivars;
314	int			bmsr, i;
315
316	ivars = malloc(sizeof(*ivars), M_DEVBUF, M_NOWAIT);
317	if (ivars == NULL)
318		return (ENOMEM);
319	ivars->ifmedia_upd = ifmedia_upd;
320	ivars->ifmedia_sts = ifmedia_sts;
321	*child = device_add_child(dev, "miibus", -1);
322	device_set_ivars(*child, ivars);
323
324	for (i = 0; i < MII_NPHY; i++) {
325		bmsr = MIIBUS_READREG(dev, i, MII_BMSR);
326                if (bmsr == 0 || bmsr == 0xffff ||
327                    (bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) {
328                        /* Assume no PHY at this address. */
329                        continue;
330                } else
331			break;
332	}
333
334	if (i == MII_NPHY) {
335		device_delete_child(dev, *child);
336		*child = NULL;
337		return(ENXIO);
338	}
339
340	bus_generic_attach(dev);
341
342	return(0);
343}
344
345/*
346 * Media changed; notify all PHYs.
347 */
348int
349mii_mediachg(struct mii_data *mii)
350{
351	struct mii_softc *child;
352	int rv;
353
354	mii->mii_media_status = 0;
355	mii->mii_media_active = IFM_NONE;
356
357	LIST_FOREACH(child, &mii->mii_phys, mii_list) {
358		rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
359		if (rv)
360			return (rv);
361	}
362	return (0);
363}
364
365/*
366 * Call the PHY tick routines, used during autonegotiation.
367 */
368void
369mii_tick(struct mii_data *mii)
370{
371	struct mii_softc *child;
372
373	LIST_FOREACH(child, &mii->mii_phys, mii_list)
374		(void) (*child->mii_service)(child, mii, MII_TICK);
375}
376
377/*
378 * Get media status from PHYs.
379 */
380void
381mii_pollstat(struct mii_data *mii)
382{
383	struct mii_softc *child;
384
385	mii->mii_media_status = 0;
386	mii->mii_media_active = IFM_NONE;
387
388	LIST_FOREACH(child, &mii->mii_phys, mii_list)
389		(void) (*child->mii_service)(child, mii, MII_POLLSTAT);
390}
391
392/*
393 * Inform the PHYs that the interface is down.
394 */
395void
396mii_down(struct mii_data *mii)
397{
398	struct mii_softc *child;
399
400	LIST_FOREACH(child, &mii->mii_phys, mii_list)
401		mii_phy_down(child);
402}
403