mii.c revision 1.16 1 /* $NetBSD: mii.c,v 1.16 1999/11/12 18:13:00 thorpej 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/device.h>
48 #include <sys/systm.h>
49 #include <sys/socket.h>
50
51 #include <net/if.h>
52 #include <net/if_media.h>
53
54 #include <dev/mii/mii.h>
55 #include <dev/mii/miivar.h>
56
57 int mii_print __P((void *, const char *));
58 int mii_submatch __P((struct device *, struct cfdata *, void *));
59
60 /*
61 * Helper function used by network interface drivers, attaches PHYs
62 * to the network interface driver parent.
63 */
64 void
65 mii_phy_probe(parent, mii, capmask, phyloc, offloc)
66 struct device *parent;
67 struct mii_data *mii;
68 int capmask, phyloc, offloc;
69 {
70 struct mii_attach_args ma;
71 struct mii_softc *child;
72 int bmsr, offset = 0;
73 int phymin, phymax;
74
75 if (phyloc != MII_PHY_ANY && offloc != MII_PHY_ANY)
76 panic("mii_phy_probe: phyloc and offloc specified");
77
78 if (phyloc == MII_PHY_ANY) {
79 phymin = 0;
80 phymax = MII_NPHY - 1;
81 } else
82 phymin = phymax = phyloc;
83
84 if ((mii->mii_flags & MIIF_INITDONE) == 0) {
85 LIST_INIT(&mii->mii_phys);
86 mii->mii_flags |= MIIF_INITDONE;
87 }
88
89 for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) {
90 /*
91 * Make sure we haven't already configured a PHY at this
92 * address. This allows mii_phy_probe() to be called
93 * multiple times.
94 */
95 for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
96 child = LIST_NEXT(child, mii_list)) {
97 if (child->mii_phy == ma.mii_phyno) {
98 /*
99 * Yes, there is already something
100 * configured at this address.
101 */
102 offset++;
103 continue;
104 }
105 }
106
107 /*
108 * Check to see if there is a PHY at this address. Note,
109 * many braindead PHYs report 0/0 in their ID registers,
110 * so we test for media in the BMSR.
111 */
112 bmsr = (*mii->mii_readreg)(parent, ma.mii_phyno, MII_BMSR);
113 if (bmsr == 0 || bmsr == 0xffff ||
114 (bmsr & BMSR_MEDIAMASK) == 0) {
115 /* Assume no PHY at this address. */
116 continue;
117 }
118
119 /*
120 * There is a PHY at this address. If we were given an
121 * `offset' locator, skip this PHY if it doesn't match.
122 */
123 if (offloc != MII_OFFSET_ANY && offloc != offset) {
124 offset++;
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 = (*mii->mii_readreg)(parent, ma.mii_phyno,
134 MII_PHYIDR1);
135 ma.mii_id2 = (*mii->mii_readreg)(parent, ma.mii_phyno,
136 MII_PHYIDR2);
137
138 ma.mii_data = mii;
139 ma.mii_capmask = capmask;
140
141 if ((child = (struct mii_softc *)config_found_sm(parent, &ma,
142 mii_print, mii_submatch)) != NULL) {
143 /*
144 * Link it up in the parent's MII data.
145 */
146 LIST_INSERT_HEAD(&mii->mii_phys, child, mii_list);
147 child->mii_offset = offset;
148 mii->mii_instance++;
149 }
150 offset++;
151 }
152 }
153
154 int
155 mii_print(aux, pnp)
156 void *aux;
157 const char *pnp;
158 {
159 struct mii_attach_args *ma = aux;
160
161 if (pnp != NULL)
162 printf("OUI 0x%06x model 0x%04x rev %d at %s",
163 MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2),
164 MII_REV(ma->mii_id2), pnp);
165
166 printf(" phy %d", ma->mii_phyno);
167 return (UNCONF);
168 }
169
170 int
171 mii_submatch(parent, cf, aux)
172 struct device *parent;
173 struct cfdata *cf;
174 void *aux;
175 {
176 struct mii_attach_args *ma = aux;
177
178 if (ma->mii_phyno != cf->cf_loc[MIICF_PHY] &&
179 cf->cf_loc[MIICF_PHY] != MIICF_PHY_DEFAULT)
180 return (0);
181
182 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
183 }
184
185 /*
186 * Media changed; notify all PHYs.
187 */
188 int
189 mii_mediachg(mii)
190 struct mii_data *mii;
191 {
192 struct mii_softc *child;
193 int rv;
194
195 mii->mii_media_status = 0;
196 mii->mii_media_active = IFM_NONE;
197
198 for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
199 child = LIST_NEXT(child, mii_list)) {
200 rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
201 if (rv)
202 return (rv);
203 }
204 return (0);
205 }
206
207 /*
208 * Call the PHY tick routines, used during autonegotiation.
209 */
210 void
211 mii_tick(mii)
212 struct mii_data *mii;
213 {
214 struct mii_softc *child;
215
216 for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
217 child = LIST_NEXT(child, mii_list))
218 (void) (*child->mii_service)(child, mii, MII_TICK);
219 }
220
221 /*
222 * Get media status from PHYs.
223 */
224 void
225 mii_pollstat(mii)
226 struct mii_data *mii;
227 {
228 struct mii_softc *child;
229
230 mii->mii_media_status = 0;
231 mii->mii_media_active = IFM_NONE;
232
233 for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
234 child = LIST_NEXT(child, mii_list))
235 (void) (*child->mii_service)(child, mii, MII_POLLSTAT);
236 }
237
238 /*
239 * Inform the PHYs that the interface is down.
240 */
241 void
242 mii_down(mii)
243 struct mii_data *mii;
244 {
245 struct mii_softc *child;
246
247 for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
248 child = LIST_NEXT(child, mii_list))
249 (void) (*child->mii_service)(child, mii, MII_DOWN);
250 }
251