mii.c revision 1.44 1 /* $NetBSD: mii.c,v 1.44 2008/01/10 07:29:41 dyoung Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000 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.
43 */
44
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: mii.c,v 1.44 2008/01/10 07:29:41 dyoung Exp $");
47
48 #include <sys/param.h>
49 #include <sys/device.h>
50 #include <sys/systm.h>
51 #include <sys/socket.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 #include "locators.h"
60
61 static int mii_print(void *, const char *);
62
63 /*
64 * Helper function used by network interface drivers, attaches PHYs
65 * to the network interface driver parent.
66 */
67 void
68 mii_attach(struct device *parent, struct mii_data *mii, int capmask,
69 int phyloc, int offloc, int flags)
70 {
71 struct mii_attach_args ma;
72 struct mii_softc *child;
73 int bmsr, offset = 0;
74 int phymin, phymax;
75 int locs[MIICF_NLOCS];
76
77 if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY)
78 panic("mii_attach: phyloc and offloc specified");
79
80 if (phyloc == MII_PHY_ANY) {
81 phymin = 0;
82 phymax = MII_NPHY - 1;
83 } else
84 phymin = phymax = phyloc;
85
86 if ((mii->mii_flags & MIIF_INITDONE) == 0) {
87 LIST_INIT(&mii->mii_phys);
88 mii->mii_flags |= MIIF_INITDONE;
89 }
90
91 for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) {
92 /*
93 * Make sure we haven't already configured a PHY at this
94 * address. This allows mii_attach() to be called
95 * multiple times.
96 */
97 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
98 if (child->mii_phy == ma.mii_phyno) {
99 /*
100 * Yes, there is already something
101 * configured at this address.
102 */
103 offset++;
104 continue;
105 }
106 }
107
108 /*
109 * Check to see if there is a PHY at this address. Note,
110 * many braindead PHYs report 0/0 in their ID registers,
111 * so we test for media in the BMSR.
112 */
113 bmsr = (*mii->mii_readreg)(parent, ma.mii_phyno, MII_BMSR);
114 if (bmsr == 0 || bmsr == 0xffff ||
115 (bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) {
116 /* Assume no PHY at this address. */
117 continue;
118 }
119
120 /*
121 * There is a PHY at this address. If we were given an
122 * `offset' locator, skip this PHY if it doesn't match.
123 */
124 if (offloc != MII_OFFSET_ANY && offloc != offset) {
125 offset++;
126 continue;
127 }
128
129 /*
130 * Extract the IDs. Braindead PHYs will be handled by
131 * the `ukphy' driver, as we have no ID information to
132 * match on.
133 */
134 ma.mii_id1 = (*mii->mii_readreg)(parent, ma.mii_phyno,
135 MII_PHYIDR1);
136 ma.mii_id2 = (*mii->mii_readreg)(parent, ma.mii_phyno,
137 MII_PHYIDR2);
138
139 ma.mii_data = mii;
140 ma.mii_capmask = capmask;
141 ma.mii_flags = flags | (mii->mii_flags & MIIF_INHERIT_MASK);
142
143 locs[MIICF_PHY] = ma.mii_phyno;
144
145 child = (struct mii_softc *)config_found_sm_loc(parent, "mii",
146 locs, &ma, mii_print, config_stdsubmatch);
147 if (child) {
148 /*
149 * Link it up in the parent's MII data.
150 */
151 callout_init(&child->mii_nway_ch, 0);
152 LIST_INSERT_HEAD(&mii->mii_phys, child, mii_list);
153 child->mii_offset = offset;
154 mii->mii_instance++;
155 }
156 offset++;
157 }
158 }
159
160 void
161 mii_activate(struct mii_data *mii, enum devact act, int phyloc, int offloc)
162 {
163 struct mii_softc *child;
164
165 if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY)
166 panic("mii_activate: phyloc and offloc specified");
167
168 if ((mii->mii_flags & MIIF_INITDONE) == 0)
169 return;
170
171 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
172 if (phyloc != MII_PHY_ANY || offloc != MII_OFFSET_ANY) {
173 if (phyloc != MII_PHY_ANY &&
174 phyloc != child->mii_phy)
175 continue;
176 if (offloc != MII_OFFSET_ANY &&
177 offloc != child->mii_offset)
178 continue;
179 }
180 switch (act) {
181 case DVACT_ACTIVATE:
182 panic("mii_activate: DVACT_ACTIVATE");
183 break;
184
185 case DVACT_DEACTIVATE:
186 if (config_deactivate(&child->mii_dev) != 0)
187 panic("%s: config_activate(%d) failed",
188 child->mii_dev.dv_xname, act);
189 }
190 }
191 }
192
193 void
194 mii_detach(struct mii_data *mii, int phyloc, int offloc)
195 {
196 struct mii_softc *child, *nchild;
197
198 if (phyloc != MII_PHY_ANY && offloc != MII_PHY_ANY)
199 panic("mii_detach: phyloc and offloc specified");
200
201 if ((mii->mii_flags & MIIF_INITDONE) == 0)
202 return;
203
204 for (child = LIST_FIRST(&mii->mii_phys);
205 child != NULL; child = nchild) {
206 nchild = LIST_NEXT(child, mii_list);
207 if (phyloc != MII_PHY_ANY || offloc != MII_OFFSET_ANY) {
208 if (phyloc != MII_PHY_ANY &&
209 phyloc != child->mii_phy)
210 continue;
211 if (offloc != MII_OFFSET_ANY &&
212 offloc != child->mii_offset)
213 continue;
214 }
215 (void) config_detach(&child->mii_dev, DETACH_FORCE);
216 }
217 }
218
219 static int
220 mii_print(void *aux, const char *pnp)
221 {
222 struct mii_attach_args *ma = aux;
223
224 if (pnp != NULL)
225 aprint_normal("OUI 0x%06x model 0x%04x rev %d at %s",
226 MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2),
227 MII_REV(ma->mii_id2), pnp);
228
229 aprint_normal(" phy %d", ma->mii_phyno);
230 return (UNCONF);
231 }
232
233 static inline int
234 phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
235 {
236 if (!device_is_active(&sc->mii_dev))
237 return ENXIO;
238 return PHY_SERVICE(sc, mii, cmd);
239 }
240
241 int
242 mii_ifmedia_change(struct mii_data *mii)
243 {
244 return ifmedia_change(&mii->mii_media, mii->mii_ifp);
245 }
246
247 /*
248 * Media changed; notify all PHYs.
249 */
250 int
251 mii_mediachg(struct mii_data *mii)
252 {
253 struct mii_softc *child;
254 int rv;
255
256 mii->mii_media_status = 0;
257 mii->mii_media_active = IFM_NONE;
258
259 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
260 rv = phy_service(child, mii, MII_MEDIACHG);
261 if (rv)
262 return (rv);
263 }
264 return (0);
265 }
266
267 /*
268 * Call the PHY tick routines, used during autonegotiation.
269 */
270 void
271 mii_tick(struct mii_data *mii)
272 {
273 struct mii_softc *child;
274
275 LIST_FOREACH(child, &mii->mii_phys, mii_list)
276 (void)phy_service(child, mii, MII_TICK);
277 }
278
279 /*
280 * Get media status from PHYs.
281 */
282 void
283 mii_pollstat(struct mii_data *mii)
284 {
285 struct mii_softc *child;
286
287 mii->mii_media_status = 0;
288 mii->mii_media_active = IFM_NONE;
289
290 LIST_FOREACH(child, &mii->mii_phys, mii_list)
291 (void)phy_service(child, mii, MII_POLLSTAT);
292 }
293
294 /*
295 * Inform the PHYs that the interface is down.
296 */
297 void
298 mii_down(struct mii_data *mii)
299 {
300 struct mii_softc *child;
301
302 LIST_FOREACH(child, &mii->mii_phys, mii_list)
303 (void)phy_service(child, mii, MII_DOWN);
304 }
305
306 static unsigned char
307 bitreverse(unsigned char x)
308 {
309 static unsigned char nibbletab[16] = {
310 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
311 };
312
313 return ((nibbletab[x & 15] << 4) | nibbletab[x >> 4]);
314 }
315
316 u_int
317 mii_oui(u_int id1, u_int id2)
318 {
319 u_int h;
320
321 h = (id1 << 6) | (id2 >> 10);
322
323 return ((bitreverse(h >> 16) << 16) |
324 (bitreverse((h >> 8) & 255) << 8) |
325 bitreverse(h & 255));
326 }
327