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