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