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