if_ep_isa.c revision 1.23 1 /* $NetBSD: if_ep_isa.c,v 1.23 1998/08/12 18:51:54 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997 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 * Copyright (c) 1997 Jonathan Stone <jonathan (at) NetBSD.org>
42 * Copyright (c) 1994 Herb Peyerl <hpeyerl (at) beer.org>
43 * All rights reserved.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed by Herb Peyerl.
56 * 4. The name of Herb Peyerl may not be used to endorse or promote products
57 * derived from this software without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
60 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
61 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
63 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
64 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
65 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
66 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
68 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69 */
70
71 #include "opt_inet.h"
72 #include "opt_ns.h"
73 #include "bpfilter.h"
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/mbuf.h>
78 #include <sys/socket.h>
79 #include <sys/ioctl.h>
80 #include <sys/errno.h>
81 #include <sys/syslog.h>
82 #include <sys/select.h>
83 #include <sys/device.h>
84 #include <sys/queue.h>
85
86 #include <net/if.h>
87 #include <net/if_dl.h>
88 #include <net/if_ether.h>
89 #include <net/if_media.h>
90
91 #ifdef INET
92 #include <netinet/in.h>
93 #include <netinet/in_systm.h>
94 #include <netinet/in_var.h>
95 #include <netinet/ip.h>
96 #include <netinet/if_inarp.h>
97 #endif
98
99 #ifdef NS
100 #include <netns/ns.h>
101 #include <netns/ns_if.h>
102 #endif
103
104 #if NBPFILTER > 0
105 #include <net/bpf.h>
106 #include <net/bpfdesc.h>
107 #endif
108
109 #include <machine/cpu.h>
110 #include <machine/bus.h>
111 #include <machine/intr.h>
112
113 #include <dev/mii/miivar.h>
114
115 #include <dev/ic/elink3var.h>
116 #include <dev/ic/elink3reg.h>
117
118 #include <dev/isa/isavar.h>
119 #include <dev/isa/elink.h>
120
121 int ep_isa_probe __P((struct device *, struct cfdata *, void *));
122 void ep_isa_attach __P((struct device *, struct device *, void *));
123
124 struct cfattach ep_isa_ca = {
125 sizeof(struct ep_softc), ep_isa_probe, ep_isa_attach
126 };
127
128 static void epaddcard __P((int, int, int, int));
129
130 /*
131 * This keeps track of which ISAs have been through an ep probe sequence.
132 * A simple static variable isn't enough, since it's conceivable that
133 * a system might have more than one ISA bus.
134 *
135 * The "er_bus" member is the unit number of the parent ISA bus, e.g. "0"
136 * for "isa0".
137 */
138 struct ep_isa_done_probe {
139 LIST_ENTRY(ep_isa_done_probe) er_link;
140 int er_bus;
141 };
142 static LIST_HEAD(, ep_isa_done_probe) ep_isa_all_probes;
143 static int ep_isa_probes_initialized;
144
145 #define MAXEPCARDS 20 /* if you have more than 20, you lose */
146
147 static struct epcard {
148 int bus;
149 int iobase;
150 int irq;
151 char available;
152 long model;
153 } epcards[MAXEPCARDS];
154 static int nepcards;
155
156 static void
157 epaddcard(bus, iobase, irq, model)
158 int bus, iobase, irq, model;
159 {
160
161 if (nepcards >= MAXEPCARDS)
162 return;
163 epcards[nepcards].bus = bus;
164 epcards[nepcards].iobase = iobase;
165 epcards[nepcards].irq = (irq == 2) ? 9 : irq;
166 epcards[nepcards].model = model;
167 epcards[nepcards].available = 1;
168 nepcards++;
169 }
170
171 /*
172 * 3c509 cards on the ISA bus are probed in ethernet address order.
173 * The probe sequence requires careful orchestration, and we'd like
174 * like to allow the irq and base address to be wildcarded. So, we
175 * probe all the cards the first time epprobe() is called. On subsequent
176 * calls we look for matching cards.
177 */
178 int
179 ep_isa_probe(parent, match, aux)
180 struct device *parent;
181 struct cfdata *match;
182 void *aux;
183 {
184 struct isa_attach_args *ia = aux;
185 bus_space_tag_t iot = ia->ia_iot;
186 bus_space_handle_t ioh, ioh2;
187 int slot, iobase, irq, i;
188 u_int16_t vendor, model;
189 struct ep_isa_done_probe *er;
190 int bus = parent->dv_unit;
191
192 if (ep_isa_probes_initialized == 0) {
193 LIST_INIT(&ep_isa_all_probes);
194 ep_isa_probes_initialized = 1;
195 }
196
197 /*
198 * Probe this bus if we haven't done so already.
199 */
200 for (er = ep_isa_all_probes.lh_first; er != NULL;
201 er = er->er_link.le_next)
202 if (er->er_bus == parent->dv_unit)
203 goto bus_probed;
204
205 /*
206 * Mark this bus so we don't probe it again.
207 */
208 er = (struct ep_isa_done_probe *)
209 malloc(sizeof(struct ep_isa_done_probe), M_DEVBUF, M_NOWAIT);
210 if (er == NULL)
211 panic("ep_isa_probe: can't allocate state storage");
212
213 er->er_bus = bus;
214 LIST_INSERT_HEAD(&ep_isa_all_probes, er, er_link);
215
216 /*
217 * Map the Etherlink ID port for the probe sequence.
218 */
219 if (bus_space_map(iot, ELINK_ID_PORT, 1, 0, &ioh)) {
220 printf("ep_isa_probe: can't map Etherlink ID port\n");
221 return 0;
222 }
223
224 for (slot = 0; slot < MAXEPCARDS; slot++) {
225 elink_reset(iot, ioh, parent->dv_unit);
226 elink_idseq(iot, ioh, ELINK_509_POLY);
227
228 /* Untag all the adapters so they will talk to us. */
229 if (slot == 0)
230 bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 0);
231
232 vendor = htons(epreadeeprom(iot, ioh, EEPROM_MFG_ID));
233 if (vendor != MFG_ID)
234 continue;
235
236 model = htons(epreadeeprom(iot, ioh, EEPROM_PROD_ID));
237 /*
238 * XXX: Add a new product id to check for other cards
239 * (3c515?) and fix the check in ep_isa_attach.
240 */
241 if ((model & 0xfff0) != PROD_ID_3C509) {
242 #ifndef trusted
243 printf(
244 "ep_isa_probe: ignoring model %04x\n", model);
245 #endif
246 continue;
247 }
248
249 iobase = epreadeeprom(iot, ioh, EEPROM_ADDR_CFG);
250 iobase = (iobase & 0x1f) * 0x10 + 0x200;
251
252 irq = epreadeeprom(iot, ioh, EEPROM_RESOURCE_CFG);
253 irq >>= 12;
254
255 /* so card will not respond to contention again */
256 bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 1);
257
258 /*
259 * XXX: this should probably not be done here
260 * because it enables the drq/irq lines from
261 * the board. Perhaps it should be done after
262 * we have checked for irq/drq collisions?
263 */
264 bus_space_write_1(iot, ioh, 0, ACTIVATE_ADAPTER_TO_CONFIG);
265
266 /*
267 * Don't attach a 3c509 in PnP mode.
268 */
269 if ((model & 0xfff0) == PROD_ID_3C509) {
270 if (bus_space_map(iot, iobase, 1, 0, &ioh2)) {
271 printf(
272 "ep_isa_probe: can't map Etherlink iobase\n");
273 return 0;
274 }
275 if (bus_space_read_2(iot, ioh2, EP_W0_EEPROM_COMMAND)
276 & EEPROM_TST_MODE) {
277 printf(
278 "3COM 3C509 Ethernet card in PnP mode\n");
279 continue;
280 }
281 bus_space_unmap(iot, ioh2, 1);
282 }
283 epaddcard(bus, iobase, irq, model);
284 }
285 /* XXX should we sort by ethernet address? */
286
287 bus_space_unmap(iot, ioh, 1);
288
289 bus_probed:
290
291 for (i = 0; i < nepcards; i++) {
292 if (epcards[i].bus != bus)
293 continue;
294 if (epcards[i].available == 0)
295 continue;
296 if (ia->ia_iobase != IOBASEUNK &&
297 ia->ia_iobase != epcards[i].iobase)
298 continue;
299 if (ia->ia_irq != IRQUNK &&
300 ia->ia_irq != epcards[i].irq)
301 continue;
302 goto good;
303 }
304 return 0;
305
306 good:
307 epcards[i].available = 0;
308 ia->ia_iobase = epcards[i].iobase;
309 ia->ia_irq = epcards[i].irq;
310 ia->ia_iosize = 0x10;
311 ia->ia_msize = 0;
312 ia->ia_aux = (void *)epcards[i].model;
313 return 1;
314 }
315
316 void
317 ep_isa_attach(parent, self, aux)
318 struct device *parent, *self;
319 void *aux;
320 {
321 struct ep_softc *sc = (void *)self;
322 struct isa_attach_args *ia = aux;
323 bus_space_tag_t iot = ia->ia_iot;
324 bus_space_handle_t ioh;
325 int chipset;
326
327 /* Map i/o space. */
328 if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh)) {
329 printf(": can't map i/o space\n");
330 return;
331 }
332
333 sc->sc_iot = iot;
334 sc->sc_ioh = ioh;
335 sc->bustype = EP_BUS_ISA;
336
337 sc->enable = NULL;
338 sc->disable = NULL;
339 sc->enabled = 1;
340
341 chipset = (int)(long)ia->ia_aux;
342 if ((chipset & 0xfff0) == PROD_ID_3C509) {
343 printf(": 3Com 3C509 Ethernet\n");
344 epconfig(sc, EP_CHIPSET_3C509, NULL);
345 } else {
346 /*
347 * XXX: Maybe a 3c515, but the check in ep_isa_probe looks
348 * at the moment only for a 3c509.
349 */
350 printf(": unknown 3Com Ethernet card: 0x%04x\n", chipset);
351 return;
352 }
353
354 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
355 IPL_NET, epintr, sc);
356 }
357