mii.c revision 1.52 1 /* $NetBSD: mii.c,v 1.52 2019/01/22 03:42:27 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.52 2019/01/22 03:42:27 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 /*
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_detach(struct mii_data *mii, int phyloc, int offloc)
162 {
163 struct mii_softc *child, *nchild;
164
165 if (phyloc != MII_PHY_ANY && offloc != MII_PHY_ANY)
166 panic("mii_detach: phyloc and offloc specified");
167
168 if ((mii->mii_flags & MIIF_INITDONE) == 0)
169 return;
170
171 for (child = LIST_FIRST(&mii->mii_phys);
172 child != NULL; child = nchild) {
173 nchild = LIST_NEXT(child, mii_list);
174 if (phyloc != MII_PHY_ANY || offloc != MII_OFFSET_ANY) {
175 if (phyloc != MII_PHY_ANY &&
176 phyloc != child->mii_phy)
177 continue;
178 if (offloc != MII_OFFSET_ANY &&
179 offloc != child->mii_offset)
180 continue;
181 }
182 (void)config_detach(child->mii_dev, DETACH_FORCE);
183 }
184 }
185
186 static int
187 mii_print(void *aux, const char *pnp)
188 {
189 struct mii_attach_args *ma = aux;
190
191 if (pnp != NULL)
192 aprint_normal("OUI 0x%06x model 0x%04x rev %d at %s",
193 MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2),
194 MII_REV(ma->mii_id2), pnp);
195
196 aprint_normal(" phy %d", ma->mii_phyno);
197 return (UNCONF);
198 }
199
200 static inline int
201 phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
202 {
203 if (!device_is_active(sc->mii_dev))
204 return ENXIO;
205 return PHY_SERVICE(sc, mii, cmd);
206 }
207
208 int
209 mii_ifmedia_change(struct mii_data *mii)
210 {
211 return ifmedia_change(&mii->mii_media, mii->mii_ifp);
212 }
213
214 /*
215 * Media changed; notify all PHYs.
216 */
217 int
218 mii_mediachg(struct mii_data *mii)
219 {
220 struct mii_softc *child;
221 int rv;
222
223 mii->mii_media_status = 0;
224 mii->mii_media_active = IFM_NONE;
225
226 LIST_FOREACH(child, &mii->mii_phys, mii_list) {
227 rv = phy_service(child, mii, MII_MEDIACHG);
228 if (rv)
229 return (rv);
230 }
231 return (0);
232 }
233
234 /*
235 * Call the PHY tick routines, used during autonegotiation.
236 */
237 void
238 mii_tick(struct mii_data *mii)
239 {
240 struct mii_softc *child;
241
242 LIST_FOREACH(child, &mii->mii_phys, mii_list)
243 (void)phy_service(child, mii, MII_TICK);
244 }
245
246 /*
247 * Get media status from PHYs.
248 */
249 void
250 mii_pollstat(struct mii_data *mii)
251 {
252 struct mii_softc *child;
253
254 mii->mii_media_status = 0;
255 mii->mii_media_active = IFM_NONE;
256
257 LIST_FOREACH(child, &mii->mii_phys, mii_list)
258 (void)phy_service(child, mii, MII_POLLSTAT);
259 }
260
261 /*
262 * Inform the PHYs that the interface is down.
263 */
264 void
265 mii_down(struct mii_data *mii)
266 {
267 struct mii_softc *child;
268
269 LIST_FOREACH(child, &mii->mii_phys, mii_list)
270 (void)phy_service(child, mii, MII_DOWN);
271 }
272
273 static unsigned char
274 bitreverse(unsigned char x)
275 {
276 static const unsigned char nibbletab[16] = {
277 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
278 };
279
280 return ((nibbletab[x & 15] << 4) | nibbletab[x >> 4]);
281 }
282
283 u_int
284 mii_oui(u_int id1, u_int id2)
285 {
286 u_int h;
287
288 h = (id1 << 6) | (id2 >> 10);
289
290 return ((bitreverse(h >> 16) << 16) |
291 (bitreverse((h >> 8) & 255) << 8) |
292 bitreverse(h & 255));
293 }
294