Home | History | Annotate | Line # | Download | only in isa
if_ep_isa.c revision 1.2
      1 /*	$NetBSD: if_ep_isa.c,v 1.2 1996/04/30 22:27:58 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996 Jason R. Thorpe <thorpej (at) beer.org>
      5  * Copyright (c) 1994 Herb Peyerl <hpeyerl (at) novatel.ca>
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by Herb Peyerl.
     19  * 4. The name of Herb Peyerl may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include "bpfilter.h"
     35 
     36 #include <sys/param.h>
     37 #include <sys/mbuf.h>
     38 #include <sys/socket.h>
     39 #include <sys/ioctl.h>
     40 #include <sys/errno.h>
     41 #include <sys/syslog.h>
     42 #include <sys/select.h>
     43 #include <sys/device.h>
     44 #include <sys/queue.h>
     45 
     46 #include <net/if.h>
     47 #include <net/if_dl.h>
     48 #include <net/if_types.h>
     49 #include <net/netisr.h>
     50 
     51 #ifdef INET
     52 #include <netinet/in.h>
     53 #include <netinet/in_systm.h>
     54 #include <netinet/in_var.h>
     55 #include <netinet/ip.h>
     56 #include <netinet/if_ether.h>
     57 #endif
     58 
     59 #ifdef NS
     60 #include <netns/ns.h>
     61 #include <netns/ns_if.h>
     62 #endif
     63 
     64 #if NBPFILTER > 0
     65 #include <net/bpf.h>
     66 #include <net/bpfdesc.h>
     67 #endif
     68 
     69 #include <machine/cpu.h>
     70 #include <machine/bus.h>
     71 
     72 #include <dev/ic/elink3var.h>
     73 #include <dev/ic/elink3reg.h>
     74 
     75 #include <dev/isa/isavar.h>
     76 #include <dev/isa/elink.h>
     77 
     78 int ep_isa_probe __P((struct device *, void *, void *));
     79 void ep_isa_attach __P((struct device *, struct device *, void *));
     80 
     81 struct cfattach ep_isa_ca = {
     82 	sizeof(struct ep_softc), ep_isa_probe, ep_isa_attach
     83 };
     84 
     85 static	void epaddcard __P((int, int, int));
     86 
     87 /*
     88  * This keeps track of which ISAs have been through an ep probe sequence.
     89  * A simple static variable isn't enough, since it's conceivable that
     90  * a system might have more than one ISA bus.
     91  *
     92  * The "er_bus" member is the unit number of the parent ISA bus, e.g. "0"
     93  * for "isa0".
     94  */
     95 struct ep_isa_done_probe {
     96 	LIST_ENTRY(ep_isa_done_probe)	er_link;
     97 	int				er_bus;
     98 };
     99 static LIST_HEAD(, ep_isa_done_probe) ep_isa_all_probes;
    100 static int ep_isa_probes_initialized;
    101 
    102 #define MAXEPCARDS	20	/* if you have more than 20, you lose */
    103 
    104 static struct epcard {
    105 	int	bus;
    106 	int	iobase;
    107 	int	irq;
    108 	char	available;
    109 } epcards[MAXEPCARDS];
    110 static int nepcards;
    111 
    112 static void
    113 epaddcard(bus, iobase, irq)
    114 	int bus, iobase, irq;
    115 {
    116 
    117 	if (nepcards >= MAXEPCARDS)
    118 		return;
    119 	epcards[nepcards].bus = bus;
    120 	epcards[nepcards].iobase = iobase;
    121 	epcards[nepcards].irq = (irq == 2) ? 9 : irq;
    122 	epcards[nepcards].available = 1;
    123 	nepcards++;
    124 }
    125 
    126 /*
    127  * 3c509 cards on the ISA bus are probed in ethernet address order.
    128  * The probe sequence requires careful orchestration, and we'd like
    129  * like to allow the irq and base address to be wildcarded. So, we
    130  * probe all the cards the first time epprobe() is called. On subsequent
    131  * calls we look for matching cards.
    132  */
    133 int
    134 ep_isa_probe(parent, match, aux)
    135 	struct device *parent;
    136 	void *match, *aux;
    137 {
    138 	struct isa_attach_args *ia = aux;
    139 	bus_chipset_tag_t bc = ia->ia_bc;
    140 	bus_io_handle_t ioh;
    141 	int slot, iobase, irq, i;
    142 	u_int16_t vendor, model;
    143 	struct ep_isa_done_probe *er;
    144 	int bus = parent->dv_unit;
    145 
    146 	if (ep_isa_probes_initialized == 0) {
    147 		LIST_INIT(&ep_isa_all_probes);
    148 		ep_isa_probes_initialized = 1;
    149 	}
    150 
    151 	/*
    152 	 * Probe this bus if we haven't done so already.
    153 	 */
    154 	for (er = ep_isa_all_probes.lh_first; er != NULL;
    155 	    er = er->er_link.le_next)
    156 		if (er->er_bus == parent->dv_unit)
    157 			goto bus_probed;
    158 
    159 	/*
    160 	 * Mark this bus so we don't probe it again.
    161 	 */
    162 	er = (struct ep_isa_done_probe *)
    163 	    malloc(sizeof(struct ep_isa_done_probe), M_DEVBUF, M_NOWAIT);
    164 	if (er == NULL)
    165 		panic("ep_isa_probe: can't allocate state storage");
    166 
    167 	er->er_bus = bus;
    168 	LIST_INSERT_HEAD(&ep_isa_all_probes, er, er_link);
    169 
    170 	/*
    171 	 * Map the Etherlink ID port for the probe sequence.
    172 	 */
    173 	if (bus_io_map(bc, ELINK_ID_PORT, 1, &ioh)) {
    174 		printf("ep_isa_probe: can't map Etherlink ID port\n");
    175 		return;
    176 	}
    177 
    178 	for (slot = 0; slot < MAXEPCARDS; slot++) {
    179 		elink_reset(bc, ioh, parent->dv_unit);
    180 		elink_idseq(bc, ioh, ELINK_509_POLY);
    181 
    182 		/* Untag all the adapters so they will talk to us. */
    183 		if (slot == 0)
    184 			bus_io_write_1(bc, ioh, 0, TAG_ADAPTER + 0);
    185 
    186 		vendor = htons(epreadeeprom(bc, ioh, EEPROM_MFG_ID));
    187 		if (vendor != MFG_ID)
    188 			continue;
    189 
    190 		model = htons(epreadeeprom(bc, ioh, EEPROM_PROD_ID));
    191 		if ((model & 0xfff0) != PROD_ID) {
    192 #ifndef trusted
    193 			printf(
    194 			 "ep_isa_probe: ignoring model %04x\n", model);
    195 #endif
    196 			continue;
    197 			}
    198 
    199 		iobase = epreadeeprom(bc, ioh, EEPROM_ADDR_CFG);
    200 		iobase = (iobase & 0x1f) * 0x10 + 0x200;
    201 
    202 		irq = epreadeeprom(bc, ioh, EEPROM_RESOURCE_CFG);
    203 		irq >>= 12;
    204 		epaddcard(bus, iobase, irq);
    205 
    206 		/* so card will not respond to contention again */
    207 		bus_io_write_1(bc, ioh, 0, TAG_ADAPTER + 1);
    208 
    209 		/*
    210 		 * XXX: this should probably not be done here
    211 		 * because it enables the drq/irq lines from
    212 		 * the board. Perhaps it should be done after
    213 		 * we have checked for irq/drq collisions?
    214 		 */
    215 		bus_io_write_1(bc, ioh, 0, ACTIVATE_ADAPTER_TO_CONFIG);
    216 	}
    217 	/* XXX should we sort by ethernet address? */
    218 
    219 	bus_io_unmap(bc, ioh, 1);
    220 
    221  bus_probed:
    222 
    223 	for (i = 0; i < nepcards; i++) {
    224 		if (epcards[i].bus != bus)
    225 			continue;
    226 		if (epcards[i].available == 0)
    227 			continue;
    228 		if (ia->ia_iobase != IOBASEUNK &&
    229 		    ia->ia_iobase != epcards[i].iobase)
    230 			continue;
    231 		if (ia->ia_irq != IRQUNK &&
    232 		    ia->ia_irq != epcards[i].irq)
    233 			continue;
    234 		goto good;
    235 	}
    236 	return 0;
    237 
    238 good:
    239 	epcards[i].available = 0;
    240 	ia->ia_iobase = epcards[i].iobase;
    241 	ia->ia_irq = epcards[i].irq;
    242 	ia->ia_iosize = 0x10;
    243 	ia->ia_msize = 0;
    244 	return 1;
    245 }
    246 
    247 void
    248 ep_isa_attach(parent, self, aux)
    249 	struct device *parent, *self;
    250 	void *aux;
    251 {
    252 	struct ep_softc *sc = (void *)self;
    253 	struct isa_attach_args *ia = aux;
    254 	bus_chipset_tag_t bc = ia->ia_bc;
    255 	bus_io_handle_t ioh;
    256 	u_short conn = 0;
    257 
    258 	/* Map i/o space. */
    259 	if (bus_io_map(bc, ia->ia_iobase, ia->ia_iosize, &ioh))
    260 		panic("ep_isa_attach: can't map i/o space");
    261 
    262 	sc->sc_bc = bc;
    263 	sc->sc_ioh = ioh;
    264 	sc->bustype = EP_BUS_ISA;
    265 
    266 	GO_WINDOW(0);
    267 	conn = bus_io_read_2(bc, ioh, EP_W0_CONFIG_CTRL);
    268 
    269 	printf(": 3Com 3C509 Ethernet\n");
    270 
    271 	epconfig(sc, conn);
    272 
    273 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    274 	    IPL_NET, epintr, sc);
    275 }
    276