xref: /netbsd-src/sys/dev/isa/if_ep_isa.c (revision 17306b8fd0952c7489f93f0230818481e5a1e2c9)
1 /*	$NetBSD: if_ep_isa.c,v 1.26 1999/04/28 01:20:01 jonathan 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@NetBSD.org>
42  * Copyright (c) 1994 Herb Peyerl <hpeyerl@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 "opt_inet.h"
72 #include "opt_ns.h"
73 #include "bpfilter.h"
74 
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/mbuf.h>
78 #include <sys/socket.h>
79 #include <sys/ioctl.h>
80 #include <sys/errno.h>
81 #include <sys/syslog.h>
82 #include <sys/select.h>
83 #include <sys/device.h>
84 #include <sys/queue.h>
85 
86 #include <net/if.h>
87 #include <net/if_dl.h>
88 #include <net/if_ether.h>
89 #include <net/if_media.h>
90 
91 #ifdef INET
92 #include <netinet/in.h>
93 #include <netinet/in_systm.h>
94 #include <netinet/in_var.h>
95 #include <netinet/ip.h>
96 #include <netinet/if_inarp.h>
97 #endif
98 
99 #ifdef NS
100 #include <netns/ns.h>
101 #include <netns/ns_if.h>
102 #endif
103 
104 #if NBPFILTER > 0
105 #include <net/bpf.h>
106 #include <net/bpfdesc.h>
107 #endif
108 
109 #include <machine/cpu.h>
110 #include <machine/bus.h>
111 #include <machine/intr.h>
112 
113 #include <dev/mii/miivar.h>
114 
115 #include <dev/ic/elink3var.h>
116 #include <dev/ic/elink3reg.h>
117 
118 #include <dev/isa/isavar.h>
119 #include <dev/isa/elink.h>
120 
121 int ep_isa_probe __P((struct device *, struct cfdata *, void *));
122 void ep_isa_attach __P((struct device *, struct device *, void *));
123 
124 struct cfattach ep_isa_ca = {
125 	sizeof(struct ep_softc), ep_isa_probe, ep_isa_attach
126 };
127 
128 static	void epaddcard __P((int, int, int, int));
129 
130 /*
131  * This keeps track of which ISAs have been through an ep probe sequence.
132  * A simple static variable isn't enough, since it's conceivable that
133  * a system might have more than one ISA bus.
134  *
135  * The "er_bus" member is the unit number of the parent ISA bus, e.g. "0"
136  * for "isa0".
137  */
138 struct ep_isa_done_probe {
139 	LIST_ENTRY(ep_isa_done_probe)	er_link;
140 	int				er_bus;
141 };
142 static LIST_HEAD(, ep_isa_done_probe) ep_isa_all_probes;
143 static int ep_isa_probes_initialized;
144 
145 #define MAXEPCARDS	20	/* if you have more than 20, you lose */
146 
147 static struct epcard {
148 	int	bus;
149 	int	iobase;
150 	int	irq;
151 	char	available;
152 	long	model;
153 } epcards[MAXEPCARDS];
154 static int nepcards;
155 
156 static void
157 epaddcard(bus, iobase, irq, model)
158 	int bus, iobase, irq, model;
159 {
160 
161 	if (nepcards >= MAXEPCARDS)
162 		return;
163 	epcards[nepcards].bus = bus;
164 	epcards[nepcards].iobase = iobase;
165 	epcards[nepcards].irq = (irq == 2) ? 9 : irq;
166 	epcards[nepcards].model = model;
167 	epcards[nepcards].available = 1;
168 	nepcards++;
169 }
170 
171 /*
172  * 3c509 cards on the ISA bus are probed in ethernet address order.
173  * The probe sequence requires careful orchestration, and we'd like
174  * like to allow the irq and base address to be wildcarded. So, we
175  * probe all the cards the first time epprobe() is called. On subsequent
176  * calls we look for matching cards.
177  */
178 int
179 ep_isa_probe(parent, match, aux)
180 	struct device *parent;
181 	struct cfdata *match;
182 	void *aux;
183 {
184 	struct isa_attach_args *ia = aux;
185 	bus_space_tag_t iot = ia->ia_iot;
186 	bus_space_handle_t ioh;
187 	int slot, iobase, irq, i;
188 	u_int16_t vendor, model;
189 	struct ep_isa_done_probe *er;
190 	int bus = parent->dv_unit;
191 
192 	if (ep_isa_probes_initialized == 0) {
193 		LIST_INIT(&ep_isa_all_probes);
194 		ep_isa_probes_initialized = 1;
195 	}
196 
197 	/*
198 	 * Probe this bus if we haven't done so already.
199 	 */
200 	for (er = ep_isa_all_probes.lh_first; er != NULL;
201 	    er = er->er_link.le_next)
202 		if (er->er_bus == parent->dv_unit)
203 			goto bus_probed;
204 
205 	/*
206 	 * Mark this bus so we don't probe it again.
207 	 */
208 	er = (struct ep_isa_done_probe *)
209 	    malloc(sizeof(struct ep_isa_done_probe), M_DEVBUF, M_NOWAIT);
210 	if (er == NULL)
211 		panic("ep_isa_probe: can't allocate state storage");
212 
213 	er->er_bus = bus;
214 	LIST_INSERT_HEAD(&ep_isa_all_probes, er, er_link);
215 
216 	/*
217 	 * Map the Etherlink ID port for the probe sequence.
218 	 */
219 	if (bus_space_map(iot, ELINK_ID_PORT, 1, 0, &ioh)) {
220 		printf("ep_isa_probe: can't map Etherlink ID port\n");
221 		return 0;
222 	}
223 
224 	for (slot = 0; slot < MAXEPCARDS; slot++) {
225 		elink_reset(iot, ioh, parent->dv_unit);
226 		elink_idseq(iot, ioh, ELINK_509_POLY);
227 
228 		/* Untag all the adapters so they will talk to us. */
229 		if (slot == 0)
230 			bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 0);
231 
232 		vendor = htons(epreadeeprom(iot, ioh, EEPROM_MFG_ID));
233 		if (vendor != MFG_ID)
234 			continue;
235 
236 		model = htons(epreadeeprom(iot, ioh, EEPROM_PROD_ID));
237 		/*
238 		 * XXX: Add a new product id to check for other cards
239 		 * (3c515?) and fix the check in ep_isa_attach.
240 		 */
241 		if ((model & 0xfff0) != PROD_ID_3C509) {
242 #ifndef trusted
243 			printf(
244 			 "ep_isa_probe: ignoring model %04x\n", model);
245 #endif
246 			continue;
247 			}
248 
249 		iobase = epreadeeprom(iot, ioh, EEPROM_ADDR_CFG);
250 		iobase = (iobase & 0x1f) * 0x10 + 0x200;
251 
252 		irq = epreadeeprom(iot, ioh, EEPROM_RESOURCE_CFG);
253 		irq >>= 12;
254 
255 		/* XXX Should ignore card if non-ISA(EISA) io address? -chb */
256 
257 		/*
258 		 * Don't attach a 3c509 in PnP mode.
259 		 * PnP mode was added with the 3C509B.
260 		 * Check some EEPROM registers to make sure this is really
261 		 * a 3C509B and test whether it is in PnP mode.
262 		 */
263 		if ((model & 0xfff0) == PROD_ID_3C509) {
264 			u_int16_t cksum, eepromrev, eeprom_cap, eeprom_hi;
265 
266 
267 			/*
268 			 * Fetch all the info we need to ascertain whether
269 			 * the card is  PnP capable and in PnP mode.
270 			 * Skip over PnP cards.
271 			 */
272 
273 			/* secondary configurable data checksum */
274 			cksum = epreadeeprom(iot, ioh, EEPROM_CHECKSUM_EL3)
275 			    & 0xFF;
276 			for (i = EEPROM_CONFIG_HIGH;
277 			    i < EEPROM_CHECKSUM_EL3; i++) {
278 				cksum ^= epreadeeprom(iot, ioh, i);
279 			}
280 			cksum = (cksum & 0xFF) ^ ((cksum >> 8) & 0xFF);
281 
282 			eepromrev = epreadeeprom(iot, ioh, EEPROM_SSI);
283 			eeprom_hi = epreadeeprom(iot, ioh, EEPROM_CONFIG_HIGH);
284 			eeprom_cap = epreadeeprom(iot, ioh, EEPROM_CAP);
285 
286 			/*
287 			 * Stop card responding to contention in future.
288 			 * (NB: stops rsponse to all reads from ID port.)
289 			 */
290 			bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 1);
291 
292 			if (cksum != 0) {
293 #if 0
294 				printf("ep_isa_probe: cksum mismatch 0x%02x\n",
295 				    (int)cksum);
296 #endif
297 			}
298 			else if ((eepromrev & 0xF) < 1) {
299 				/* 3C509B is adapter revision level 1. */
300 #if 0
301 				printf("ep_isa_probe revision level 0\n");
302 #endif
303 			}
304 			else if (eeprom_cap != 0x2083) {
305 #if 0
306 				printf("ep_isa_probe: capabilities word mismatch0x%03x\n",
307 				    (int)epreadeeprom(iot, ioh, EEPROM_CAP));
308 #endif
309 			}
310 			else
311 			  /*
312 			   * we have a 3c509B with PnP capabilities.
313 			   * Test partly documented bit which toggles when
314 			   * in  PnP mode.
315 			   */
316 			if ((eeprom_hi & 8) != 0) {
317 				printf("3COM 3C509B Ethernet card in PnP mode\n");
318 				continue;
319 			}
320 		}
321 
322 		/*
323 		 * XXX: this should probably not be done here
324 		 * because it enables the drq/irq lines from
325 		 * the board. Perhaps it should be done after
326 		 * we have checked for irq/drq collisions?
327 		 *
328 		 * According to the 3COM docs, this does not enable
329 		 * the irq lines. -chb
330 		 */
331 		bus_space_write_1(iot, ioh, 0, ACTIVATE_ADAPTER_TO_CONFIG);
332 
333 		epaddcard(bus, iobase, irq, model);
334 	}
335 	/* XXX should we sort by ethernet address? */
336 
337 	bus_space_unmap(iot, ioh, 1);
338 
339 bus_probed:
340 
341 	for (i = 0; i < nepcards; i++) {
342 		if (epcards[i].bus != bus)
343 			continue;
344 		if (epcards[i].available == 0)
345 			continue;
346 		if (ia->ia_iobase != IOBASEUNK &&
347 		    ia->ia_iobase != epcards[i].iobase)
348 			continue;
349 		if (ia->ia_irq != IRQUNK &&
350 		    ia->ia_irq != epcards[i].irq)
351 			continue;
352 		goto good;
353 	}
354 	return 0;
355 
356 good:
357 	epcards[i].available = 0;
358 	ia->ia_iobase = epcards[i].iobase;
359 	ia->ia_irq = epcards[i].irq;
360 	ia->ia_iosize = 0x10;
361 	ia->ia_msize = 0;
362 	ia->ia_aux = (void *)epcards[i].model;
363 	return 1;
364 }
365 
366 void
367 ep_isa_attach(parent, self, aux)
368 	struct device *parent, *self;
369 	void *aux;
370 {
371 	struct ep_softc *sc = (void *)self;
372 	struct isa_attach_args *ia = aux;
373 	bus_space_tag_t iot = ia->ia_iot;
374 	bus_space_handle_t ioh;
375 	int chipset;
376 
377 	/* Map i/o space. */
378 	if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh)) {
379 		printf(": can't map i/o space\n");
380 		return;
381 	}
382 
383 	sc->sc_iot = iot;
384 	sc->sc_ioh = ioh;
385 	sc->bustype = ELINK_BUS_ISA;
386 
387 	sc->enable = NULL;
388 	sc->disable = NULL;
389 	sc->enabled = 1;
390 
391 	chipset = (int)(long)ia->ia_aux;
392 	if ((chipset & 0xfff0) == PROD_ID_3C509) {
393 		printf(": 3Com 3C509 Ethernet\n");
394 		epconfig(sc, ELINK_CHIPSET_3C509, NULL);
395 	} else {
396 		/*
397 		 * XXX: Maybe a 3c515, but the check in ep_isa_probe looks
398 		 * at the moment only for a 3c509.
399 		 */
400 		printf(": unknown 3Com Ethernet card: %04x\n", chipset);
401 		return;
402 	}
403 
404 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
405 	    IPL_NET, epintr, sc);
406 }
407