Home | History | Annotate | Line # | Download | only in isa
if_ep_isa.c revision 1.17
      1 /*	$NetBSD: if_ep_isa.c,v 1.17 1997/10/14 21:32:48 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 
     90 #ifdef INET
     91 #include <netinet/in.h>
     92 #include <netinet/in_systm.h>
     93 #include <netinet/in_var.h>
     94 #include <netinet/ip.h>
     95 #include <netinet/if_inarp.h>
     96 #endif
     97 
     98 #ifdef NS
     99 #include <netns/ns.h>
    100 #include <netns/ns_if.h>
    101 #endif
    102 
    103 #if NBPFILTER > 0
    104 #include <net/bpf.h>
    105 #include <net/bpfdesc.h>
    106 #endif
    107 
    108 #include <machine/cpu.h>
    109 #include <machine/bus.h>
    110 #include <machine/intr.h>
    111 
    112 #include <dev/ic/elink3var.h>
    113 #include <dev/ic/elink3reg.h>
    114 
    115 #include <dev/isa/isavar.h>
    116 #include <dev/isa/elink.h>
    117 
    118 #ifdef __BROKEN_INDIRECT_CONFIG
    119 int ep_isa_probe __P((struct device *, void *, void *));
    120 #else
    121 int ep_isa_probe __P((struct device *, struct cfdata *, void *));
    122 #endif
    123 void ep_isa_attach __P((struct device *, struct device *, void *));
    124 
    125 struct cfattach ep_isa_ca = {
    126 	sizeof(struct ep_softc), ep_isa_probe, ep_isa_attach
    127 };
    128 
    129 static	void epaddcard __P((int, int, int, int));
    130 
    131 /*
    132  * This keeps track of which ISAs have been through an ep probe sequence.
    133  * A simple static variable isn't enough, since it's conceivable that
    134  * a system might have more than one ISA bus.
    135  *
    136  * The "er_bus" member is the unit number of the parent ISA bus, e.g. "0"
    137  * for "isa0".
    138  */
    139 struct ep_isa_done_probe {
    140 	LIST_ENTRY(ep_isa_done_probe)	er_link;
    141 	int				er_bus;
    142 };
    143 static LIST_HEAD(, ep_isa_done_probe) ep_isa_all_probes;
    144 static int ep_isa_probes_initialized;
    145 
    146 #define MAXEPCARDS	20	/* if you have more than 20, you lose */
    147 
    148 static struct epcard {
    149 	int	bus;
    150 	int	iobase;
    151 	int	irq;
    152 	char	available;
    153 	long	model;
    154 } epcards[MAXEPCARDS];
    155 static int nepcards;
    156 
    157 static void
    158 epaddcard(bus, iobase, irq, model)
    159 	int bus, iobase, irq, model;
    160 {
    161 
    162 	if (nepcards >= MAXEPCARDS)
    163 		return;
    164 	epcards[nepcards].bus = bus;
    165 	epcards[nepcards].iobase = iobase;
    166 	epcards[nepcards].irq = (irq == 2) ? 9 : irq;
    167 	epcards[nepcards].model = model;
    168 	epcards[nepcards].available = 1;
    169 	nepcards++;
    170 }
    171 
    172 /*
    173  * 3c509 cards on the ISA bus are probed in ethernet address order.
    174  * The probe sequence requires careful orchestration, and we'd like
    175  * like to allow the irq and base address to be wildcarded. So, we
    176  * probe all the cards the first time epprobe() is called. On subsequent
    177  * calls we look for matching cards.
    178  */
    179 int
    180 ep_isa_probe(parent, match, aux)
    181 	struct device *parent;
    182 #ifdef __BROKEN_INDIRECT_CONFIG
    183 	void *match;
    184 #else
    185 	struct cfdata *match;
    186 #endif
    187 	void *aux;
    188 {
    189 	struct isa_attach_args *ia = aux;
    190 	bus_space_tag_t iot = ia->ia_iot;
    191 	bus_space_handle_t ioh, ioh2;
    192 	int slot, iobase, irq, i;
    193 	u_int16_t vendor, model;
    194 	struct ep_isa_done_probe *er;
    195 	int bus = parent->dv_unit;
    196 
    197 	if (ep_isa_probes_initialized == 0) {
    198 		LIST_INIT(&ep_isa_all_probes);
    199 		ep_isa_probes_initialized = 1;
    200 	}
    201 
    202 	/*
    203 	 * Probe this bus if we haven't done so already.
    204 	 */
    205 	for (er = ep_isa_all_probes.lh_first; er != NULL;
    206 	    er = er->er_link.le_next)
    207 		if (er->er_bus == parent->dv_unit)
    208 			goto bus_probed;
    209 
    210 	/*
    211 	 * Mark this bus so we don't probe it again.
    212 	 */
    213 	er = (struct ep_isa_done_probe *)
    214 	    malloc(sizeof(struct ep_isa_done_probe), M_DEVBUF, M_NOWAIT);
    215 	if (er == NULL)
    216 		panic("ep_isa_probe: can't allocate state storage");
    217 
    218 	er->er_bus = bus;
    219 	LIST_INSERT_HEAD(&ep_isa_all_probes, er, er_link);
    220 
    221 	/*
    222 	 * Map the Etherlink ID port for the probe sequence.
    223 	 */
    224 	if (bus_space_map(iot, ELINK_ID_PORT, 1, 0, &ioh)) {
    225 		printf("ep_isa_probe: can't map Etherlink ID port\n");
    226 		return 0;
    227 	}
    228 
    229 	for (slot = 0; slot < MAXEPCARDS; slot++) {
    230 		elink_reset(iot, ioh, parent->dv_unit);
    231 		elink_idseq(iot, ioh, ELINK_509_POLY);
    232 
    233 		/* Untag all the adapters so they will talk to us. */
    234 		if (slot == 0)
    235 			bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 0);
    236 
    237 		vendor = htons(epreadeeprom(iot, ioh, EEPROM_MFG_ID));
    238 		if (vendor != MFG_ID)
    239 			continue;
    240 
    241 		model = htons(epreadeeprom(iot, ioh, EEPROM_PROD_ID));
    242 		/*
    243 		 * XXX: Add a new product id to check for other cards
    244 		 * (3c515?) and fix the check in ep_isa_attach.
    245 		 */
    246 		if ((model & 0xfff0) != PROD_ID_3C509) {
    247 #ifndef trusted
    248 			printf(
    249 			 "ep_isa_probe: ignoring model %04x\n", model);
    250 #endif
    251 			continue;
    252 			}
    253 
    254 		iobase = epreadeeprom(iot, ioh, EEPROM_ADDR_CFG);
    255 		iobase = (iobase & 0x1f) * 0x10 + 0x200;
    256 
    257 		irq = epreadeeprom(iot, ioh, EEPROM_RESOURCE_CFG);
    258 		irq >>= 12;
    259 
    260 		/* so card will not respond to contention again */
    261 		bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 1);
    262 
    263 		/*
    264 		 * XXX: this should probably not be done here
    265 		 * because it enables the drq/irq lines from
    266 		 * the board. Perhaps it should be done after
    267 		 * we have checked for irq/drq collisions?
    268 		 */
    269 		bus_space_write_1(iot, ioh, 0, ACTIVATE_ADAPTER_TO_CONFIG);
    270 
    271 		/*
    272 		 * Don't attach a 3c509 in PnP mode.
    273 		 */
    274 		if ((model & 0xfff0) == PROD_ID_3C509) {
    275 			if (bus_space_map(iot, iobase, 1, 0, &ioh2)) {
    276 				printf(
    277 				"ep_isa_probe: can't map Etherlink iobase\n");
    278 				return 0;
    279 			}
    280 			if (bus_space_read_2(iot, ioh2, EP_W0_EEPROM_COMMAND)
    281 			    & EEPROM_TST_MODE) {
    282 				printf(
    283 				 "3COM 3C509 Ethernet card in PnP mode\n");
    284 				continue;
    285 			}
    286 			bus_space_unmap(iot, ioh2, 1);
    287 		}
    288 		epaddcard(bus, iobase, irq, model);
    289 	}
    290 	/* XXX should we sort by ethernet address? */
    291 
    292 	bus_space_unmap(iot, ioh, 1);
    293 
    294 bus_probed:
    295 
    296 	for (i = 0; i < nepcards; i++) {
    297 		if (epcards[i].bus != bus)
    298 			continue;
    299 		if (epcards[i].available == 0)
    300 			continue;
    301 		if (ia->ia_iobase != IOBASEUNK &&
    302 		    ia->ia_iobase != epcards[i].iobase)
    303 			continue;
    304 		if (ia->ia_irq != IRQUNK &&
    305 		    ia->ia_irq != epcards[i].irq)
    306 			continue;
    307 		goto good;
    308 	}
    309 	return 0;
    310 
    311 good:
    312 	epcards[i].available = 0;
    313 	ia->ia_iobase = epcards[i].iobase;
    314 	ia->ia_irq = epcards[i].irq;
    315 	ia->ia_iosize = 0x10;
    316 	ia->ia_msize = 0;
    317 	ia->ia_aux = (void *)epcards[i].model;
    318 	return 1;
    319 }
    320 
    321 void
    322 ep_isa_attach(parent, self, aux)
    323 	struct device *parent, *self;
    324 	void *aux;
    325 {
    326 	struct ep_softc *sc = (void *)self;
    327 	struct isa_attach_args *ia = aux;
    328 	bus_space_tag_t iot = ia->ia_iot;
    329 	bus_space_handle_t ioh;
    330 	int chipset;
    331 
    332 	/* Map i/o space. */
    333 	if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh))
    334 		panic("ep_isa_attach: can't map i/o space");
    335 
    336 	sc->sc_iot = iot;
    337 	sc->sc_ioh = ioh;
    338 	sc->bustype = EP_BUS_ISA;
    339 
    340 	sc->enable = NULL;
    341 	sc->disable = NULL;
    342 	sc->enabled = 1;
    343 
    344 	chipset = (int)(long)ia->ia_aux;
    345 	if ((chipset & 0xfff0) == PROD_ID_3C509) {
    346 		printf(": 3Com 3C509 Ethernet\n");
    347 		epconfig(sc, EP_CHIPSET_3C509, NULL);
    348 	} else {
    349 		/*
    350 		 * XXX: Maybe a 3c515, but the check in ep_isa_probe looks
    351 		 * at the moment only for a 3c509.
    352 		 */
    353 		printf(": unknown 3Com Ethernet card: %04x\n", chipset);
    354 		epconfig(sc, EP_CHIPSET_UNKNOWN, NULL);
    355 	}
    356 
    357 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    358 	    IPL_NET, epintr, sc);
    359 }
    360