xref: /netbsd-src/sys/dev/mii/mii.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: mii.c,v 1.56 2021/04/24 23:36:56 thorpej 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.56 2021/04/24 23:36:56 thorpej 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
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 				 CFARG_SUBMATCH, config_stdsubmatch,
178 				 CFARG_IATTR, "mii",
179 				 CFARG_LOCATORS, locs,
180 				 CFARG_EOL));
181 		if (child) {
182 			/* Link it up in the parent's MII data. */
183 			callout_init(&child->mii_nway_ch, 0);
184 			mii_lock(mii);
185 			LIST_INSERT_HEAD(&mii->mii_phys, child, mii_list);
186 			child->mii_offset = offset;
187 			mii->mii_instance++;
188 		} else {
189 			mii_lock(mii);
190 		}
191 		offset++;
192 	}
193 
194 	/* All done! */
195 	mii->mii_flags &= ~MIIF_PROBING;
196 	cv_signal(&mii->mii_probe_cv);
197 	mii_unlock(mii);
198 }
199 
200 void
201 mii_detach(struct mii_data *mii, int phyloc, int offloc)
202 {
203 	struct mii_softc *child, *nchild;
204 	bool exiting = false;
205 
206 	if (phyloc != MII_PHY_ANY && offloc != MII_PHY_ANY)
207 		panic("mii_detach: phyloc and offloc specified");
208 
209 	mii_lock(mii);
210 
211 	if ((mii->mii_flags & MIIF_INITDONE) == 0 ||
212 	    (mii->mii_flags & MIIF_EXITING) != 0) {
213 		mii_unlock(mii);
214 		return;
215 	}
216 
217 	/*
218 	 * XXX This is probably not the best hueristic.  Consider
219 	 * XXX adding an argument to mii_detach().
220 	 */
221 	if (phyloc == MII_PHY_ANY && MII_PHY_ANY == MII_OFFSET_ANY)
222 		exiting = true;
223 
224 	if (exiting) {
225 		mii->mii_flags |= MIIF_EXITING;
226 		cv_broadcast(&mii->mii_probe_cv);
227 	}
228 
229 	/* Wait for everyone else to get out. */
230 	mii->mii_probe_waiters++;
231 	for (;;) {
232 		/*
233 		 * If we've been waiting to do a less-than-exit, and
234 		 * *someone else* initiated an exit, then get out.
235 		 */
236 		if (!exiting && (mii->mii_flags & MIIF_EXITING) != 0) {
237 			mii->mii_probe_waiters--;
238 			cv_signal(&mii->mii_probe_cv);
239 			mii_unlock(mii);
240 			return;
241 		}
242 		if ((mii->mii_flags & MIIF_PROBING) == 0 &&
243 		    (!exiting || (exiting && mii->mii_probe_waiters == 1)))
244 			break;
245 		cv_wait(&mii->mii_probe_cv, mii->mii_media.ifm_lock);
246 	}
247 	mii->mii_probe_waiters--;
248 
249 	if (!exiting)
250 		mii->mii_flags |= MIIF_PROBING;
251 
252 	LIST_FOREACH_SAFE(child, &mii->mii_phys, mii_list, nchild) {
253 		if (phyloc != MII_PHY_ANY || offloc != MII_OFFSET_ANY) {
254 			if (phyloc != MII_PHY_ANY &&
255 			    phyloc != child->mii_phy)
256 				continue;
257 			if (offloc != MII_OFFSET_ANY &&
258 			    offloc != child->mii_offset)
259 				continue;
260 		}
261 		LIST_REMOVE(child, mii_list);
262 		mii_unlock(mii);
263 		(void)config_detach(child->mii_dev, DETACH_FORCE);
264 		mii_lock(mii);
265 	}
266 
267 	if (exiting) {
268 		cv_destroy(&mii->mii_probe_cv);
269 	} else {
270 		mii->mii_flags &= ~MIIF_PROBING;
271 		cv_signal(&mii->mii_probe_cv);
272 	}
273 
274 	mii_unlock(mii);
275 }
276 
277 static int
278 mii_print(void *aux, const char *pnp)
279 {
280 	struct mii_attach_args *ma = aux;
281 
282 	if (pnp != NULL)
283 		aprint_normal("OUI 0x%06x model 0x%04x rev %d at %s",
284 		    MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2),
285 		    MII_REV(ma->mii_id2), pnp);
286 
287 	aprint_normal(" phy %d", ma->mii_phyno);
288 	return UNCONF;
289 }
290 
291 static inline int
292 phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
293 {
294 
295 	if (!device_is_active(sc->mii_dev))
296 		return ENXIO;
297 	return PHY_SERVICE(sc, mii, cmd);
298 }
299 
300 int
301 mii_ifmedia_change(struct mii_data *mii)
302 {
303 
304 	KASSERT(mii_locked(mii) ||
305 		ifmedia_islegacy(&mii->mii_media));
306 	return ifmedia_change(&mii->mii_media, mii->mii_ifp);
307 }
308 
309 /*
310  * Media changed; notify all PHYs.
311  */
312 int
313 mii_mediachg(struct mii_data *mii)
314 {
315 	struct mii_softc *child;
316 	int rv = 0;
317 
318 	IFMEDIA_LOCK_FOR_LEGACY(&mii->mii_media);
319 	KASSERT(mii_locked(mii));
320 
321 	mii->mii_media_status = 0;
322 	mii->mii_media_active = IFM_NONE;
323 
324 	LIST_FOREACH(child, &mii->mii_phys, mii_list) {
325 		rv = phy_service(child, mii, MII_MEDIACHG);
326 		if (rv)
327 			break;
328 	}
329 	IFMEDIA_UNLOCK_FOR_LEGACY(&mii->mii_media);
330 	return rv;
331 }
332 
333 /*
334  * Call the PHY tick routines, used during autonegotiation.
335  */
336 void
337 mii_tick(struct mii_data *mii)
338 {
339 	struct mii_softc *child;
340 
341 	IFMEDIA_LOCK_FOR_LEGACY(&mii->mii_media);
342 	KASSERT(mii_locked(mii));
343 
344 	LIST_FOREACH(child, &mii->mii_phys, mii_list)
345 		(void)phy_service(child, mii, MII_TICK);
346 
347 	IFMEDIA_UNLOCK_FOR_LEGACY(&mii->mii_media);
348 }
349 
350 /*
351  * Get media status from PHYs.
352  */
353 void
354 mii_pollstat(struct mii_data *mii)
355 {
356 	struct mii_softc *child;
357 
358 	IFMEDIA_LOCK_FOR_LEGACY(&mii->mii_media);
359 	KASSERT(mii_locked(mii));
360 
361 	mii->mii_media_status = 0;
362 	mii->mii_media_active = IFM_NONE;
363 
364 	LIST_FOREACH(child, &mii->mii_phys, mii_list)
365 		(void)phy_service(child, mii, MII_POLLSTAT);
366 
367 	IFMEDIA_UNLOCK_FOR_LEGACY(&mii->mii_media);
368 }
369 
370 /*
371  * Inform the PHYs that the interface is down.
372  */
373 void
374 mii_down(struct mii_data *mii)
375 {
376 	struct mii_softc *child;
377 
378 	IFMEDIA_LOCK_FOR_LEGACY(&mii->mii_media);
379 	KASSERT(mii_locked(mii));
380 
381 	LIST_FOREACH(child, &mii->mii_phys, mii_list)
382 		(void)phy_service(child, mii, MII_DOWN);
383 
384 	IFMEDIA_UNLOCK_FOR_LEGACY(&mii->mii_media);
385 }
386 
387 static unsigned char
388 bitreverse(unsigned char x)
389 {
390 	static const unsigned char nibbletab[16] = {
391 		0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
392 	};
393 
394 	return ((nibbletab[x & 15] << 4) | nibbletab[x >> 4]);
395 }
396 
397 u_int
398 mii_oui(uint16_t id1, uint16_t id2)
399 {
400 	u_int h;
401 
402 	h = (id1 << 6) | (id2 >> 10);
403 
404 	return ((bitreverse(h >> 16) << 16) |
405 		(bitreverse((h >> 8) & 255) << 8) |
406 		bitreverse(h & 255));
407 }
408