1 /* $NetBSD: mii.c,v 1.58 2022/08/14 20:34:26 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000, 2020 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 /*
34 * MII bus layer, glues MII-capable network interface drivers to sharable
35 * PHY drivers.
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: mii.c,v 1.58 2022/08/14 20:34:26 riastradh Exp $");
40
41 #define __IFMEDIA_PRIVATE
42
43 #include <sys/param.h>
44 #include <sys/device.h>
45 #include <sys/systm.h>
46 #include <sys/socket.h>
47
48 #include <net/if.h>
49 #include <net/if_media.h>
50
51 #include <dev/mii/mii.h>
52 #include <dev/mii/miivar.h>
53
54 #include "locators.h"
55
56 static int mii_print(void *, const char *);
57
58 /*
59 * Helper function used by network interface drivers, attaches PHYs
60 * to the network interface driver parent.
61 */
62 void
mii_attach(device_t parent,struct mii_data * mii,int capmask,int phyloc,int offloc,int flags)63 mii_attach(device_t parent, struct mii_data *mii, int capmask,
64 int phyloc, int offloc, int flags)
65 {
66 struct mii_attach_args ma;
67 struct mii_softc *child;
68 int offset = 0;
69 uint16_t bmsr;
70 int phymin, phymax;
71 int locs[MIICF_NLOCS];
72 int rv;
73
74 KASSERT(mii->mii_media.ifm_lock != NULL);
75
76 if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY)
77 panic("mii_attach: phyloc and offloc specified");
78
79 if (phyloc == MII_PHY_ANY) {
80 phymin = 0;
81 phymax = MII_NPHY - 1;
82 } else
83 phymin = phymax = phyloc;
84
85 mii_lock(mii);
86
87 if ((mii->mii_flags & MIIF_INITDONE) == 0) {
88 LIST_INIT(&mii->mii_phys);
89 cv_init(&mii->mii_probe_cv, "mii_attach");
90 mii->mii_flags |= MIIF_INITDONE;
91 }
92
93 /*
94 * Probing temporarily unlocks the MII; wait until anyone
95 * else who might be doing this to finish.
96 */
97 mii->mii_probe_waiters++;
98 for (;;) {
99 if (mii->mii_flags & MIIF_EXITING) {
100 mii->mii_probe_waiters--;
101 cv_signal(&mii->mii_probe_cv);
102 mii_unlock(mii);
103 return;
104 }
105 if ((mii->mii_flags & MIIF_PROBING) == 0)
106 break;
107 cv_wait(&mii->mii_probe_cv, mii->mii_media.ifm_lock);
108 }
109 mii->mii_probe_waiters--;
110
111 /* ...and old others off. */
112 mii->mii_flags |= MIIF_PROBING;
113
114 for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) {
115 /*
116 * Make sure we haven't already configured a PHY at this
117 * address. This allows mii_attach() to be called
118 * multiple times.
119 */
120 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
121 if (child->mii_phy == ma.mii_phyno) {
122 /*
123 * Yes, there is already something
124 * configured at this address.
125 */
126 offset++;
127 continue;
128 }
129 }
130
131 /*
132 * Check to see if there is a PHY at this address. Note,
133 * many braindead PHYs report 0/0 in their ID registers,
134 * so we test for media in the BMSR.
135 */
136 bmsr = 0;
137 rv = (*mii->mii_readreg)(parent, ma.mii_phyno, MII_BMSR,
138 &bmsr);
139 if ((rv != 0) || bmsr == 0 || bmsr == 0xffff ||
140 (bmsr & (BMSR_EXTSTAT | BMSR_MEDIAMASK)) == 0) {
141 /* Assume no PHY at this address. */
142 continue;
143 }
144
145 /*
146 * There is a PHY at this address. If we were given an
147 * `offset' locator, skip this PHY if it doesn't match.
148 */
149 if (offloc != MII_OFFSET_ANY && offloc != offset) {
150 offset++;
151 continue;
152 }
153
154 /*
155 * Extract the IDs. Braindead PHYs will be handled by
156 * the `ukphy' driver, as we have no ID information to
157 * match on.
158 */
159 ma.mii_id1 = ma.mii_id2 = 0;
160 rv = (*mii->mii_readreg)(parent, ma.mii_phyno,
161 MII_PHYIDR1, &ma.mii_id1);
162 rv |= (*mii->mii_readreg)(parent, ma.mii_phyno,
163 MII_PHYIDR2, &ma.mii_id2);
164 if (rv != 0)
165 continue;
166
167 ma.mii_data = mii;
168 ma.mii_capmask = capmask;
169 ma.mii_flags = flags | (mii->mii_flags & MIIF_INHERIT_MASK);
170
171 locs[MIICF_PHY] = ma.mii_phyno;
172
173 mii_unlock(mii);
174
175 child = device_private(
176 config_found(parent, &ma, mii_print,
177 CFARGS(.submatch = config_stdsubmatch,
178 .iattr = "mii",
179 .locators = locs)));
180 if (child) {
181 /* Link it up in the parent's MII data. */
182 if (child->mii_flags & MIIF_AUTOTSLEEP)
183 cv_init(&child->mii_nway_cv, "miiauto");
184 else
185 callout_init(&child->mii_nway_ch, 0);
186 mii_lock(mii);
187 LIST_INSERT_HEAD(&mii->mii_phys, child, mii_list);
188 child->mii_offset = offset;
189 mii->mii_instance++;
190 } else {
191 mii_lock(mii);
192 }
193 offset++;
194 }
195
196 /* All done! */
197 mii->mii_flags &= ~MIIF_PROBING;
198 cv_signal(&mii->mii_probe_cv);
199 mii_unlock(mii);
200 }
201
202 void
mii_detach(struct mii_data * mii,int phyloc,int offloc)203 mii_detach(struct mii_data *mii, int phyloc, int offloc)
204 {
205 struct mii_softc *child, *nchild;
206 bool exiting = false;
207
208 if (phyloc != MII_PHY_ANY && offloc != MII_PHY_ANY)
209 panic("mii_detach: phyloc and offloc specified");
210
211 mii_lock(mii);
212
213 if ((mii->mii_flags & MIIF_INITDONE) == 0 ||
214 (mii->mii_flags & MIIF_EXITING) != 0) {
215 mii_unlock(mii);
216 return;
217 }
218
219 /*
220 * XXX This is probably not the best hueristic. Consider
221 * XXX adding an argument to mii_detach().
222 */
223 if (phyloc == MII_PHY_ANY && MII_PHY_ANY == MII_OFFSET_ANY)
224 exiting = true;
225
226 if (exiting) {
227 mii->mii_flags |= MIIF_EXITING;
228 cv_broadcast(&mii->mii_probe_cv);
229 }
230
231 /* Wait for everyone else to get out. */
232 mii->mii_probe_waiters++;
233 for (;;) {
234 /*
235 * If we've been waiting to do a less-than-exit, and
236 * *someone else* initiated an exit, then get out.
237 */
238 if (!exiting && (mii->mii_flags & MIIF_EXITING) != 0) {
239 mii->mii_probe_waiters--;
240 cv_signal(&mii->mii_probe_cv);
241 mii_unlock(mii);
242 return;
243 }
244 if ((mii->mii_flags & MIIF_PROBING) == 0 &&
245 (!exiting || (exiting && mii->mii_probe_waiters == 1)))
246 break;
247 cv_wait(&mii->mii_probe_cv, mii->mii_media.ifm_lock);
248 }
249 mii->mii_probe_waiters--;
250
251 if (!exiting)
252 mii->mii_flags |= MIIF_PROBING;
253
254 LIST_FOREACH_SAFE(child, &mii->mii_phys, mii_list, nchild) {
255 if (phyloc != MII_PHY_ANY || offloc != MII_OFFSET_ANY) {
256 if (phyloc != MII_PHY_ANY &&
257 phyloc != child->mii_phy)
258 continue;
259 if (offloc != MII_OFFSET_ANY &&
260 offloc != child->mii_offset)
261 continue;
262 }
263 LIST_REMOVE(child, mii_list);
264 mii_unlock(mii);
265 (void)config_detach(child->mii_dev, DETACH_FORCE);
266 mii_lock(mii);
267 }
268
269 if (exiting) {
270 cv_destroy(&mii->mii_probe_cv);
271 } else {
272 mii->mii_flags &= ~MIIF_PROBING;
273 cv_signal(&mii->mii_probe_cv);
274 }
275
276 mii_unlock(mii);
277 }
278
279 static int
mii_print(void * aux,const char * pnp)280 mii_print(void *aux, const char *pnp)
281 {
282 struct mii_attach_args *ma = aux;
283
284 if (pnp != NULL)
285 aprint_normal("OUI 0x%06x model 0x%04x rev %d at %s",
286 MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2),
287 MII_REV(ma->mii_id2), pnp);
288
289 aprint_normal(" phy %d", ma->mii_phyno);
290 return UNCONF;
291 }
292
293 static inline int
phy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)294 phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
295 {
296
297 if (!device_is_active(sc->mii_dev))
298 return ENXIO;
299 return PHY_SERVICE(sc, mii, cmd);
300 }
301
302 int
mii_ifmedia_change(struct mii_data * mii)303 mii_ifmedia_change(struct mii_data *mii)
304 {
305
306 KASSERT(mii_locked(mii) ||
307 ifmedia_islegacy(&mii->mii_media));
308 return ifmedia_change(&mii->mii_media, mii->mii_ifp);
309 }
310
311 /*
312 * Media changed; notify all PHYs.
313 */
314 int
mii_mediachg(struct mii_data * mii)315 mii_mediachg(struct mii_data *mii)
316 {
317 struct mii_softc *child;
318 int rv = 0;
319
320 IFMEDIA_LOCK_FOR_LEGACY(&mii->mii_media);
321 KASSERT(mii_locked(mii));
322
323 mii->mii_media_status = 0;
324 mii->mii_media_active = IFM_NONE;
325
326 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
327 rv = phy_service(child, mii, MII_MEDIACHG);
328 if (rv)
329 break;
330 }
331 IFMEDIA_UNLOCK_FOR_LEGACY(&mii->mii_media);
332 return rv;
333 }
334
335 /*
336 * Call the PHY tick routines, used during autonegotiation.
337 */
338 void
mii_tick(struct mii_data * mii)339 mii_tick(struct mii_data *mii)
340 {
341 struct mii_softc *child;
342
343 IFMEDIA_LOCK_FOR_LEGACY(&mii->mii_media);
344 KASSERT(mii_locked(mii));
345
346 LIST_FOREACH(child, &mii->mii_phys, mii_list)
347 (void)phy_service(child, mii, MII_TICK);
348
349 IFMEDIA_UNLOCK_FOR_LEGACY(&mii->mii_media);
350 }
351
352 /*
353 * Get media status from PHYs.
354 */
355 void
mii_pollstat(struct mii_data * mii)356 mii_pollstat(struct mii_data *mii)
357 {
358 struct mii_softc *child;
359
360 IFMEDIA_LOCK_FOR_LEGACY(&mii->mii_media);
361 KASSERT(mii_locked(mii));
362
363 mii->mii_media_status = 0;
364 mii->mii_media_active = IFM_NONE;
365
366 LIST_FOREACH(child, &mii->mii_phys, mii_list)
367 (void)phy_service(child, mii, MII_POLLSTAT);
368
369 IFMEDIA_UNLOCK_FOR_LEGACY(&mii->mii_media);
370 }
371
372 /*
373 * Inform the PHYs that the interface is down.
374 */
375 void
mii_down(struct mii_data * mii)376 mii_down(struct mii_data *mii)
377 {
378 struct mii_softc *child;
379
380 IFMEDIA_LOCK_FOR_LEGACY(&mii->mii_media);
381 KASSERT(mii_locked(mii));
382
383 LIST_FOREACH(child, &mii->mii_phys, mii_list)
384 (void)phy_service(child, mii, MII_DOWN);
385
386 IFMEDIA_UNLOCK_FOR_LEGACY(&mii->mii_media);
387 }
388
389 static unsigned char
bitreverse(unsigned char x)390 bitreverse(unsigned char x)
391 {
392 static const unsigned char nibbletab[16] = {
393 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
394 };
395
396 return ((nibbletab[x & 15] << 4) | nibbletab[x >> 4]);
397 }
398
399 u_int
mii_oui(uint16_t id1,uint16_t id2)400 mii_oui(uint16_t id1, uint16_t id2)
401 {
402 u_int h;
403
404 h = (id1 << 6) | (id2 >> 10);
405
406 return ((bitreverse(h >> 16) << 16) |
407 (bitreverse((h >> 8) & 255) << 8) |
408 bitreverse(h & 255));
409 }
410