Home | History | Annotate | Line # | Download | only in pcmcia
if_ep_pcmcia.c revision 1.9
      1  1.9  jonathan /*	$NetBSD: if_ep_pcmcia.c,v 1.9 1998/07/05 00:51:24 jonathan Exp $	*/
      2  1.2   thorpej 
      3  1.2   thorpej /*
      4  1.2   thorpej  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
      5  1.2   thorpej  *
      6  1.2   thorpej  * Redistribution and use in source and binary forms, with or without
      7  1.2   thorpej  * modification, are permitted provided that the following conditions
      8  1.2   thorpej  * are met:
      9  1.2   thorpej  * 1. Redistributions of source code must retain the above copyright
     10  1.2   thorpej  *    notice, this list of conditions and the following disclaimer.
     11  1.2   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.2   thorpej  *    notice, this list of conditions and the following disclaimer in the
     13  1.2   thorpej  *    documentation and/or other materials provided with the distribution.
     14  1.2   thorpej  * 3. All advertising materials mentioning features or use of this software
     15  1.2   thorpej  *    must display the following acknowledgement:
     16  1.2   thorpej  *	This product includes software developed by Marc Horowitz.
     17  1.2   thorpej  * 4. The name of the author may not be used to endorse or promote products
     18  1.2   thorpej  *    derived from this software without specific prior written permission.
     19  1.2   thorpej  *
     20  1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  1.2   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  1.2   thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.2   thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  1.2   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  1.2   thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  1.2   thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  1.2   thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  1.2   thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  1.2   thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  1.2   thorpej  */
     31  1.2   thorpej 
     32  1.9  jonathan #include "opt_inet.h"
     33  1.2   thorpej #include "bpfilter.h"
     34  1.2   thorpej 
     35  1.2   thorpej #include <sys/param.h>
     36  1.2   thorpej #include <sys/systm.h>
     37  1.2   thorpej #include <sys/mbuf.h>
     38  1.2   thorpej #include <sys/socket.h>
     39  1.2   thorpej #include <sys/ioctl.h>
     40  1.2   thorpej #include <sys/errno.h>
     41  1.2   thorpej #include <sys/syslog.h>
     42  1.2   thorpej #include <sys/select.h>
     43  1.2   thorpej #include <sys/device.h>
     44  1.2   thorpej 
     45  1.2   thorpej #include <net/if.h>
     46  1.2   thorpej #include <net/if_dl.h>
     47  1.2   thorpej #include <net/if_ether.h>
     48  1.2   thorpej #include <net/if_media.h>
     49  1.2   thorpej 
     50  1.2   thorpej #ifdef INET
     51  1.2   thorpej #include <netinet/in.h>
     52  1.2   thorpej #include <netinet/in_systm.h>
     53  1.2   thorpej #include <netinet/in_var.h>
     54  1.2   thorpej #include <netinet/ip.h>
     55  1.2   thorpej #include <netinet/if_inarp.h>
     56  1.2   thorpej #endif
     57  1.2   thorpej 
     58  1.2   thorpej #ifdef NS
     59  1.2   thorpej #include <netns/ns.h>
     60  1.2   thorpej #include <netns/ns_if.h>
     61  1.2   thorpej #endif
     62  1.2   thorpej 
     63  1.2   thorpej #if NBPFILTER > 0
     64  1.2   thorpej #include <net/bpf.h>
     65  1.2   thorpej #include <net/bpfdesc.h>
     66  1.2   thorpej #endif
     67  1.2   thorpej 
     68  1.2   thorpej #include <machine/cpu.h>
     69  1.2   thorpej #include <machine/bus.h>
     70  1.2   thorpej #include <machine/intr.h>
     71  1.2   thorpej 
     72  1.2   thorpej #include <dev/ic/elink3var.h>
     73  1.2   thorpej #include <dev/ic/elink3reg.h>
     74  1.2   thorpej 
     75  1.2   thorpej #include <dev/pcmcia/pcmciareg.h>
     76  1.2   thorpej #include <dev/pcmcia/pcmciavar.h>
     77  1.2   thorpej 
     78  1.2   thorpej #define	PCMCIA_MANUFACTURER_3COM	0x101
     79  1.2   thorpej #define	PCMCIA_PRODUCT_3COM_3C562	0x562
     80  1.2   thorpej #define	PCMCIA_PRODUCT_3COM_3C589	0x589
     81  1.2   thorpej 
     82  1.2   thorpej int	ep_pcmcia_match __P((struct device *, struct cfdata *, void *));
     83  1.2   thorpej void	ep_pcmcia_attach __P((struct device *, struct device *, void *));
     84  1.2   thorpej 
     85  1.2   thorpej int	ep_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
     86  1.2   thorpej int	ep_pcmcia_enable __P((struct ep_softc *));
     87  1.2   thorpej void	ep_pcmcia_disable __P((struct ep_softc *));
     88  1.2   thorpej 
     89  1.7   thorpej int	ep_pcmcia_enable1 __P((struct ep_softc *));
     90  1.7   thorpej void	ep_pcmcia_disable1 __P((struct ep_softc *));
     91  1.7   thorpej 
     92  1.2   thorpej struct ep_pcmcia_softc {
     93  1.2   thorpej 	struct ep_softc sc_ep;			/* real "ep" softc */
     94  1.2   thorpej 
     95  1.2   thorpej 	/* PCMCIA-specific goo */
     96  1.2   thorpej 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     97  1.2   thorpej 	int sc_io_window;			/* our i/o window */
     98  1.2   thorpej 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     99  1.2   thorpej };
    100  1.2   thorpej 
    101  1.2   thorpej struct cfattach ep_pcmcia_ca = {
    102  1.2   thorpej 	sizeof(struct ep_pcmcia_softc), ep_pcmcia_match, ep_pcmcia_attach
    103  1.2   thorpej };
    104  1.2   thorpej 
    105  1.2   thorpej int
    106  1.2   thorpej ep_pcmcia_match(parent, match, aux)
    107  1.2   thorpej 	struct device *parent;
    108  1.3  drochner 	struct cfdata *match;
    109  1.2   thorpej 	void *aux;
    110  1.2   thorpej {
    111  1.2   thorpej 	struct pcmcia_attach_args *pa = aux;
    112  1.2   thorpej 
    113  1.2   thorpej 	if (pa->manufacturer == PCMCIA_MANUFACTURER_3COM) {
    114  1.2   thorpej 		switch (pa->product) {
    115  1.2   thorpej 		case PCMCIA_PRODUCT_3COM_3C562:
    116  1.2   thorpej 		case PCMCIA_PRODUCT_3COM_3C589:
    117  1.2   thorpej 			if (pa->pf->number == 0)
    118  1.2   thorpej 				return (1);
    119  1.2   thorpej 		}
    120  1.2   thorpej 	}
    121  1.2   thorpej 
    122  1.2   thorpej 	return (0);
    123  1.2   thorpej }
    124  1.2   thorpej 
    125  1.2   thorpej int
    126  1.2   thorpej ep_pcmcia_enable(sc)
    127  1.2   thorpej 	struct ep_softc *sc;
    128  1.2   thorpej {
    129  1.2   thorpej 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    130  1.5      marc 	struct pcmcia_function *pf = psc->sc_pf;
    131  1.2   thorpej 
    132  1.2   thorpej 	/* establish the interrupt. */
    133  1.5      marc 	sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, epintr, sc);
    134  1.2   thorpej 	if (sc->sc_ih == NULL) {
    135  1.2   thorpej 		printf("%s: couldn't establish interrupt\n",
    136  1.2   thorpej 		    sc->sc_dev.dv_xname);
    137  1.2   thorpej 		return (1);
    138  1.2   thorpej 	}
    139  1.2   thorpej 
    140  1.7   thorpej 	return (ep_pcmcia_enable1(sc));
    141  1.7   thorpej }
    142  1.7   thorpej 
    143  1.7   thorpej int
    144  1.7   thorpej ep_pcmcia_enable1(sc)
    145  1.7   thorpej 	struct ep_softc *sc;
    146  1.7   thorpej {
    147  1.7   thorpej 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    148  1.7   thorpej 	struct pcmcia_function *pf = psc->sc_pf;
    149  1.7   thorpej 	int ret;
    150  1.7   thorpej 
    151  1.5      marc 	if ((ret = pcmcia_function_enable(pf)))
    152  1.7   thorpej 		return (ret);
    153  1.7   thorpej 
    154  1.5      marc 	if (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) {
    155  1.5      marc 		int reg;
    156  1.5      marc 
    157  1.5      marc 		/* turn off the serial-disable bit */
    158  1.5      marc 
    159  1.5      marc 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    160  1.5      marc 		if (reg & 0x08) {
    161  1.7   thorpej 			reg &= ~0x08;
    162  1.7   thorpej 			pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    163  1.5      marc 		}
    164  1.5      marc 
    165  1.5      marc 	}
    166  1.5      marc 
    167  1.5      marc 	return (ret);
    168  1.2   thorpej }
    169  1.2   thorpej 
    170  1.2   thorpej void
    171  1.2   thorpej ep_pcmcia_disable(sc)
    172  1.2   thorpej 	struct ep_softc *sc;
    173  1.2   thorpej {
    174  1.2   thorpej 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    175  1.2   thorpej 
    176  1.7   thorpej 	ep_pcmcia_disable1(sc);
    177  1.7   thorpej 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    178  1.7   thorpej }
    179  1.7   thorpej 
    180  1.7   thorpej void
    181  1.7   thorpej ep_pcmcia_disable1(sc)
    182  1.7   thorpej 	struct ep_softc *sc;
    183  1.7   thorpej {
    184  1.7   thorpej 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    185  1.7   thorpej 
    186  1.2   thorpej 	pcmcia_function_disable(psc->sc_pf);
    187  1.2   thorpej }
    188  1.2   thorpej 
    189  1.2   thorpej void
    190  1.2   thorpej ep_pcmcia_attach(parent, self, aux)
    191  1.2   thorpej 	struct device  *parent, *self;
    192  1.2   thorpej 	void           *aux;
    193  1.2   thorpej {
    194  1.2   thorpej 	struct ep_pcmcia_softc *psc = (void *) self;
    195  1.2   thorpej 	struct ep_softc *sc = &psc->sc_ep;
    196  1.2   thorpej 	struct pcmcia_attach_args *pa = aux;
    197  1.2   thorpej 	struct pcmcia_config_entry *cfe;
    198  1.2   thorpej 	int i;
    199  1.2   thorpej 	u_int8_t myla[ETHER_ADDR_LEN];
    200  1.2   thorpej 	u_int8_t *enaddr;
    201  1.2   thorpej 	char *model;
    202  1.2   thorpej 
    203  1.2   thorpej 	psc->sc_pf = pa->pf;
    204  1.2   thorpej 	cfe = pa->pf->cfe_head.sqh_first;
    205  1.2   thorpej 
    206  1.2   thorpej 	/* Enable the card. */
    207  1.2   thorpej 	pcmcia_function_init(pa->pf, cfe);
    208  1.7   thorpej 	if (ep_pcmcia_enable1(sc))
    209  1.2   thorpej 		printf(": function enable failed\n");
    210  1.2   thorpej 
    211  1.2   thorpej 	sc->enabled = 1;
    212  1.2   thorpej 
    213  1.2   thorpej 	if (cfe->num_memspace != 0)
    214  1.2   thorpej 		printf(": unexpected number of memory spaces %d should be 0\n",
    215  1.2   thorpej 		    cfe->num_memspace);
    216  1.2   thorpej 
    217  1.2   thorpej 	if (cfe->num_iospace != 1)
    218  1.2   thorpej 		printf(": unexpected number of I/O spaces %d should be 1\n",
    219  1.2   thorpej 		    cfe->num_iospace);
    220  1.2   thorpej 
    221  1.2   thorpej 	if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
    222  1.2   thorpej 		bus_addr_t maxaddr = (pa->pf->sc->iobase + pa->pf->sc->iosize);
    223  1.2   thorpej 
    224  1.6  christos 		for (i = pa->pf->sc->iobase; i < maxaddr; i += 0x10) {
    225  1.6  christos 			/*
    226  1.6  christos 			 * the 3c562 can only use 0x??00-0x??7f
    227  1.6  christos 			 * according to the Linux driver
    228  1.6  christos 			 */
    229  1.6  christos 			if (i & 0x80)
    230  1.6  christos 				continue;
    231  1.2   thorpej 			if (pcmcia_io_alloc(pa->pf, i, cfe->iospace[0].length,
    232  1.2   thorpej 			    0, &psc->sc_pcioh) == 0)
    233  1.2   thorpej 				break;
    234  1.2   thorpej 		}
    235  1.2   thorpej 		if (i >= maxaddr) {
    236  1.2   thorpej 			printf(": can't allocate i/o space\n");
    237  1.2   thorpej 			return;
    238  1.2   thorpej 		}
    239  1.2   thorpej 	} else {
    240  1.2   thorpej 		if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
    241  1.2   thorpej 		    cfe->iospace[0].length, &psc->sc_pcioh))
    242  1.2   thorpej 			printf(": can't allocate i/o space\n");
    243  1.2   thorpej 	}
    244  1.2   thorpej 
    245  1.2   thorpej 	sc->sc_iot = psc->sc_pcioh.iot;
    246  1.2   thorpej 	sc->sc_ioh = psc->sc_pcioh.ioh;
    247  1.2   thorpej 
    248  1.2   thorpej 	if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
    249  1.2   thorpej 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8), 0, cfe->iospace[0].length,
    250  1.2   thorpej 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    251  1.2   thorpej 		printf(": can't map i/o space\n");
    252  1.2   thorpej 		return;
    253  1.2   thorpej 	}
    254  1.2   thorpej 	if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
    255  1.6  christos 		/*
    256  1.6  christos 		 * 3c562a-c use this; 3c562d does it in the regular way.
    257  1.6  christos 		 * we might want to check the revision and produce a warning
    258  1.6  christos 		 * in the future.
    259  1.6  christos 		 */
    260  1.6  christos 		if (pcmcia_scan_cis(parent, ep_pcmcia_get_enaddr, myla) != 1)
    261  1.6  christos 			enaddr = NULL;
    262  1.6  christos 		else
    263  1.6  christos 			enaddr = myla;
    264  1.2   thorpej 	} else {
    265  1.2   thorpej 		enaddr = NULL;
    266  1.2   thorpej 	}
    267  1.2   thorpej 
    268  1.2   thorpej 	sc->bustype = EP_BUS_PCMCIA;
    269  1.2   thorpej 
    270  1.2   thorpej 	switch (pa->product) {
    271  1.2   thorpej 	case PCMCIA_PRODUCT_3COM_3C589:
    272  1.2   thorpej 		model = "3Com 3C589 Ethernet";
    273  1.2   thorpej 		break;
    274  1.2   thorpej 	case PCMCIA_PRODUCT_3COM_3C562:
    275  1.2   thorpej 		model = "3Com 3C562 Ethernet";
    276  1.2   thorpej 		break;
    277  1.2   thorpej 	default:
    278  1.2   thorpej 		model = "3Com Ethernet, model unknown";
    279  1.2   thorpej 		break;
    280  1.2   thorpej 	}
    281  1.2   thorpej 
    282  1.2   thorpej 	printf(": %s\n", model);
    283  1.2   thorpej 
    284  1.2   thorpej 	sc->enable = ep_pcmcia_enable;
    285  1.2   thorpej 	sc->disable = ep_pcmcia_disable;
    286  1.2   thorpej 
    287  1.2   thorpej 	epconfig(sc, EP_CHIPSET_3C509, enaddr);
    288  1.2   thorpej 
    289  1.2   thorpej 	sc->enabled = 0;
    290  1.2   thorpej 
    291  1.7   thorpej 	ep_pcmcia_disable1(sc);
    292  1.2   thorpej }
    293  1.2   thorpej 
    294  1.2   thorpej int
    295  1.2   thorpej ep_pcmcia_get_enaddr(tuple, arg)
    296  1.2   thorpej 	struct pcmcia_tuple *tuple;
    297  1.2   thorpej 	void *arg;
    298  1.2   thorpej {
    299  1.2   thorpej 	u_int8_t *myla = arg;
    300  1.2   thorpej 	int i;
    301  1.2   thorpej 
    302  1.6  christos 	/* this is 3c562a-c magic */
    303  1.2   thorpej 	if (tuple->code == 0x88) {
    304  1.2   thorpej 		if (tuple->length < ETHER_ADDR_LEN)
    305  1.2   thorpej 			return (0);
    306  1.2   thorpej 
    307  1.2   thorpej 		for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
    308  1.2   thorpej 			myla[i] = pcmcia_tuple_read_1(tuple, i + 1);
    309  1.2   thorpej 			myla[i + 1] = pcmcia_tuple_read_1(tuple, i);
    310  1.2   thorpej 		}
    311  1.2   thorpej 
    312  1.2   thorpej 		return (1);
    313  1.2   thorpej 	}
    314  1.2   thorpej 	return (0);
    315  1.2   thorpej }
    316