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