Home | History | Annotate | Line # | Download | only in isa
if_ep_isa.c revision 1.19
      1 /*	$NetBSD: if_ep_isa.c,v 1.19 1997/10/20 18:43:12 thorpej 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 (at) NetBSD.org>
     42  * Copyright (c) 1994 Herb Peyerl <hpeyerl (at) 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 "bpfilter.h"
     72 
     73 #include <sys/param.h>
     74 #include <sys/systm.h>
     75 #include <sys/mbuf.h>
     76 #include <sys/socket.h>
     77 #include <sys/ioctl.h>
     78 #include <sys/errno.h>
     79 #include <sys/syslog.h>
     80 #include <sys/select.h>
     81 #include <sys/device.h>
     82 #include <sys/queue.h>
     83 
     84 #include <net/if.h>
     85 #include <net/if_dl.h>
     86 #include <net/if_ether.h>
     87 #include <net/if_media.h>
     88 
     89 #ifdef INET
     90 #include <netinet/in.h>
     91 #include <netinet/in_systm.h>
     92 #include <netinet/in_var.h>
     93 #include <netinet/ip.h>
     94 #include <netinet/if_inarp.h>
     95 #endif
     96 
     97 #ifdef NS
     98 #include <netns/ns.h>
     99 #include <netns/ns_if.h>
    100 #endif
    101 
    102 #if NBPFILTER > 0
    103 #include <net/bpf.h>
    104 #include <net/bpfdesc.h>
    105 #endif
    106 
    107 #include <machine/cpu.h>
    108 #include <machine/bus.h>
    109 #include <machine/intr.h>
    110 
    111 #include <dev/ic/elink3var.h>
    112 #include <dev/ic/elink3reg.h>
    113 
    114 #include <dev/isa/isavar.h>
    115 #include <dev/isa/elink.h>
    116 
    117 #ifdef __BROKEN_INDIRECT_CONFIG
    118 int ep_isa_probe __P((struct device *, void *, void *));
    119 #else
    120 int ep_isa_probe __P((struct device *, struct cfdata *, void *));
    121 #endif
    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 #ifdef __BROKEN_INDIRECT_CONFIG
    182 	void *match;
    183 #else
    184 	struct cfdata *match;
    185 #endif
    186 	void *aux;
    187 {
    188 	struct isa_attach_args *ia = aux;
    189 	bus_space_tag_t iot = ia->ia_iot;
    190 	bus_space_handle_t ioh, ioh2;
    191 	int slot, iobase, irq, i;
    192 	u_int16_t vendor, model;
    193 	struct ep_isa_done_probe *er;
    194 	int bus = parent->dv_unit;
    195 
    196 	if (ep_isa_probes_initialized == 0) {
    197 		LIST_INIT(&ep_isa_all_probes);
    198 		ep_isa_probes_initialized = 1;
    199 	}
    200 
    201 	/*
    202 	 * Probe this bus if we haven't done so already.
    203 	 */
    204 	for (er = ep_isa_all_probes.lh_first; er != NULL;
    205 	    er = er->er_link.le_next)
    206 		if (er->er_bus == parent->dv_unit)
    207 			goto bus_probed;
    208 
    209 	/*
    210 	 * Mark this bus so we don't probe it again.
    211 	 */
    212 	er = (struct ep_isa_done_probe *)
    213 	    malloc(sizeof(struct ep_isa_done_probe), M_DEVBUF, M_NOWAIT);
    214 	if (er == NULL)
    215 		panic("ep_isa_probe: can't allocate state storage");
    216 
    217 	er->er_bus = bus;
    218 	LIST_INSERT_HEAD(&ep_isa_all_probes, er, er_link);
    219 
    220 	/*
    221 	 * Map the Etherlink ID port for the probe sequence.
    222 	 */
    223 	if (bus_space_map(iot, ELINK_ID_PORT, 1, 0, &ioh)) {
    224 		printf("ep_isa_probe: can't map Etherlink ID port\n");
    225 		return 0;
    226 	}
    227 
    228 	for (slot = 0; slot < MAXEPCARDS; slot++) {
    229 		elink_reset(iot, ioh, parent->dv_unit);
    230 		elink_idseq(iot, ioh, ELINK_509_POLY);
    231 
    232 		/* Untag all the adapters so they will talk to us. */
    233 		if (slot == 0)
    234 			bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 0);
    235 
    236 		vendor = htons(epreadeeprom(iot, ioh, EEPROM_MFG_ID));
    237 		if (vendor != MFG_ID)
    238 			continue;
    239 
    240 		model = htons(epreadeeprom(iot, ioh, EEPROM_PROD_ID));
    241 		/*
    242 		 * XXX: Add a new product id to check for other cards
    243 		 * (3c515?) and fix the check in ep_isa_attach.
    244 		 */
    245 		if ((model & 0xfff0) != PROD_ID_3C509) {
    246 #ifndef trusted
    247 			printf(
    248 			 "ep_isa_probe: ignoring model %04x\n", model);
    249 #endif
    250 			continue;
    251 			}
    252 
    253 		iobase = epreadeeprom(iot, ioh, EEPROM_ADDR_CFG);
    254 		iobase = (iobase & 0x1f) * 0x10 + 0x200;
    255 
    256 		irq = epreadeeprom(iot, ioh, EEPROM_RESOURCE_CFG);
    257 		irq >>= 12;
    258 
    259 		/* so card will not respond to contention again */
    260 		bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 1);
    261 
    262 		/*
    263 		 * XXX: this should probably not be done here
    264 		 * because it enables the drq/irq lines from
    265 		 * the board. Perhaps it should be done after
    266 		 * we have checked for irq/drq collisions?
    267 		 */
    268 		bus_space_write_1(iot, ioh, 0, ACTIVATE_ADAPTER_TO_CONFIG);
    269 
    270 		/*
    271 		 * Don't attach a 3c509 in PnP mode.
    272 		 */
    273 		if ((model & 0xfff0) == PROD_ID_3C509) {
    274 			if (bus_space_map(iot, iobase, 1, 0, &ioh2)) {
    275 				printf(
    276 				"ep_isa_probe: can't map Etherlink iobase\n");
    277 				return 0;
    278 			}
    279 			if (bus_space_read_2(iot, ioh2, EP_W0_EEPROM_COMMAND)
    280 			    & EEPROM_TST_MODE) {
    281 				printf(
    282 				 "3COM 3C509 Ethernet card in PnP mode\n");
    283 				continue;
    284 			}
    285 			bus_space_unmap(iot, ioh2, 1);
    286 		}
    287 		epaddcard(bus, iobase, irq, model);
    288 	}
    289 	/* XXX should we sort by ethernet address? */
    290 
    291 	bus_space_unmap(iot, ioh, 1);
    292 
    293 bus_probed:
    294 
    295 	for (i = 0; i < nepcards; i++) {
    296 		if (epcards[i].bus != bus)
    297 			continue;
    298 		if (epcards[i].available == 0)
    299 			continue;
    300 		if (ia->ia_iobase != IOBASEUNK &&
    301 		    ia->ia_iobase != epcards[i].iobase)
    302 			continue;
    303 		if (ia->ia_irq != IRQUNK &&
    304 		    ia->ia_irq != epcards[i].irq)
    305 			continue;
    306 		goto good;
    307 	}
    308 	return 0;
    309 
    310 good:
    311 	epcards[i].available = 0;
    312 	ia->ia_iobase = epcards[i].iobase;
    313 	ia->ia_irq = epcards[i].irq;
    314 	ia->ia_iosize = 0x10;
    315 	ia->ia_msize = 0;
    316 	ia->ia_aux = (void *)epcards[i].model;
    317 	return 1;
    318 }
    319 
    320 void
    321 ep_isa_attach(parent, self, aux)
    322 	struct device *parent, *self;
    323 	void *aux;
    324 {
    325 	struct ep_softc *sc = (void *)self;
    326 	struct isa_attach_args *ia = aux;
    327 	bus_space_tag_t iot = ia->ia_iot;
    328 	bus_space_handle_t ioh;
    329 	int chipset;
    330 
    331 	/* Map i/o space. */
    332 	if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh)) {
    333 		printf(": can't map i/o space\n");
    334 		return;
    335 	}
    336 
    337 	sc->sc_iot = iot;
    338 	sc->sc_ioh = ioh;
    339 	sc->bustype = EP_BUS_ISA;
    340 
    341 	sc->enable = NULL;
    342 	sc->disable = NULL;
    343 	sc->enabled = 1;
    344 
    345 	chipset = (int)(long)ia->ia_aux;
    346 	if ((chipset & 0xfff0) == PROD_ID_3C509) {
    347 		printf(": 3Com 3C509 Ethernet\n");
    348 		epconfig(sc, EP_CHIPSET_3C509, NULL);
    349 	} else {
    350 		/*
    351 		 * XXX: Maybe a 3c515, but the check in ep_isa_probe looks
    352 		 * at the moment only for a 3c509.
    353 		 */
    354 		printf(": unknown 3Com Ethernet card: %04x\n", chipset);
    355 		epconfig(sc, EP_CHIPSET_UNKNOWN, NULL);
    356 	}
    357 
    358 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    359 	    IPL_NET, epintr, sc);
    360 }
    361