mii.c revision 1.11 1 /* $NetBSD: mii.c,v 1.11 1999/02/05 02:46:34 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998 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. This exports an interface compatible with BSD/OS 3.0's,
43 * plus some NetBSD extensions.
44 */
45
46 #include <sys/param.h>
47 #include <sys/device.h>
48 #include <sys/systm.h>
49 #include <sys/socket.h>
50
51 #include <net/if.h>
52 #include <net/if_media.h>
53
54 #include <dev/mii/mii.h>
55 #include <dev/mii/miivar.h>
56
57 int mii_print __P((void *, const char *));
58 int mii_submatch __P((struct device *, struct cfdata *, void *));
59
60 /*
61 * Helper function used by network interface drivers, attaches PHYs
62 * to the network interface driver parent.
63 */
64 void
65 mii_phy_probe(parent, mii, capmask)
66 struct device *parent;
67 struct mii_data *mii;
68 int capmask;
69 {
70 struct mii_attach_args ma;
71 struct mii_softc *child;
72 int bmsr;
73
74 LIST_INIT(&mii->mii_phys);
75
76 for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) {
77 /*
78 * Check to see if there is a PHY at this address. Note,
79 * many braindead PHYs report 0/0 in their ID registers,
80 * so we test for media in the BMSR.
81 */
82 bmsr = (*mii->mii_readreg)(parent, ma.mii_phyno, MII_BMSR);
83 if (bmsr == 0 || bmsr == 0xffff ||
84 (bmsr & BMSR_MEDIAMASK) == 0) {
85 /* Assume no PHY at this address. */
86 continue;
87 }
88
89 /*
90 * Extract the IDs. Braindead PHYs will be handled by
91 * the `ukphy' driver, as we have no ID information to
92 * match on.
93 */
94 ma.mii_id1 = (*mii->mii_readreg)(parent, ma.mii_phyno,
95 MII_PHYIDR1);
96 ma.mii_id2 = (*mii->mii_readreg)(parent, ma.mii_phyno,
97 MII_PHYIDR2);
98
99 ma.mii_data = mii;
100 ma.mii_capmask = capmask;
101
102 if ((child = (struct mii_softc *)config_found_sm(parent, &ma,
103 mii_print, mii_submatch)) != NULL) {
104 /*
105 * Link it up in the parent's MII data.
106 */
107 LIST_INSERT_HEAD(&mii->mii_phys, child, mii_list);
108 mii->mii_instance++;
109 }
110 }
111 }
112
113 int
114 mii_print(aux, pnp)
115 void *aux;
116 const char *pnp;
117 {
118 struct mii_attach_args *ma = aux;
119
120 if (pnp != NULL)
121 printf("OUI 0x%06x model 0x%04x rev %d at %s",
122 MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2),
123 MII_REV(ma->mii_id2), pnp);
124
125 printf(" phy %d", ma->mii_phyno);
126 return (UNCONF);
127 }
128
129 int
130 mii_submatch(parent, cf, aux)
131 struct device *parent;
132 struct cfdata *cf;
133 void *aux;
134 {
135 struct mii_attach_args *ma = aux;
136
137 if (ma->mii_phyno != cf->cf_loc[MIICF_PHY] &&
138 cf->cf_loc[MIICF_PHY] != MIICF_PHY_DEFAULT)
139 return (0);
140
141 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
142 }
143
144 /*
145 * Given an ifmedia word, return the corresponding ANAR value.
146 */
147 int
148 mii_anar(media)
149 int media;
150 {
151 int rv;
152
153 switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) {
154 case IFM_ETHER|IFM_10_T:
155 rv = ANAR_10|ANAR_CSMA;
156 break;
157 case IFM_ETHER|IFM_10_T|IFM_FDX:
158 rv = ANAR_10_FD|ANAR_CSMA;
159 break;
160 case IFM_ETHER|IFM_100_TX:
161 rv = ANAR_TX|ANAR_CSMA;
162 break;
163 case IFM_ETHER|IFM_100_TX|IFM_FDX:
164 rv = ANAR_TX_FD|ANAR_CSMA;
165 break;
166 case IFM_ETHER|IFM_100_T4:
167 rv = ANAR_T4|ANAR_CSMA;
168 break;
169 default:
170 rv = 0;
171 break;
172 }
173
174 return (rv);
175 }
176
177 /*
178 * Given a BMCR value, return the corresponding ifmedia word.
179 */
180 int
181 mii_media_from_bmcr(bmcr)
182 int bmcr;
183 {
184 int rv = IFM_ETHER;
185
186 if (bmcr & BMCR_S100)
187 rv |= IFM_100_TX;
188 else
189 rv |= IFM_10_T;
190 if (bmcr & BMCR_FDX)
191 rv |= IFM_FDX;
192
193 return (rv);
194 }
195
196 /*
197 * Media changed; notify all PHYs.
198 */
199 int
200 mii_mediachg(mii)
201 struct mii_data *mii;
202 {
203 struct mii_softc *child;
204 int rv;
205
206 mii->mii_media_status = 0;
207 mii->mii_media_active = IFM_NONE;
208
209 for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
210 child = LIST_NEXT(child, mii_list)) {
211 rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
212 if (rv)
213 return (rv);
214 }
215 return (0);
216 }
217
218 /*
219 * Call the PHY tick routines, used during autonegotiation.
220 */
221 void
222 mii_tick(mii)
223 struct mii_data *mii;
224 {
225 struct mii_softc *child;
226
227 for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
228 child = LIST_NEXT(child, mii_list))
229 (void) (*child->mii_service)(child, mii, MII_TICK);
230 }
231
232 /*
233 * Get media status from PHYs.
234 */
235 void
236 mii_pollstat(mii)
237 struct mii_data *mii;
238 {
239 struct mii_softc *child;
240
241 mii->mii_media_status = 0;
242 mii->mii_media_active = IFM_NONE;
243
244 for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
245 child = LIST_NEXT(child, mii_list))
246 (void) (*child->mii_service)(child, mii, MII_POLLSTAT);
247 }
248
249 /*
250 * Initialize generic PHY media based on BMSR, called when a PHY is
251 * attached. We expect to be set up to print a comma-separated list
252 * of media names. Does not print a newline.
253 */
254 void
255 mii_add_media(mii, bmsr, instance)
256 struct mii_data *mii;
257 int bmsr, instance;
258 {
259 const char *sep = "";
260
261 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
262 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
263
264 if (bmsr & BMSR_10THDX) {
265 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, instance), 0);
266 PRINT("10baseT");
267 }
268 if (bmsr & BMSR_10TFDX) {
269 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, instance),
270 BMCR_FDX);
271 PRINT("10baseT-FDX");
272 }
273 if (bmsr & BMSR_100TXHDX) {
274 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, instance),
275 BMCR_S100);
276 PRINT("100baseTX");
277 }
278 if (bmsr & BMSR_100TXFDX) {
279 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, instance),
280 BMCR_S100|BMCR_FDX);
281 PRINT("100baseTX-FDX");
282 }
283 if (bmsr & BMSR_100T4) {
284 /*
285 * XXX How do you enable 100baseT4? I assume we set
286 * XXX BMCR_S100 and then assume the PHYs will take
287 * XXX watever action is necessary to switch themselves
288 * XXX into T4 mode.
289 */
290 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, instance),
291 BMCR_S100);
292 PRINT("100baseT4");
293 }
294 if (bmsr & BMSR_ANEG) {
295 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, instance),
296 BMCR_AUTOEN);
297 PRINT("auto");
298 }
299 #undef ADD
300 #undef PRINT
301 }
302