if_ep_isa.c revision 1.4 1 /* $NetBSD: if_ep_isa.c,v 1.4 1996/05/10 05:27:53 thorpej 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 #include <net/netisr.h>
51
52 #ifdef INET
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/in_var.h>
56 #include <netinet/ip.h>
57 #include <netinet/if_ether.h>
58 #endif
59
60 #ifdef NS
61 #include <netns/ns.h>
62 #include <netns/ns_if.h>
63 #endif
64
65 #if NBPFILTER > 0
66 #include <net/bpf.h>
67 #include <net/bpfdesc.h>
68 #endif
69
70 #include <machine/cpu.h>
71 #include <machine/bus.h>
72
73 #include <dev/ic/elink3var.h>
74 #include <dev/ic/elink3reg.h>
75
76 #include <dev/isa/isavar.h>
77 #include <dev/isa/elink.h>
78
79 int ep_isa_probe __P((struct device *, void *, void *));
80 void ep_isa_attach __P((struct device *, struct device *, void *));
81
82 struct cfattach ep_isa_ca = {
83 sizeof(struct ep_softc), ep_isa_probe, ep_isa_attach
84 };
85
86 static void epaddcard __P((int, int, int));
87
88 /*
89 * This keeps track of which ISAs have been through an ep probe sequence.
90 * A simple static variable isn't enough, since it's conceivable that
91 * a system might have more than one ISA bus.
92 *
93 * The "er_bus" member is the unit number of the parent ISA bus, e.g. "0"
94 * for "isa0".
95 */
96 struct ep_isa_done_probe {
97 LIST_ENTRY(ep_isa_done_probe) er_link;
98 int er_bus;
99 };
100 static LIST_HEAD(, ep_isa_done_probe) ep_isa_all_probes;
101 static int ep_isa_probes_initialized;
102
103 #define MAXEPCARDS 20 /* if you have more than 20, you lose */
104
105 static struct epcard {
106 int bus;
107 int iobase;
108 int irq;
109 char available;
110 } epcards[MAXEPCARDS];
111 static int nepcards;
112
113 static void
114 epaddcard(bus, iobase, irq)
115 int bus, iobase, irq;
116 {
117
118 if (nepcards >= MAXEPCARDS)
119 return;
120 epcards[nepcards].bus = bus;
121 epcards[nepcards].iobase = iobase;
122 epcards[nepcards].irq = (irq == 2) ? 9 : irq;
123 epcards[nepcards].available = 1;
124 nepcards++;
125 }
126
127 /*
128 * 3c509 cards on the ISA bus are probed in ethernet address order.
129 * The probe sequence requires careful orchestration, and we'd like
130 * like to allow the irq and base address to be wildcarded. So, we
131 * probe all the cards the first time epprobe() is called. On subsequent
132 * calls we look for matching cards.
133 */
134 int
135 ep_isa_probe(parent, match, aux)
136 struct device *parent;
137 void *match, *aux;
138 {
139 struct isa_attach_args *ia = aux;
140 bus_chipset_tag_t bc = ia->ia_bc;
141 bus_io_handle_t ioh;
142 int slot, iobase, irq, i;
143 u_int16_t vendor, model;
144 struct ep_isa_done_probe *er;
145 int bus = parent->dv_unit;
146
147 if (ep_isa_probes_initialized == 0) {
148 LIST_INIT(&ep_isa_all_probes);
149 ep_isa_probes_initialized = 1;
150 }
151
152 /*
153 * Probe this bus if we haven't done so already.
154 */
155 for (er = ep_isa_all_probes.lh_first; er != NULL;
156 er = er->er_link.le_next)
157 if (er->er_bus == parent->dv_unit)
158 goto bus_probed;
159
160 /*
161 * Mark this bus so we don't probe it again.
162 */
163 er = (struct ep_isa_done_probe *)
164 malloc(sizeof(struct ep_isa_done_probe), M_DEVBUF, M_NOWAIT);
165 if (er == NULL)
166 panic("ep_isa_probe: can't allocate state storage");
167
168 er->er_bus = bus;
169 LIST_INSERT_HEAD(&ep_isa_all_probes, er, er_link);
170
171 /*
172 * Map the Etherlink ID port for the probe sequence.
173 */
174 if (bus_io_map(bc, ELINK_ID_PORT, 1, &ioh)) {
175 printf("ep_isa_probe: can't map Etherlink ID port\n");
176 return 0;
177 }
178
179 for (slot = 0; slot < MAXEPCARDS; slot++) {
180 elink_reset(bc, ioh, parent->dv_unit);
181 elink_idseq(bc, ioh, ELINK_509_POLY);
182
183 /* Untag all the adapters so they will talk to us. */
184 if (slot == 0)
185 bus_io_write_1(bc, ioh, 0, TAG_ADAPTER + 0);
186
187 vendor = htons(epreadeeprom(bc, ioh, EEPROM_MFG_ID));
188 if (vendor != MFG_ID)
189 continue;
190
191 model = htons(epreadeeprom(bc, ioh, EEPROM_PROD_ID));
192 if ((model & 0xfff0) != PROD_ID) {
193 #ifndef trusted
194 printf(
195 "ep_isa_probe: ignoring model %04x\n", model);
196 #endif
197 continue;
198 }
199
200 iobase = epreadeeprom(bc, ioh, EEPROM_ADDR_CFG);
201 iobase = (iobase & 0x1f) * 0x10 + 0x200;
202
203 irq = epreadeeprom(bc, ioh, EEPROM_RESOURCE_CFG);
204 irq >>= 12;
205 epaddcard(bus, iobase, irq);
206
207 /* so card will not respond to contention again */
208 bus_io_write_1(bc, ioh, 0, TAG_ADAPTER + 1);
209
210 /*
211 * XXX: this should probably not be done here
212 * because it enables the drq/irq lines from
213 * the board. Perhaps it should be done after
214 * we have checked for irq/drq collisions?
215 */
216 bus_io_write_1(bc, ioh, 0, ACTIVATE_ADAPTER_TO_CONFIG);
217 }
218 /* XXX should we sort by ethernet address? */
219
220 bus_io_unmap(bc, ioh, 1);
221
222 bus_probed:
223
224 for (i = 0; i < nepcards; i++) {
225 if (epcards[i].bus != bus)
226 continue;
227 if (epcards[i].available == 0)
228 continue;
229 if (ia->ia_iobase != IOBASEUNK &&
230 ia->ia_iobase != epcards[i].iobase)
231 continue;
232 if (ia->ia_irq != IRQUNK &&
233 ia->ia_irq != epcards[i].irq)
234 continue;
235 goto good;
236 }
237 return 0;
238
239 good:
240 epcards[i].available = 0;
241 ia->ia_iobase = epcards[i].iobase;
242 ia->ia_irq = epcards[i].irq;
243 ia->ia_iosize = 0x10;
244 ia->ia_msize = 0;
245 return 1;
246 }
247
248 void
249 ep_isa_attach(parent, self, aux)
250 struct device *parent, *self;
251 void *aux;
252 {
253 struct ep_softc *sc = (void *)self;
254 struct isa_attach_args *ia = aux;
255 bus_chipset_tag_t bc = ia->ia_bc;
256 bus_io_handle_t ioh;
257 u_short conn = 0;
258
259 /* Map i/o space. */
260 if (bus_io_map(bc, ia->ia_iobase, ia->ia_iosize, &ioh))
261 panic("ep_isa_attach: can't map i/o space");
262
263 sc->sc_bc = bc;
264 sc->sc_ioh = ioh;
265 sc->bustype = EP_BUS_ISA;
266
267 GO_WINDOW(0);
268 conn = bus_io_read_2(bc, ioh, EP_W0_CONFIG_CTRL);
269
270 printf(": 3Com 3C509 Ethernet\n");
271
272 epconfig(sc, conn);
273
274 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
275 IPL_NET, epintr, sc);
276 }
277