mii.c revision 1.6 1 /* $NetBSD: mii.c,v 1.6 1998/08/10 23:55:16 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
73 LIST_INIT(&mii->mii_phys);
74
75 for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) {
76 /*
77 * Check to see if there is a PHY at this address. If
78 * the register contains garbage, assume no.
79 */
80 ma.mii_id1 = (*mii->mii_readreg)(parent, ma.mii_phyno,
81 MII_PHYIDR1);
82 ma.mii_id2 = (*mii->mii_readreg)(parent, ma.mii_phyno,
83 MII_PHYIDR2);
84 if (ma.mii_id1 == 0 || ma.mii_id1 == 0xffff ||
85 ma.mii_id2 == 0 || ma.mii_id2 == 0xffff)
86 continue;
87
88 ma.mii_data = mii;
89 ma.mii_capmask = capmask;
90
91 if ((child = (struct mii_softc *)config_found_sm(parent, &ma,
92 mii_print, mii_submatch)) != NULL) {
93 /*
94 * Link it up in the parent's MII data.
95 */
96 LIST_INSERT_HEAD(&mii->mii_phys, child, mii_list);
97 mii->mii_instance++;
98 }
99 }
100 }
101
102 int
103 mii_print(aux, pnp)
104 void *aux;
105 const char *pnp;
106 {
107 struct mii_attach_args *ma = aux;
108
109 if (pnp != NULL)
110 printf("PHY oui 0x%x model 0x%x rev 0x%x at %s",
111 MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2),
112 MII_REV(ma->mii_id2), pnp);
113
114 printf(" phy %d", ma->mii_phyno);
115 return (UNCONF);
116 }
117
118 int
119 mii_submatch(parent, cf, aux)
120 struct device *parent;
121 struct cfdata *cf;
122 void *aux;
123 {
124 struct mii_attach_args *ma = aux;
125
126 if (ma->mii_phyno != cf->cf_loc[MIIBUSCF_PHY] &&
127 cf->cf_loc[MIIBUSCF_PHY] != MIIBUSCF_PHY_DEFAULT)
128 return (0);
129
130 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
131 }
132
133 /*
134 * Given an ifmedia word, return the corresponding ANAR value.
135 */
136 int
137 mii_anar(media)
138 int media;
139 {
140 int rv;
141
142 switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) {
143 case IFM_ETHER|IFM_10_T:
144 rv = ANAR_10|ANAR_CSMA;
145 break;
146 case IFM_ETHER|IFM_10_T|IFM_FDX:
147 rv = ANAR_10_FD|ANAR_CSMA;
148 break;
149 case IFM_ETHER|IFM_100_TX:
150 rv = ANAR_TX|ANAR_CSMA;
151 break;
152 case IFM_ETHER|IFM_100_TX|IFM_FDX:
153 rv = ANAR_TX_FD|ANAR_CSMA;
154 break;
155 case IFM_ETHER|IFM_100_T4:
156 rv = ANAR_T4|ANAR_CSMA;
157 break;
158 default:
159 rv = 0;
160 break;
161 }
162
163 return (rv);
164 }
165
166 /*
167 * Media changed; notify all PHYs.
168 */
169 int
170 mii_mediachg(mii)
171 struct mii_data *mii;
172 {
173 struct mii_softc *child;
174 int rv;
175
176 mii->mii_media_status = 0;
177 mii->mii_media_active = IFM_NONE;
178
179 for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
180 child = LIST_NEXT(child, mii_list)) {
181 rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
182 if (rv)
183 return (rv);
184 }
185 return (0);
186 }
187
188 /*
189 * Call the PHY tick routines, used during autonegotiation.
190 */
191 void
192 mii_tick(mii)
193 struct mii_data *mii;
194 {
195 struct mii_softc *child;
196
197 for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
198 child = LIST_NEXT(child, mii_list))
199 (void) (*child->mii_service)(child, mii, MII_TICK);
200 }
201
202 /*
203 * Get media status from PHYs.
204 */
205 void
206 mii_pollstat(mii)
207 struct mii_data *mii;
208 {
209 struct mii_softc *child;
210
211 mii->mii_media_status = 0;
212 mii->mii_media_active = IFM_NONE;
213
214 for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
215 child = LIST_NEXT(child, mii_list))
216 (void) (*child->mii_service)(child, mii, MII_POLLSTAT);
217 }
218
219 /*
220 * Initialize generic PHY media based on BMSR, called when a PHY is
221 * attached. We expect to be set up to print a comma-separated list
222 * of media names. Does not print a newline.
223 */
224 void
225 mii_add_media(mii, bmsr, instance)
226 struct mii_data *mii;
227 int bmsr, instance;
228 {
229 const char *sep = "";
230
231 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
232 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
233
234 if (bmsr & BMSR_10THDX) {
235 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, instance), 0);
236 PRINT("10baseT");
237 }
238 if (bmsr & BMSR_10TFDX) {
239 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, instance),
240 BMCR_FDX);
241 PRINT("10baseT-FDX");
242 }
243 if (bmsr & BMSR_100TXHDX) {
244 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, instance),
245 BMCR_S100);
246 PRINT("100baseTX");
247 }
248 if (bmsr & BMSR_100TXFDX) {
249 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, instance),
250 BMCR_S100|BMCR_FDX);
251 PRINT("100baseTX-FDX");
252 }
253 if (bmsr & BMSR_100T4) {
254 /*
255 * XXX How do you enable 100baseT4? I assume we set
256 * XXX BMCR_S100 and then assume the PHYs will take
257 * XXX watever action is necessary to switch themselves
258 * XXX into T4 mode.
259 */
260 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, instance),
261 BMCR_S100);
262 PRINT("100baseT4");
263 }
264 if (bmsr & BMSR_ANEG) {
265 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, instance),
266 BMCR_AUTOEN);
267 PRINT("auto");
268 }
269 #undef ADD
270 #undef PRINT
271 }
272