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