if_ep_isa.c revision 1.21 1 /* $NetBSD: if_ep_isa.c,v 1.21 1998/07/05 00:51:21 jonathan 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 "bpfilter.h"
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/mbuf.h>
77 #include <sys/socket.h>
78 #include <sys/ioctl.h>
79 #include <sys/errno.h>
80 #include <sys/syslog.h>
81 #include <sys/select.h>
82 #include <sys/device.h>
83 #include <sys/queue.h>
84
85 #include <net/if.h>
86 #include <net/if_dl.h>
87 #include <net/if_ether.h>
88 #include <net/if_media.h>
89
90 #ifdef INET
91 #include <netinet/in.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/in_var.h>
94 #include <netinet/ip.h>
95 #include <netinet/if_inarp.h>
96 #endif
97
98 #ifdef NS
99 #include <netns/ns.h>
100 #include <netns/ns_if.h>
101 #endif
102
103 #if NBPFILTER > 0
104 #include <net/bpf.h>
105 #include <net/bpfdesc.h>
106 #endif
107
108 #include <machine/cpu.h>
109 #include <machine/bus.h>
110 #include <machine/intr.h>
111
112 #include <dev/ic/elink3var.h>
113 #include <dev/ic/elink3reg.h>
114
115 #include <dev/isa/isavar.h>
116 #include <dev/isa/elink.h>
117
118 int ep_isa_probe __P((struct device *, struct cfdata *, void *));
119 void ep_isa_attach __P((struct device *, struct device *, void *));
120
121 struct cfattach ep_isa_ca = {
122 sizeof(struct ep_softc), ep_isa_probe, ep_isa_attach
123 };
124
125 static void epaddcard __P((int, int, int, int));
126
127 /*
128 * This keeps track of which ISAs have been through an ep probe sequence.
129 * A simple static variable isn't enough, since it's conceivable that
130 * a system might have more than one ISA bus.
131 *
132 * The "er_bus" member is the unit number of the parent ISA bus, e.g. "0"
133 * for "isa0".
134 */
135 struct ep_isa_done_probe {
136 LIST_ENTRY(ep_isa_done_probe) er_link;
137 int er_bus;
138 };
139 static LIST_HEAD(, ep_isa_done_probe) ep_isa_all_probes;
140 static int ep_isa_probes_initialized;
141
142 #define MAXEPCARDS 20 /* if you have more than 20, you lose */
143
144 static struct epcard {
145 int bus;
146 int iobase;
147 int irq;
148 char available;
149 long model;
150 } epcards[MAXEPCARDS];
151 static int nepcards;
152
153 static void
154 epaddcard(bus, iobase, irq, model)
155 int bus, iobase, irq, model;
156 {
157
158 if (nepcards >= MAXEPCARDS)
159 return;
160 epcards[nepcards].bus = bus;
161 epcards[nepcards].iobase = iobase;
162 epcards[nepcards].irq = (irq == 2) ? 9 : irq;
163 epcards[nepcards].model = model;
164 epcards[nepcards].available = 1;
165 nepcards++;
166 }
167
168 /*
169 * 3c509 cards on the ISA bus are probed in ethernet address order.
170 * The probe sequence requires careful orchestration, and we'd like
171 * like to allow the irq and base address to be wildcarded. So, we
172 * probe all the cards the first time epprobe() is called. On subsequent
173 * calls we look for matching cards.
174 */
175 int
176 ep_isa_probe(parent, match, aux)
177 struct device *parent;
178 struct cfdata *match;
179 void *aux;
180 {
181 struct isa_attach_args *ia = aux;
182 bus_space_tag_t iot = ia->ia_iot;
183 bus_space_handle_t ioh, ioh2;
184 int slot, iobase, irq, i;
185 u_int16_t vendor, model;
186 struct ep_isa_done_probe *er;
187 int bus = parent->dv_unit;
188
189 if (ep_isa_probes_initialized == 0) {
190 LIST_INIT(&ep_isa_all_probes);
191 ep_isa_probes_initialized = 1;
192 }
193
194 /*
195 * Probe this bus if we haven't done so already.
196 */
197 for (er = ep_isa_all_probes.lh_first; er != NULL;
198 er = er->er_link.le_next)
199 if (er->er_bus == parent->dv_unit)
200 goto bus_probed;
201
202 /*
203 * Mark this bus so we don't probe it again.
204 */
205 er = (struct ep_isa_done_probe *)
206 malloc(sizeof(struct ep_isa_done_probe), M_DEVBUF, M_NOWAIT);
207 if (er == NULL)
208 panic("ep_isa_probe: can't allocate state storage");
209
210 er->er_bus = bus;
211 LIST_INSERT_HEAD(&ep_isa_all_probes, er, er_link);
212
213 /*
214 * Map the Etherlink ID port for the probe sequence.
215 */
216 if (bus_space_map(iot, ELINK_ID_PORT, 1, 0, &ioh)) {
217 printf("ep_isa_probe: can't map Etherlink ID port\n");
218 return 0;
219 }
220
221 for (slot = 0; slot < MAXEPCARDS; slot++) {
222 elink_reset(iot, ioh, parent->dv_unit);
223 elink_idseq(iot, ioh, ELINK_509_POLY);
224
225 /* Untag all the adapters so they will talk to us. */
226 if (slot == 0)
227 bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 0);
228
229 vendor = htons(epreadeeprom(iot, ioh, EEPROM_MFG_ID));
230 if (vendor != MFG_ID)
231 continue;
232
233 model = htons(epreadeeprom(iot, ioh, EEPROM_PROD_ID));
234 /*
235 * XXX: Add a new product id to check for other cards
236 * (3c515?) and fix the check in ep_isa_attach.
237 */
238 if ((model & 0xfff0) != PROD_ID_3C509) {
239 #ifndef trusted
240 printf(
241 "ep_isa_probe: ignoring model %04x\n", model);
242 #endif
243 continue;
244 }
245
246 iobase = epreadeeprom(iot, ioh, EEPROM_ADDR_CFG);
247 iobase = (iobase & 0x1f) * 0x10 + 0x200;
248
249 irq = epreadeeprom(iot, ioh, EEPROM_RESOURCE_CFG);
250 irq >>= 12;
251
252 /* so card will not respond to contention again */
253 bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 1);
254
255 /*
256 * XXX: this should probably not be done here
257 * because it enables the drq/irq lines from
258 * the board. Perhaps it should be done after
259 * we have checked for irq/drq collisions?
260 */
261 bus_space_write_1(iot, ioh, 0, ACTIVATE_ADAPTER_TO_CONFIG);
262
263 /*
264 * Don't attach a 3c509 in PnP mode.
265 */
266 if ((model & 0xfff0) == PROD_ID_3C509) {
267 if (bus_space_map(iot, iobase, 1, 0, &ioh2)) {
268 printf(
269 "ep_isa_probe: can't map Etherlink iobase\n");
270 return 0;
271 }
272 if (bus_space_read_2(iot, ioh2, EP_W0_EEPROM_COMMAND)
273 & EEPROM_TST_MODE) {
274 printf(
275 "3COM 3C509 Ethernet card in PnP mode\n");
276 continue;
277 }
278 bus_space_unmap(iot, ioh2, 1);
279 }
280 epaddcard(bus, iobase, irq, model);
281 }
282 /* XXX should we sort by ethernet address? */
283
284 bus_space_unmap(iot, ioh, 1);
285
286 bus_probed:
287
288 for (i = 0; i < nepcards; i++) {
289 if (epcards[i].bus != bus)
290 continue;
291 if (epcards[i].available == 0)
292 continue;
293 if (ia->ia_iobase != IOBASEUNK &&
294 ia->ia_iobase != epcards[i].iobase)
295 continue;
296 if (ia->ia_irq != IRQUNK &&
297 ia->ia_irq != epcards[i].irq)
298 continue;
299 goto good;
300 }
301 return 0;
302
303 good:
304 epcards[i].available = 0;
305 ia->ia_iobase = epcards[i].iobase;
306 ia->ia_irq = epcards[i].irq;
307 ia->ia_iosize = 0x10;
308 ia->ia_msize = 0;
309 ia->ia_aux = (void *)epcards[i].model;
310 return 1;
311 }
312
313 void
314 ep_isa_attach(parent, self, aux)
315 struct device *parent, *self;
316 void *aux;
317 {
318 struct ep_softc *sc = (void *)self;
319 struct isa_attach_args *ia = aux;
320 bus_space_tag_t iot = ia->ia_iot;
321 bus_space_handle_t ioh;
322 int chipset;
323
324 /* Map i/o space. */
325 if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh)) {
326 printf(": can't map i/o space\n");
327 return;
328 }
329
330 sc->sc_iot = iot;
331 sc->sc_ioh = ioh;
332 sc->bustype = EP_BUS_ISA;
333
334 sc->enable = NULL;
335 sc->disable = NULL;
336 sc->enabled = 1;
337
338 chipset = (int)(long)ia->ia_aux;
339 if ((chipset & 0xfff0) == PROD_ID_3C509) {
340 printf(": 3Com 3C509 Ethernet\n");
341 epconfig(sc, EP_CHIPSET_3C509, NULL);
342 } else {
343 /*
344 * XXX: Maybe a 3c515, but the check in ep_isa_probe looks
345 * at the moment only for a 3c509.
346 */
347 printf(": unknown 3Com Ethernet card: %04x\n", chipset);
348 epconfig(sc, EP_CHIPSET_UNKNOWN, NULL);
349 }
350
351 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
352 IPL_NET, epintr, sc);
353 }
354