xref: /netbsd-src/sys/dev/isa/if_ep_isa.c (revision fdecd6a253f999ae92b139670d9e15cc9df4497c)
1 /*	$NetBSD: if_ep_isa.c,v 1.15 1997/06/23 05:25:40 cjs Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Jonathan Stone <jonathan@NetBSD.org>
5  * Copyright (c) 1996 Jason R. Thorpe <thorpej@beer.org>
6  * Copyright (c) 1994 Herb Peyerl <hpeyerl@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, 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 	long	model;
118 } epcards[MAXEPCARDS];
119 static int nepcards;
120 
121 static void
122 epaddcard(bus, iobase, irq, model)
123 	int bus, iobase, irq, model;
124 {
125 
126 	if (nepcards >= MAXEPCARDS)
127 		return;
128 	epcards[nepcards].bus = bus;
129 	epcards[nepcards].iobase = iobase;
130 	epcards[nepcards].irq = (irq == 2) ? 9 : irq;
131 	epcards[nepcards].model = model;
132 	epcards[nepcards].available = 1;
133 	nepcards++;
134 }
135 
136 /*
137  * 3c509 cards on the ISA bus are probed in ethernet address order.
138  * The probe sequence requires careful orchestration, and we'd like
139  * like to allow the irq and base address to be wildcarded. So, we
140  * probe all the cards the first time epprobe() is called. On subsequent
141  * calls we look for matching cards.
142  */
143 int
144 ep_isa_probe(parent, match, aux)
145 	struct device *parent;
146 #ifdef __BROKEN_INDIRECT_CONFIG
147 	void *match;
148 #else
149 	struct cfdata *match;
150 #endif
151 	void *aux;
152 {
153 	struct isa_attach_args *ia = aux;
154 	bus_space_tag_t iot = ia->ia_iot;
155 	bus_space_handle_t ioh, ioh2;
156 	int slot, iobase, irq, i;
157 	u_int16_t vendor, model;
158 	struct ep_isa_done_probe *er;
159 	int bus = parent->dv_unit;
160 
161 	if (ep_isa_probes_initialized == 0) {
162 		LIST_INIT(&ep_isa_all_probes);
163 		ep_isa_probes_initialized = 1;
164 	}
165 
166 	/*
167 	 * Probe this bus if we haven't done so already.
168 	 */
169 	for (er = ep_isa_all_probes.lh_first; er != NULL;
170 	    er = er->er_link.le_next)
171 		if (er->er_bus == parent->dv_unit)
172 			goto bus_probed;
173 
174 	/*
175 	 * Mark this bus so we don't probe it again.
176 	 */
177 	er = (struct ep_isa_done_probe *)
178 	    malloc(sizeof(struct ep_isa_done_probe), M_DEVBUF, M_NOWAIT);
179 	if (er == NULL)
180 		panic("ep_isa_probe: can't allocate state storage");
181 
182 	er->er_bus = bus;
183 	LIST_INSERT_HEAD(&ep_isa_all_probes, er, er_link);
184 
185 	/*
186 	 * Map the Etherlink ID port for the probe sequence.
187 	 */
188 	if (bus_space_map(iot, ELINK_ID_PORT, 1, 0, &ioh)) {
189 		printf("ep_isa_probe: can't map Etherlink ID port\n");
190 		return 0;
191 	}
192 
193 	for (slot = 0; slot < MAXEPCARDS; slot++) {
194 		elink_reset(iot, ioh, parent->dv_unit);
195 		elink_idseq(iot, ioh, ELINK_509_POLY);
196 
197 		/* Untag all the adapters so they will talk to us. */
198 		if (slot == 0)
199 			bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 0);
200 
201 		vendor = htons(epreadeeprom(iot, ioh, EEPROM_MFG_ID));
202 		if (vendor != MFG_ID)
203 			continue;
204 
205 		model = htons(epreadeeprom(iot, ioh, EEPROM_PROD_ID));
206 		/*
207 		 * XXX: Add a new product id to check for other cards
208 		 * (3c515?) and fix the check in ep_isa_attach.
209 		 */
210 		if ((model & 0xfff0) != PROD_ID_3C509) {
211 #ifndef trusted
212 			printf(
213 			 "ep_isa_probe: ignoring model %04x\n", model);
214 #endif
215 			continue;
216 			}
217 
218 		iobase = epreadeeprom(iot, ioh, EEPROM_ADDR_CFG);
219 		iobase = (iobase & 0x1f) * 0x10 + 0x200;
220 
221 		irq = epreadeeprom(iot, ioh, EEPROM_RESOURCE_CFG);
222 		irq >>= 12;
223 
224 		/* so card will not respond to contention again */
225 		bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 1);
226 
227 		/*
228 		 * XXX: this should probably not be done here
229 		 * because it enables the drq/irq lines from
230 		 * the board. Perhaps it should be done after
231 		 * we have checked for irq/drq collisions?
232 		 */
233 		bus_space_write_1(iot, ioh, 0, ACTIVATE_ADAPTER_TO_CONFIG);
234 
235 		/*
236 		 * Don't attach a 3c509 in PnP mode.
237 		 */
238 		if ((model & 0xfff0) == PROD_ID_3C509) {
239 			if (bus_space_map(iot, iobase, 1, 0, &ioh2)) {
240 				printf(
241 				"ep_isa_probe: can't map Etherlink iobase\n");
242 				return 0;
243 			}
244 			if (bus_space_read_2(iot, ioh2, EP_W0_EEPROM_COMMAND)
245 			    & EEPROM_TST_MODE) {
246 				printf(
247 				 "3COM 3C509 Ethernet card in PnP mode\n");
248 				continue;
249 			}
250 			bus_space_unmap(iot, ioh2, 1);
251 		}
252 		epaddcard(bus, iobase, irq, model);
253 	}
254 	/* XXX should we sort by ethernet address? */
255 
256 	bus_space_unmap(iot, ioh, 1);
257 
258 bus_probed:
259 
260 	for (i = 0; i < nepcards; i++) {
261 		if (epcards[i].bus != bus)
262 			continue;
263 		if (epcards[i].available == 0)
264 			continue;
265 		if (ia->ia_iobase != IOBASEUNK &&
266 		    ia->ia_iobase != epcards[i].iobase)
267 			continue;
268 		if (ia->ia_irq != IRQUNK &&
269 		    ia->ia_irq != epcards[i].irq)
270 			continue;
271 		goto good;
272 	}
273 	return 0;
274 
275 good:
276 	epcards[i].available = 0;
277 	ia->ia_iobase = epcards[i].iobase;
278 	ia->ia_irq = epcards[i].irq;
279 	ia->ia_iosize = 0x10;
280 	ia->ia_msize = 0;
281 	ia->ia_aux = (void *)epcards[i].model;
282 	return 1;
283 }
284 
285 void
286 ep_isa_attach(parent, self, aux)
287 	struct device *parent, *self;
288 	void *aux;
289 {
290 	struct ep_softc *sc = (void *)self;
291 	struct isa_attach_args *ia = aux;
292 	bus_space_tag_t iot = ia->ia_iot;
293 	bus_space_handle_t ioh;
294 	int chipset;
295 
296 	/* Map i/o space. */
297 	if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh))
298 		panic("ep_isa_attach: can't map i/o space");
299 
300 	sc->sc_iot = iot;
301 	sc->sc_ioh = ioh;
302 	sc->bustype = EP_BUS_ISA;
303 
304 	chipset = (int)(long)ia->ia_aux;
305 	if ((chipset & 0xfff0) == PROD_ID_3C509) {
306 		printf(": 3Com 3C509 Ethernet\n");
307 		epconfig(sc, EP_CHIPSET_3C509);
308 	} else {
309 		/*
310 		 * XXX: Maybe a 3c515, but the check in ep_isa_probe looks
311 		 * at the moment only for a 3c509.
312 		 */
313 		printf(": unknown 3Com Ethernet card: %04x\n", chipset);
314 		epconfig(sc, EP_CHIPSET_UNKNOWN);
315 	}
316 
317 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
318 	    IPL_NET, epintr, sc);
319 }
320